简体   繁体   中英

How to orbit around the Z-axis in 3D

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.

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 = x 0 + r sin(θ) cos(φ)
y = y 0 + r sin(θ) sin(φ)
z = z 0 + r cos(θ)

I would pick two unit perpendicular vectors v, w that define the plane in which to orbit, then loop over the angle and pick the proper ratio of these vectors v and w to build your vector p = av + bw.

More details are coming.

EDIT:

This might be of help

http://en.wikipedia.org/wiki/Orbit_equation

EDIT: I think it is actually

center + sin(angle) * v * radius1 + cos(angle) * w * radius2

Here v and w are your unit vectors for the circle.

In 2D they were (1,0) and (0,1).

In 3D you will need to compute them - depends on orientation of the plane.

If you set radius1 = radius 2, you will get a circle. Otherwise, you should get an ellipse.

如果您绕Z轴旋转,那么您只需执行第一个代码,并保持Z坐标不变。

如果你只是想让轨道发生在一个有角度的平面并且不介意它是椭圆形的,你可以做一些像z = 0.2*x + 0.2*y ,或者你想要的任何组合,在你确定了x和y之后坐标。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM