• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Math: vertical viewing angle
#2
An alternative way of doing both of these together is through some vector calculations. You want to essentially do step 3 of the image below:


[Image: angle_vectors.png]

Some rough code (that I didn't test for accuracy) would look like:

PHP Code:
new Float:xn, Float:yn, Float:zn;
FCNPC_GetPosition(npcid, xn, yn, zn);

// Get player position
new Float:xp, Float:yp, Float:zp;
if(!
IsPlayerNPC(playerid)) {
   GetPlayerPos(playerid, xp, yp, zp);
}
 else {
    FCNPC_GetPosition(playerid, xp, yp, zp);
}

// First the vector from the NPC to the player
Float:vector_diff[3] = {xp-xnyp-ynzp-zn};


Float:facing_angle FCNPC_GetAngle(npcid) + 90// add 90 degrees to align with unit circle
// Then a unit vector for the angle the NPC is facing - nb. norm of this vector does not matter, but for unit vectors it's 1
Float:vector_facing[3] = {floatcos(facing_angle), floatsin(facing_angle), 0.0f};

// Calculate the dot product, to get the numerator: 
Float:dot_product vector_diff[0] * vector_facing[0] + 
                    vector_diff[1] * vector_facing[1] + 
                    vector_diff[2] * vector_facing[2];

// Calculate the denominator
Float:normcalc VectorSize(vector_diff[0], vector_diff[1], vector_diff[2]); // VectorSize(vector_facing) = 1, so multiplication is not needed in this special case

//Then this is the angle between the facing angle and the player
Float:angle acos(dot_product/normcalc);

return 
angle <= max_viewing_angle// Insert your own variable here 
  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: