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

2014/4/11

Making objects move in a circular pattern?

allyand17 allyand17

2014/4/11

#
Is there a way I can make an object move in a circle around the center of the world at any speed (changeable int)?
lordhershey lordhershey

2014/4/11

#
Compute the angle to center, turn 90 degrees relative to that move forward repeat. I wonder if you can say turn towards center, get rotation, set rotation (current rotation - 90) , move (distance)
danpost danpost

2014/4/11

#
The following will have your actor facing in the direction of movement:
1
2
3
4
5
6
7
8
9
10
int rotationalSpeed = 1;
int radius = 200; // adjust as needed
 
public void orbitWorldCenter()
{
    setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2);
    turn(rotationalSpeed-90);
    move(radius);
    turn(90);
}
danpost danpost

2014/4/11

#
lordhershey wrote...
Compute the angle to center, turn 90 degrees relative to that move forward repeat. I wonder if you can say turn towards center, get rotation, set rotation (current rotation - 90) , move (distance)
Either way you explained here will have each move increasing the radius slightly. Eventually the actor will be bumping in the edges of the world.. Test them out.
lordhershey lordhershey

2014/4/11

#
I might, I was just thought about it very briefly, I took to mean they wanted it to orbit the center.
lordhershey lordhershey

2014/4/11

#
You were correct at 90 degrees the orbit expands, compensated by pinching the angle more and you get a little bounce but this does stabilize:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void act()
   {
       int cx = getWorld().getWidth()/2;
       int cy = getWorld().getHeight()/2;
       int speed= 5;
        
       turnTowards(cx,cy);
       int rotation = getRotation();
        
       setRotation(rotation + 89);
       move (speed);
        
   }
it will orbit the point of cx,cy I could make it adjust the turn in fact depending on speed using a little trig but I am too lazy.
danpost danpost

2014/4/12

#
Sounds like the radius is being determined by the speed. You are probably very limited on what the radius can be. Might be lucky to have a dozen or so possibles (I am guessing, here) for what the radius could be (in a world maybe as large as 600x600). One for each rate of speed up to whatever (12 or so).
lordhershey lordhershey

2014/4/12

#
I played around with it, it is not the best - a better way would be to have a distance you want to be from the object and then zip along, normalize distance, then adjust angle, you can get any radius and and speed that way. I am operating under the parameters that you want to make this object orbit a point. The only limitation to this method is you could not circle an object in any time faster than 4 (but not including) ticks even at infinite speed.
allyand17 allyand17

2014/4/13

#
How could I accomplish that? I wanted to make the object that orbited the center start at a particular position (differentiating positions, since I want multiple in different locations), but I'm not sure what the easiest way to do this is without using overly complicated math. It would also be nice if I could make it so that the radius could be a bit more "fine tuned" since I have fairly close radii and I need them to be in a fairly specific location.
lordhershey lordhershey

2014/4/13

#
I can goof around with it a little bit to see what is needed.
danpost danpost

2014/4/13

#
My method above requires no Maths at all. Grant me this: no matter what location your orbiters start at, they will be a specific radius and a specific angle from the center. These values could be passed to the constructor of the orbiter; or you could call methods in the class to set those values after creating them.
You need to login to post a reply.