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

2012/3/27

How to make something circle?

kiarocks kiarocks

2012/3/27

#
I want to make my ship circle another ship, and the turnTowards method was not what I was looking for. How do I make it keep the same distance while circling and point to the ship?
Morran Morran

2012/3/27

#
Let's see. I think that's going to use some trigonometry. You're going to need a variable, an "int" or a "double", to keep track of the ship's distance along the circle. Call it "distanceAlongCirc". Then you have to update it by a certain number of degrees each frame. Than you need to update the position. It should come together like this:
public void circleAroundPoint(int otherX, int otherY)
{
   distanceAlongCirc += 25;
   int newX = cos(distanceAlongCirc) * radius;
   int newY = sin(distanceAlongCirc) * radius;

   setLocation(otherX + newX, getY() + newY);
}
Where radius is the size of the circle you want the ship to trace. You'll also need "sin()" and "cos()" methods that use degrees and not radians. I think they should be in Greenfoot's "Rotator" class. I hope this helps, kiarocks!
Busch2207 Busch2207

2012/3/27

#
Take a look at the Number class in this scenario: http://www.greenfoot.org/scenarios/3422 there is a method called 'LocationAroundPoint'. Maybe this help you! (I'm although using it! x)
kiarocks kiarocks

2012/3/28

#
Thanks Morran! But what do I hand the method? My x and y, his x and y, or my x minus his x and my y minus his y?
kiarocks kiarocks

2012/3/28

#
NVM! His x and his y :)
danpost danpost

2012/3/28

#
@kiarocks, are you there? Sorry, time is up!!
kiarocks kiarocks

2012/3/28

#
For what?
kiarocks kiarocks

2012/3/28

#
New question: how do I make an arrow pint at something off screen, but without going of screen?
mik mik

2012/3/30

#
To answer your first question: You could use turnTowards and then turn(90). You would always move at a tangent...
You need to login to post a reply.