Moving an object in a circle

Hi guys! ;D

I’m not very good at trig ::slight_smile: and I’ve been reading up on some pretty useful stuff regarding sin and cos, etc.

I’m trying to make something move in circle motion but at the moment it moves really, really fast. I was thinking I need to use some speed variable but i’m not really sure how to do that with the trig code I have.

Code:



public void move(){
  angle += 1;
  x += (float) (20 * Math.sin(angle));
  y += (float) (20 * Math.cos(angle));
}

Any help/resources would be appreciated :slight_smile: thank you!


speed = .3f; // or something

public void move(){
  angle += 1;
  x += (float) (20 * Math.sin(speed * angle));
  y += (float) (20 * Math.cos(speed * angle));
}

This is equivalent to incrementing angle by less than 1 each update. Less change in angle per time = less angular velocity.
(Edit: Also yeah, you might have meant degrees but are using radians, which are ~57x greater in magnitude)

Do keep in mind that Java’s Math sin and cos methods use radians and not angles.

You’ll need to use Math.toRadians() to use angles.

e.g.


Math.sin(Math.toRadians(speed * angle));
Math.cos(Math.toRadians(speed * angle));

Ah crap I totally forgot to convert! I’ve done this before :persecutioncomplex:

I’ll pop some values in and see what happens. Thanks guys ::slight_smile:

Although this works, the code lost all meaning.

Why would you feed sin/cos an angle multiplied by speed ?

…wonders off, to higher ground

I agree, I meant it more as a confirmation of "I was thinking I need to use some speed variable" followed by farther explanation of what that actually means.
Optimal refactoring is left as an exercise to the reader implementer, as we don’t know the surrounding context.

Thanks for the help once again guys, I got it working and I’ve been playing around with the different features of cos and sin. I’ve noticed that I’ve never really seen tan being used much, is there a specific reason for this? I’ve only ever used atan2 for getting the angle of the player to the mouse ::slight_smile:

Oh also! Say I have 10 enemies in a circle and want them all to follow the same circle motion, would I just find the cos and sin of the next enemy in the list and make them move on that angle?