Page 1 of 1
Applying force doesn't do anything
Posted: Fri Aug 01, 2008 1:27 pm
by bronxbomber92
I'm trying to add a simple force when my rigidbody collides with something. I know chipmunk doesn't zero forces, so in my update function I call cpBodyResetForces(). But, when I use this, the force seems to not have any affect, no matter what the magnitude of the force is (both 10 and 1000 have the same result)! My rigidbody's mass is 10, it's elasticity is 0.9 and it's friction coefficient is 0.0.
cpVect force = cpvmult(contacts[0].n, 30.0f);
cpBodyApplyForce(body, force, cpvzero);
What am I doing wrong?
Thanks
Re: Applying force doesn't do anything
Posted: Fri Aug 01, 2008 2:27 pm
by Michael Buckley
Just for kicks, what happens if you remove the cpBodyResetForces() call?
Re: Applying force doesn't do anything
Posted: Fri Aug 01, 2008 3:01 pm
by bronxbomber92
The forces works, but they are persistent.
Re: Applying force doesn't do anything
Posted: Fri Aug 01, 2008 4:21 pm
by Michael Buckley
Then without seeing the whole code, it sounds like you're resetting the forces before they're actually acted upon. The forces don't get applied immediately. They get applied during a call to cpSpaceStep.
Re: Applying force doesn't do anything
Posted: Sat Aug 02, 2008 11:27 am
by bronxbomber92
Yeah, that's what happening I think. I don't understand why Chipmunk doesn't zero the forces it's self, because it seems almost impossible to apply any type of force because of the order of execution.
Here's my code:
Code: Select all
void BlackMarbleEntity::Update(float dt)
{
//if(shouldClearForces)
cpBodyResetForces(body);
//else
// shouldClearForces = true;
}
void BlackMarbleEntity::OnCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
if(b->collision_type == kLeftRightBoundary)
{
body->v.x = -body->v.x;
}
else if(b->collision_type == kTopBottomBoundary)
{
body->v.y = -body->v.y;
}
if(b->collision_type == kPlayer)
{
for(int i = 0; i < numContacts; i++)
{
cpVect force = cpvmult(contacts[i].n, 10.0f);
cpBodyApplyForce(body, force, cpvzero);
}
//shouldClearForces = false;
}
}
My update is called before my physics step. What do I need to do to get this to work?
Re: Applying force doesn't do anything
Posted: Sat Aug 02, 2008 4:53 pm
by Michael Buckley
I think the problem is that you're applying the forces in the collision function. IIRC, the collision function is called during cpSpaceStep after the forces are already applied, so any forces you apply in the collision function won't take effect until the next call to cpSpaceStep. Thus, your Update method is going to need to reset only the forces of the bodies that haven't had forces applied to them in the collision. You'll probably also have to zero the force of each body in the collision function before you apply a new force to it.
Re: Applying force doesn't do anything
Posted: Sat Aug 09, 2008 9:05 pm
by bronxbomber92
Here's what I'm doing now. The problem is that the there seems to be an uncontrollable force causing the rigidbody to go in random directions like it's being pulled by some magnet(s).
Code: Select all
void BlackMarbleEntity::Update(float dt)
{
cpBodyResetForces(body);
if(shouldApplyCollisonForce)
{
cpBodyApplyForce(body, force, cpvzero);
force = cpvzero;
}
shouldApplyCollisonForce = false;
}
void BlackMarbleEntity::OnCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
if(b->collision_type == kPlayer)
{
cpVect f = cpvmult(cpvmult(contacts[0].n, normal_coef), 700.0f);
force = cpvadd(f, force);
shouldApplyCollisonForce = true;
}
}
What's wrong now?
Re: Applying force doesn't do anything
Posted: Sun Aug 10, 2008 12:59 pm
by bronxbomber92
Ok, I've found that this only happens when I add static shapes to the scene. Why would this be?