This may have been asked/answered before, but I couldn't find anything, so .....
If I want to implement Drag n Drop from a GUI button, how can I convert the button's position to a World Space position?
For example, this is what I would like to be able to do:
- Create a GUI with some buttons down the Left Hand Side. Each button has an image of an item that can be placed in the game.
If I then click on a button and hold the mouse button down, I can drag the actual item (model) to a specific place in the game world.
When I let go of the mouse button, the item will drop onto the nearest surface in the game world.
- Code: Select all
sky.screenToWorld ( mouseX, mouseY )
function (which, unfortunately, doesn't look like it's simple to code!).
As an example, I found this on StackOverflow http://stackoverflow.com/questions/31613832/converting-screen-2d-to-world-3d-coordinates:
- Code: Select all
mat = worldMatrix * inverse ( ProjectionMatrix )
dir = transpose ( mat ) * <x_screen, y_screen, 0.5, 1>
dir /= mat[3] + mat[7] + mat[11] + mat[15]
dir -= camera.position
The WorldMatrix would be calculated from the Camera as follows:
- Code: Select all
worldMatrix = Translate ( x, y, z ) * RotateZ ( z_angle ) * RotateY ( y_angle ) * RotateX ( x_angle );
The ProjectionMatrix is somewhat harder:
The projection matrix can be calculated from the aspect ratio, field of view angle, and near and far planes.
So, this is starting to look like it might be too hard to do in Lua and I have also come across posts that seem to suggest I also need a ModelViewMatrix??
Unfortunately, I can't use raycasts as I can't guarantee that there will be an object directly under the cursor, and my tests indicate that raycasts give a result of 0, 0, 0 if no object is found.
Any help would be greatly appreciated.
Thanks
Shando