ApplyForce issues

skeen
Posts: 24
Joined: Wed Dec 08, 2010 11:59 am

ApplyForce issues

Post by skeen »

ISSUE SOLVED: THE BODY WAS GOING DEACTIVATED

I'm currently working on a extension for a GameEngine at my university, that is using bullet, and it works, mostly.

I'm having issues with the following code:

Code: Select all

    DynamicBody* db;
    list<Key> keysPressed;

    void Handle(ProcessEventArg arg)
    {
        //Pushed here the body moves
        // keysPressed.push_back(KEY_w);
        list<Key>::iterator key;
        for(key=keysPressed.begin(); key != keysPressed.end(); ++key)
            HandleDown(*key);
    }

    void Handle(KeyboardEventArg arg)
    {
        //Pushed here it doesn't
        //keysPressed.push_back(KEY_w);
    }

    void HandleDown(Key key)
    {
        db->ApplyForce(Vector<3,float>(1,0,0));
    }
The handle events, are called by the engine event system, that is 'Handle(Keyboard)' is called, once the underlaying framework sets a keyinput event, and the ProcessEvent is a part of the main engine loop.

The issues is that once I push keys into the list, from within Handle(Keyboard) the code doesn't work, if I push a key from within Handle(ProcessEvent), then it works. - That would be perfectly understandable if the call to Handle(Keyboard) never happend, however it does, and the call to db->ApplyForce() happens in both cases, however it only works on one of them.

The call to ApplyForces, simply pushes the force into a list of forces, that affects the body, and these are applied using the following code (inside the engine extension):

Code: Select all

    btBody.clearForces();
    btBody.applyTorque(toBtVec(dynBody.GetTorque()));
    for(list<DynamicBody::ForceAtPosition>::iterator it = dynBody.GetForces().begin(); it != dynBody.GetForces().end(); it++)
    {
         DynamicBody::ForceAtPosition & f = *it;
         btBody.applyForce(toBtVec(f.get<0>()), toBtVec(f.get<1>()));
    }
    dynBody.ResetForces();
Where btBody is of type btRigidBody, and dynBody is a DynamicBody.
'toBtVec' translates our vector type, to the bullet one, dynBody.GetForces(), returns a list of forces.
ResetForces() clears the list of forces.

The forces, in the list is of type: 'boost::tuple<const Vector<3,float>, const Vector<3,float> >'.

The physics plugin, handles the dynamic bodies, by applying forces, with the above code (among with something else), then a call to stepSimulation(), followed by an updating of the rendering data, for the body.

Anyone see, where the issue would be? - I've tried like everything, and what bugs me, is that running a debugger. I can see that the same code gets executed, no matter where I'm pushing the keypressed from.

If you require any other code or test, please let me know, and I'll supply and test.

Note: The code is compiled using GCC 4.5.1 (MinGW under Windows 7).
Note: Compiling for x86-32
Last edited by skeen on Thu Dec 09, 2010 12:27 am, edited 1 time in total.
skeen
Posts: 24
Joined: Wed Dec 08, 2010 11:59 am

Re: ApplyForce issues

Post by skeen »

Now after using the entire night, debugging the code; I just realized that the body (ofc), got deactivated. - So once I disable deactivation it works.

The deactivation happed, because the ProcessEvent would happen before any key event, and right there it would be deactivated. - However shouldn't a call to ApplyForce reactivate it?
randomMesh
Posts: 2
Joined: Sat Dec 04, 2010 2:34 pm

Re: ApplyForce issues

Post by randomMesh »

skeen wrote:shouldn't a call to ApplyForce reactivate it?
No, you need to call body->activate(); manually before applying any forces or torques.
Something like this

Code: Select all

void Player::turn(const bool left)
{
	if (!this->body)
		return;

	static const btScalar turnSpeed = 5.0;

	this->body->activate();
	this->body->applyTorque(btVector3(0, left ? -turnSpeed : turnSpeed, 0));
}
skeen
Posts: 24
Joined: Wed Dec 08, 2010 11:59 am

Re: ApplyForce issues

Post by skeen »

randomMesh wrote:
skeen wrote:shouldn't a call to ApplyForce reactivate it?
No, you need to call body->activate(); manually before applying any forces or torques.
Something like this

Code: Select all

void Player::turn(const bool left)
{
	if (!this->body)
		return;

	static const btScalar turnSpeed = 5.0;

	this->body->activate();
	this->body->applyTorque(btVector3(0, left ? -turnSpeed : turnSpeed, 0));
}
Wonderful :) - I'll have to implement that in the engine, once I get a little spare time, till then I'm just gonna disable the one object that is affected by forces from the outside.