Setting Rigid Rotation
by Charlie Sibbach · in Torque Game Engine · 02/13/2006 (10:37 pm) · 2 replies
Hey all,
I'm working on modifying the FlyingVehicle control code, and I'm trying to figure out the best way to get the effect I want. I'm emulating the control scheme from Escape Velocity, a great shareware game. In that game, your ship has inertia for velocity (confined to a 2D plane), and keeps moving in whatever direction you were last thrusting in, which is how physics normally work. But turning is a different matter- as long as you press the turn key, you turn at a defined rate, and when you let up, you stop turning. So it's all very simple, you thrust in the direction you are pointed, and you control your pointing direction using turn keys.
I've found the code I need to change to remove the mouse control and go to strictly keyboard control, in flyingVehicle.cc, in the functions updateMove() and updateForces(), mostly the latter. updateForces() does a lot of manuevering and in the end comes up with a force on the Rigid and a torque on the Rigid that represents the vehicle. I stripped out almost everything from updateForces(), until the only part that's doing any work is this (lots of commenting out of forces that no longer apply):
// Maneuvering jets
force += yv * (mThrust.y * mDataBlock->maneuveringForce);
force += xv * (mThrust.x * mDataBlock->maneuveringForce);
// Steering
Point2F steering;
steering.x = mSteering.x / mDataBlock->maxSteeringAngle;
steering.x *= mFabs(steering.x);
steering.y = mSteering.y / mDataBlock->maxSteeringAngle;
steering.y *= mFabs(steering.y);
torque -= xv * steering.y * mDataBlock->steeringForce;
torque -= zv * steering.x * mDataBlock->steeringForce;
//
mRigid.force = force;
mRigid.torque = torque;
Which actually works, sort of. The problem is that once I hit turn, I start to turn, but I never stop turning- in fact the turn slowly accelerates, even after the key is released. There's two ways I can think of to get the effect I want- generate a counter-torque to stop the spin dead, or skip the torque physics and just manually rotate mRigid. I'm leaning towards the latter option, as the first requires even more math, and I don't know a good way to easily trigger code on key release.
Unfortunately, and here's the heart of the problem, I took a 3D graphics class but we never made it to Quaternions, which is how Rigid represents the Angular Position/rotation. Looking at the class, it looks like a vector, but I'm so rusty I can't even remember how to represent rotation with one of those, or if it can be done (I'm pretty sure you needed a matrix for rotation).
So, anybody have a quick code snippet for simply setting the rotation of a QuatF in the X-Y plane, or a better way to get the effect I'm looking for? I've been looking at the other vehicles and some other resources, but everything I've found is either for wheeled vehicles, which don't work anywhere near the same as spaceships, or for mouse-controlled systems. My game won't even be using the mouse except for pointing.
On a side note, Torque is not using a mathematical coordinate system, where the Z-axis is "depth" but the variant with Z as height, correct?
Thanks for any help you can give me!
I'm working on modifying the FlyingVehicle control code, and I'm trying to figure out the best way to get the effect I want. I'm emulating the control scheme from Escape Velocity, a great shareware game. In that game, your ship has inertia for velocity (confined to a 2D plane), and keeps moving in whatever direction you were last thrusting in, which is how physics normally work. But turning is a different matter- as long as you press the turn key, you turn at a defined rate, and when you let up, you stop turning. So it's all very simple, you thrust in the direction you are pointed, and you control your pointing direction using turn keys.
I've found the code I need to change to remove the mouse control and go to strictly keyboard control, in flyingVehicle.cc, in the functions updateMove() and updateForces(), mostly the latter. updateForces() does a lot of manuevering and in the end comes up with a force on the Rigid and a torque on the Rigid that represents the vehicle. I stripped out almost everything from updateForces(), until the only part that's doing any work is this (lots of commenting out of forces that no longer apply):
// Maneuvering jets
force += yv * (mThrust.y * mDataBlock->maneuveringForce);
force += xv * (mThrust.x * mDataBlock->maneuveringForce);
// Steering
Point2F steering;
steering.x = mSteering.x / mDataBlock->maxSteeringAngle;
steering.x *= mFabs(steering.x);
steering.y = mSteering.y / mDataBlock->maxSteeringAngle;
steering.y *= mFabs(steering.y);
torque -= xv * steering.y * mDataBlock->steeringForce;
torque -= zv * steering.x * mDataBlock->steeringForce;
//
mRigid.force = force;
mRigid.torque = torque;
Which actually works, sort of. The problem is that once I hit turn, I start to turn, but I never stop turning- in fact the turn slowly accelerates, even after the key is released. There's two ways I can think of to get the effect I want- generate a counter-torque to stop the spin dead, or skip the torque physics and just manually rotate mRigid. I'm leaning towards the latter option, as the first requires even more math, and I don't know a good way to easily trigger code on key release.
Unfortunately, and here's the heart of the problem, I took a 3D graphics class but we never made it to Quaternions, which is how Rigid represents the Angular Position/rotation. Looking at the class, it looks like a vector, but I'm so rusty I can't even remember how to represent rotation with one of those, or if it can be done (I'm pretty sure you needed a matrix for rotation).
So, anybody have a quick code snippet for simply setting the rotation of a QuatF in the X-Y plane, or a better way to get the effect I'm looking for? I've been looking at the other vehicles and some other resources, but everything I've found is either for wheeled vehicles, which don't work anywhere near the same as spaceships, or for mouse-controlled systems. My game won't even be using the mouse except for pointing.
On a side note, Torque is not using a mathematical coordinate system, where the Z-axis is "depth" but the variant with Z as height, correct?
Thanks for any help you can give me!
#2
Right now we just decided to put the mouselook version back in and quit denying that we're in 3D land, and play with the Helicopter resource a bit.
I also figured out (I think) that I could have linked the $mvYaw variable to the keyboard and had it rotate correctly the wya I wanted it to without so many changes to the physics. Haven't tried it yet, though. Anybody know if that's the case?
02/16/2006 (1:04 pm)
Hmm. Interesting thought. I will definately give it a thorough look. Thanks for the pointer, Tim.Right now we just decided to put the mouselook version back in and quit denying that we're in 3D land, and play with the Helicopter resource a bit.
I also figured out (I think) that I could have linked the $mvYaw variable to the keyboard and had it rotate correctly the wya I wanted it to without so many changes to the physics. Haven't tried it yet, though. Anybody know if that's the case?
Torque Owner Tim Hutcheson
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9480
Might be interesting to set it up as an engine call though because of the completion routines. You would need some sort of flag set in the completion to signal your engine thread that it is done.