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?
Wall bounces
-
- OGRE Retired Moderator
- Posts: 4823
- Joined: Fri Jun 18, 2004 1:40 pm
- Location: Berlin, Germany
- x 7
waxor, this is not how it works, actually it is much easier. 
And you should read a bit about the basics of pointers, references and how to use these. Your code could have been crashed.
Quaternion::fromAxes(Vector3*) expects a pointer to an array of vectors with at least three vectors in it. you provided only one. And when you create something with new you have to delete it when you don't need it anymore. You can get a pointer to a variable of yours with the & operator, so &vec is a pointer to a vec, you don't need to create something with new, to get a pointer to it.

Code: Select all
// Get the facing of the ship,
const Quaternion& facing = node->getWorldOrientation();
Vector3 vec = Vector3(0,0,-1);
vec = facing * vec;
// reflect that node against the proper wall and point the ship accordingly.
node->setDirection(vec.reflect(Vector3(-1,0,0)), Node::TS_WORLD);
Quaternion::fromAxes(Vector3*) expects a pointer to an array of vectors with at least three vectors in it. you provided only one. And when you create something with new you have to delete it when you don't need it anymore. You can get a pointer to a variable of yours with the & operator, so &vec is a pointer to a vec, you don't need to create something with new, to get a pointer to it.
-
- Gnoblar
- Posts: 7
- Joined: Wed Mar 17, 2004 5:49 pm