Keep pushing myself over with addForce

CrazyLegz

18-04-2009 17:10:32

Hi all,

I have set up a First Person sandbox in which I can experiment some. It's just a large cube in which I can look around and move in first person style, controlling the camera with the mouse and the motion with the w,a,s,d keys. Everything is fine except that I am having some trouble with controlling the motion. My collision body is an Ellipsoid and after applying some force to move forward I fall over which seems logical btw. What is the best way to control this motion? Or am I just using the wrong kind of body?
I've read through alot of topics and wiki articles and so I came to addForce. Am I using it wrong perhaps?


void Player::forceCallBack(OgreNewt::Body* body)
{
Ogre::Real mass;
Ogre::Vector3 inertia;

m_playerBody->getMassMatrix(mass, inertia);
Ogre::Vector3 force(m_moveAlongXAxis, -9.8f, m_moveAlongZAxis);
force *= mass;

m_playerBody->addForce(force);
}


The m_moveAlongXAxis and m_moveAlongZAxis variables are set when the w,a,s,d keys are pressed. When they are released the values are set to 0.0f.

What I basically want is to move when the w,a,s,d keys are pressed and stop moving when they are released, so I won't keep sliding.

Thanks in advance.

Regards,

CrazyLegz.

melven

18-04-2009 23:14:32

I assume the body won't stop after you stop adding a force - this is the physically correct behavior...
If you only want to do some experiments, try to set the force that is needed for a given velocity...
In order to calculate the force needed, you can use the function OgreNewt::Body::calculateInverseDynamicsForce (if you are using newton 2.0 beta).
You can calculate the correct force by yourself (but newton will be more accurate)... Since the timesteps differ, you can use something like:

Vector3 neededVelocity = ...;
Vector3 currentVelocity = body->getVelocity();
Vector3 inertia;
Real mass;
body->getInvMass(invMass, invInertia);
Real delay = 0.05;
Vector3 force = (neededVelocity-currentVelocity) * mass / delay;

This will work (but it's not a nice solution), but if you need a real character controller, this is a lot more complicated!!
(you'll need rotation, then nice behavior with different timesteps (with simple formulas as above, you'll get sometimes very far jumps and other strange things in a real game world), handling of obstacles etc...)

CrazyLegz

19-04-2009 12:46:50

Hi Melven,

Thanks for your response.
I think I'll upgrade to newton 2.0 beta then and let newton do most of the work and keep my code as clean as possible. But first I'll try out some of your code and play and fiddle some with it. But my initial problem was/is that I have an ellipsoid as a body, and after applying some force to move forward, I fall over to the front so I am facing the ground. This is ofcourse logical and could be fixed by using a box for example and make it larger so it can't be pushed over and just moves forward. But it don't think this is the way to go. I have added an up-vector to the body which prevents it from falling over.
I already have quite some things sorted out, like the rotation and camera control for example.

Doesn't newton or ogrenewt already have some sort of structure for this? Some class(es) with which you could manage character behavior. Something like a first person character controller class perhaps? Or does any other physics library like physx or bullet have some system for this?

I've read somewhere on the forum that the new ogrenewt will have some sort of charactercontroller class but it isn't working yet. So that won't work for me at the moment.

melven

19-04-2009 21:40:47

Yes, I'm trying to implement the newton-player-controller example (that is shipped with newton) in ogrenewt... But as there's still a bug in newton 2.0 beta concerning the convexcasts, so I cannot continue until a new newton beta comes available.