Character Physics, only a few bugs left... Using forces

Arkasha

16-05-2006 23:24:06

Hey,

I tried to work out a PlayerForceCallback Method, it works ok but still is glitched :



void TocNewtonForceAndTorqueCallback::PlayerRelativeForceCallback( OgreNewt::Body *body ){

int playerDirState;
int playerRotateState;
int const ROTATION_SPEED = 2; // must be positive
int const SPEED = 10;

Ogre::Real mass;
Ogre::Vector3 inertia;
Ogre::Quaternion orient;
Ogre::Vector3 pos;
body->getPositionOrientation( pos, orient );
body->getMassMatrix( mass, inertia );
//Gravity
Ogre::Vector3 gravity( 0, -9.8, 0 );
Ogre::Vector3 f1( 0, 0, 0 );
Ogre::Vector3 f2( 0, 0, 0 );

Player* player = (Player*) body->getUserData();
playerDirState = player->getDirState();
playerRotateState = player->getRotateState();
body->setAngularDamping( Ogre::Vector3( Ogre::Vector3( 50, 50, 50 ) ) );
body->setLinearDamping( 50 );

// Stop rotating
if( playerRotateState == Player::ROTATE_STATE_IDLE )
{
body->setOmega( Vector3::ZERO );
}

// turn right
if( playerRotateState == Player::ROTATE_STATE_RIGHT )
{
f1.z += 0.5;
f2.z -= 0.5;
}

// turn left
if( playerRotateState == Player::ROTATE_STATE_LEFT )
{
f1.z -= 0.5;
f2.z += 0.5;
}

// be idle
if( playerDirState == Player::DIR_STATE_IDLE)
{


}

// go forwards
if( playerDirState == Player::DIR_STATE_FORWARDS)
{
f1.z += SPEED;
f2.z += SPEED;
}

// go backwards
if( playerDirState == Player::DIR_STATE_BACKWARDS)
{
f1.z -= SPEED;
f2.z -= SPEED;
}

Vector3 centerPos( 0, 0, 0 );
Vector3 leftThrust( 5, 0, 0 );
Vector3 rightThrust( -5, 0, 0 );

body->addLocalForce( f1* mass , leftThrust );
body->addLocalForce( f2* mass , rightThrust );
body->addLocalForce( gravity* mass , centerPos );

}


I am trying to use two thrusters to move the player like a helicopter like in the OgreNewt wiki http://walaber.com/newton_wiki/index.php?title=How_can_I_control_an_object_using_a_physics_engine%3F but i am very unhappy with
body->setOmega( Vector3::ZERO );

to stop rotating though...

I also kindda guessing when I am using the damping...

Anyway, I hope some of you can use it and perhaps others may help me improve it!

Arkasha

19-05-2006 17:44:40

This works ok, We should all put our heads together and make ready-to-use Character ForceCall back functions

Arkasha

29-05-2006 10:24:42

OK, ive worked on this a lot....

This works pretty good and I tried to be as "true" to force usage as possible.


void TocNewtonForceAndTorqueCallback::PlayerRelativeForceCallback( OgreNewt::Body *body ){

//int scale = 25; parametre de test utilisé lors de l'intégration du dotSceneLoader
int playerDirState;
int playerRotateState;
int const ROTATION_SPEED = 1; // must be positive
int const ROTATION_LIMIT = 3;
int const SPEED = 8;
int const SPEED_LIMIT = 4;

Ogre::Real mass;
Ogre::Vector3 inertia;
Ogre::Quaternion orient;
Ogre::Vector3 pos;
body->getPositionOrientation( pos, orient );
body->getMassMatrix( mass, inertia );
//Gravity
Ogre::Vector3 gravity( 0, -9.8, 0 );
Ogre::Vector3 f1( 0, 0, 0 );
Ogre::Vector3 f2( 0, 0, 0 );

Player* player = (Player*) body->getUserData();
playerDirState = player->getDirState();
playerRotateState = player->getRotateState();
body->setAngularDamping( Ogre::Vector3( Ogre::Vector3( 1, 1, 1 ) ) );
body->setLinearDamping( 1 );

Real rotationVelocity = abs( body->getOmega().y );
Real speedVelocity = abs( value.x ) + abs( value.z );

// Stop rotating
if( playerRotateState == Player::ROTATE_STATE_IDLE )
{
//body->setOmega( Vector3::ZERO );
Real Omega = body->getOmega().y;
if( Omega > 0 )
body->addTorque( Vector3( 0, -20, 0 ) );
if( Omega < 0 )
body->addTorque( Vector3( 0, 20, 0 ) );
}

// turn right
if( playerRotateState == Player::ROTATE_STATE_RIGHT && rotationVelocity < ROTATION_LIMIT )
{
f1.z += ROTATION_SPEED;
f2.z -= ROTATION_SPEED;
}

// turn left
if( playerRotateState == Player::ROTATE_STATE_LEFT && rotationVelocity < ROTATION_LIMIT )
{
f1.z -= ROTATION_SPEED;
f2.z += ROTATION_SPEED;
}

// be idle
if( playerDirState == Player::DIR_STATE_IDLE)
{


}

// go forwards
if( playerDirState == Player::DIR_STATE_FORWARDS && speedVelocity < SPEED_LIMIT && player->getOnFloor() )
{
f1.z += SPEED;
f2.z += SPEED;
}

// go backwards
if( playerDirState == Player::DIR_STATE_BACKWARDS && speedVelocity < SPEED_LIMIT && player->getOnFloor() )
{
f1.z -= SPEED;
f2.z -= SPEED;
}

Vector3 centerPos( 0, 0, 0 );
Vector3 leftThrust( 5, 0, 0 );
Vector3 rightThrust( -5, 0, 0 );

body->addLocalForce( f1* mass , leftThrust );
body->addLocalForce( f2* mass , rightThrust );
body->addLocalForce( gravity* mass , centerPos );

// if body is falling, its off the ground
if( body->getVelocity().y < -3 )
player->setOnFloor( false );

//event generation
return PlayerGeneralCallback();
}