• Create Account

#ActualShervanator

Posted 05 October 2012 - 08:47 AM

Hi, I've been working on getting some object to orbit around another object by using a spherical coordinate system. This has worked fine when the object is orbiting horizontally and vertically, however if you try to get the object orbiting diagonally it runs into issues with converting the spherical coordinates back to Cartesian coordinates.

Then every frame this information is updated in the following way:

direction = Vector2.Normalize(direction);
theta += timeDelta * direction.X * orbitSpeed;
phi -= timeDelta * direction.Y * orbitSpeed;

pos.X = radius * (float)Math.Cos(theta) * (float)Math.Sin(phi);
pos.Z = radius * (float)Math.Sin(theta) * (float)Math.Sin(phi);

pos.Normalize();


Where direction is the heading direction, radius is the distance away from the centre, timeDelta is the time since the last frame, orbit speed is a speed constant for the orbit, theta is the angle about the y-axis, phi is the angle about the x-axis, and pos is the final position vector for the object.

This works fine when direction = (1, 0) or (0, 1), however when it is made to equal (1, 1) then the object does weird orbits and it seems to be due pos.z not being calculated correctly.

Below is an image of the problem:

The white repeating boxes show the motion of the bad orbit.

The expected orbit can be seen below:

[attachment=11653:expected orbit.jpg]

I will be very happy with any help I could get,

Thanks!

#1Shervanator

Posted 05 October 2012 - 08:39 AM

Hi, I've been working on getting some object to orbit around another object by using a spherical coordinate system. This have worked fine when the object is orbiting horizontally and vertically, however if you try to get the object orbiting diagonally it runs into issues with converting the spherical coordinates back to Cartesian coordinates.

Then every frame this information is updated in the following way:

direction = Vector2.Normalize(direction);
theta += timeDelta * direction.X * orbitSpeed;
phi -= timeDelta * direction.Y * orbitSpeed;

pos.X = radius * (float)Math.Cos(theta) * (float)Math.Sin(phi);
pos.Z = radius * (float)Math.Sin(theta) * (float)Math.Sin(phi);

pos.Normalize();


Where direction is the heading direction, radius is the distance away from the centre, timeDelta is the time since the last frame, orbit speed is a speed constant for the orbit, theta is the angle about the y-axis, phi is the angle about the x-axis, and pos is the final position vector for the object.

This works fine when direction = (1, 0) or (0, 1), however when it is made to equal (1, 1) then the object does weird orbits and it seems to be due pos.z not being calculated correctly.

Below is an image of the problem: