Box2d - Attaching a fired arrow to a moving enemy

Posted by Satchmo Brown on Game Development See other posts from Game Development or by Satchmo Brown
Published on 2014-06-20T04:25:40Z Indexed on 2014/08/24 22:36 UTC
Read the original article Hit count: 452

Filed under:
|

I am firing an arrow from the player to moving enemies. When the arrow hits the enemy, I want it to attach exactly where it hit and cause the enemy (a square) to tumble to the ground. Excluding the logistics of the movement and the spin (it already works), I am stuck on the attaching of the two bodies. I tried to weld them together initially but when they fell, they rotated in opposite directions.

I have figured that a revolute joint is probably what I am after. The problem is that I can't figure out a way to attach them right where they collide.

Using code from iforce2d:

b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.bodyA = m_body;
revoluteJointDef.bodyB = m_e->m_body;
revoluteJointDef.collideConnected = true;
revoluteJointDef.localAnchorA.Set(0,0);//the top right corner of the box
revoluteJointDef.localAnchorB.Set(0,0);//center of the circle
b2RevoluteJoint m_joint = *(b2RevoluteJoint*)m_game->m_world->CreateJoint( &revoluteJointDef );
m_body->SetLinearVelocity(m_e->m_body->GetLinearVelocity());

This attaches them but in the center of both of their points.

Does anyone know how I would go about getting the exact point of collision so I can link these? Is this even the right method of doing this?

Update:

I have the exact point of collision. But I still am not sure this is even the method I want to go about this. Really, I just want to attach body A to B and have body B unaffected in any way.

© Game Development or respective owner

Related posts about box2d

Related posts about cocos2d-x