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

2013/4/28

How can I make objects move diagonally in Greenfoot?

QWERTY894 QWERTY894

2013/4/28

#
I want my object to move down the screen at an angle without changing its rotation
davmac davmac

2013/4/28

#
You can rotate an object, move it, and then rotate it back immediately. The visible effect will be that it moves at a different angle to what it is facing.
Gevater_Tod4711 Gevater_Tod4711

2013/4/28

#
I think it would be easyer to use a modified move method like this one:
public void move(double angle, double distance) {
        angle = Math.toRadians(angle);
        setLocation((int) (getX() + Math.cos(angle) * distance), (int) (getY() + Math.sin(angle) * distance));
    }
In this method you can set the angle and the distance to move.
danpost danpost

2013/4/28

#
With the method Gevater_Tod4711 gave, your actor will be restricted to specific angles of movement. The slower your actor moves, the fewer angles that the actor can fall at (see my Radial Graphs scenario). For falling at any angle, use the following:
// with these fields
int direction; // the angle you want the actor to fall
int speed; // the speed you want the actor to fall
int cycle; // tracks the time that the actor falls
int initX, initY; // holds the initial location of the actor

// using this method to initialize the location fields
public void addedToWorld(World world)
{
    initX = getX();
    initY = getY();
}

// move the actor with this in act or method it calls
cycle++; // increment the fall time
setLocation(initX, initY); // move actor to initial location
setRotation(direction); // set direction to move
move(speed*cycle); // move appropriate distance
setRotation(0); // reset rotation of actor
Please note: the 'addedToWorld' method is called automatically when the actor is placed in the world; you do not call this method in your code.
QWERTY894 QWERTY894

2013/4/28

#
Thanks! but now my actor appears on the screen and does not move..
danpost danpost

2013/4/28

#
You need to set values for 'direction' and 'speed' in the constructor for this object.
You need to login to post a reply.