Author Topic: How to calculate the reflect vector  (Read 2395 times)

Offline luozi

  • byte
  • *
  • Posts: 20
    • View Profile
How to calculate the reflect vector
« on: October 26, 2011, 02:37:13 pm »
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!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12297
    • View Profile
    • http://www.jpct.net
Re: How to calculate the reflect vector
« Reply #1 on: October 26, 2011, 03:14:08 pm »
This should do it:

Code: [Select]
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.

Offline luozi

  • byte
  • *
  • Posts: 20
    • View Profile
Re: How to calculate the reflect vector
« Reply #2 on: October 26, 2011, 04:46:14 pm »
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!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12297
    • View Profile
    • http://www.jpct.net
Re: How to calculate the reflect vector
« Reply #3 on: October 26, 2011, 09:56:46 pm »
Quote
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?
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);

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: How to calculate the reflect vector
« Reply #4 on: October 27, 2011, 06:44:22 am »
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?

Offline luozi

  • byte
  • *
  • Posts: 20
    • View Profile
Re: How to calculate the reflect vector
« Reply #5 on: October 28, 2011, 02:48:20 am »
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!