I have a player ship with 4 armour locations, front, rear, left, right.
I'm trying to determine the angle a bullet hits the player ship so to determine hit location (like which armour takes damage). 0 is front, 90 is right side, 180 is rear, 270 is left.
The world angles are zero to the left, then clockwise around.
I know the player's angle of rotation. I know the world angle the bullet comes from with respect to the player.
I just don't know how to translate that to a hit location. Please see the image below:
<img src="http://gordonlittle.ca/images/example.jpg" border="0" />
If I understand right, in the image :
Angle(World,Player) = 45
Angle(World,Enemy) = 270
(By the way, usually, angles are handled counter-clockwise)
What you want is Angle(Enemy,Player). Let's decompose angles then :
= Angle(Enemy,World) + Angle(World,Player)
= - Angle(World,Enemy) + Angle(World,Player)
In the schema's case :
Angle(Enemy,Player) = - 270 + 45 = -225? = 135?, seems about right.
Then, hit location is just a switch case.
If the angle(Enemy,Player) is between -45 and +45, it's the front side.
If the angle(Enemy,Player) is between 45 and 135, it's the right side.
If the angle(Enemy,Player) is between -45 and -135, it's the left side.
If the angle(Enemy,Player) is between 135 and 225, it's the back side.
Develop games in your browser. Powerful, performant & highly capable.
Perhaps another approach:
add 4 origin points point to your player, each side one.
add 4 invisible collision sprites, each one pinned on one of the origin locations.
Determine hits by means of detecting collisions on the given sprites.
edit: I suggested this, because when you use the angle approach and the players rotate, the results will be off.
OK I think I came up with something (with a lot of help) that works and takes the player's rotation into account.
<img src="http://gordonlittle.ca/images/Capture.PNG" border="0" />