Ageia PhysX Math Primer 1. How do I take a position in world space and transform it into the local space of an actor? Basic implementation: NxActor* actor; … NxVec3 localPos, worldPos; NxMat34 mat, invMat; worldPos = NxVec3(0,0,1); mat = actor->getGlobalPose(); mat.getInverse(invMat); localPos = invMat * worldPos; Optimized implementation: NxVec3 localPos, worldPos; worldPos = NxVec3(0,0,1); localPos = actor->getGlobalPose() % worldPos; (From NxMat34.h: NX_INLINE NxVec3 operator% (const NxVec3 & src) const { NxVec3 dest; multiplyByInverseRT(src, dest); return dest; } ) 2. How do I take a position in the local space of an actor and transform it into world space? NxActor* actor; … NxVec3 localPos, worldPos; NxMat34 mat; localPos = NxVec3(0,0,1); mat = actor->getGlobalPose(); worldPos = mat * localPos; 3. How do I take a vector in world space and transform it into the local space of an actor? Copyright (c) 2002, 2003, 2004, 2005, 2006, AGEIA Technologies, Inc., USA. All rights reserved http://www.ageia.com 1 Basic implementation: NxVec3 localVec, worldVec; NxMat33 orient, invOrient; worldVec = NxVec3(0,0,1); orient = actor->getGlobalOrientation(); orient.getInverse(invOrient); localVec = invOrient * worldVec; Optimized implementation: NxVec3 localVec, worldVec; worldVec = NxVec3(0,0,1); localVec = actor->getGlobalOrientation() % worldVec; 4. How do I take a vector in the local space of an actor and transform it into world space? Basic implementation: NxVec3 localVec, worldVec; NxMat33 orient, invOrient; localVec = NxVec3(0,0,1); orient = actor->getGlobalOrientation(); orient.getInverse(invOrient); invOrient.setTransposed(); worldVec = invOrient * localVec; Optimized implementation: NxVec3 localVec, worldVec; localVec = NxVec3(0,0,1); worldVec = actor->getGlobalOrientation() * localVec; 5. How do I create a quaternion from an angle and an axis? NxVec3 v(0,1,0); NxReal ang = 90; NxQuat q; q.fromAngleAxis(ang, v); This quaternion is a rotation of the identity transformation about the vector, v, through the angle, ang. Copyright (c) 2002, 2003, 2004, 2005, 2006, AGEIA Technologies, Inc., USA. All rights reserved http://www.ageia.com 2 6. How do I get an orientation matrix from a quaternion? NxQuat q; … NxMat33 orient; orient.fromQuat(q); 7. How do I extract the 3 local axes of an actor? NxMat33 orient; NxVec3 xaxis, yaxis, zaxis; orient = actor->getGlobalOrientation(); orient.getRow(0, xaxis); orient.getRow(1, yaxis); orient.getRow(2, zaxis); 8. How do I build an orientation matrix from a vector? NxVec3 axis, binormal, normal; ... axis.normalize(); NxNormalToTangents(axis, binormal, normal); NxMat33 orient(normal, axis, binormal); 9. How do I use Euler Angles to set the orientation of an actor? NxActor* actor; ... // Euler angle NxVec3 eulerAngle = NxVec3(90,90,30); // Quaternion NxQuat q1, q2, q3; q1.fromAngleAxis(eulerAngle.x, NxVec3(1,0,0)); q2.fromAngleAxis(eulerAngle.y, NxVec3(0,1,0)); q3.fromAngleAxis(eulerAngle.z, NxVec3(0,0,1)); // // // NxQuat q; q = q3*q2*q1; // Use global axes q = q1*q2*q3; // Use local axes // Orientation matrix Copyright (c) 2002, 2003, 2004, 2005, 2006, AGEIA Technologies, Inc., USA. All rights reserved http://www.ageia.com 3 NxMat33 orient1, orient2, orient3; orient1.fromQuat(q1); orient2.fromQuat(q2); orient3.fromQuat(q3); NxMat33 orient; orient = orient3*orient2*orient1; // Use global axes // orient = orient1*orient2*orient3; // Use local axes // 1. Set actor orientation from NxMat33 actor->setGlobalOrientation(orient); // // // 2. Get NxMat33 from NxQuat orient.fromQuat(q); actor->setGlobalOrientation(orient); // 3. Set actor orientation directly from NxQuat // actor->setGlobalOrientationQuat(q); PhysX does its rotations in a right-handed manner. In the uncommented code, I assume that you're rotating your object about the global axes (meaning you need to multiply the transformations M3*M2*M1). If you want to rotate your object about its local axes, you need to uncomment the local axes code (M1*M2*M3). We use NxQuat::fromAngleAxis() to get the 3 quaternions and then NxMat33::fromQuat() to convert the quaternions into 3 3x3 matrices which we multiply together to get our final orientation matrix (1). You can also multiply your quaternions together, then convert the final quaternion into your final orientation matrix (2). Or, you can bypass matrices altogether and set the orientation of the actor using NxActor::setGlobalOrientationQuat() (3). 10. How do I rotate a vector about another vector? You can rotate a vector, v, around another vector, w, like this: NxVec3 w(0,0,1) NxQuat q; NxReal ang = 30; q.fromAngleAxis(ang, w); NxVec3 v(1,0,0); q.rotate(v); We basically build a quaternion, q, that is a rotation of the identity orientation matrix about the vector, w, through the angle, ang. (see #5 above) Copyright (c) 2002, 2003, 2004, 2005, 2006, AGEIA Technologies, Inc., USA. All rights reserved http://www.ageia.com 4 In this case, w = (0,0,1) and ang = 30. The vector we rotate is v = (1,0,0). We then use NxQuat::rotate() to rotate the vector, v, about w through ang. Copyright (c) 2002, 2003, 2004, 2005, 2006, AGEIA Technologies, Inc., USA. All rights reserved http://www.ageia.com 5