Keep body's speed constant

Official forum for the Chipmunk2D Physics Library.
Post Reply
milliams
Posts: 11
Joined: Tue Sep 18, 2007 7:11 pm
Location: Portsmouth, UK
Contact:

Keep body's speed constant

Post by milliams »

I'm writing a small breakout type game using Chipmunk. Basically, I would like the ball that gets bounced around, breaking blocks etc. to have a constant speed (or momentum I suppose technically). So even if the ball collided with a body attached to a spring, the body would recoil as expected but the ball wouldn't lose any momentum.

What would be the best way to achieve this with Chipmunk? Can I just set the momentum each frame or is there another way which is less likely to break the system?

Thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Keep body's speed constant

Post by slembcke »

I've pondered about trying to make a breakout myself. I think the conclusion that I came to was to make the velocity converge on a specific value.

Maybe apply something like this every frame to the velocity:
a += (b - a)*damp a is the current velocity, b is the target velocity

Which would translate roughly to:

Code: Select all

cpFloat v_mag = cpvlength(v);
v = cpvmult(v, 1.0f + (v_desired - v_mag)/v_mag*damping_value);
If the damping value is low enough, you'll get a nice gentle acceleration to the desired velocity.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
milliams
Posts: 11
Joined: Tue Sep 18, 2007 7:11 pm
Location: Portsmouth, UK
Contact:

Re: Keep body's speed constant

Post by milliams »

That sounds like a sensible way to do it, I'll give it a try.

Thanks :)
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests