1
votes

I am building a 3D simulation.

I am using one class to display sun, planets & moon with different size, texture, speed and orbit. Here's my code which does the magic job of calculating rotation + revolution

world = Matrix.CreateScale(size,size,size) * Matrix.CreateTranslation(0,0,0) * 
 Matrix.CreateRotationY(rotation) * 
 Matrix.CreateTranslation(position) * 
 Matrix.CreateRotationY(revolution); 
  • size is my variable to scale the object individually
  • rotation is my variable to rotate the object around its own axis
  • position is my variable to position the planet on orbit, new Vector3(240,0,0)
  • revolution is my variable to revolve the planet around the origin

now i want the display the moons around earth and around other planets but i just can't figure out how to do it. In terms of Matrix Multiplication.

The complication is the moon's revolution is around a planet which is not on origin and is always changing.

How to do it ?

2
It's highly unclear what your matrices are doing, but I suggest you apply the revolution of the moon around its planet, then the revolution of the planet around the sun.Beta
this is my technique of revolution round origin, i need a technique for revolution around another body, i am unable to figure it out.Moon
Not sure, but try multiplying by the planet's position matrix instead of the origin. What that should do is shift your calculation so instead of calculating where the moon is in world space, you're doing it in the planet's space and then translating it to world space.fire.eagle

2 Answers

2
votes

In your Draw method, after you've drawn the planet/moon that you want to orbit around:

Matrix satellite = Matrix.Identity
                 * Matrix.CreateScale(orbitScale)
                 * Matrix.CreateRotationY(orbitSpin)
                 * Matrix.CreateTranslation(0f, orbitDistance, 0f)
                 * Matrix.CreateFromAxisAngle(orbitAxis, orbitRotation);
effect.View = satellite * camera.View;

So, in your Update method, you can then do:

orbitRotation += MathHelper.PiOver2 * (float)gameTime.ElapsedGameTime.TotalSeconds;
orbitRotation = MathHelper.WrapAngle(orbitRotation);
orbitSpin += MathHelper.TwoPi * (float)gameTime.ElapsedGameTime.TotalSeconds;
orbitSpin = MathHelper.WrapAngle(orbitSpin);

I've defined orbitAxis as a Vector3. orbitDistance, orbitRotation, orbitScale and orbitSpin as float's.

0
votes

i figured it out...

Translate The Origin to the Parent planet's Current Location

and keep doing it in the update method

world = Matrix.CreateScale(size,size,size)
        * Matrix.CreateTranslation(0,0,0) 
        * Matrix.CreateRotationY(rotation)
        * Matrix.CreateTranslation(position)
        * Matrix.CreateRotationY(revolution); 

world *= Matrix.CreateTranslation( parent.getPosition() );

what i am doing here is that i m revolving the moon around the origin (Sun) but i translate its origin to any planet's current location... this worked for me.