How to calculate the reflect vector

Started by luozi, October 26, 2011, 02:37:13 PM

Previous topic - Next topic

luozi

I am trying to imlement a program that object A hits B, and then A goes  in the direction of the reflect vector.  I add a collisonlistener which can return the ID of the polygon that was in collision. Then I use  the object3D's function get the polygon's  normal. Now, I want to calculate the reflect vector. Is there a convenient method for this? By the way I don't entirely understand the function in the SimpleVector :     SimpleVecor rotate(SimpleVector rot).  Can you explain how the vector rotate, and how the rot give the  angle of the rotate? Thank you very much!

EgonOlsen

This should do it:


public SimpleVector getReflectionVector(SimpleVector incoming, SimpleVector normal)
  {
    SimpleVector cross = incoming.calcCross(normal);
    cross = normal.calcCross(cross);
    cross.scalarMul(2);
    cross = incoming.calcSub(cross);
    return cross;
  }


If it works, i'll add it to the API for the next version.

luozi

I'm so gratefull that you reply me so quickly, and I am so sorry I hold up so much time to reply you . It's just beacause my computer had a problem, and the files was deleted. I rewrite the code a moment ago, and your code woks very well, it would be nice that you add the function to the next version, thank you very much, it gives me and my classmates a  great help!

EgonOlsen

QuoteBy the way I don't entirely understand the function in the SimpleVector :     SimpleVecor rotate(SimpleVector rot).  Can you explain how the vector rotate, and how the rot give the  angle of the rotate?
It simply rotates around x,y,z like the values in the given vector. It's basically the same thing as rotateX(SimpleVector.x); rotateY(SimpleVector.y); rotateZ(SimpleVector.z);

AGP

I think his confusion is related to thinking of a SimpleVector as a point, so that rotating it doesn't seem to make sense. Is that right?

luozi

Oh, no, I know the function was to rotate around the x,y,z axis , but I can't understand that the vector's x,y,z axis's values will be the angles of the rotate., maybe my english is poor or my math, please excuse me my humble. Ho ho!