SA-MP Forums Archive
OnPlayerLookAtPlayer - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: OnPlayerLookAtPlayer (/showthread.php?tid=562179)



OnPlayerLookAtPlayer - Om3n - 07.02.2015

Hello, have anyone proper working OnPlayerLookAtPlayer function? i found it here one times but it was fully buged.


AW: OnPlayerLookAtPlayer - Nero_3D - 07.02.2015

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


Re: OnPlayerLookAtPlayer - Om3n - 07.02.2015

I mean if camera is looking on player. so this should work ?


AW: OnPlayerLookAtPlayer - Nero_3D - 07.02.2015

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
pawn Код:
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);
}
pawn Код:
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;
}



Re: OnPlayerLookAtPlayer - Tamer - 07.02.2015

At 0.3z this is not possible (or would be inefficient), but with 0.3.7's release, it will be possible.