Blog

JigLibFlash Object Rotation – Pitch, Yaw and Roll

March 27, 2009 by Devin Reimer

JigLibFlash Rotation Example Screenshot

I get asked the following question a lot: “How do I rotate a JigLibFlash object?”. I do understand why people have this question as rotating pieces of geometry in JigLibFlash is not very straight forward. It also can end up being kind of challenging if your not familiar with exactly how rotation matrix work. The bottom line is it is not as easy as Papervision3D ( ex: pitch(10) ). So instead of me trying to explain any formulas I decided to add a few functions to the RigidBody class in JigLibFlash to make it easier for people to use.  I have created the following functions.

public static var toDEGREES:Number = 180/Math.PI;
public static var toRADIANS:Number = Math.PI/180;
 
public function pitch(angleDeg:Number):void
{
	SetOrientation(JMatrix3D.multiply(CurrentState.Orientation, JMatrix3D.rotationX(angleDeg*toRADIANS)));
}
 
public function yaw(angleDeg:Number):void
{
	SetOrientation(JMatrix3D.multiply(CurrentState.Orientation, JMatrix3D.rotationY(-angleDeg * toRADIANS)));
}
 
public function roll(angleDeg:Number):void
{
	SetOrientation(JMatrix3D.multiply(CurrentState.Orientation, JMatrix3D.rotationZ(angleDeg*toRADIANS)));
}
 
public function setRotationsDegrees(xRotation:Number, yRotation:Number, zRotation:Number):void
{
	SetOrientation(JMatrix3D.euler2matrix(new JNumber3D(xRotation, yRotation, zRotation)));
}
 
public function get rotationX():Number
{
	return JMatrix3D.matrix2euler(CurrentState.Orientation).x;
}
 
public function get rotationY():Number
{
	return JMatrix3D.matrix2euler(CurrentState.Orientation).y;
}
 
public function get rotationZ():Number
{
	return JMatrix3D.matrix2euler(CurrentState.Orientation).z;
}

You can copy this into the RigidBody Class within JigLibFlash (jiglib/physics/). Or you can click here to download a modified version of the RigidBody class to add to your JigLibFlash source. Once complete you can now use these functions from any standard JigLibFlash geometry object.

What you might notice is this source is missing a set function for rotationX, rotationY and rotationZ. I have included commented out version of my function in the source, but be advised that they are commented out because they can cause some weird results when rotating on more then one axis. You are free to use them, but you have been warned. I’m not a math genius so if anyone has any optimizations or additions please let me know.

These functions should hold everyone over until the JigLibFlash team has finished its rewrite of this project.  To read more about what the new syntax will be when completed, check out this recent post by drojdjou.
http://code.google.com/p/jiglibflash/wiki/New_API_Very_Short_Tutorial

To play the demo click here.

To get the demo source click here.

Update: As of April 12 (rev. 81), these functions are now part of JigLibFlash source, so you will no longer need to copy in these functions into your project. :)

3 Responses to "JigLibFlash Object Rotation – Pitch, Yaw and Roll"

  1. rkrasky says:

    Hi! Thanks a lot for your code… but I copy and paste it and it didn’t work.
    The as file are One rotation is no problem, but if i change 2 values the result is weierd… i work with the actual svn version of jiglibflash… thanks!

  2. Devin says:

    Hi Rkrasky

    The issue you are having is due to you apply one rotation, then apply another. What happens using this code is unlike papervision it apply the first one, then applies the second rotation to the first rotation. So as long as you keep that in mind it works good.
    That being said as of last night (April 6), a new version of JigLibFlash launched, and it now contains good version of RotationX, RotationY and RotationZ functions so you can use them. It does not contain pitch, roll and yaw functions yet. So I will work on creating new versions of these functions to work with the new verison of JigLibFlash.

  3. anony says:

    I love you. Thank you for this.

Leave a response