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

2014/3/28

Circular movement WITHOUT rotation?

Hadis Hadis

2014/3/28

#
Hello everybody, I'm working on a schoolproject and i have a big problem that keeps me from progress: I have to move an object with a circular movement. In order to achieve this, I startet with trying to move another object in a perfect circle with the 2 methods "setLocation()" and "move()" and then let the object which is not allowed to rotate follow it, but I didnt even got the perfect circle to work(it makes a "curvy" circle).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class asdf extends Projectiles
{
    private int a = 0;
    public void act()
    {
        if(a != 360) {
            a++;
        }else{
            a = 0;   
        }   
        setRotation(a);
        move(1);
    }
}
My guess is that my map might be to small for a perfect circle (528, 699) does someone know how to solve this problem? (I just need help with the circular movement. The following already works) -Hadis
danpost danpost

2014/3/28

#
The easiest way to move in a circle is to (1) increment the angle field ('a') (2) set object at center of circle (3) set the rotation to new angle (4) move radius distance from center of circle using the 'move' method (4) reset the rotation back to zero
Hadis Hadis

2014/3/28

#
I dont realy get, what the new angle is and how placing the object at the center of the circle and moving it by the radius distance helps. Could you explain that to me? - Hadis
danpost danpost

2014/3/29

#
Put the following in an Actor class:
1
2
3
4
5
6
7
8
9
10
private angle = 0;
 
public void act()
{
    setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2); // set center
    setRotation(angle+1); // set new angle
    angle = getRotation(); // save new angle
    move(100); // move from center along radius to circle
    setRotation(0); // fix rotation
}
and see what it does.
Hadis Hadis

2014/3/29

#
Ohh, that was what you ment! Thank you! And I dont even need my follow-code. :) - Hadis
You need to login to post a reply.