Cap Force/Velocity? (I made a PayPal donation)

Official forum for the Chipmunk2D Physics Library.
Post Reply
clint
Posts: 11
Joined: Mon May 11, 2009 11:55 am
Contact:

Cap Force/Velocity? (I made a PayPal donation)

Post by clint »

Am I going to publicize my PayPal donation (confirmation # ending in 76059) in hopes of getting help on the forums? Yes, yes I am. ;)

Does anyone know how I can cap the force or velocity of objects?

I have several cpBodies (circles, specifically) being displayed in an iPhone game. You can use your fingertip to push circles out of the way (i.e., an invisible circle exists under your finger and pushes the other bodies out of the way). You can also tap directly on the circles--which are about the size of your fingertip--and drag them around.

The problem is that if you start pinching several of the circles together one of them will occasionally shoot off with a ridiculous velocity (enough to escape the bounds of the game).

I'm thinking one (perhaps not the best) simple way to fix this is to figure out how to set a max velocity or force. Is a custom velocity integration function (e.g., cpBodyUpdateVelocity()) the best way to do this? I realize that I could also modify the timestep iterations (or even the thickness of my bounding box walls) to help keep objects on the screen, but the objects would still "shoot off" at crazy speeds and look weird.

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

Re: Cap Force/Velocity? (I made a PayPal donation)

Post by slembcke »

Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
clint
Posts: 11
Joined: Mon May 11, 2009 11:55 am
Contact:

Re: Cap Force/Velocity? (I made a PayPal donation)

Post by clint »

Oops--sorry to be the guy who couldn't find the already-existing answer. Thanks.
clint
Posts: 11
Joined: Mon May 11, 2009 11:55 am
Contact:

Re: Cap Force/Velocity? (I made a PayPal donation)

Post by clint »

Unfortunately the method suggested in those posts aren't working for me (i.e., gradually reducing, or even capping, the velocities in a custom velocity integration function). More specifically, they work with "normal" velocities but not when I "pinch" a shape using two other shapes under my fingertips.

Again, the phenomenon is something like this: you have three coins on a table, you place your thumb on a coin and your index finger on another coin, then you use them to "pinch" the third coin--which shoots out like a bullet.

Perhaps the forces from the two pinching shapes are so great that the velocity reduction gets ignored? Does Chipmunk integrate forces AFTER the custom velocity integration function executes?

Again thanks for any help or advice. I'm really scratching my head on this one...
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Cap Force/Velocity? (I made a PayPal donation)

Post by slembcke »

Well, reading this again, your problem is that objects sometimes go fast enough to escape the game. Not that you want to keep objects moving at a particular velocity. I suppose in that case maybe you should just clamp the velocity off when it gets to high. In your custom integration function just add a call to cpvclamp(). It's a new function in trunk, here is the code for it in case you aren't running that version:

Code: Select all

static inline cpVect
cpvclamp(const cpVect v, const cpFloat len)
{
        return (cpvdot(v,v) > len*len) ? cpvmult(cpvnormalize(v), len) : v;
}
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jeapostrophe
Posts: 13
Joined: Thu Jul 23, 2009 7:54 am
Contact:

Re: Cap Force/Velocity? (I made a PayPal donation)

Post by jeapostrophe »

This happens to me too. I set up static bodies around the space and things can still get out. Shouldn't that be considered a bug? Or is a result of the iterative approximation that is unavoidable?

Any suggestions for deciding what is "too fast"? Just whatever looks right in our situation or do you have an idea of what speeds cause approximation errors?

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

Re: Cap Force/Velocity? (I made a PayPal donation)

Post by slembcke »

It's not a bug, it's a limitation because Chipmunk does not do swept collisions. This is partly for speed, partly for simplicity, and partly because swept collisions are neither easy or fun to implement.

A simple but effective trick I use it to make the edges of the world thicker. For instance, instead of just using line segments to outline the world, use line segments with a big beefy beveling radius.

If your game is simply fast paced and object are always moving fast, you probably need to be using a smaller timestep. That will help avoid missed collisions between all objects.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
clint
Posts: 11
Joined: Mon May 11, 2009 11:55 am
Contact:

Re: Cap Force/Velocity? (I made a PayPal donation)

Post by clint »

The cpvclamp() code (see above) worked like a charm. THANKS!
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests