How to orbit around the Z-axis in 3D

c++, geometry, math, openframeworks

Solution

If you are orbiting around the z-axis, you are leaving your z-coordinate fixed and changing your x- and y-coordinates. So your first code sample is what you are looking for.

To rotate around the x-axis (or y-axes), just replace `x` (or `y`) with `z`. Use `Cos` on whichever axis you want to be 0-degrees; the choice is arbitrary.

If what you actually want is to orbit an object around a point in 3d-space, you'll need two angles to describe the orbit: its elevation angle and its inclination angle. See here and here. For reference, those equations are (where θ and φ are your angles)

x = x0 + r sin(θ) cos(φ) y = y0 + r sin(θ) sin(φ) z = z0 + r cos(θ)

Problem

I'm primarily a Flash AS3 dev, but I'm jumping into openframeworks and having trouble using 3D (these examples are in AS) In 2D you can simulate an object orbiting a point by using `Math.Sin()` and `Math.cos()`, like so ``` function update(event:Event):void { dot.x = xCenter + Math.cos(angle*Math.PI/180) * range; dot.y = yCenter + Math.sin(angle*Math.PI/180) * range; angle+=speed; } ``` I am wondering how I would translate this into a 3D orbit, if I wanted to also orbit in the third dimension. ``` function update(event:Event):void { ... dot.z = zCenter + Math.sin(angle*Math.PI/180) * range; // is this valid? } ``` An help is greatly appreciated.

Original source