I think I recently answered a similar question. Here's the example I posted for the other person:
set display mode 1024,768,32,1
sync on
autocam off
ZDepth as float : ZDepth=10
#constant WorldVector 1
#constant ViewMatrix 2
#constant ProjectionMatrix 3
a=make vector4(WorldVector)
a=make matrix4(ViewMatrix)
a=make matrix4(ProjectionMatrix)
make matrix 1,100,100,10,10
set matrix wireframe on 1
position camera 0,50,4,50
make object cube 1,0.5
#constant CenterX (screen width()/2.0)
#constant CenterY (screen height()/2.0)
set camera range 0,0.01,3000.0
do
set cursor 0,0
`Control camera
move camera 0,0.5*(keystate(17)-keystate(31))
turn camera right 0,0.5*(keystate(32)-keystate(30))
`Set the cube's distance away from the camera
inc ZDepth,ZDepth*(mousemovez()/500.0)
print "Camera Info:"
print "World Position"
print "X: ";camera position x(0)
print "Y: ";camera position y(0)
print "Z: ";camera position z(0)
print
`Get the necessary transform matrices
view matrix4 ViewMatrix
projection matrix4 ProjectionMatrix
`Get their inverses
a=inverse matrix4(ViewMatrix,ViewMatrix)
a=inverse matrix4(ProjectionMatrix,ProjectionMatrix)
`Get the Screen space coordinate of the mouse pointer at the near clipping plane
set vector4 WorldVector,(mousex()-CenterX)/CenterX,(CenterY-mousey())/CenterY,0,1
`Transform into View space
transform vector4 WorldVector,WorldVector,ProjectionMatrix
`Complete the transform by normalizing the vector
divide vector4 WorldVector,w vector4(WorldVector)
`Move the object ZDepth units down the camera's look axis along the line from the camera to the mouse pointer.
`After normalization, the Z value of the vector contains the position of the near clipping plane
`I use set vector4 instead of multiply vector4 to avoid having to reset the w value of the vector, which must remain equal to 1
set vector4 WorldVector,x vector4(WorldVector)*(ZDepth/z vector4(WorldVector)),y vector4(WorldVector)*(ZDepth/z vector4(WorldVector)),ZDepth,1
print "Cube Info:"
print "View Position"
print "X: ";x vector4(WorldVector)
print "Y: ";y vector4(WorldVector)
print "Z: ";z vector4(WorldVector)
print
`Transform into World space
transform vector4 WorldVector,WorldVector,ViewMatrix
print "World Position"
print "X: ";x vector4(WorldVector)
print "Y: ";y vector4(WorldVector)
print "Z: ";z vector4(WorldVector)
position object 1,x vector4(WorldVector),y vector4(WorldVector),z vector4(WorldVector)
sync
loop
end
Copy+Paste that into a new window and run it to see if it's the kind of thing that you're looking for. WASD moves/turns the camera and the scroll wheel changes the distance.
There's quite a bit of complex math, and I haven't looked into simple alternatives, so there's probably something better for your needs out there. The person I gave this to was interested in the math aspect of it.
That thread is
here.