Rotations axis orientation indipendent using quaternions

Software: Away3D 4.x

Avatar
Luca, Sr. Member
Posted: 16 July 2013 08:51 AM   Total Posts: 230

The code smile

// GLOBAL VARS
   
public var CubeRotation:Vector3D = new Vector3D();
  public var 
PrevCubeRotation:Vector3D = new Vector3D();
  public var 
QRotationX:Quaternion = new Quaternion();
  public var 
QRotationY:Quaternion = new Quaternion();
  public var 
QRotationZ:Quaternion = new Quaternion();
  
  public var 
QAccum:Quaternion = new Quaternion();
  public var 
Mat:Matrix3D = new Matrix3D();
  
   
// CONVERSIONE GRADI > RADIANTI
  
protected const DEGREES_TO_RADIANS:Number = Math.PI / 180.0;
 
   
// SET MATH 11, 22, 33, 44 to 1 
   
Mat.identity();
 
  
/**
   * PITCH = Z
   * 
   */
  
public function SetPitch( angle:Number )
  
{
   
var DRotationAngle:Number = ( CubeRotation.z - PrevCubeRotation.z ) * DEGREES_TO_RADIANS;
   
   
QRotationZ.copyFrom( QAccum );
   
QRotationZ.fromAxisAngle( Vector3D.Z_AXIS, DRotationAngle );
   
QAccum.multiply( QAccum, QRotationZ );
   
QAccum.normalize();
   
PrevCubeRotation.z = CubeRotation.z;
  
}
  
  
/**
   * YAW = Y
   * 
   */
  
public function SetYaw( angle:Number )
  
{
   
var DRotationAngle:Number = ( CubeRotation.y - PrevCubeRotation.y ) * DEGREES_TO_RADIANS;

   
QRotationY.copyFrom( QAccum );
   
QRotationY.fromAxisAngle( Vector3D.Y_AXIS, DRotationAngle );
   
QAccum.multiply( QAccum, QRotationY );
   
QAccum.normalize();
   
PrevCubeRotation.y = CubeRotation.y;
  
}
  
  
/**
   * ROLL = X
   * 
   */
  
public function SetRoll( angle:Number )
  
{
   
var DRotationAngle:Number = ( CubeRotation.x - PrevCubeRotation.x ) * DEGREES_TO_RADIANS;

   
QRotationX.copyFrom( QAccum );
   
QRotationX.fromAxisAngle( Vector3D.X_AXIS, DRotationAngle );
   
QAccum.multiply( QAccum, QRotationX );
   
QAccum.normalize();
   
PrevCubeRotation.x = CubeRotation.x;
  
}
  
  
// APPLY THE TRANSFORM TO THE OBJECT...
  
OrientationCube.Mat = OrientationCube.QAccum.toMatrix3D();
  
OrientationCube.Cube.transform = OrientationCube.Mat; 
   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X