I've written a little example you might find helpful. Assuming that you have a View space Z coordinate in mind, the process for getting World coordinates from Screen coordinates is pretty much the same as the other way around, but in reverse order, using inverse matrices.
1. Obtain the Screen space coordinate you want to transform with
X=(Pixel Coordinate X-(Screen Width/2))/(Screen Width/2)
Y=((Screen Height/2)-Pixel Coordinate Y)/(Screen Height/2)
Z=0
.
2. Apply a reverse projection using the inverse projection matrix. Afterwards, normalize the vector by dividing it by its W value to complete the transformation from Screen space into View space. At this point, the Z coordinate contains the position of the near clipping plane (default for DBPro is 1.0).
3. Multiply the X,Y, and Z components of the vector by the desired Z coordinate (ZDepth) divided by the position of the near plane (the current Z value of the vector). This will give you View space coordinates that are on the plane facing the camera, that is ZDepth units away from the camera, down its look axis.
4. Apply the inverse view matrix to transform the View space coordinates into World space.
Here's the example:
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
WASD moves/turns the camera and the scroll wheel changes the Z depth of the cube.
I hope I understood the problem
Edit: After rereading your post, I thought maybe I could help explain what the world, view, and projection matrices do. They are all 4x4 transform matrices, or matrices that transform a vector from one space to another. What that really means is that, when multiplied by a vector, they yield a different vector. For example, this matrix will rotate a vector 27 degrees around the Y axis:
[cos(27) 0 sin(27) 0] [0.891007 0.0 0.45399 0.0]
Matrix = [0 1 0 0] = [0.0 1.0 0.0 0.0]
[-sin(27) 0 cos(27) 0] [-0.45399 0.0 0.891007 0.0]
[0 0 0 1] [0.0 0.0 0.0 1.0]
And to rotate a vector containing (0,0,1), you'd do the transformation:
[0] [0.45399 ]
Matrix * [0] = [0.0 ]
[1] [0.891007]
[1] [1.0 ]
The position of a point (0,0,1) after being rotated 27 degrees around the Y axis is (0.45399,0.0,0.891007).
In DBPro,
world matrix4 will always return the identity matrix (
[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]
) because the identity matrix does not change vectors at all, and the coordinates you gave
project vector3 were already in world space. However, an object (that can be rotated and positioned anywhere in the world) has its own world matrix that transforms its vertices from Object space (centered around the objects origin) to World space (centered around the World's origin). The object's world matrix would be composed of five different transform matrices multiplied together, something like this:
ObjectMatrix= Translation*Scale*ZRotation*YRotation*XRotation
All of those matrices together create a perfect description of how to position the object's vertices in the world.
Anyhoo, that's a crash course in transform matrices. Feel free to ask anything.