2

I am working on a game and have the following information

Player Position - Vector3
Player Rotation - float (Radiant)
Enemy Location - Vector3
Player attack box - length, width, height

What I need to do it test if the Enemy Location is inside of the player attack box. I know somehow I would either have to rotate the attack box around the player or rotate the enemy location around the player then test it. The rotation is only left to right no up and down. That is why it is a single Radiant value.

I have tried to code the rotation of both the player attack box and the enemy but I feel like I do not have enough knowledge about vector math to properly come up with a solution.

1 Answers1

2

As far as I understand, it is enough to determine whether enemy is inside rotated (around z-axis) parallelepiped centered at player

We can transform enemy coordinates into player coordinate system. To perform this task, we should make translation (to provide player position in origin), then make rotation by reverse angle

newEnemyX = (enemy.x - player.x) * Cos(P_Rotation) + (enemy.y - player.y) * Sin(P_Rotation)
newEnemyY = -(enemy.x - player.x) * Sin(P_Rotation) + (enemy.y - player.y) * Cos(P_Rotation)
newEnemyZ = enemy.z - player.z

Now compare (I assume length-width-height correspond to x,y,z)

 -length/2 <= newEnemyX <= length/2
 -width/2 <= newEnemyY <= width/2
 -height/2 <= newEnemyZ <= height/2
MBo
  • 77,366
  • 5
  • 53
  • 86