# Objects move clockwise instead of counter-clockwise

I'm trying to write an orbital simulation with patched conics. For my sources I'm using https://orbital-mechanics.space/ and Fundamentals of Astrodynamics (Second Edition).

Both sources suggest to use the following method for determining the position with orbital elements and true anomaly.

1. Calculate the position in the perifocal frame $$r = \frac{h^2}{\mu} \frac{1}{1 + e\cos{v}}$$ $$p = r \cos{v} q = r \sin{v}$$

2. Rotate the perifocal coordinates by performing 3 rotations:

By performing 3 rotations in order:

1. Around the Z (up) axis by $$-\omega$$ (argument of periapsis)
2. Around the X (right) axis by $$-i$$ (inclination)
3. Around the Z (up) axis by $$-\Omega$$ (longitude of the ascending node)

Now using these methods - no matter what I do I can't get my orbits to look right. I'm comparing them using NASA Orbit Viewer - mostly looking at Mercury and Mars orbits due to their high eccentricity.

And either my orbits are flipped or moving clockwise.

For comparison here's how they should look like:

And here's how they actually look like (Mercury orbit is white, Mars orbit is red):

Cleary flipped.

The obvious solution seems to be to flip $$\sin$$ and $$\cos$$ in the perifocal position calculation. But this then breaks in other palces (i.e. calculating the position of the ascending node as the position at $$v = \omega$$) and I would rather not stray away from the literature.

What am I missing here?

The project in question is open source, the relevant pieces of code are:

Calculating position at true anomaly

Conversion from perifocal to equatorial

• Check your rotation matrices. Are they like the ones here? en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions I suspect you've transposed them. Nov 30, 2023 at 22:17
• Is your graphics rendering system right-handed or left-handed (e.g. Unity is left handed). This might account for the difference, depending on where you put the camera. See nbodyphysics.com/blog/gravity-engine-doc-1-3-2-2-2/… for more. Nov 30, 2023 at 22:25
• @nbodyphysics My coordinate system is right-handed y-up. The calculations are done in right-handed z-up. But no matter what I do here the planets either move clockwise or the system is flipped. Nov 30, 2023 at 23:13
• @PM2Ring matrices seem to be correct I've even pasted the cell-by-cell matrix from the book and it was the same as generated by my code. Nov 30, 2023 at 23:15
• I edited the question to include code links Nov 30, 2023 at 23:18