Orbits in 3 Dimensions (tutorial)

I’ve been adding some neat features to SkyCars, including lasers, jump pads, and lava. But one of the most formidable development challenges so far has been orbits. By orbits, I’m talking about asteroids, comets, or even platforms that rotate around an arbitrary point in space. I’d like to share with you my solution for 3 dimensional orbits.

Visualizing the orbit path

Let us imagine an asteroid with a circular orbit of an arbitrary orientation (or axis angle). We may visualize the orbit path like so:

Note that the asteroid has a circular orbit of radius R around an origin point. At any given time, the asteroid will have the coordinates (Rx, Ry, Rz). We will treat R as a vector from the center of the orbit’s path to the asteroid’s position. Applying the distance formula, we know that R = sqrt ( Rx2 + Ry2 + Rz2 ). However, we also know that R casts a shadow (or projection) onto the x/z plane, which we will call Rxz. We also know the distance of Rxz = sqrt ( Rx2 + Rz2 ). This will be important later.

Now I’m going to jump ahead a bit so that we know what our goal is. If we want our asteroid to orbit the center point, we need to find out the orbit’s normal vector (N), which has Nx, Ny, Nz components and also originates from the orbit path’s center. Here’s what this looks like:

This vector describes an imaginary line perpendicular to the orbit plane. If we know the values of this vector, we can use a “rotate by axis angle” command on our asteroid. In Shiva, the command is simply:

object.rotateAxisAngle ( hObject, nAxisX, nAxisY, nAxisZ, nAngle, kSpace )

nAxisX, nAxisY, nAxisZ will take the values of Nx, Ny, Nz. nAngle is the asteroid’s orbital angular velocity, and kSpace should be set to global. In other game engines, there are similar commands. If you do not have such libraries, here is a good tutorial on how to perform such rotations using a rotation matrix. Shiva users: As object.rotateAxisAngle does not accept position offsets, I had to parent the asteroid to a helper object placed in the center of the orbit.

Now let’s discuss how to get from our asteroid vector R to the normal vector N. Notice that R and N are orthogonal (perpendicular. You may be tempted to say that if we know R, we can just rotate this vector back by 90 degrees to get N. However, we do not have enough information because there are an infinite number of normals orthogonal to R! Or if you prefer, we could spin N around the R axis by all values of the angle phi and still obtain an orthogonal normal. This is actually a good thing. We can choose our own value for phi and in effect choose the amount of bank or tilt in our orbit.

If we want to find N which is orthogonal vector to R, we can use the vector cross product. The cross product takes two input vectors (together which define a plane), and outputs an orthogonal vector (to this plane). R is our first input vector; we can use phi to determine the necessary secondary vector which we will call Q.

Let’s create a vector aligned with the Z+ axis called Phi0. First we’ll rotate it upward around the X axis by angle phi to reach an intermediate vector Phi1.

Next, we’ll rotate this vector around the Y-axis so that it is tangent to the asteroid. The result of this second transformation will give us Q.

In order to perform this second transform, we need to know the angle of Rxz. As this vector is composed of Rx and Rz, we can determine the angle we need to rotate Phi1 by to get Q, or angle rho. While we do not need to calculate this angle, we do need to make note of the following identities:

sin rho = Rz / Rxz
cos rho = Rx / Rxz

Also remember that Rxy = sqrt ( Rx2 + Rz2 ) and that Rx and Rz are given.
Doing a little mathemagics, we can determine the components of Q. The transformation from Phi0 to Phi1 has the components:

( 0, sin phi, cos phi )

When we add in the second transformation, the resultant vector Q takes on the form:

( cos phi * sin rho,
sin phi,
cos phi * cos rho )

We can simplify further using substitution:

( cos phi * Rz / Rxz ,
sin phi,
cos phi * Rx / Rxz )

Also note that Q is now tangent to the asteroid’s orbit. This means that we now have two valid input vectors, Q and R, which describe the plane of the orbit path! We can now use the vector cross product to solve for N:

N = Q × R

Don’t forget to normalize N (scale the vector to a magnitude of 1) when we’re done solving. Now just rotate the asteroid about the normal, which is the axis angle for our orbit path! We’re done.

The code

In Shiva, this all looks somewhat like this:

-- When initializing your rotating object:
local Rxz = math.sqrt ( Rx*Rx + Rz*Rz )
local Qx = math.cos ( phi ) * Rz/Rxz
local Qy = math.sin ( phi )
local Qz = math.cos ( phi ) * Rx/Rxz

local Nx, Ny, Nz = math.vectorNormalize (
   math.vectorCrossProduct ( Qx, Qy, Qz, Rx, Ry, Rz )
)

-- Store Nx, Ny, Nz somewhere. During the game loop, rotate the object using:
object.rotateAxisAngle ( hObject, Nx, Ny, Nz, dR, object.kGlobalSpace )

-- note: dR is the orbit speed that you choose.

A few final thoughts…

We have created a function that allows an object to orbit in a circular path of any orientation around a center point. However, if we choose a center point directly above the object, we cannot use the above method perform an orbit because vectors R and Q will either be identical or incorrect, meaning that N cannot be computed. A similar problem may occur with a center point directly below the object. I resolved this issue by manually setting appropriate values for Q so that it is parallel to the x/z plane.

If you see “circular path” as a constraint, you can achieve elliptical paths by having a center point that moves with time. Specifically, you would want a center point that moves back and forth along a line. The velocity of the center point along this line should be sinusoidal, with the same frequency (or dR) and phase as your orbit. You could even have the center point of your orbit move on an arbitrary path to create a complex orbit, such as a moon orbiting a planet orbiting a sun.

I would like to thank my good friend Kyle Jewhurst for helping me work through and verify the math on this topic. Also now that I’ve nailed down the orbit code, videos of SkyCars are coming soon!

78 Trackbacks

  1. [...] Uncategorized Very informative little article on the math behind plotting three dimensional orbits here http://www.nebulacafe.com/blog/?p=122 No comments for this entry [...]

  2. [...] « Orbits in 3 Dimensions (tutorial) [...]

  3. By payday loans in the uk on May 3, 2013 at 12:28 am

    payday loans in the uk…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  4. By payday loans on May 3, 2013 at 2:58 am

    payday loans…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  5. By payday loan on May 3, 2013 at 3:32 am

    payday loan…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  6. By payday loans uk on May 3, 2013 at 3:53 am

    payday loans uk…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  7. By electronic cigarettes on May 5, 2013 at 3:38 am

    electronic cigarettes…

    Un rapport am�ricain est venu constater l’effet n�faste des cigarettes �lectroniques cependant, to that of inhaled baccy smoke, Patch no baccy, green goddess, or burning is in reality mired in its surgical operation….

  8. By http://www.onlinecasinoshome.co.uk/ on May 5, 2013 at 1:01 pm

    http://www.onlinecasinoshome.co.uk/…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  9. By cigarette uk on May 6, 2013 at 6:36 pm

    cigarette uk…

    Why, it’s nearly as different, how foresightful a Magazine lasts varies, as comfortably….

  10. By RbxzIyEj on May 6, 2013 at 7:20 pm

    RbxzIyEj…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  11. By PPSOQqbh on May 6, 2013 at 7:20 pm

    PPSOQqbh…

    –…

  12. By LxNYmXAH on May 6, 2013 at 7:21 pm

    LxNYmXAH…

    –…

  13. By miu miu bags on May 6, 2013 at 9:48 pm

    miu miu bags…

    –…

  14. By prada bags on May 6, 2013 at 9:49 pm

    prada bags…

    –…

  15. By diablo 3 gold on May 7, 2013 at 12:06 am

    diablo 3 gold…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  16. By seo software on May 7, 2013 at 12:08 am

    seo software…

    –…

  17. By louis vuitton bags on May 7, 2013 at 2:23 am

    louis vuitton bags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  18. By where to get payday loans on May 7, 2013 at 8:46 am

    where to get payday loans…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  19. By celine bags on May 7, 2013 at 6:14 pm

    celine bags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  20. By nike lebron 10 on May 7, 2013 at 8:56 pm

    nike lebron 10…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  21. By burberry bags on May 7, 2013 at 11:21 pm

    burberry bags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  22. By 整形美容 on May 8, 2013 at 1:47 am

    整形美容…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  23. By chanel bags on May 8, 2013 at 4:12 am

    chanel bags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  24. By mulberry bags on May 8, 2013 at 6:48 am

    mulberry bags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  25. By coach outlet on May 8, 2013 at 10:16 am

    coach outlet…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  26. By hermes bags on May 8, 2013 at 2:49 pm

    hermes bags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  27. By electronic on May 8, 2013 at 3:34 pm

    electronic…

    Summation, any of those methods tends testament create vaporisation automatically….

  28. By cheap jordans on May 8, 2013 at 6:33 pm

    cheap jordans…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  29. By jeremy scott adidas on May 10, 2013 at 6:27 am

    jeremy scott adidas…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  30. By cheap soccer cleats on May 10, 2013 at 11:02 am

    cheap soccer cleats…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  31. By cheap prom dresses on May 10, 2013 at 4:21 pm

    cheap prom dresses…

    –…

  32. By wow gold on May 10, 2013 at 8:14 pm

    wow gold…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  33. By cheap louis vuitton bags on May 11, 2013 at 12:11 am

    cheap louis vuitton bags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  34. By mulberry sale on May 11, 2013 at 9:53 am

    mulberry sale…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  35. By longchamp bags on May 11, 2013 at 7:29 pm

    longchamp bags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  36. By michael kors bags on May 12, 2013 at 3:29 am

    michael kors bags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  37. By Cheap Ray Ban Sunglasses on May 12, 2013 at 7:41 am

    Cheap Ray Ban Sunglasses…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  38. By www.databytez.com on May 12, 2013 at 5:46 pm

    http://www.databytez.com...

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  39. By CHEAP OAKLEY SUNGLASSES on May 12, 2013 at 10:07 pm

    CHEAP OAKLEY SUNGLASSES…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  40. By cheap oakley on May 13, 2013 at 2:20 am

    cheap oakley…

    –…

  41. By lacoste outlet on May 13, 2013 at 2:20 am

    lacoste outlet…

    –…

  42. By Louis Vuitton Replica on May 13, 2013 at 11:45 am

    Louis Vuitton Replica…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  43. By coach bags on May 13, 2013 at 5:31 pm

    coach bags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  44. By NIKE PAS CHER on May 13, 2013 at 9:55 pm

    NIKE PAS CHER…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  45. By NIKE FRANCE on May 14, 2013 at 2:49 am

    NIKE FRANCE…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  46. By 2013 fitflop on May 14, 2013 at 4:57 am

    2013 fitflop…

    Though production and yields have risen, TSMC (TSM +1.1%) is still fully using up its 28nm chip manufacturing capacity thanks to strong demand from mobile clients, Digitimes reports. The report comes shortly after rival UMC warned of a Q1 operating los…

  47. By Ralph Lauren Polo on May 14, 2013 at 8:06 am

    Ralph Lauren Polo…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  48. By pig on May 14, 2013 at 2:02 pm

    pig…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  49. By Chaussures Christian Louboutin on May 14, 2013 at 7:16 pm

    Chaussures Christian Louboutin…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  50. By Chanel Handbags on May 15, 2013 at 12:12 am

    Chanel Handbags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  51. By birkenstockcanada.webs.com on May 15, 2013 at 1:05 am

    birkenstockcanada.webs.com…

    “This book will introduce you to the magic that is inside your blood vessels. It comes in the shape of a molecule, one of the simplest molecules found in nature. This molecule is nitric oxide, or NO, a substance so powerful that it can actually protec…

  52. By QmlDDyHs on May 15, 2013 at 2:10 am

    QmlDDyHs…

    –…

  53. By ahqvDpZa on May 15, 2013 at 2:11 am

    ahqvDpZa…

    –…

  54. By louis vuitton outlet on May 15, 2013 at 5:04 am

    louis vuitton outlet…

    Df4 louis vuitton outlet online JQj louis vuitton factory outlet GPq louis vuitton outlet store…

  55. By CHANEL OUTLET on May 15, 2013 at 6:01 am

    CHANEL OUTLET…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  56. By michael kors outlet on May 15, 2013 at 10:51 pm

    michael kors outlet…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  57. By HERMES REPLICA on May 16, 2013 at 6:11 am

    HERMES REPLICA…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  58. By ルイヴィトン on May 17, 2013 at 1:29 am

    ルイヴィトン…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  59. By 群发 on May 17, 2013 at 6:45 am

    群发…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  60. By vibram uk on May 17, 2013 at 9:14 am

    vibram uk…

    welcome to vibram five fingers store…

  61. By prada handbags on May 17, 2013 at 1:04 pm

    prada handbags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  62. By christian louboutin outlet on May 18, 2013 at 12:43 am

    christian louboutin outlet…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  63. By JIMMY CHOO OUTLET on May 18, 2013 at 7:54 am

    JIMMY CHOO OUTLET…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  64. By fake louis vuitton handbags cheap on May 18, 2013 at 1:02 pm

    fake louis vuitton handbags cheap…

    Genuinely when someone doesn’t know after that its up to other people that they will help, so here it occursOrbits in 3 Dimensions (tutorial) – NebulaCafe….

  65. By Discount Iphone 5 on May 18, 2013 at 3:03 pm

    Discount Iphone 5…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  66. By CELINE OUTLET on May 18, 2013 at 8:42 pm

    CELINE OUTLET…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  67. By LOUIS VUITTON PAS CHER on May 19, 2013 at 7:33 am

    LOUIS VUITTON PAS CHER…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  68. By Anonymous on May 19, 2013 at 10:16 am

    casinos…

    particularly knowledge of plot rules, scholarship some strategy, and unceasingly scholarship….

  69. By michael kors uk on May 19, 2013 at 11:51 am

    michael kors uk…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  70. By Cheap Beats By Dre on May 19, 2013 at 3:13 pm

    Cheap Beats By Dre…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  71. By how to buy on May 19, 2013 at 9:03 pm

    how to buy…

    Excellent site. A lot of useful information here. I am sending it to several friends ans additionally sharing in delicious. And certainly, thank you in your effort!…

  72. By Louis vuitton uk on May 20, 2013 at 3:00 am

    Louis vuitton uk…

    welcome to vibram five fingers store…

  73. By cheap louis vuitton handbags on May 20, 2013 at 3:47 am

    cheap louis vuitton handbags…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  74. By christian louboutin shoes on May 20, 2013 at 11:40 am

    christian louboutin shoes…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  75. By binary options brokers on May 20, 2013 at 12:53 pm

    binary options brokers…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  76. http://www.qietingqicai.com/plus/view.php?aid=38748…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  77. By hnUgjSxR on May 20, 2013 at 6:28 pm

    hnUgjSxR…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

  78. By Nike free run on May 21, 2013 at 1:04 am

    Nike free run…

    Orbits in 3 Dimensions (tutorial) – NebulaCafe…

Post a Comment

You must be logged in to post a comment.