Jump to content
  • Advertisement
Sign in to follow this  

Moving to a Point on a Circle

This topic is 2758 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, been working on this issue for a little bit now and I seem to have hit a road block, it just isn't doing what I'm expecting it to do.

What I'm trying to accomplish is making the Cam move to a point a MaxDistance away from the Selected object. I don't understand why my current code isn't working, are my angles incorrect? What am I overlooking? I attached an image to help clarify what I'm trying to do.
Vector3 CalculateNewMoveToPosition(Vector3 Cam, Vector3 Selected, float MaxDistance)
Vector3 calculated = Vector3.zero;
float angle_between = 0;
float angle_mod = 0;
float dx = 0;
float dz = 0;

if (Cam.x >= Selected.x)
dx = Cam.x - Selected.x;
dx = Selected.x - Cam.x;
if (Cam.z >= Selected.z)
dz = Cam.z - Selected.z;
dz = Selected.z - Cam.z;

if (Cam.x < Selected.x)
angle_mod = Mathf.PI;
if (Cam.z < Selected.z)
angle_mod = -Mathf.PI;
if (Cam.x > Selected.x && Cam.z < Selected.z)
angle_mod = 2*Mathf.PI;

angle_between = Mathf.Rad2Deg * (Mathf.Abs(angle_mod - Mathf.Atan(dz / dx)));
calculated.x = Selected.x + (MaxDistance * Mathf.Cos(angle_between));
calculated.z = Selected.z + (MaxDistance * Mathf.Sin(angle_between));
calculated.y = Cam.y;

return calculated;

Any help would be greatly appreciated. Thank you very much.

Share this post

Link to post
Share on other sites
Uhm... maybe I'm missing something, but what exactly would be wrong with not putting up with any angles at all? There seems to be an unhealthy obsession with trigonometry where nobody needs it. (Also, all those confusing if's make my head hurt.. math for the most part doesn't generally doesn't work that way).

Look at what you're doing. You already KNOW the vector and instead of simply scaling it to the right length, you bash the problem with the trig club to calculate angles you then use to calculate a shorter version of that same vector. That is simply overcomplicating the problem with a pointless detour.

const Vector3 directionToCam = (Cam - Selected).normalize();
return Selected + (directionToCam * MaxDistance);

//If you really want to be on a circle (not a sphere):
//However keep in mind that if you move it to that position, you will no longer be centered on the object.

Vector3 directionToCam(Cam.x - Selected.x, 0, Cam.z - Selected.z);
Vector3 Calculated = Selected + (directionToCam.normalize() * MaxDistance);
Calculated.y = Cam.y;
return Calculated;

Share this post

Link to post
Share on other sites
Ah yea, I agree. Thanks for that, I need to start using Vector algebra more.

On a side note, I just got the original code working... I never needed to convert to degrees. /facepalm


Thank you. :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!