how to generate instant forces

Elimentz

12-05-2010 19:55:35

Hi,

I managed to include OgreNewt in my project, but I'm having some trouble with generating forces. My game is a golf simulation, so an event (e.g. a mouse click) is a stick hitting the ball, which in turn should move the ball with a certain force. Now, I managed to apply some force to my ball, but only by building the force gradually: while holding the mouse button, the ball moves faster and faster; when I release the mouse button, no force is applied and the ball continues to roll until it stops

But that's not what I want... what I need is to generate more force by holding the right mouse button and instantly shoot the ball simply by clicking the left mouse button.

This is what I'm doing right now:


if( leftMouseClicked() )
{
OgreNewtBody->setCustomForceAndTorqueCallback<Entity>(&Entity::applyForceCallback, this);
}
else
{
OgreNewtBody->setStandardForceCallback();
}



void CustomEntity::applyForceCallback( OgreNewt::Body *OgreNewtBody, float timeStep, int threadIndex )
{
Ogre::Vector3 force(-400,0,0);
Ogre::Vector3 pointToApply(0,0,0);
Ogre::Vector3 inertia;
Ogre::Real mass;
OgreNewtBody->getMassMatrix(mass, inertia);

OgreNewtBody->addGlobalForce( force, pointToApply );

// Add gravity
Ogre::Vector3 gravity = Ogre::Vector3(0,-9.8,0) * mass;
OgreNewtBody->addForce(gravity);
}


thx in advance

Elimentz

13-05-2010 10:24:13

ok, problem more or less solved by doing


mNtBody->setContinuousCollisionMode(1);
mNtBody->addImpulse( Ogre::Vector3(60,0,0), mNtBody->getPosition() );



but still a question: can someone please explain to me what the function


mNtBody->setContinuousCollisionMode(1);


does? Is it really nessecairy? I got this sample code from the MinimalOgreNewton example

thx!!

SFCBias

14-05-2010 16:05:41

I think you've misunderstood the usage of the forceCallback. The forceCallback is used to apply constant force on an object e.g. gravity based on mass etc.

So by setting body->setStandardForceCallback, all you're doing is adding gravity.

I think what you want is to have a static ball (a golf ball) and a stick(the club) and when the club strikes the golf ball with however much force, that force is applied to the ball.

So that should be able to be achieved by simply adding the code you had of

mNtBody->addImpulse( Ogre::Vector3(60,0,0), mNtBody->getPosition() );

And the mNtBody->setContinuousCollisionMode(1); simple says makes it so that if a body has a chance of "tunneling" (missing a collision) e.g. a fast moving ball then it continuously checks for collision to prevent this from happening.