# Cocos2d/Cocos2d-x Attaching an arrow (sprite) to another body sprite (person)

I am trying to set up a simple bow and arrow game. When the arrow hits the enemy body, the arrow's body is deleted and the arrow sprite continues to update, keeping the position correct in relation to the enemy it hit. Picture an arrow sticking into a body and that body still rotating and moving. My problem is that the rotation is completely wrong when the enemy rotates. I know how to do this in 3d with matrix transformation but I can't seem to figure it out in 2d with Cocos.

Here is my method.

1. I save offset at which the arrow hit the enemy.
2. Every frame, I make the rotation of the sprite match the rotation of the enemy.
3. Then, I apply the offset I took initially which is where the arrow hit the enemy.

When they rotate, they rotate about their respective anchors and I am wondering if I need to set the anchor of the arrow to the center of the sprite. Does anyone know of an easy way to do this. If not, I will try to create an algorithm where the anchor is set to the offset divided by the width and height of the sprite image hopefully giving me the correct anchor values.

Then I assume I need to reposition the sprite.

Does anyone have a simpler way to do this?

• possible duplicate of Box2d - Attaching a fired arrow to a moving enemy Commented Jun 25, 2014 at 21:06
• @Nautical This question doesn't mention Box2D, whereas the question you've linked relies heavily on it. Why might it be a duplicate?
– Anko
Commented Jun 26, 2014 at 9:38
• I guess I did not read this question closely enough, my mistake. Commented Jun 26, 2014 at 14:06
• just a question, why don't you change arrows parent, setting it to body on contact? Commented Dec 20, 2015 at 23:32

I think you could accomplish this by applying the following algorithm (shown in pseudocode):

rotateEnemyAndArrow() {
start = arrowPointOfContact - enemyCenter
destination = start.rotate()   // Rotate the start vector by the enemy's rotation
enemy.rotateAroundPoint(enemyCenter)
arrow.rotateAroundPoint(arrowPointOfContact)
arrow.translate(destination - start)
}


arrowPointOfContact and enemyCenter represent the point of contact of the arrow on the enemy and the enemy's center of rotation, respectively.

To illustrate:

Before any rotation is done, we have:

where the blue arrow represents the start vector.

After performing all rotations as listed in the algorithm (in this case it is a 90 degree rotation, but it will work for any) we have:

where the yellow arrow represents the destination vector, and the magenta arrow is our destination - start vector, which the green arrow sprite must be translated by to get to its final position.

• I don't understand destination = start.rotate(). What am I rotating by? The rotation of the enemy sprite? Commented Jun 25, 2014 at 6:02
• After every frame, does the destination vector become the start vector for the next frame's calculation? Commented Jun 25, 2014 at 6:31
• destination = start.rotate() means that you're creating a new vector that is equal to the start vector rotated by the same amount as the enemy
– Ryan
Commented Jun 25, 2014 at 6:36
• And that's correct, the destination vector will be the start vector for the next frame (assuming the algorithm is run once per frame)
– Ryan
Commented Jun 25, 2014 at 6:44
• Still having issues. Am I supposed to be using a coordinate system relative to the center of the enemy sprite? If not, I am not sure what is going on especially given that subtraction of two vectors with such large numbers is going to be offscreen. Commented Jun 25, 2014 at 6:58