Tutorial 6 - Position, Orientaion, and Pivot Points

Tutorial 6 - Position, Orientaion, and Pivot Points

Top  Previous  Next

We have already seen Position, Orientation, and Pivot Points in Tutorial 5 - The Emitter.  In this tutorial, we will look at the classes that provide this functionality and show how they can be used for any objects, not just the Emitter.

 

Load the Tutorial 6 project and open up Game1.cs.

 

 

Position, Velocity, and Acceleration

 

The Position2D and Position3D classes provide position, velocity, and acceleration in 2 Dimensions and 3 Dimensions respectively.  The Position3D class is used for the Emitter's PositionData property.  When the Postion2D/3D Update() function is called, the Velocity is updated according to the Acceleration, and then the Position is updated according to the Velocity.  You can use these classes for any object that you want to have a position, velocity, and acceleration associated with, not just particles.

 

To use these classes, simply declare an instance of the class in your object's class.  For example, the Emitter class declares and instantiates the PositionData variable, which is of the type Position3D.  If you want your object to have an acceleration, set the Acceleration property.  If you want it to have a velocity, set the Velocity property.  From there just call the Update() function each frame, providing how much time has elapsed since the last frame as a parameter, then draw your object at the location specified by the Position property.

 

 

Orientation, Rotational Velocity, and Rotational Acceleration

 

To make rotation calculations easy, DPSF provides the Orientation2D and Orientation3D classes.  These classes provide properties and functions to specify Orientation, Rotational Velocity, and Rotational Acceleration.  The Orientation2D class provides properties and functions to rotate 2 Dimensional objects.  Because 2D objects always face the camera, they can only be rotated around the Z-axis (i.e. only roll rotations can be performed).  This means that the Orientation, RotationalVelocity, and RotationalAcceleration are all expresses as singe float values.  An Orientation value of zero means the object is not rotated at all, a value of Pi means it is rotated 180 degrees, and a value of 2*Pi means it is rotated 360 degrees.

 

The Orientation3D class expresses the Orientation as a Quaternion, and the RotationalVelocity and RotationalAcceleration as Vector3s.  As shown in Tutorial 5 - The Emitter, these properties can be set, and then the Orientation property can be updated accordingly by calling the Update() function each frame.  In this tutorial, we show instead how to rotate a 3D object by using the static functions provided by the Orientation3D class.  In this tutorial, the object we are rotating happens to be a particle in the particle system, but it could be any 3D object that expresses its orientation as a quaternion when using the Orientation3D class, and any object that expresses its orientation as a single float when using the Orientation2D class.  Because we needed a 3D particle to do 3D rotations, a Textured Quad is used in this example instead of a Sprite, which would only allow 2D rotations.

 

Near the very bottom of Game1.cs, Tutorial 6 shows how to update an object's orientation to rotate around the 3 axis about the object's center when the V, B, and N keys are pressed:

 

       if (KeyIsDown(Keys.V))

     {

               // Rotate the Particle around the Y-axis about its center

           cParticle.Orientation = Orientation3D.Rotate(Matrix.CreateFromYawPitchRoll(0.1f, 0, 0), cParticle.Orientation);

       }

 

       if (KeyIsDown(Keys.B))

       {

               // Rotate the Particle around the X-axis about its center

               cParticle.Orientation = Orientation3D.Rotate(Matrix.CreateFromYawPitchRoll(0, 0.1f, 0), cParticle.Orientation);

       }

 

       if (KeyIsDown(Keys.N))

       {

               // Rotate the Particle around the Z-axis about its center

               cParticle.Orientation = Orientation3D.Rotate(Matrix.CreateFromYawPitchRoll(0, 0, 0.1f), cParticle.Orientation);

       }

 

 

Pivot Points, Pivot Rotational Velocity, and Pivot Rotational Acceleration

 

To make rotations around a pivot point easy, DPSF provides the PivotPoint2D and PivotPoint3D classes.  These classes provide properties and functions to specify a Pivot Point (an arbitrary point in space) to rotate around, as well as a Pivot Rotational Velocity and Pivot Rotational Acceleration, which indicates how fast to rotate around the PivotPoint.  In addition to rotating the object around the pivot point, the classes provide functions and properties to update the object's orientation as well, so you can have an object's orientation rotate as it rotates around the pivot point, or simply just update the object's position as it rotates around the pivot point.

 

Near the very bottom of Game1.cs, Tutorial 6 shows how to update an object's position to rotate around the pivot point when the A, S, and D keys are pressed:

 

       if (KeyIsDown(Keys.A))

     {

          // Rotate the Particle around the Y-axis, without changing its Orientation

           cParticle.Position = PivotPoint3D.RotatePosition(Matrix.CreateFromYawPitchRoll(0.05f, 0, 0), msPivotPoint, cParticle.Position);

     }

         

    if (KeyIsDown(Keys.S))

     {

           // Rotate the Particle around the X-axis, without changing its Orientation

           cParticle.Position = PivotPoint3D.RotatePosition(Matrix.CreateFromYawPitchRoll(0, 0.05f, 0), msPivotPoint, cParticle.Position);

     }

 

    if (KeyIsDown(Keys.D))

     {

           // Rotate the Particle around the Z-axis, without changing its Orientation

           cParticle.Position = PivotPoint3D.RotatePosition(Matrix.CreateFromYawPitchRoll(0, 0, 0.05f), msPivotPoint, cParticle.Position);

     }

 

And below that it shows how to update an object's position and orientation to rotate around the pivot point when the T, Y, and U keys are pressed:

 

       if (KeyIsDown(Keys.T))

     {

           // Rotate the Particle around the Y-axis, changing its Position and Orientation

          PivotPoint3D.RotatePositionAndOrientation(Matrix.CreateFromYawPitchRoll(0.05f, 0, 0), msPivotPoint, ref cParticle.Position, ref cParticle.Orientation);

     }

 

    if (KeyIsDown(Keys.Y))

     {

           // Rotate the Particle around the X-axis, changing its Position and Orientation

          PivotPoint3D.RotatePositionAndOrientation(Matrix.CreateFromYawPitchRoll(0, 0.05f, 0), msPivotPoint, ref cParticle.Position, ref cParticle.Orientation);

     }

 

    if (KeyIsDown(Keys.U))

     {

           // Rotate the Particle around the Z-axis, changing its Position and Orientation

          PivotPoint3D.RotatePositionAndOrientation(Matrix.CreateFromYawPitchRoll(0, 0, 0.05f), msPivotPoint, ref cParticle.Position, ref cParticle.Orientation);

       }

 

If you are using actual instances of the PivotPoint2D or PivotPoint3D classes in an object's properties (not just calling their static functions to rotate an external object), then you can specify if the orientation should be rotated as well by setting the RotateOrientationToo property.

 

 

Remarks

 

Quaternions are used instead of Matrices for 3D orientation because they avoid the problem of gimbal lock.  Conveniently enough, XNA's Quaternion and Matrix classes provide functions to transform one into another, so if you are more comfortable using rotation matrices, they can easily be converted into a quaternion and vice versa.

 

For specific information on these classes' functions and properties see the DPSF API Documentation