简体   繁体   中英

Check collision speed in Chipmunk Physics

I'm using Chipmunk 5 for iPhone, with Cocos2D. Upon collision between two specific objects I'd like to run a method which checks the velocity of that collision, if it's over x it runs one set of code, if it's under x it runs another.

Now, I understand the basics but I can't work out the code to run a method on collision detection, and the code to check impact speed.

Any help or pointers would be greatly appreciated.

Cheers.

For Chipmunk 5, assuming that you want to play a sound or apply damage from the collision, I'd recommend calling the cpArbiterTotalImpulse() function from a post-solve callback. You can find more information in the docs here. http://chipmunk-physics.net/release/Chipmunk-5.x/Chipmunk-5.3.5-Docs/#Callbacks

Also, Chipmunk 5 is fairly old at this point. If it's not a hassle, I'd suggest upgrading to 6.x. Then you could use the cpArbiterTotalKE() function. (If you are near the end of an existing project though, I'd just stick with 5.x.) The kinetic energy is an even better indicator of how "bad" a collision is than the impulse strength.

Check out the addCollisionCallbackBetweenType function in SpaceManager.h That might help with registering collisions.

After that, you can check the v property for the cpBody objects, a cpVect of the velocity. ( http://code.google.com/p/chipmunk-physics/source/browse/trunk/include/chipmunk/cpBody.h , not linked, new user.)

I am not experienced with chipmunk, but that's what a little googling turned up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM