Page 1 of 1

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

Posted: Fri Jul 17, 2009 2:13 pm
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!

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

Posted: Fri Jul 17, 2009 4:03 pm
by slembcke

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

Posted: Fri Jul 17, 2009 4:44 pm
by clint
Oops--sorry to be the guy who couldn't find the already-existing answer. Thanks.

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

Posted: Tue Jul 21, 2009 10:17 am
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...

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

Posted: Fri Jul 24, 2009 9:29 am
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;
}

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

Posted: Fri Jul 24, 2009 9:56 am
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

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

Posted: Fri Jul 24, 2009 11:10 am
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.

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

Posted: Sun Aug 16, 2009 9:38 am
by clint
The cpvclamp() code (see above) worked like a charm. THANKS!