Working on flight-sim-like physics. Hit a first bump.
by Christian Boutin · in Torque Game Engine · 10/31/2004 (2:25 pm) · 14 replies
Goal : simulate semi-realistic flight physics and flight control (roll instead of yaw/steer).
At first I tweaked the FlyingVehicle class but I decided there wasn't enough in common with what I wanted to I created a new class called "Plane". This class inherits the "Vehicle" class. It currently only deals with thrust, so it doesn't roll or pitch or yaw ( I thought, one axis at a time :-) ). The thrust behaves as it should for the most part except if I happen to be on a flat terrain AND my velocity reaches perfect 0, then I'll never ever be able to move again, no matter how much "force" I apply. Here's the code for my UpdateMove and UpdateForce methods:
Any tip would be greatly appreciated.
void Plane::updateMove(const Move* move)
{
Parent::updateMove(move);
// this just sets what the pilot wants his speed to be.
wantedSpeed += (move->y)*20;
if (wantedSpeed < 0) wantedSpeed = 0;
if (wantedSpeed > mDataBlock->maxSpeed){
wantedSpeed = mDataBlock->maxSpeed;
}
}
void Plane::updateForces(F32 /*dt*/)
{
bool isGrounded = isOnTheGround();
MatrixF currPosMat;
mRigid.getTransform(&currPosMat);
mRigid.atRest = false;
Point3F massCenter;
currPosMat.mulP(mDataBlock->massCenter,&massCenter);
Point3F xv,yv,zv;
currPosMat.getColumn(0,&xv);
currPosMat.getColumn(1,&yv);
currPosMat.getColumn(2,&zv);
F32 speed = mRigid.linVelocity.len();
sprintf(infoString, ">");
Point3F force;
Point3F torque = Point3F(0, 0, 0);
if (isGrounded && mRigid.linVelocity.len() == 0 && wantedSpeed > 0){
sprintf(infoString, "%s [AG]", infoString);
// Should makes the plane go completely nuts if a grounded plane's linear velocity is null but it's wanted speed isn't. Unfortunately it doesn't help (tried positive and negative values on all vectors) :-(
force = Point3F(-1000, -1000, -1000);
torque = Point3F(125,12,44);
}else{
force = Point3F(0, 0, sPlaneGravity * mRigid.mass * mGravityMod);
}
// Is the linear velocity less than what the pilot wants? If yes then apply thrust
if (speed < wantedSpeed){
sprintf(infoString, "%s [TR]", infoString);
force += yv*mDataBlock->thrust;
}
// Is the linear velocity more than what the pilot wants? If yes then apply brakes (airbrakes if airborne, also wheelbrakes if grounded)
if (speed > wantedSpeed){
sprintf(infoString, "%s
", infoString);
force -= mRigid.linVelocity * (mDataBlock->airBrakes);
if (isGrounded){
force -= mRigid.linVelocity * (mDataBlock->wheelBrakes);
sprintf(infoString, "%s [WB]", infoString);
}
}
// Prints out info line
sprintf(infoString, "%s W : %2.2f V : %2.2f V: [%2.2f,%2.2f,%2.2f] F: [%2.2f,%2.2f,%2.2f]", infoString, wantedSpeed, speed, mRigid.linVelocity.x, mRigid.linVelocity.y, mRigid.linVelocity.z, force.x, force.y, force.z);
// Reduces torque to stabilize aircraft.
torque -= mRigid.angMomentum * 5;
// Applies force and torque
mRigid.force = force;
mRigid.torque = torque;
}
At first I tweaked the FlyingVehicle class but I decided there wasn't enough in common with what I wanted to I created a new class called "Plane". This class inherits the "Vehicle" class. It currently only deals with thrust, so it doesn't roll or pitch or yaw ( I thought, one axis at a time :-) ). The thrust behaves as it should for the most part except if I happen to be on a flat terrain AND my velocity reaches perfect 0, then I'll never ever be able to move again, no matter how much "force" I apply. Here's the code for my UpdateMove and UpdateForce methods:
Any tip would be greatly appreciated.
void Plane::updateMove(const Move* move)
{
Parent::updateMove(move);
// this just sets what the pilot wants his speed to be.
wantedSpeed += (move->y)*20;
if (wantedSpeed < 0) wantedSpeed = 0;
if (wantedSpeed > mDataBlock->maxSpeed){
wantedSpeed = mDataBlock->maxSpeed;
}
}
void Plane::updateForces(F32 /*dt*/)
{
bool isGrounded = isOnTheGround();
MatrixF currPosMat;
mRigid.getTransform(&currPosMat);
mRigid.atRest = false;
Point3F massCenter;
currPosMat.mulP(mDataBlock->massCenter,&massCenter);
Point3F xv,yv,zv;
currPosMat.getColumn(0,&xv);
currPosMat.getColumn(1,&yv);
currPosMat.getColumn(2,&zv);
F32 speed = mRigid.linVelocity.len();
sprintf(infoString, ">");
Point3F force;
Point3F torque = Point3F(0, 0, 0);
if (isGrounded && mRigid.linVelocity.len() == 0 && wantedSpeed > 0){
sprintf(infoString, "%s [AG]", infoString);
// Should makes the plane go completely nuts if a grounded plane's linear velocity is null but it's wanted speed isn't. Unfortunately it doesn't help (tried positive and negative values on all vectors) :-(
force = Point3F(-1000, -1000, -1000);
torque = Point3F(125,12,44);
}else{
force = Point3F(0, 0, sPlaneGravity * mRigid.mass * mGravityMod);
}
// Is the linear velocity less than what the pilot wants? If yes then apply thrust
if (speed < wantedSpeed){
sprintf(infoString, "%s [TR]", infoString);
force += yv*mDataBlock->thrust;
}
// Is the linear velocity more than what the pilot wants? If yes then apply brakes (airbrakes if airborne, also wheelbrakes if grounded)
if (speed > wantedSpeed){
sprintf(infoString, "%s
", infoString);
force -= mRigid.linVelocity * (mDataBlock->airBrakes);
if (isGrounded){
force -= mRigid.linVelocity * (mDataBlock->wheelBrakes);
sprintf(infoString, "%s [WB]", infoString);
}
}
// Prints out info line
sprintf(infoString, "%s W : %2.2f V : %2.2f V: [%2.2f,%2.2f,%2.2f] F: [%2.2f,%2.2f,%2.2f]", infoString, wantedSpeed, speed, mRigid.linVelocity.x, mRigid.linVelocity.y, mRigid.linVelocity.z, force.x, force.y, force.z);
// Reduces torque to stabilize aircraft.
torque -= mRigid.angMomentum * 5;
// Applies force and torque
mRigid.force = force;
mRigid.torque = torque;
}
About the author
Recent Threads
#2
11/01/2004 (3:28 am)
Well when I switch to the editor and select it, the box is red. But I thought it to be normal since it's basically being pulled downward by gravity and should(?) be always "colliding" with the ground. Interesting though, trying to move it forward in the editor resulted in it biting into the ground somehow and only moving by bigger steps. Perhaps I should add wheels like the car model has for when the plane is taxiing. Seemed at first like a bigger hassle than it needed since the plane will spend so little time on the ground (and the viewpoint will be mainly first person) but maybe that would help solve this taxiing problem. What do you guys think?
#3
I have that exact same problem with my flying vehicles. And for the most part, not much has been changed except for the hoverheight fix.
11/01/2004 (3:40 am)
Quote:The thrust behaves as it should for the most part except if I happen to be on a flat terrain AND my velocity reaches perfect 0, then I'll never ever be able to move again, no matter how much "force" I apply.
I have that exact same problem with my flying vehicles. And for the most part, not much has been changed except for the hoverheight fix.
#4
Kind of solved the problem by maintainig a minimum speed on the ground.
P.S.
Gonzo, are you a clown or an exterminator ? Or both :-)
11/01/2004 (5:12 am)
Ditto.Kind of solved the problem by maintainig a minimum speed on the ground.
P.S.
Gonzo, are you a clown or an exterminator ? Or both :-)
#5
LOL
11/01/2004 (8:12 am)
Both. Sometimes I'm Exterminator when I get on a bug solving bender. The rest of the time I'm mainly a clown trying to keep things lively and humorous in a place where frustration can be abundant. What I don't get is some people just don't seem to like Pie in their faces. Go figure.LOL
#6
11/01/2004 (8:25 am)
My object will need to come to a complete stop eventually (that's part of the game) unless I find a speed that, although not completely stopped, will create a movement that cannot be perceived. Looks like a mean and potentially bug-prone way to patch it. Do you think adding wheels to it like it's a car would solve the problem? Only other alternative would be to scrap takeoffs and landings from the game, but what's a flight sim without takeoffs and landings?
#8
11/01/2004 (11:43 am)
Have you tried rendering each detected collision point? There might be a bug in the collision code in that case.
#9
Sorry to sound like a newbie, but I don't really know how to render each detected collision point. If there is a bug in the collision code I can profide all my code and scripts and such if it can help you guys to figure it out.
BTW I replaced my plane.dts shape that I made with buggy.dts, just in case it were my model that contained errors, but it didn't change anything.
11/01/2004 (2:55 pm)
@BenSorry to sound like a newbie, but I don't really know how to render each detected collision point. If there is a bug in the collision code I can profide all my code and scripts and such if it can help you guys to figure it out.
BTW I replaced my plane.dts shape that I made with buggy.dts, just in case it were my model that contained errors, but it didn't change anything.
#10
If that doesn't work for you ... try this.
Use the HoverHeight fix as Gonzo suggested. This way your vehicle has a hard limit above the terrain. Add your landing gear to your model, but do not include it in the collision mesh. Make sure they extend for at least the hoverheight. The effect is the same, and your collision mesh will be less prone to interfere w/ the ground in a manner that just "glues" you in place.
11/01/2004 (9:14 pm)
Definately check out the link that Bruno suggested.If that doesn't work for you ... try this.
Use the HoverHeight fix as Gonzo suggested. This way your vehicle has a hard limit above the terrain. Add your landing gear to your model, but do not include it in the collision mesh. Make sure they extend for at least the hoverheight. The effect is the same, and your collision mesh will be less prone to interfere w/ the ground in a manner that just "glues" you in place.
#11
11/02/2004 (6:42 am)
That's a really good suggestion. You could add the wheels with MountImage instead of MountObject. This way they won't have a collision mesh.
#12
11/02/2004 (11:41 am)
Basically, somewhere in the code its getting a list of all its collisions and reacting based on that information. It might be that the collision detection is getting funky results in the zero velocity case, and drawing those points will help you identify the source of the error.
#13
I have no idea how to draw them or where to find them is what I'm saying. I guess that's not a simple switch to render collision points. Anyone else ever done that?
I tried something that kind of worked. If the plane is considered below a certain height I cancel gravity and give him a tiny boost up. However now it keeps on bouncing, even when it doesn't have any trust (for obvious reasons). I can't find a balance that would not be noticeable and that also prevents the bug.
I guess the next step is to implement a fully wheeled version of the plane and see how that goes. That's a bit of a concern since not knowing why it does that means I may only be pushing the problem forward and it'll just end up biting my butt later in dev.
One theory though, if a collision creates a force negative to the object's current force to create a bouncing effect, and the speed of the object at the time of the collision is 0, maybe that just creates an endless collision situation. Just a thought.
I still wonder howcome people and cars can come to a complete stop and start again.
11/02/2004 (3:20 pm)
@BenI have no idea how to draw them or where to find them is what I'm saying. I guess that's not a simple switch to render collision points. Anyone else ever done that?
I tried something that kind of worked. If the plane is considered below a certain height I cancel gravity and give him a tiny boost up. However now it keeps on bouncing, even when it doesn't have any trust (for obvious reasons). I can't find a balance that would not be noticeable and that also prevents the bug.
I guess the next step is to implement a fully wheeled version of the plane and see how that goes. That's a bit of a concern since not knowing why it does that means I may only be pushing the problem forward and it'll just end up biting my butt later in dev.
One theory though, if a collision creates a force negative to the object's current force to create a bouncing effect, and the speed of the object at the time of the collision is 0, maybe that just creates an endless collision situation. Just a thought.
I still wonder howcome people and cars can come to a complete stop and start again.
#14
Around line 1629 there is a loop which renders the collision points. You turn it on by enabling bounding box rendering through a global variable. A quick find in files on that variable name reveals that $GameBase::boundingBox is the variable you need to manipulate to turn on the collision point rendering.
Vehicle physics are complex. Be ready to get your hands dirty. But they can be done well.
11/02/2004 (7:05 pm)
Have you looked at the C++ code at all?Around line 1629 there is a loop which renders the collision points. You turn it on by enabling bounding box rendering through a global variable. A quick find in files on that variable name reveals that $GameBase::boundingBox is the variable you need to manipulate to turn on the collision point rendering.
Vehicle physics are complex. Be ready to get your hands dirty. But they can be done well.
Associate Kyle Carter