My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum


Reply
 
LinkBack Thread Tools Display Modes
March 29th, 2010, 11:11 AM   #1
Newbie
 
Joined: Mar 2010

Posts: 1
Thanks: 0

3D Perspective Projection

Hello all, I'm currently working on a lock-on system in a multiplayer mod for Grand Theft Auto: San Andreas where it creates a cross hair-like image over top of the closest player to you. As far as I know, given the camera's position, camera's rotation, the point to be projected, and the position of the "eye" (I believe?), I can use them in a "camera transform".

Here's my reference: http://en.wikipedia.org/wiki/3D_project ... projection

Just before I hop into the code itself, it's in the PAWN language. PAWN is a relatively C-like language where variables are declared with the "new" identifier (as-is "stock", but it's used for declaring functions), and hopefully the rest is self-explanatory.

Here it is:
Code:
stock GetXYScreenCoords(playerid, Float:tX, Float:tY, Float:tZ, &Float:X, &Float:Y)
{
	new Float:d[3], Float:cX, Float:cY, Float:cZ, Float:aX, Float:aY, Float:aZ;

	GetPlayerCameraPos(playerid, cX, cY, cZ);
	GetPlayerCameraRot(playerid, aX, aY, aZ);	


	d[0] = floatcos(aY, degrees) 	* 
		(floatsin(aZ, degrees) 	* (tY - cY) 	+ 
		floatcos(aZ,degrees) 	* (tX - cX)) 	-
		floatsin(aY, degrees) 	* (tZ - cZ);

	d[1] = floatsin(aX, degrees) 	*
		(floatcos(aY, degrees) 	* (tZ - cZ) 	+
		floatsin(aY, degrees) 	* 
		(floatsin(aZ, degrees) 	* (tY - cY) 	+
		floatcos(aZ, degrees) 	* (tX - cX))) 	+
		floatcos(aX, degrees) 	* 
		(floatcos(aZ, degrees) 	* (tY - cY) 	-
		floatsin(aZ, degrees) 	* (tX - cX));

	d[2] = floatcos(aX, degrees) 	*
		(floatcos(aY, degrees) 	* (tZ - cZ) 	+
		floatsin(aY, degrees) 	* 
		(floatsin(aZ, degrees) 	* (tY - cY) 	+ 
		floatcos(aZ, degrees) 	* (tX - cX))) 	-
		floatsin(aX, degrees) 	* 
		(floatcos(aZ, degrees) 	* (tY - cY) 	-
		floatsin(aZ, degrees) 	* (tX - cX));

	X = (d[0] - eX) * (eZ / d[2]);
	Y = (d[1] - eY) * (eZ / d[2]);
}

stock GetCameraRot(playerid, &Float:X, &Float:Y, &Float:Z)
{
	new Float:cx[2], Float:cy[2], Float:cz[2], Float:a[3];
	GetPlayerCameraPos(playerid, cx[0], cy[0], cz[0]);
	GetPlayerCameraFrontVector(playerid, cx[1], cy[1], cz[1]);

	a[0] = atan2((cy[0] - (cy[1] + cy[0])), (cz[0] - (cz[1] + cz[0])));
	a[1] = atan2((cx[0] - (cx[1] + cx[0])), (cz[0] - (cz[1] + cz[0])));
	a[2] = atan2((cx[0] - (cx[1] + cx[0])), (cy[0] - (cy[1] + cy[0])));

	if(a[0] < 0.0) 		a[0] += 360.0;
	if(a[0] > 360.0)	a[0] -= 360.0;
	if(a[1] < 0.0) 		a[1] += 360.0;
	if(a[1] > 360.0)	a[1] -= 360.0;
	if(a[2] < 0.0) 		a[2] += 360.0;
	if(a[2] > 360.0)	a[2] -= 360.0;

	X = a[0];
	Y = a[1];
	Z = a[2];
}
Note: It's currently untested, so I may have made a mistake or two.
Another note: {tX, tY, tZ} = the point to be projected, {cX, cY, cZ} = the camera's position, {aX, aY, aZ} = camera's angles, d is the variable to store the result in.

Now on to my question: What is this "eye" (eX, eY, and eZ in the code) that is necessary to find the coordinates? I've looked all over and cannot seem to find the answer.

Thanks,
Tannz0rz
Tannz0rz is offline  
 
May 16th, 2010, 10:48 PM   #2
Newbie
 
Joined: Apr 2009

Posts: 19
Thanks: 0

Re: 3D Perspective Projection

It describes the "screen" you are projecting onto. When projecting, you trace a line from the point to project to the camera "origin", and compute the intersection with the "screen". eZ is the distance of the screen to the origin, eX and eZ describe its size.
hkoehler is offline  
Reply

  My Math Forum > Science Forums > Computer Science

Tags
perspective, projection



Search tags for this page
Click on a term to search for related topics.
Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Geometry: perspective projection xzardaz Geometry 1 October 9th, 2012 09:36 AM
A question about perspective Galux Algebra 1 September 23rd, 2012 10:25 PM
Perspective field for Mathematician Joey1987 New Users 2 July 30th, 2010 01:22 AM
Cubes perspective problem ricardoainati Algebra 6 May 14th, 2010 06:01 AM
Perspective Triangles dr_jekyll Algebra 7 December 17th, 2009 12:43 PM





Copyright © 2019 My Math Forum. All rights reserved.