How player.AimDir is calculated?

habi

  • Sr. Member
  • Posts: 400
How player.AimDir is calculated?
«  »Last edited
Do anybody know how exactly aimdirection is calculated?
I have tried for two days:
Code: [Select]
function GetAimDir(aimPos, targetPos)
{
local x,y,z;
local angle = atan2( -(targetPos.x-aimPos.x), targetPos.y-aimPos.y ); //vcmp angle
local alpha=atan2( targetPos.z - aimPos.z, sqrt( pow( targetPos.y - aimPos.y, 2 ) + pow( targetPos.x - aimPos.x , 2 ) ) );
if(angle >0 ) //angle 0 to 3.14
{
//Quadrant can be North West or South West
if(angle <PI/2)
{
//Quadrant is North West
z=angle;
x=0.0;
if( alpha > 0 )
{
y= -PI + alpha;
}else
y= PI + alpha;
}else
{
//Quadrant is South West
z=PI-angle;
x=PI;
y= alpha;
}
}else
{
//Quadrant can be North East or South East
if( angle > -PI/2 )
{
//Quadrant is North East
z= angle;
x= 0.0;
if( alpha > 0 )
{
y= -PI + alpha;
}else
y= PI + alpha;
}else
{
//Quadrant is South East
z= -PI-angle;
x= PI;
y= alpha;
}
}
return Vector(x,y,z);
}

But this is not exact. The bullet misses the point.

AimPos seems to be independent of player's Position. By altering packets, i found out that the player shoots a particular point if (AimPos, AimDir) are specified. Then changing position with former variables i.e. aimpos and aimdir same, still the player shoots the point.
However, if the position is too 'out of range', the player shoots somewhere else.

So the question is how is aim direction calculated, given the position to hit?

Update- Using aiming weapons like 27, using my code hits the target perfectly, the directions being calculated as above. But it hit somewhere when in normal firing (without aiming or right click). It hits near, but not exact.
Re: How player.AimDir is calculated?
« Reply #1,  »
i don't know how it is calculated, but found a way to make the player shoot npc.
The server sends data to clients aimpos and a three co-ordinates which are "a normalized direction vector " multiplied by 16.0
( Thanks to the reVC codes )
If a RayTrace is created with aimpos as start and range * this vector, then the RayTrace hits npc's head, as my experiments show.