Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / 3d Co-ordinate Select - matrix4 and vector3 usage question

Author
Message
Merk
18
Years of Service
User Offline
Joined: 5th Feb 2006
Location:
Posted: 9th May 2010 10:01
Hi all,

Recently I've been trying to get acquainted with the matrix4 and vector commands for transforming co-ordinates between the screen and the 3d world. So far as getting 3d positions to 2d sceen co-ordinates it seems like a relatively simple task. For anyone interested in that you can see the code below:



What I'd like to do now is work the other way. That is, get a 2d screen co-ordinate (for example, mouse position) and project that into the 3d world space. I assume I'll need to use the transform vector command, however I'm unsure as to what the matrix that command requires should be? I haven't been able to find much information with regards to what the world, view and projection matrices actually hold. For just selecting objects in a scene with the mouse I know you can use the pick object command, but I'm more looking to select specific co-ordinates in 3d space with the mouse.

Any help with this would be much appreciated.
owlman
15
Years of Service
User Offline
Joined: 8th Apr 2009
Location: Italy
Posted: 9th May 2010 12:30
I think one problem might be that 2d world space has no z dimension and so you would need a way to supply z coords beyond what you can expect to obtain from the mouse position.
Merk
18
Years of Service
User Offline
Joined: 5th Feb 2006
Location:
Posted: 9th May 2010 13:33
Thanks for the reply owlman. Yes I'm aware of the lack of z for the screen co-ordinates, there should be a way however of projecting a straight line vector. Assume that I have a pre-determined z depth or distance in mind (eg: a vector from the screen to a co-ordinate on a flate plain, or even a terrain, to set a waypoint or marker). Hopefully that makes it more clear.
Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 11th May 2010 03:32 Edited at: 11th May 2010 03:58
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
.
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:


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:


And to rotate a vector containing (0,0,1), you'd do the transformation:


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 (
) 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.

Merk
18
Years of Service
User Offline
Joined: 5th Feb 2006
Location:
Posted: 11th May 2010 04:15
That's perfect! Many thanks for the help, it's much appreciated.

Login to post a reply

Server time is: 2024-04-27 02:36:21
Your offset time is: 2024-04-27 02:36:21