View Full Version : Vectors: angle of attack vs pawn's direction
05-10-2012, 01:24 PM
I need to check the direction an attack came from versus the angle the player is facing. Basically, if an attack hit the player in the front or in the back. Under certain conditions the pawn will take less damage if they are facing the attack.
The TakeDamage function has vector input for HitLocation... is this supposed to be where the attack hit the pawn's body? Or is it something else like where he was hit in the world?
If that is a vector for where the attack hit on the pawn, then how to I use that vector to calculate if the pawn was facing the attack or not?
If not, then what should I do to calculate this?
There are so many possible varieties for an attack; I don't know how to properly account for them all. Certainly projectiles are easy to determine, but melee attacks and explosions? I am unsure how I can properly calculate these.
05-10-2012, 02:32 PM
Hitlocation is where the attack hit the pawn, in world coordinates.
you can compare the attacker's location with the receiver's rotation to determine if the hit came from behind/sides/etc, via vector operations (I used a dot product for this). it could also work for melee attacks and probably explosions as well (using the explosion's location with the receiver's rotation)
05-13-2012, 09:24 PM
How would one go about comparing the attacker's location with the receiver's rotation via vector operations?
05-13-2012, 09:37 PM
There's different ways to do it. There is probably a most optimal way. But for a start, get a unit vector from the receiver to the attacker, then use dot product to get the acos of that compared with the unit vector of the receivers facing direction.
05-14-2012, 03:05 AM
this is of course code in the Pawn class. here you go:
local float EnemyDot;
// check where the enemy faces in relation to us
EnemyDot = Normal(EnemyP.Location - Location) dot Normal(Vector(EnemyP.Rotation)); // results: -1:front, 0:either side, 1:back
05-14-2012, 03:12 AM
It shouldn't be necessary to normalise the second part, rotators cast to vectors should produce unit vectors.
05-14-2012, 12:28 PM
Thank you. I had never used dot products before, but now I have. Thank you very much.
EDIT: I do remember this from math class, long ago, like adding matrices?
05-14-2012, 12:40 PM
Ok it is working, but not like it is supposed to. It returns -1 for the player being attacked in the front and the back.
Here is the code. I have the code in the enemy controller of the enemy pawn. After it chooses a target it will store the target in a variable, then:
EnemyDot = Normal(Pawn.Location - thePlayer.Location) dot Normal(Vector(Pawn.Rotation)); // results: -1:front, 0:either side, 1:back
The variable "thePlayer" stores the targeted player, and the Pawn is a reference to the pawn that owns this controller.
05-14-2012, 02:06 PM
Ok, I got it to work by changing things a little:
EnemyDot = Normal(Pawn.Location - thePlayer.Location) dot Normal(Vector(thePlayer.Rotation)); // results: 1:front, 0:either side, -1:back
I changed Normal(Pawn.Rotation)) to Normal(Vector(thePlayer.Rotation)), so now it gets the normal of the vector conversion of the rotation of the player being targeted(our player in the game, or perhaps another pawn or something else being targeted)
But now, 1 is the front, 0 are either side, and -1 is the back.
05-14-2012, 05:08 PM
Normal(A - B) and Normal(B - A) produces different facing vectors, hence why the change works.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.