# Box2d - Attaching a fired arrow to a moving enemy

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.

• iforce2d.net/b2dtut/sticky-projectiles Jun 20, 2014 at 14:16
• Does body A (the arrow) need to react to other collisions after it is embedded in the enemy? If it is just the visual representation you need, then destroy the body A, but continue to draw the arrow sprite at the appropriate offset vector from body B. Jun 24, 2014 at 18:42
• @NauticalMile You're completely correct. I don't even need to have the body still there. How do I keep drawing the sprite at the correct position even given rotation of the body it has attached to? Jun 25, 2014 at 1:49

You really just need to call something like enemySprite->addChild(arrowSprite), and destroy body A (or queue it for destruction). I haven't tested it out, but the documentation is pretty clear that child sprites are drawn relative to the parent sprite's transform.