How to apply damping to one body?

Official forum for the Chipmunk2D Physics Library.
Post Reply
MobileDev
Posts: 3
Joined: Wed Nov 21, 2012 4:37 am
Contact:

How to apply damping to one body?

Post by MobileDev »

Hi,
I have a top down scene. So I have a background image, and some bodies placed on top of that image. So unfortunately I cannot use friction. I have found the function
cpSpaceSetDamping() to be very useful. Unfortunately it affect all the elements in space. I want to affect just a few. Is there a similar method or property that acts as cpSpaceDamping() but affect only one body?

Alternative would be to create two separate spaces. Can I do this in Chipmunk? And have them at the same time on the screen?

Also does english word space have plural? Can I say two spaces? :)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to apply damping to one body?

Post by slembcke »

Damping makes for really crummy, floating looking friction. You should check out the Tank demo that comes with the Chipmunk source. It will show you how to use joints to get accurate friction for a top-down game.

However, to answer your original question, you can apply damping (or gravity) to a single body. You need to implement a velocity update function though. That's not a well documented feature, but the Planet demo has an example.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
MobileDev
Posts: 3
Joined: Wed Nov 21, 2012 4:37 am
Contact:

Re: How to apply damping to one body?

Post by MobileDev »

Thanks for your answer. I looked at the Planet.c file inside the Demo folder.
I can see that you are setting up velocity update function with

Code: Select all

body->velocity_func = planetGravityVelocityFunc;
Later he function is also defined

Code: Select all

static void
planetGravityVelocityFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
{
	// Gravitational acceleration is proportional to the inverse square of
	// distance, and directed toward the origin. The central planet is assumed
	// to be massive enough that it affects the satellites but not vice versa.
	cpVect p = cpBodyGetPos(body);
	cpFloat sqdist = cpvlengthsq(p);
	cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));
	
	cpBodyUpdateVelocity(body, g, damping, dt);
}
But I don't understand who sets up all those parameters? Who calls this function? I just want to lower the speed of two bodies on the screen. How can I do equivalent of cpSpaceSetDamping(space1,0.5); but so it affect only one body? Thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to apply damping to one body?

Post by slembcke »

Well, to be more clear. I would not recommend using damping to approximate friction, and would highly recommend using the constraint based approach from the Tank demo. Damping looks fairly distinct from friction as the deceleration rate is not constant at all.

The velocity functions are called from cpSpaceStep() on every non-sleeping body added to the space. https://github.com/slembcke/Chipmunk-Ph ... tep.c#L385 So the gravity and damping are values passed to the body from the space. If you *really really* still want to use damping/velocity functions for this, then you'll need to calculate your own damping coefficient. Notice how it's calculated on line 386 above. The damping value passed to the velocity function is how much damping should be applied per frame, and not per second like the space stores.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests