-
Active Member
[Classic] 1.13.2.32089 - Angle from player to point
Hi,
I struggle with the function to determine the angle from the player to a point/mob, to know if the point/mob is behind.
I have these functions:
Code:
public float DotProduct(Vector3 unitPosition)
{
unitPosition.Z = PlayerPosition.Z;
var rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, PlayerRotation);
var forward = Vector3.Normalize(Vector3.Transform(new Vector3(0, 1, 0), rotation));
var targetDirection = Vector3.Normalize(unitPosition - PlayerPosition);
var dot = Vector3.Dot(targetDirection, forward);
return dot;
}
public bool IsBehind(Vector3 unitPosition)
{
var angle = Angle(unitPosition);
if (angle <= 180)
{
return false;
}
else
{
return true;
}
}
public float Angle(Vector3 unitPosition)
{
unitPosition.Z = PlayerPosition.Z;
var rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, PlayerRotation);
var forward = Vector3.Normalize(Vector3.Transform(PlayerPosition, rotation));
var targetDirection = Vector3.Normalize(PlayerPosition - unitPosition);
return forward.Angle(targetDirection);
}
The DotProduct, seems to be working well, but the Angle method return different values depending where i stand and look at the mob, any one have solved this, and can tell me what i'm doing wrong?
-
Contributor
isFacing(Self,Unit)
return ((selfPosX-targetPosX)*math.cos(-Rotation))-((selfPosY-targetPosY)*math.sin(-Rotation)) < 0 or distance(self,target) < 1.5;
-
Post Thanks / Like - 1 Thanks
NoxiaZ (1 members gave Thanks to ChrisIsMe for this useful post)
-
Active Member
Originally Posted by
ChrisIsMe
isFacing(Self,Unit)
return ((selfPosX-targetPosX)*math.cos(-Rotation))-((selfPosY-targetPosY)*math.sin(-Rotation)) < 0 or distance(self,target) < 1.5;
Thank you so much, this is working perfectly.
May i ask which language you use?
-
Contributor
Originally Posted by
NoxiaZ
Thank you so much, this is working perfectly.
May i ask which language you use?
C++, this is lua though.