How do I calculate the position on a circle based on a progress value?

Started by
8 comments, last by alvaro 4 years, 3 months ago

Currently I'm working on a orbit system for a game. I've got it so an object will move along a circle based on a progress value that'll be between 0.0 and 1.0 (0.5 being half way around the circle). I calculate this like this:

float angle = Mathf.Deg2Rad * 360 * t;
float xPos = Mathf.Sin(angle) * xAxis;
float yPos = Mathf.Cos(angle) * yAxis;

return new Vector3(xPos, yPos, 0.0f);

With t simply being deltatime and the xAxis/yAxis variables being the radius of the circle. What I'm a little stuck on currently though is how I could possibly get the progress around the circle based on a poisition. So if I have an object that hits the bottom of the circle, how do I calculate that to be a progress of 0.5?


Advertisement

To get the progress around the circle, you want the angle, and to get that Mathf should have a ‘atan2’ function which takes the x and y coordinates as parameters and gives an angle. You still need to take into account the convention of the angles(your code seems to take the angle from the positive y-axis, and most atan2 functions I have seen take the angle from the positive x-axis).

@RulerOfNothing So from what I currently understand, I ended with this:

float angle = Mathf.Deg2Rad * 360;
float xPos = pos.x + xAxis * Mathf.Cos(angle);
float yPos = pos.y + yAxis * Mathf.Sin(angle);

float progress = (Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg);
progress -= 90;

Where I get a point on the circle based on the impact point, and then get the angle of that collision point, shifting it by 90 degrees so the top is always 0, left of that is positive and right of that is negative. The issue now though is I'm not exactly sure how I can translate that into a progress between 0 and 1

[…]The issue now though is I'm not exactly sure how I can translate that into a progress between 0 and 1

Just divide by the same constant you multiplied to compute your angle (Mathf.Deg2Rad * 360).

Also, your code would be easier to read and easier to get right if you embraced radians and stop using degrees.

Codelyy said:

Currently I'm working on a orbit system for a game. I've got it so an object will move along a circle based on a progress value that'll be between 0.0 and 1.0 (0.5 being half way around the circle). I calculate this like this:

float angle = Mathf.Deg2Rad * 360 * t;
float xPos = Mathf.Sin(angle) * xAxis;
float yPos = Mathf.Cos(angle) * yAxis;

return new Vector3(xPos, yPos, 0.0f);

With t simply being deltatime and the xAxis/yAxis variables being the radius of the circle. What I'm a little stuck on currently though is how I could possibly get the progress around the circle based on a poisition. So if I have an object that hits the bottom of the circle, how do I calculate that to be a progress of 0.5?


your “t” should be in 0-1 range… should not be deltaTime…

actually it should be t+= deltaTime;

fleabay said:

Enzio599 said:
float angle = Mathf.Deg2Rad * 360 * t;

That doesn't look right. What is the 360 for? Try pi*2 if your logic is correct.

`Mathf.Deg2Rad * 360' is a very complicated way to say `pi*2'.

Enzio599 said:

Codelyy said:

Currently I'm working on a orbit system for a game. I've got it so an object will move along a circle based on a progress value that'll be between 0.0 and 1.0 (0.5 being half way around the circle). I calculate this like this:

float angle = Mathf.Deg2Rad * 360 * t;
float xPos = Mathf.Sin(angle) * xAxis;
float yPos = Mathf.Cos(angle) * yAxis;

return new Vector3(xPos, yPos, 0.0f);

With t simply being deltatime and the xAxis/yAxis variables being the radius of the circle. What I'm a little stuck on currently though is how I could possibly get the progress around the circle based on a poisition. So if I have an object that hits the bottom of the circle, how do I calculate that to be a progress of 0.5?

In C++ it would be this:

constexpr double Tau = std::atan(1.0) * 8.0; // a.k.a. `2*pi'

double angle = std::atan2(xPos, yPos);
if (angle < 0.0)
  angle += Tau;
double progress = angle / Tau;

fleabay said:

alvaro said:
Mathf.Deg2Rad

I see now. Mathf.Deg2Rad is a const that equals pi/180. I wouldn't say it's very complicated unless you were being facetious. ?

OK, it's not very complicated. But it makes no sense to bring degrees into a conversion from [0,1] to [0,2*pi].

As I've said many times in these forums, you should try to not use angles if at all possible (modulus-1 complex numbers are a better alternative most of the time, often much much better). If you have to use angles, use radians. If you need to display an angle in degrees to the user, do the conversion as close to the displaying code as possible; but for calculations keep things in radians.

This topic is closed to new replies.

Advertisement