1
\$\begingroup\$

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?

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

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:

before rotation

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:

after rotation

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.

\$\endgroup\$
7
  • \$\begingroup\$ I don't understand destination = start.rotate(). What am I rotating by? The rotation of the enemy sprite? \$\endgroup\$ Jun 25 '14 at 6:02
  • \$\begingroup\$ After every frame, does the destination vector become the start vector for the next frame's calculation? \$\endgroup\$ Jun 25 '14 at 6:31
  • \$\begingroup\$ 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 \$\endgroup\$
    – Ryan
    Jun 25 '14 at 6:36
  • \$\begingroup\$ And that's correct, the destination vector will be the start vector for the next frame (assuming the algorithm is run once per frame) \$\endgroup\$
    – Ryan
    Jun 25 '14 at 6:44
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 25 '14 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.