The other day I wrote a script which takes the mouse coordinates (and among other things, the parameters for the 3D projection) and converts it into a 3D vector representing the direction the mouse is pointing in 3D space. With this vector, it is a trivial matter to detect if the mouse is pointing at an object or to project the mouse cursor onto a plane. I'm still tidying up the example of it, but I thought I'd go ahead and release the script now.

{ var mm,dX,dY,dZ,uX,uY,uZ,vX,vY,vZ,mX,mY,mZ, width, height, tFOV; dX = argument3-argument0; dY = argument4-argument1; dZ = argument5-argument2; mm = sqrt(dX*dX+dY*dY+dZ*dZ); dX /= mm; dY /= mm; dZ /= mm; uX = argument6; uY = argument7; uZ = argument8; mm = uX*dX+uY*dY+uZ*dZ; uX -= mm*dX; uY -= mm*dY; uZ -= mm*dZ mm = sqrt(uX*uX+uY*uY+uZ*uZ); uX /= mm; uY /= mm; uZ /= mm; // v = u x d vX = uY*dZ-dY*uZ; vY = uZ*dX-dZ*uX; vZ = uX*dY-dX*uY; tFOV = tan(argument9*pi/360); uX *= tFOV; uY *= tFOV; uZ *= tFOV; vX *= tFOV*argument10; vY *= tFOV*argument10; vZ *= tFOV*argument10; width = window_get_width(); height = window_get_height(); mX = dX+uX*(1-2*mouse_y/height)+vX*(2*mouse_x/width-1); mY = dY+uY*(1-2*mouse_y/height)+vY*(2*mouse_x/width-1); mZ = dZ+uZ*(1-2*mouse_y/height)+vZ*(2*mouse_x/width-1); mm = sqrt(mX*mX+mY*mY+mZ*mZ); global.mouse_dx = mX/mm; global.mouse_dy = mY/mm; global.mouse_dz = mZ/mm; }

The arguments to the script are the same as d3d_set_projection_ext() (except for the last two). After the script executes, the components of the vector are stored in <global.mouse_dx, global.mouse_dy, global.mouse_dz>. This vector has its base at the camera's position. As I said earlier, I'm tidying up an example of its use and I'll upload that in the next few days. I may also write a tutorial on the use of vectors.