Skip to main content
GameDev.net gamedev.net
🔒 Locked

2D To 3D

Started by PsyVision Apr 28, 2001 at 1:29 PM 3 replies 450+ views
Original Post
PsyVision
PsyVision
hi, if i have my mouse position at a point on my window (nx,ny) then how can i use open gl or maths to project this point from 2D into 3D then to test if it hits a plane. I mainly need the projection part ????
PsYvIsIoN
Null and Void
Null and Void
You can use the selection buffer if you want. I haven''t messed around with it much, but I''m fairly sure it can do exactly what you''re looking for without the need for projecting the 2D point.

Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/
zedzeek
zedzeek
note that as planes are infinite unless the plane is parallel to the eye->point vector the eye->point->vector will always intersect the plane.

u prolly want to use someit like gluUnProject though
http://www.frii.com/~martz/oglfaq/ section 4

http://members.xoom.com/myBollux
Poya
Poya
Ok here we go

I''m assuming that you have a camera and the camera has a Matrix which describes its rotation and position. From this matrix you can get three vectors. Up, In and Left. the Up vector points up from camera. In vector points into world (ie in the direction you are looking at) and Left vector points left.

lets assume your matrix looks like this:

Matrix[0][0]=1;Matrix[0][1]=0;Matrix[0][2]=0;Matrix[0][3]=0;
Matrix[1][0]=0;Matrix[1][1]=1;Matrix[1][2]=0;Matrix[1][3]=0;
Matrix[2][0]=0;Matrix[2][1]=0;Matrix[2][2]=1;Matrix[2][3]=0;
Matrix[3][0]=0;Matrix[3][1]=0;Matrix[3][2]=0;Matrix[3][3]=1;

then Left is:
Up.x = -Matrix[0][0];
Up.y = -Matrix[0][1];
Up.z = -Matrix[0][2];

Up is:
Up.x = Matrix[1][0];
Up.y = Matrix[1][1];
Up.z = Matrix[1][2];

In is:
Up.x = Matrix[2][0];
Up.y = Matrix[2][1];
Up.z = Matrix[2][2];

Now notice this is for Direct3D. For OGL the matrix gets flipped to its side. You gotta figure it out yourself


Anyway. what you want to do is, get these 3 vectors. Then scale the left vector by the the x distance of mouse from the center of screen, scale the up vector with y distance of mouse from the center of screen, and finally scale the In vector by some number which will work for you (it should actually be a function of FOV)

Then add these three vectors, and if you got everything right this vector should be pointing into the world and in the direction of the mouse. All you need to do now is do collision detection between the camera and the end of this vector (you can scale it really long if you think objects are gonna be really far away from the camera)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.