Force of Physics Impact

I’m playing around with Chipmunk and I want to have a projectile deal damage based on how hard it hits an object, but I’m not having much luck searching for the answer. Anybody possibly know how to do this?

Force is mass * velocity.

So if you have the body, then use the following…

float f = body->GetMass() * body->GetLinearVelocity().length();

But angle of impact affects the force, doesn’t it? If something hits at an angle, it will deal less damage than hitting it straight on.

I think in that case, you can take the normal component, i.e. the component of the velocity that is in the direction of the object being collided with. For 2 circles of equal size, it would be the line between the 2 centers. For a vertical wall, it would be the horizontal velocity component.

This is also approximate anyway since it doesn’t take restitution into account. But should be close enough to use for determining damage or choosing sounds etc.

So I’m assuming Chipmunk doesn’t have this calculation built in? Oh well, thanks. I’ll use your idea.

I don’t know Chipmunk well enough to say. I was actually looking at Box2d for the example I gave.

I don’t use Chipmunk, but in Box2d you would use pre-solve or post-solve event (see http://blog.allanbishop.com/box-2d-2-1a-tutorial-part-6-collision-strength/ for an example)

I don’t know if there is something similar in chipmunk - but it might help your searching…

Thanks. It got me searching. I found a solution, although to use that solution I needed to expose a private data field, so I’m unsure if there’s another way to do it or if Cocos doesn’t pass along the necessary data to achieve this.

Maybe I should out Box2d, considering both of you use it.

For inbuilt chipmunk you have set listener.
You can set listener for onContactBegin, onContactPostSolve, onContactPreSolve & onContactSeperate.

auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(GameClass::onContactBegin, this);
contactListener->onContactSeperate = CC_CALLBACK_1(GameClass::onContactSeperate, this);
	this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);

and you can get both bodies by

bool GameClass::onContactBegin(cocos2d::PhysicsContact &contact) {
	PhysicsBody *a = contact.getShapeA()->getBody();
	PhysicsBody *b = contact.getShapeB()->getBody();
return true;
}

Yes, I have those. I have physics working, including collisions. It was how to determine the force of the impacts that I was trying to figure out. The solution which seems to work requires the cpArbiter, which I don’t think you can get from the PhysicsContact object. It has a cpArbiter object, but its private and has no getter.

I use Box2d because that’s what I started using, before chipmunk was adopted by Cocos2d-x. so there was no ‘this is better’ reasoning.

I do find it quite easy to use, and the docco is good - and iForce2d site has lots of answers to the questions I seem to ask!

But this cold well be true of chipmunk - I have just never used it.

Have a google for Box2d vs chipmunk - the major advantage of Box2d I think is in Bullets - Box2d can prevent tunnelling - but there are advantages and disadvantages of both

Yeah, I’ll do that. Thanks.