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 );

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?


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.

  • 1
    \$\begingroup\$ iforce2d.net/b2dtut/sticky-projectiles \$\endgroup\$ Commented Jun 20, 2014 at 14:16
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 24, 2014 at 18:42
  • \$\begingroup\$ @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? \$\endgroup\$ Commented Jun 25, 2014 at 1:49

1 Answer 1


I would recommend destroying the physics body of the arrow when it comes to rest, but continue to draw the sprite at the location where it contacted the enemy.

The cleanest way to do this is with object aggregation: i.e. make the arrow a 'child' of entity B. It appears this feature is built-in to cocos2d-x.

I assume you already have a technique for stopping the arrow after it penetrates body B by some distance, so I will just talk about what should happen when the arrow comes to rest relative to body B.

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.

Like in one of my previous answers, I will point out that if you can encapsulate this collision-attachment code with the arrow objects, you won't need to worry about what the arrow is embedding itself into (could be enemies, wagons, bridges, molasses, etc...), it will happily attach itself to the other sprite.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .