Applying force doesn't do anything

Official forum for the Chipmunk2D Physics Library.
Post Reply
bronxbomber92
Posts: 8
Joined: Sat Dec 01, 2007 6:06 pm
Contact:

Applying force doesn't do anything

Post 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
Michael Buckley
Posts: 46
Joined: Tue Aug 21, 2007 10:30 am
Contact:

Re: Applying force doesn't do anything

Post by Michael Buckley »

Just for kicks, what happens if you remove the cpBodyResetForces() call?
bronxbomber92
Posts: 8
Joined: Sat Dec 01, 2007 6:06 pm
Contact:

Re: Applying force doesn't do anything

Post by bronxbomber92 »

The forces works, but they are persistent.
Michael Buckley
Posts: 46
Joined: Tue Aug 21, 2007 10:30 am
Contact:

Re: Applying force doesn't do anything

Post 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.
bronxbomber92
Posts: 8
Joined: Sat Dec 01, 2007 6:06 pm
Contact:

Re: Applying force doesn't do anything

Post 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?
Michael Buckley
Posts: 46
Joined: Tue Aug 21, 2007 10:30 am
Contact:

Re: Applying force doesn't do anything

Post 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.
bronxbomber92
Posts: 8
Joined: Sat Dec 01, 2007 6:06 pm
Contact:

Re: Applying force doesn't do anything

Post 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?
bronxbomber92
Posts: 8
Joined: Sat Dec 01, 2007 6:06 pm
Contact:

Re: Applying force doesn't do anything

Post by bronxbomber92 »

Ok, I've found that this only happens when I add static shapes to the scene. Why would this be?
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests