Wall bounces
Posted: Mon Feb 21, 2005 6:53 am
I need to have an object bounce off of a wall, much like in pong.
Assume the object is bouncing off a wall with normal (-1,0,0), so the wall on the positive x axis (the right side of the screen if you will). I am using razor.mesh which has a default facing of (0,0,-1).
The object is moving along at some speed. I can gets its facing as a quaternions and multiply that by Vector3(0,0,-1) to get its global facing.
I then reflect the facing vector off the normal of the wall. Then I use Quaternion::FromAxes() to convert the reflected vector to a facing and assign that to the object that is moving.
Right? ok. Sounds somewhat ok to me.
Here is my code:
// Get the facing of the ship,
const Quaternion& facing = node->getOrientation();
Vector3 vec = Vector3(0,0,-1);
vec = facing * vec;
// reflect that node against the proper wall
vec = vec.reflect(Vector3(-1,0,0));
//Put the new facing in a pointer to make Ogre Happy
const Vector3* vec_ptr = new Vector3(vec);
// Create a new quaternion, generated from the new axis.
Quaternion newFace;
newFace.FromAxes(vec_ptr);
const Quaternion& q = newFace;
// Set the new orientation
node->setOrientation(q);
This all sounds OK to me except the FromAxes call, looking at it I am not sure that it does what I think it does.
Any math people out there? Is my logic faulty?
Assume the object is bouncing off a wall with normal (-1,0,0), so the wall on the positive x axis (the right side of the screen if you will). I am using razor.mesh which has a default facing of (0,0,-1).
The object is moving along at some speed. I can gets its facing as a quaternions and multiply that by Vector3(0,0,-1) to get its global facing.
I then reflect the facing vector off the normal of the wall. Then I use Quaternion::FromAxes() to convert the reflected vector to a facing and assign that to the object that is moving.
Right? ok. Sounds somewhat ok to me.
Here is my code:
// Get the facing of the ship,
const Quaternion& facing = node->getOrientation();
Vector3 vec = Vector3(0,0,-1);
vec = facing * vec;
// reflect that node against the proper wall
vec = vec.reflect(Vector3(-1,0,0));
//Put the new facing in a pointer to make Ogre Happy
const Vector3* vec_ptr = new Vector3(vec);
// Create a new quaternion, generated from the new axis.
Quaternion newFace;
newFace.FromAxes(vec_ptr);
const Quaternion& q = newFace;
// Set the new orientation
node->setOrientation(q);
This all sounds OK to me except the FromAxes call, looking at it I am not sure that it does what I think it does.
Any math people out there? Is my logic faulty?