Page 1 of 1

How to apply damping to one body?

Posted: Wed Nov 21, 2012 4:59 am
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? :)

Re: How to apply damping to one body?

Posted: Wed Nov 21, 2012 10:07 am
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.

Re: How to apply damping to one body?

Posted: Wed Nov 28, 2012 11:37 am
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

Re: How to apply damping to one body?

Posted: Wed Nov 28, 2012 11:52 am
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.