Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualDgekGD

Posted 21 November 2012 - 12:04 PM

You can use Dot Product for this purpose.

Consider PlayerDir as a vector pointing in direction the player looks and EnemyDir is the direction in which an enemy moves.

Using following calculation :
     (PlayerDir.x * EnemyDir.x + PlayerDir.y * EnemyDir.y);


you get the value of cosine between their directions. Importantly, make sure that PlayerDir and EnemyDir are normalized! Otherwise you have to divide your result by length of both vectors!

If result was positive it means, that player and enemy moved in the same direction, so the player was hit in the back. If negative than, they moved in opposite directions. If you get value really close to 0, it means the player was hit from the side.

#2DgekGD

Posted 21 November 2012 - 12:01 PM

You can use Dot Product for this purpose.

Consider PlayerDir as a vector pointing in direction the player looks and EnemyDir is the direction in which an enemy moves.

Using following calculation :
     (PlayerDir.x * EnemyDir.x + PlayerDir.y * EnemyDir.y);


you get the value of cosine between their directions. Importantly, make sure that PlayerDir and EnemyDir are normalized! Otherwise you have to divide your result by length of both vectors!

If Dot Project was positive it means, that player and enemy moved in the same direction, so the player was hit in the back. If negative than, they moved in opposite directions. If you get value really close to 0, it means the player was hit from the side.

#1DgekGD

Posted 21 November 2012 - 12:00 PM

You can use Dot Product for this purpose.

Consider PlayerDir as a vector pointing in direction the player looks and EnemyDir is the direction in which an enemy moves.

Using following calculation :
     (PlayerDirec1.x * EnemyDir.x + PlayerDir.y * EnemyDir.y);


you get the value of cosine between their directions. Importantly, make sure that PlayerDir and EnemyDir are normalized! Otherwise you have to divide your result by length of both vectors!

If Dot Project was positive it means, that player and enemy moved in the same direction, so the player was hit in the back. If negative than, they moved in opposite directions. If you get value really close to 0, it means the player was hit from the side.

PARTNERS