Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 05 Oct 2012
Offline Last Active May 08 2014 08:18 AM

Topics I've Started

Spherical coordinates for orbital motion

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 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.

All orbiting objects start with an initial theta, phi, and radius values and also a 2D heading vector to specify the orbit.

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.Y = radius * (float)Math.Cos(phi);
pos.Z = radius * (float)Math.Sin(theta) * (float)Math.Sin(phi);

pos = radius * pos;

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:

Attached File  bad orbit.jpg   221.57KB   30 downloads

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

The expected orbit can be seen below:

Attached File  expected orbit.jpg   146.2KB   17 downloads

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