Search

open.mp forum

RegisterLogin

Discussion

General
Chat
Tech
Life
Art
Programming
open.mp
Development Updates
Questions and Suggestions
SA-MP
General Discussions
Videos and Screenshots
Support
Pawn Scripting
Tutorials
Releases
Plugins
Libraries
Filterscripts
Gamemodes
Maps
Advertisements
Other languages
Spanish/Espa?ol
Programaci?n
Discusi?n GTA SA Multijugador
Mods
Offtopic
Juegos
Portuguese/Portugu?s
Russian/???????
Italian/Italiano
Dutch/Nederlands
German/Deutsch
Romanian/Rom?na
Ex-Yu
Polish/Polski
Og?lne
Serwery
Skryptowanie
Filmiki i zdjecia
Lithuanian/Lietuvi?kas
French/Fran?ais
Hungarian/Magyar
Hindi/Urdu
Turkish
Other
Internal
Team
Hidden
Archived

Library

 Collections Links Members Roles

Math: vertical viewing angle

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...

ID
d6ouivfilegovhg7bcd0
author
freaksken's avatar

Freaksken

@freaksken


View profile
Copy link
  Report member
started
Jul 15, 2019
replies
0
participating
No

scroll to top

powered by storyden

Login
Discussion
Pawn Scripting
Math: vertical viewing angle
freaksken's avatar

Freaksken

@freaksken


View profile
Copy link
  Report member
• 6y
Pawn Scripting

Math: vertical viewing angle

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)?



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).

static bool:FAI_IsPlayerInAggroViewingAngle(playerid, npcid) {
	// Get NPC position
	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);
	}

	// Calculate the angle between these 2 points
	new Float:angleBetweenPoints = atan2(xp - xn, yp - 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/2) {
		return true;
	}
	return false;
}



Here's another visualisation of what the result should look like:

0 likes0 replies

    Please sign up or log in to reply