This site requires JavaScript, please enable it in your browser!
Greenfoot back
wahaj
wahaj wrote ...

2012/4/14

object A hitting object B

wahaj wahaj

2012/4/14

#
lets say i have two objects, A and B. A is moveable and B is stationary, an obstacle in A's path. how can i make it so that when A hits B, A stops and cant go forward? it can move back though so it can change direction and avoid B. since object B is an obstacle i will need lots of them. how can i change the rotation of some of them while keeping the rotation of the rest to default?
drhorriblejr drhorriblejr

2012/4/14

#
Here is a class I modifyed and I think it might help you.
import greenfoot.*;

import java.util.List;
import java.util.ArrayList;
public class Mainsight extends Actor
{
    private static final double WALKING_SPEED = 5.0;
    
    /**
     * Constructor for Mainsight - nothing to do.
     */
    public Mainsight()
    {
    }

    /**
     * Act - empty method. Mainsights have no default action.
     */
    public void act()
    {
    }
    
    
    /**
     * Turn 'angle' degrees towards the right (clockwise).
     */
    public void turn(int angle)
    {
        setRotation(getRotation() + angle);
    }
    

    /**
     * Move forward in the current direction.
     */
    public void move()
    {
        double angle = Math.toRadians( getRotation() );
        int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
        int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
        
        setLocation(x, y);
    }

    
    /**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }
    
    
    /**
     * Return true if we can see an object of class 'clss' right where we are. 
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

    
    /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
}
wahaj wahaj

2012/4/14

#
nope i still dont get it. i see that you are using the canSee method to check if the actor is touching the edge. i suppose i can use that for my case. what about rotating an certain objects but leaving the rest the same from the same Actor?. if you put that in your code as well, then im sorry i cant see it. im not good with reading code yet.really new to programming
SPower SPower

2012/4/14

#
@drhorriblejr you can remove the act() and the mainsight() method(you don't need them). You copied this code from one of the tutorial projects of the greenfoot book, didn't you? It would also help for people who don't know what to do to just give them the code they need, not the code of an entire clas....
SPower SPower

2012/4/14

#
You can also move the list and array list imports at the top, drhorriblejr
danpost danpost

2012/4/14

#
wahaj wrote...
how can i make it so that when A hits B, A stops and cant go forward? it can move back though so it can change direction and avoid B.
Let evaluate what you said here (in pseudo-code) A moves; (you said 'A is moveable and B is stationary') if (A intersects B) { (you said 'A hits B') A moves back; (you said 'it can move back') A turns; (you said 'it can change direction') } Now, real code (in ObjectA's class):
move(1);
if (canSee(ObjectB.class)) // see drhorriblejr code line 64
{
    move(-1);
    turn(90); // see drhorriblejr code line 27
}
How easy was that!! And it does not matter how many ObjectB objects are in the world, as they will all block the ObjectA object. Also, only the ObjectA objects that get blocked will turn (and only when they are blocked).
wahaj wahaj

2012/4/14

#
right i understood that from drhorriblejr's code danpost. and i get what to do now. thanks for the reply. i don't mean to be rude but the my second question is still unanswered. i'm trying to build the boundaries for a race track and im using an actor (with a brick picture) to do so. but i need some of the bricks need to be rotated a bit so i can make a curve. i dont know where to put the setRotation method though. my track is created in the world's constructor. and im using for loops to make the track for (int a = 0; a<5; a++ ) { addObject ( new Brick(), 15+ 30*a, 555); } for (int a = 0; a<3; a++) { addObject ( new Brick2(), 236 , 590-30*a); } this is a sample from the constructor. but say i want to rotate a few brick that i make in a for loop 30 degrees. how would i do that? im sure i cant put the setRotation method inside for loop and putting it in the actor itself would cause all of the bricks to rotate. so what do i do here
wahaj wahaj

2012/4/14

#
sorry double posted by accident.....
danpost danpost

2012/4/15

#
You could put it in the Brick and Brick2 constructors. You could determine the brick's rotation in the world constructor, and send that value to the brick constructor (send zero for no rotation).
//example
Brick brick = new Brick(30);
addObject(brick, ...
or just 'addObject(new Brick(30),...'. You will need a constructor in the brick classes to recieve the value
public Brick(int rot)
{
    setRotation(rot);
}
wahaj wahaj

2012/4/15

#
sweet thanks a whole lot for the help. ahh parameters, i need to use them more often =)
drhorriblejr drhorriblejr

2012/4/15

#
And sorry I'm not that great at code :(
wahaj wahaj

2012/4/15

#
no its fine we all start somewhere. lets hope that we both become good programmers =)
You need to login to post a reply.