SA-MP Forums Archive
Detecting if a player is infront or behind a player - 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: Detecting if a player is infront or behind a player (/showthread.php?tid=578426)



Detecting if a player is infront or behind a player - Puppy - 19.06.2015

Hello.

How can I make functions such as:
IsPlayerBehindPlayer(player1, player2) - if the player is behind the player / looking at their back then it returns true, else false.
IsPlayerInfrontofPlayer(player1, player2) - Same as above but for the front.

Basically, I know it can be done by comparing facing angles but would they be the same? Or how else could I compare them to accurately return the desired result? Thanks for any help.


Re: Detecting if a player is infront or behind a player - Lajko1 - 19.06.2015

This will turn player to another player but I guess you can combine something out of this ^^

pawn Код:
stock SetPlayerFacePlayer(playerid, giveplayerid) {
    new
        Float: pX,
        Float: pY,
        Float: pZ,
        Float: gX,
        Float: gY,
        Float: gZ
    ;
    if(GetPlayerPos(playerid, pX, pY, pZ) && GetPlayerPos(giveplayerid, gX, gY, gZ)) {
        SetPlayerFacingAngle(playerid, (pX = -atan2((gX - pX), (gY - pY))));
        return SetPlayerFacingAngle(giveplayerid, (pX + 180.0));
    }
    return false;
}
Not my code, searched for you.


Re: Detecting if a player is infront or behind a player - Lawbringer - 19.06.2015

Assuming that GTA SA is one big coordinate plane with X and Y, then (0,0) is right in Blueberry.

Quadrant I is (+,+) [Top Right = Las Venturas area]
Quadrant II is (-,+) [Top Left = San Fierro, Bayside, etc.]
Quadrant III is (-,-) [Bottom Left = Missionary Hill, Angel Pine, Mount Chiliad, etc.]
Quadrant IV is (+,-) [Bottom Right = Los Santos]

By applying a basic algebraic algorithm, you can determine which quadrant Player A resides in, and then do an appropriate calculation to determine which player is in front of which based on their facing angles.

I'll lay out a piece of it for you (because i'm certainly not going to do it all for you, then you wouldn't learn anything).

stock GetPlayerMapQuadrant(playerid)
{
new Floatx, Float: py, Floatz;
GetPlayerPos(playerid,px,py,pz);

if(px >= 0)
{ // Top
if(py >= 0) return 1;
else return 2;
}
else
{ // Bottom
if(py >= 0) return 4;
else return 3;
}
}
--

Using this quadrant function, you can determine what kind of trigonometry you have to apply to get your answer. I'll give you a hint:
- You're going to want to basically imagine three points - One at (0,0) in Blueberry, one at player A and one at player B.
- This angle must be a right angle, because it assumes straight-line distance from Blueberry and straight-line LOS from Player A to B (thus creating a right triangle in various orientations).
- Because it is a right triangle, you can apply various trig theorems on it - that is what you need to figure out.

There's your hint! Good luck!