How move a spaceship correctly

kain

18-12-2007 21:10:02

Hello,

my goals were to put a spaceship into ogre, move it and detect collision with another spaceship. Well it's almost working but i have a problem with using ogreNewt force on the ship, i cannot have a smooth render:

If i put to much force on the ship, the ship is blinking, and too slow is ... well too slow ;)

So i figure maybe i must do something wrong, here is my ship :


OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::Box( mWorld, scale );
mNewtShip = new OgreNewt::Body( mWorld, col );

mNewtMatID = new OgreNewt::MaterialID( mWorld );
mNewtShip->setMaterialGroupID( mNewtMatID );
mNewtShip->attachToNode(mNode);

mNewtShip->setPositionOrientation( startPosition, orientation );
Ogre::Real mass = 0.01;
Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcBoxSolid( mass, scale );
mNewtShip->setMassMatrix( mass, inertia);
mNewtShip->setCustomForceAndTorqueCallback( boost::bind( &Object3d::forceBodyCallback, this, _1 ) );
mNewtShip->setAutoFreeze(0);


Here is my callback function for the ship :



void Object3d::forceBodyCallback (OgreNewt::Body* me)
{

me->addForce(spd);
};


Framelistener for the ship and the move methods :

Framelistener:
if( mKeyboard->isKeyDown( KC_E ) )
{
mGameManager->mMainShip->move_f();
KeyDwn=1;
}

method:

void Object3d::move_f()
{
if (mNewtShip->getVelocity().y < 0)
spd=Vector3(0,acceleration,0)+ (-mNewtShip->getVelocity());
else
spd=Vector3(0,acceleration,0);

}

void Object3d::move_l()
{

if (mNewtShip->getVelocity().x > 0)
spd=Vector3(-acceleration,0,0)+ (-mNewtShip->getVelocity());
else
spd=Vector3(-acceleration,0,0);
}



Dont know why, but with this system i tried to change the way i call the force method or change the spd but it don't change the fact that my ship is not flying with a smooth render.

Regards,
Jr



subquantum

18-12-2007 22:35:14

I don't think it's nessisary to calculate your force based on current velocity.

For instance, your ship weights 0.01 kg, and is traveling at 10m/s. If your acceleration value there was 5, say, then the magnitude of speed would be 50. The addForce function takes a force of the units (mass * distance / time / time), usually Newtons (kg m / s^2) if you're metric or Pounds if you're imperial. The resultant acceleration of the object you apply it to is the force divided by the mass. With a force of 50 N, and a mass of 0.01 kg, you get an acceleration (in one frame) of 5000 m/s, clearly not what you were aiming for.

If you want your ship to accelerate at a given speed, take your acceleration, multiply it by the mass of your ship, and then divide by the frame's duration. This'll give you the correct force to apply per frame. If you were to model an actual rocket, for instance, their thrust is given in Newtons, so you could just plug in that value. The more massive the object you're trying to move, the less acceleration the force will have. Newton's second law, Force = mass * acceleration.

kain

19-12-2007 13:51:16

Tks for the answer, so maybe my camera is too close that is why i am thinking the acceleration is too slow ..
In fact, it is hard to make a ship mouvement for a shootemup realistic.

I want that the ship have quick acceleration, and for that it seems that using force and mass is quick difficult.

Because with 5000m/s it is good, but i understand why the render is "hashed".
I don't understand why, with a good speed lets say (60m/s), i don't have the feeling that my ship is going fast. maybe i should move the ship and the camera should follow it while the other object come with a opposite speed...