SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 24/03/2010, 06:20 PM   #1
JernejL
Beta Tester
 
JernejL's Avatar
 
Join Date: Jan 2006
Location: Slovenia
Posts: 569
Reputation: 368
Default Camera Vectors in R5 - how to use, READ THIS!

First of all, discard the "UP" vector, the only vector of use currently is the FRONT vector.

The FRONT vector is simply a unit length (1 unit) vector direction in which the player is aiming from the camera position.

How to make sense of this..:

Basically, if you add camera front vector to the camera position vector, you will get another point in game space which is exactly 1 unit apart from the player and is in the direction where the player is looking at.

That camera target point can be then moved forward and backwards to have a point of reference against a specific target and see if the player is aiming towards it.

Example usage:

1. calculate distance from camera position to target check position (say a specific object)
2. rescale the front vector so it's length will be the distance to object to check, THIS IS VERY EASY - Since the vector is always of 1.0 length you can simply multiply the front vector XYZ values by the distance from #1, we will call this vector a tempcamtarget vector.
3. Now since you have the tempcamtarget point that is of known distance away from the target point and towards what the player is looking at, you can do a simple distance check from the tempcamtarget to the object position, the distance returned will be how far off from target object the player is looking, so you can know if he's looking at a certain object, or looking away from it.

EXAMPLE CODE:

Code:
Float:DistanceCameraTargetToLocation(Float:CamX, Float:CamY, Float:CamZ,  Float:ObjX, Float:ObjY, Float:ObjZ,  Float:FrX, Float:FrY, Float:FrZ) {
	
	new Float:TGTDistance;
	
	// get distance from camera to target
	TGTDistance = floatsqroot((CamX - ObjX) * (CamX - ObjX) + (CamY - ObjY) * (CamY - ObjY) + (CamZ - ObjZ) * (CamZ - ObjZ));
	
	new Float:tmpX, Float:tmpY, Float:tmpZ;
	
	tmpX = FrX * TGTDistance + CamX;
	tmpY = FrY * TGTDistance + CamY;
	tmpZ = FrZ * TGTDistance + CamZ;
	
	return floatsqroot((tmpX - ObjX) * (tmpX - ObjX) + (tmpY - ObjY) * (tmpY - ObjY) + (tmpZ - ObjZ) * (tmpZ - ObjZ));
}
The result of the function is how close to the object centre the player is looking at, use it like - if value is < 1.0 then the player is looking at the object within target diameter of 2.0 units (for 1.0 radius.. diameter is 2.0)

Code is untested, but compiles and should work.
__________________


samp://198.50.173.59:7777 - Partyserver - TurtleServer (now with extra turtles)!

SA-MP map editor: Not a in-game script but a proper windows application!
http://forum.sa-mp.com/showthread.php?t=282801
JernejL is offline   Reply With Quote
Old 24/03/2010, 06:42 PM   #2
Norn
High-roller
 
Norn's Avatar
 
Join Date: Jul 2008
Location: Northern Ireland
Posts: 2,718
Reputation: 206
Default Re: Camera Vectors in R5 - how to use, READ THIS!

From this can we calculate a vehicle in front of the players distance/players distance simply by looking at it?
__________________
Pursuing other endeavours.

Skype: datinvictusguy
Norn is offline   Reply With Quote
Old 24/03/2010, 06:44 PM   #3
JernejL
Beta Tester
 
JernejL's Avatar
 
Join Date: Jan 2006
Location: Slovenia
Posts: 569
Reputation: 368
Default Re: Camera Vectors in R5 - how to use, READ THIS!

Quote:
Originally Posted by Norn [BINMAN
]
From this can we calculate a vehicle in front of the players distance/players distance simply by looking at it?
Yes, the script can know exact what car you are looking at.
__________________


samp://198.50.173.59:7777 - Partyserver - TurtleServer (now with extra turtles)!

SA-MP map editor: Not a in-game script but a proper windows application!
http://forum.sa-mp.com/showthread.php?t=282801
JernejL is offline   Reply With Quote
Old 24/03/2010, 06:47 PM   #4
wafffllesss
Huge Clucker
 
Join Date: Apr 2007
Posts: 413
Reputation: 1
Default Re: Camera Vectors in R5 - how to use, READ THIS!

Will be waiting for your example code.

( More upgrades to my zombie script =D )
wafffllesss is offline   Reply With Quote
Old 24/03/2010, 07:23 PM   #5
Norn
High-roller
 
Norn's Avatar
 
Join Date: Jul 2008
Location: Northern Ireland
Posts: 2,718
Reputation: 206
Default Re: Camera Vectors in R5 - how to use, READ THIS!

Quote:
Originally Posted by RedShirt (JernejL)
Quote:
Originally Posted by Norn [BINMAN
]
From this can we calculate a vehicle in front of the players distance/players distance simply by looking at it?
Yes, the script can know exact what car you are looking at.
Epic thanks for explaining this in simple terms.
__________________
Pursuing other endeavours.

Skype: datinvictusguy
Norn is offline   Reply With Quote
Old 24/03/2010, 09:13 PM   #6
JernejL
Beta Tester
 
JernejL's Avatar
 
Join Date: Jan 2006
Location: Slovenia
Posts: 569
Reputation: 368
Default Re: Camera Vectors in R5 - how to use, READ THIS!

CODE EDITED - THIS IS CORRECT AND TESTED!

Code:
Float:DistanceCameraTargetToLocation(Float:CamX, Float:CamY, Float:CamZ,  Float:ObjX, Float:ObjY, Float:ObjZ,  Float:FrX, Float:FrY, Float:FrZ) {
	
	new Float:TGTDistance;
	
	// get distance from camera to target
	TGTDistance = floatsqroot((CamX - ObjX) * (CamX - ObjX) + (CamY - ObjY) * (CamY - ObjY) + (CamZ - ObjZ) * (CamZ - ObjZ));
	
	new Float:tmpX, Float:tmpY, Float:tmpZ;
	
	tmpX = FrX * TGTDistance + CamX;
	tmpY = FrY * TGTDistance + CamY;
	tmpZ = FrZ * TGTDistance + CamZ;
	
	return floatsqroot((tmpX - ObjX) * (tmpX - ObjX) + (tmpY - ObjY) * (tmpY - ObjY) + (tmpZ - ObjZ) * (tmpZ - ObjZ));
}
Code:
// EXAMPLE USAGE:

checkforcamera(PlayerID) {
	
	new Float:CameraPos[3];
	new Float:CameraFront[3];
	new Float:CheckObjectPos[3] = {-2661.2764,915.7676,79.6685}; // trash can near partyserver torenos
	
	GetPlayerCameraPos(PlayerID, CameraPos[0], CameraPos[1], CameraPos[2]);
	GetPlayerCameraFrontVector(PlayerID, CameraFront[0], CameraFront[1], CameraFront[2]);
	
	new Float:distc;
	distc = DistanceCameraTargetToLocation(CameraPos[0], CameraPos[1], CameraPos[2],  CheckObjectPos[0], CheckObjectPos[1], CheckObjectPos[2],  CameraFront[0], CameraFront[1], CameraFront[2]);
	
	new string[128];
	if (distc < 2.0) {
		format(string,sizeof(string),"YOU SEE IT.");
		SendClientMessage(PlayerID, COLOR_RED, string);
	}
	
	return true;
}
__________________


samp://198.50.173.59:7777 - Partyserver - TurtleServer (now with extra turtles)!

SA-MP map editor: Not a in-game script but a proper windows application!
http://forum.sa-mp.com/showthread.php?t=282801
JernejL is offline   Reply With Quote
Old 25/03/2010, 05:15 AM   #7
CracK
Huge Clucker
 
CracK's Avatar
 
Join Date: Nov 2007
Posts: 425
Reputation: 2
Default Re: Camera Vectors in R5 - how to use, READ THIS!

pawn Code:
stock IsPlayerAimingAt(playerid, Float:x, Float:y, Float:z, Float:radius)
{
    new Float:cx,Float:cy,Float:cz,Float:fx,Float:fy,Float:fz;
    GetPlayerCameraPos(playerid, cx, cy, cz);
    GetPlayerCameraFrontVector(playerid, fx, fy, fz);
    return (radius >= DistanceCameraTargetToLocation(cx, cy, cz, x, y, z, fx, fy, fz));
}
returns 1, if a player is looking at position x,y,z
Use radius 1.0 for players
__________________
Ľ Custom Enex loader 1.0
Ľ map2pwn
CracK is offline   Reply With Quote
Old 25/03/2010, 07:08 AM   #8
Burridge
High-roller
 
Burridge's Avatar
 
Join Date: Nov 2008
Location: England
Posts: 2,085
Reputation: 26
Default Re: Camera Vectors in R5 - how to use, READ THIS!

Thanks, RedShirt. Good post.
__________________
Burridge is offline   Reply With Quote
Old 25/03/2010, 07:18 PM   #9
boelie
Gangsta
 
boelie's Avatar
 
Join Date: Apr 2009
Location: San Fierro
Posts: 661
Reputation: 1
Default Re: Camera Vectors in R5 - how to use, READ THIS!

Hmm this might sound noobish but

im trying IsPlayerAimingAt in a command but im not sure if im doing it correct.
Can someone give an example ?
__________________
boelie is offline   Reply With Quote
Old 25/03/2010, 07:30 PM   #10
JernejL
Beta Tester
 
JernejL's Avatar
 
Join Date: Jan 2006
Location: Slovenia
Posts: 569
Reputation: 368
Default Re: Camera Vectors in R5 - how to use, READ THIS!

Quote:
Originally Posted by boelie
Hmm this might sound noobish but

im trying IsPlayerAimingAt in a command but im not sure if im doing it correct.
Can someone give an example ?
Just call it with playerid, target coords and target radius, how hard is that?
__________________


samp://198.50.173.59:7777 - Partyserver - TurtleServer (now with extra turtles)!

SA-MP map editor: Not a in-game script but a proper windows application!
http://forum.sa-mp.com/showthread.php?t=282801
JernejL is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vectors potato Help Archive 1 08/08/2010 05:09 AM
Camera Help TKZ227 Help Archive 1 04/06/2010 11:24 PM
camera pos Kar Help Archive 8 03/06/2010 08:12 AM
Camera Help CharlieScene Help Archive 3 13/04/2010 05:46 PM
Please Read >>>> How To Add Play Class Selection Music? <<<< Please Read DeltaAirlines12 Help Archive 8 15/08/2009 10:41 PM


All times are GMT. The time now is 07:48 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.