Get angle from object in 3D space


/ Published in: ActionScript 3
Save to your folder(s)

If you have multiple objects orbiting a 3D object and you want to determine the angle of a specific object, this will help you determine what rotationX you need to set your camera to if you want to be directly behind it.

Note: It works completely in 3d space. So if your Y, Z, X are not inversed or directly related to your comparison object, the angle will be a little off.


Copy this code and paste it in your HTML
  1. var tempMatrix:Matrix3D = Matrix3D.inverse( new Matrix3D([0,0,0]) );
  2.  
  3. var vectorA:Number3D = new Number3D( 0,0,1 );
  4. Matrix3D.multiplyVector( *OBJECT*.getPlane().transform, vectorA );
  5. vectorA.normalize();
  6.  
  7. var vectorB:Number3D = new Number3D( 0,0,1 );
  8. tempMatrix = Matrix3D.clone( *CLICKED-OBJECT*.transform );
  9. Matrix3D.multiplyVector( tempMatrix, vectorB );
  10. vectorB.normalize();
  11.  
  12. var angle:Number = Math.acos( Number3D.dot(vectorB, vectorA) / (vectorA.modulo*vectorB.modulo) )*180/Math.PI;
  13.  
  14. trace( angle );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.