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

2012/4/11

Moving an Object in circle

USBest USBest

2012/4/11

#
Hi! In my little crab scenario I want to add a crocodile, that moves in circle. I've got:
public void act()
{     if(this!=null)
   turn(15);
     move(2);;
        EatCrab();            }
And it more walks in an polygon, than in circle. Where is my mistake?
nccb nccb

2012/4/11

#
You are turning too much each frame. I'd try "turn(4);", which will produce something more like a circle. The less degrees you turn each frame, the smoother the circle. Conversely, the sharper you turn, the less like a circle it will be. If you turn 15 degrees each frame, you'll make a (360/15=24) 24-sided shape. But 4 degrees will be a (360/4=90) 90-sided shape, which is getting indistinguishable from a circle. A few other things: - You don't need the "if (this != null)" check. "this" can never be null in Java (unlike C++) - You've got two semi-colons after move.
USBest USBest

2012/4/11

#
Thank you nccb. It works better, now!
USBest USBest

2012/4/11

#
I changed move(2) to move(1). The Crocodile still turns. But the way, it walks is an octagon?
Busch2207 Busch2207

2012/4/11

#
Well... That is cause. the standard getX() and getY() methods return an integer and the method setLocation expects two integer. Now, when you will move 1 pixel, the computer computer calculates the next Location from xour location with getX() and getY(), which are both integers... For example x=10 and y=10. And your rotation is for e.g something between 60 and 70.... Now the computer calculates for x =10.666...and for y 10.333... but this two numbers are decimal numbers... and he will round them to integers... so he has for x=11 and for y=10... so you next location will be x=11 and y=10 and when he calculates the next time, he takes 11 and 10 as starting prarameter... So if you want a perfect circle, you have to write a code, that is using double numbers, or to get a better circle use a higher number (e.g. 4 or 5)
nccb nccb

2012/4/11

#
Ah yes, Busch2207 has a good answer. Moving 1 pixel, you can only move at 0 degrees, 90 degrees or 45 degrees, but nothing inbetween. For such slow-moving objects, you need to keep track of the positions as floating-point. The Greenfoot 2.2.0 beta (see other forum thread), which will be finalised in the next week or two, has an Import Class mechanism where you can import a SmoothMover class. You can use that as a parent class, and it will handle all the sub-pixel precision, which would solve your problem when moving at slow speeds.
USBest USBest

2012/4/11

#
Ah ok! First I was a bit confused, but now I understand it! Thank you busch and nccb
Busch2207 Busch2207

2012/4/25

#
Hey USBest! If you still want to let your crocodile move in a perfect circle, check out this scenario from SPower: http://www.greenfoot.org/scenarios/4873
You need to login to post a reply.