Wall bounces

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
waxor
Gnoblar
Posts: 7
Joined: Wed Mar 17, 2004 5:49 pm

Wall bounces

Post by waxor »

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?
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 7

Post by haffax »

waxor, this is not how it works, actually it is much easier. :)

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);
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.
team-pantheon programmer
creators of Rastullahs Lockenpracht
waxor
Gnoblar
Posts: 7
Joined: Wed Mar 17, 2004 5:49 pm

Post by waxor »

Thanks for the code, that did exactly what I needed.

I know about pointers, the delete was farther down, I nix'd it cause it was not important to the flow.

Now I just have to get the object to bounce off each other.....