Body rotation and moving in facing direction

Burkey

23-04-2007 23:44:30

Hi all,

I'm a complete newbie to NxOgre, however given the fact that I need to have a small demo done by next week I'm having to learn pretty quick. Anyway, my problem is as follows: I have a tank body which I want the player to be able to rotate (at a steady rate, as opposed to adding torque) and then move forward in the direction which the tank is facing. I found trying to rotate the tank to be extremely difficult, but settled upon something that worked:

if(isKeyDown(P1_LEFT)){

//rotate half a degree each frame, since Quaternion(sqrt(0.5), 0, 0, sqrt(0.5)) is 90' rotation around Z axis
quat = p1Tank->getGlobalOrientation() * Ogre::Quaternion(sqrt(0.5), 0, 0, sqrt(0.5/90)/2);

p1Tank->setGlobalOrientation(quat);
}


I'm pretty sure theres a better way to do this, but its all I have that works for now. My next question is how can I get the tank to move at a steady rate (again, not like adding forces) along a flat plane in the direction it is facing? I've tried many different things, such as multiplying the X and Z position vector by the X and Z rotation values (obviously my 3D mathematics is rusty!), but none are giving the desired result.

Any help is much appreciated, and if you could make it as clear as possible that would also be helpful :lol: This is obviously out of my programming league for now, but unfortunately I do not have a choice.

luis

24-04-2007 08:27:58

My next question is how can I get the tank to move at a steady rate
You have to multiply everything by the elapsed time ;)

Quaternion in Ogre has a nice constructor with Angle/ axis params...
try something like this:

using namespace Ogre;
const Real angularSpeed = 0.01;
Quaternion Q( Radian( angularSpeed*timeSinceLastFrame ), Vector3::UNIT_Y );
p1Tank->setGlobalOrientation( p1Tank->getGlobalOrientation()*Q );

Burkey

25-04-2007 10:42:27

Ok, thanks, I've used the following to rotate my tank:

if(isKeyDown(P1_LEFT)){
//multiply current orientation quaternion by new one to rotate left
Quaternion Q( Degree(5), Vector3::UNIT_Z );
quat = p1Tank->getGlobalOrientation() * Q;

p1Tank->setGlobalOrientation(quat);
}


That works fine for rotating. However, how do I now get my tank to move in the direction it is facing when the player presses the up key? I've been reading the quaternion primer and still having no luck (tried the 2*Math::ACos(quat.z); mathematics but the results it give are boggling my mind, as the degree of rotation seems to be 180 when facing down, 90 when facing up, and ~240 when facing left). Any help would be much appreciated.

betajaen

25-04-2007 10:55:42

It goes something like this:


Vector3 directionToMove = Vector3::UNIT_X;
Vector3 movementVector = quat * directionToMove;


You should either translate the scene node or add it as a force to the body for it to move.

Burkey

25-04-2007 14:11:18

You guys are life savers. I really appreciate it, worked wonders, thank you!