• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Math: vertical viewing angle
#1
I have a system in which I've worked out the code for giving an NPC a horizontal viewing angle (left image). How would I achieve a similar result, but now for the vertical direction (right image)?
[Image: viewing_angle.png?raw=1]

Below is the code for the left image, for reference. This is a math question, thus the code isn't really necessary, but might help you understand the problem. Just ignore that the angles start from the NPC's center instead of his eyes, that's easy enough to fix. As you can see, the z-position is irrelevant for the horizontal viewing angle, but probably not for the vertical viewing angle (not sure).
PHP Code:
static bool:FAI_IsPlayerInAggroViewingAngle(playeridnpcid) {
    
// Get NPC position
    
new Float:xnFloat:ynFloat:zn;
    
FCNPC_GetPosition(npcidxnynzn);

    
// Get player position
    
new Float:xpFloat:ypFloat:zp;
    if(!
IsPlayerNPC(playerid)) {
        
GetPlayerPos(playeridxpypzp);
    } else {
        
FCNPC_GetPosition(playeridxpypzp);
    }

    
// Calculate the angle between these 2 points
    
new Float:angleBetweenPoints atan2(xp xnyp yn);

    
// Get the NPC facing angle adjusted for the weird GTA angle system
    
new Float:npcFacingAngle 360.0 FCNPC_GetAngle(npcid);

    
// Calculate the smallest difference between these 2 angles as a value between -180.0 and 180.0
    
new Float:angleDifference angleBetweenPoints npcFacingAngle;
    if(
angleDifference 180.0) {
        
angleDifference -= 360.0;
    }
    if(
angleDifference < -180.0) {
        
angleDifference 360.0;
    }

    
// Get the absolute value of this angle
    
angleDifference floatabs(angleDifference);

    
// Check if the player is within the aggro viewing angle
    
if(angleDifference <= FAI_NPCs[npcid][FAI_NPC_AGGRO_VIEWING_ANGLE][playerid]/2) {
        return 
true;
    }
    return 
false;


Here's another visualisation of what the result should look like:
[Image: viewing_angle2.png?raw=1]
Always keep in mind that a lot of people are active on this forum in their spare time.

They are sacrificing time they could easily spend on things they would rather do, to help you instead.

  Reply


Messages In This Thread
Math: vertical viewing angle - by Freaksken - 2019-07-15, 08:18 PM
RE: Math: vertical viewing angle - by hiddos - 2019-07-15, 09:27 PM
RE: Math: vertical viewing angle - by Freaksken - 2019-07-15, 10:07 PM
RE: Math: vertical viewing angle - by hiddos - 2019-07-16, 06:20 PM
RE: Math: vertical viewing angle - by Freaksken - 2019-08-15, 08:26 PM

Forum Jump: