PDA

View Full Version : OnPlayerLookAtPlayer

Om3n
07/02/2015, 12:52 PM
Hello, have anyone proper working OnPlayerLookAtPlayer function? i found it here one times but it was fully buged.

Nero_3D
07/02/2015, 01:29 PM
There isn't any accurate function, they simple calculate the shortest distance between a line (where the player is looking at) and a point (the other player), if it is smaller than x (lets say 5.0) than the player is looking at the other one

Also you should specify if you mean the camera or the player character itself

the formula for the distance is d = |front vector x (player2 pos - player1 pos)| / |front vector|
The front vector is from player1 who is supposed to look at player2

Om3n
07/02/2015, 03:37 PM
I mean if camera is looking on player. so this should work ?

Nero_3D
07/02/2015, 06:13 PM
Yes it should

@theory
But if you only calculate the distance between line and point you didn't count the distance to the player at all
If player2 is near player1 and 5m away from the camera center of player1 than he is far more of the screen than player3 who is pretty far way from player1

To make it more accurate you should norm if afterwards [(distance point to line) / (distance player 1 instersection)]
@theory end

Just wrote some code, didn't test that but it compiles so give it a try

stock Float: GetDistranceBetweenLineAndPoint(Float: sx, Float: sy, Float: sz, Float: vx, Float: vy, Float: vz, Float: px, Float: py, Float: pz) {
px -= sx;
py -= sy;
pz -= sz;

return VectorSize(vy * pz - vz * py, vz * px - vx * pz, vx * py - vy * px) / VectorSize(vx, vy, vz);
}
stock IsPlayerLookAtPlayer(playerid, giveplayerid, Float: offset = 1.0) {
new
Float: cX,
Float: cY,
Float: cZ,
Float: gX,
Float: gY,
Float: gZ
;
if(GetPlayerPos(giveplayerid, gX, gY, gZ) && GetPlayerCameraPos(playerid, cX, cY, cZ)) {
new
Float: vX,
Float: vY,
Float: vZ
;
GetPlayerCameraFrontVector(playerid, vX, vY, vZ);

vX = GetDistranceBetweenLineAndPoint(cX, cY, cZ, vX, vY, vZ, gX, gY, gZ); // opposite
vY = VectorSize(gX - cX, gY - cY, gZ - cZ); // hypotenuse
vZ = floatsqroot(vY * vY - vX * vX); // adjacent

return (vX / vZ) < offset;
}
return false;
}

Tamer
07/02/2015, 06:15 PM
At 0.3z this is not possible (or would be inefficient), but with 0.3.7's release, it will be possible.