Game Development Community

Falling Tree: Rotation Matrix and Impact Vector

by Derk Adams · in Torque Game Engine · 06/09/2006 (10:33 am) · 4 replies

Greetings,

I understand matrices in concept, but not well enough to actually make them do what I want.

I have an object (a tree) at a position with a rotation matrix of "0 0 1 0".
I have a normalized vector "x y 0" defining the impact direction (I am not using z information).

I would like to rotate the tree 90 degrees, i.e. make it fall, in the directon of impact. But I don't know how to apply the vector to the matrix (especially in pieces to make it happen over time).

I have been unable to find any solution by searching the forums (although I might not have searched on the right things).

Any suggestions?

Thanks.

#1
06/09/2006 (10:51 am)
You want the tree to rotate around an axis that's perpendicular to the direction of impact, i think.
so, take the cross-product of your impact direction with the "up" vector - aka "0 0 1".
this will give you a vector perpendicular to both "impact" and "up".
use that vector as the first 3 numbers in your rotation.
if it rotates backwards, use minus 90, or switch the order of the inputs to the corss product.
#2
06/09/2006 (2:28 pm)
Orion,

Thanks. It was enough to get me to the solution. I also found out that I had solved it the "hard" way as the transform information got messed up over the 32 ticks for the animation. I found that I had to send the transform data through the schedule to force it to "fall correctly."

For any who wander by later the script is:
function Tree::fall(%this, %obj, %collObj)
{
  if (isObject(%collObj)) {
    %point = %obj.getPosition();
    %newVec = VectorSub(%point,%collObj.getPosition());
    %newVec = getWords(%newVec,0,1) SPC 0;
    %newVec = VectorNormalize(%newVec);
    %newRot = VectorCross(%newVec,"0 0 1");
    %newTrans = %point SPC %newRot SPC "1.57";
    %obj.setTransform(%newTrans);
  }
}

Thanks.
#3
06/09/2006 (2:30 pm)
Huh. glad you got it working, and kudos for posting the code!
#4
06/09/2006 (2:44 pm)
Yes, thank you for that code snippet, that will undoubtedly come in handy for my calculus-deficient brain.