TGC Codebase Backup



3D from 2D Mouse In Any Resolution by LeeBamber

20th Aug 2010 15:11
Summary

Get a true 3D coordinate from the 2D mouse position easily with this code.



Description

Using inverse projection and view matrices from the 3DMATH command set, this code shows you step by step how to calculate the true 3D coordinate of the 2D coordinate of your mouse pointer, no matter what resolution or camera orientation you are in. Useful for a variety of 3D interface situations, and especially true of new touch based approaches.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Project: 3Dfrom2DMouseAnyResolution
Rem Created: Friday, August 20, 2010
r=make matrix4(1)
r=make matrix4(2)
r=make matrix4(3)
r=make matrix4(4)
r=make vector3(11)
set display mode desktop width(),desktop height(),32,1
sync on
sync rate 0
make object cube 1,4
do
 set cursor 0,0
 print "Steps To Get 3D from 2D in any resolution"
 print
 print "1. projection matrix4 1 = put current projection matrix in slot 1"
 projection matrix4 1
 print "2. r=inverse matrix4(2,1) = invert the projection matrix held in slot 1, copy to slot 2"
 r=inverse matrix4(2,1)
 print "3. view matrix4 3 = put current view matrix in slot 3"
 view matrix4 3
 print "4. r=inverse matrix4(4,3) = invert the view matrix held in slot 3, copy to slot 4"
 r=inverse matrix4(4,3)
 print "5. r=inverse matrix4(4,3) = invert the view matrix held in slot 3, copy to slot 4"
 scrmx#=((mousex()+0.0)-(screen width()/2.0))/(screen width()+0.0)
 scrmy#=((screen height()/2.0)-(mousey()+0.0))/(screen height()+0.0)
 mousex#=mousex() : scrwidth#=screen width() : scrmx#=(mousex#/scrwidth#)*2-1
 mousey#=mousey() : scrheight#=screen height() : scrmy#=(mousey#/scrheight#)*-2+1
 print "6. Convert your mouse coordinates into screen space (i.e. -1,-1 to 1,1)"
 print "   mousex#=mousex() : scrwidth#=screen width() : scrmx#=(mousex#/scrwidth#)*2-1"
 print "   mousey#=mousey() : scrheight#=screen height() : scrmy#=(mousey#/scrheight#)*-2+1"
 print "   scrmx#=";scrmx#
 print "   scrmy#=";scrmy#
 set vector3 11,scrmx#,scrmy#,0.99
 print "7. set vector3 11,scrmx#,scrmy#,0.99 = Copy that screen space vector into a vector3"
 transform coords vector3 11,11,2
 print "8. transform coords vector3 11,11,2 = matrix multiply vector with invprojection matrix"
 transform coords vector3 11,11,4
 print "8. transform coords vector3 11,11,4 = matrix multiply new vector with invview matrix"
 print "   x vector3(11)=";x vector3(11)
 print "   y vector3(11)=";y vector3(11)
 print "   z vector3(11)=";z vector3(11)
 position object 1,x vector3(11),y vector3(11),z vector3(11)
 print "9. vector now stored is a usable 3D coordinate representing the mouse coordinate"
 sync
loop