## [bb] Mouse XY to 3D XY by MusicianKool [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:43

#### BlitzBot

Title : Mouse XY to 3D XY
Author : MusicianKool
Posted : 1+ years ago

Description : Should be helpful Creating 2D like games in 3D were the mouse pointer is needed.
Code (blitzbasic) Select
`Graphics3D 800,600,32,2SeedRnd MilliSecs()Global Camera = CreateCamera()Depth1 = -10 ;Depth1 is the distance of the Camera to 0,0,0PositionEntity Camera,0,0,Depth1AmbientLight 255,255,255tex = CreateTexture(128,128) ;Create Point TextureSetBuffer TextureBuffer(tex)Color 255,255,255Line 0,126,126,126Line 127,127,64,0Line 64,0,0,127Color 255,0,0Line 64,0,64,127SetBuffer BackBuffer()Pivot = CreatePivot()Point = CreateQuad(False,pivot) ;Create Point QuadDepth2 = 5 ;Depth2 is the distance of the Point entity to 0,0,0PositionEntity Point,5,0,Depth2EntityTexture Point,texWhile Not KeyHit(1) TurnEntity Pivot,0,0,1 ;turn the parent Pivot of Point y# = EntityY(Point,1) ;Get the Y location of Point x# = EntityX(Point,1) ;Get the X location of Point If MouseHit(1) Then depth2 = depth2 - 1 If MouseHit(2) Then depth2 = depth2 + 1 PositionEntity Point,5,0,depth2 ;Position Point closer or farther away ;Get the angle from Mouse position to point position a# = 90+ -ATan2((MouseYTo3D#(Depth1-depth2)-y),(MouseXTo3D#(Depth1-Depth2))-x) ;Rotate Point RotateEntity Point,0,0,-a,True RenderWorld Color 255,255,255 Locate 0,0 Print "Move mouse around, the quad will always point to the mouse" Print "LeftMouse button to bring quad closer to camera" Print "RightMouse button to take quad farther from camera" FlipWend;Returns the X location of the mouse at Z_Depth;Note - Only tested on (800x600) resolution, But should work on all resolutionsFunction MouseXTo3D#(Z_Depth#) Return Float-(MouseX()- GraphicsWidth()/2)/GraphicsWidth()*(Z_Depth#* Float(GraphicsWidth()/ Float(GraphicsWidth()/2)))End Function;Returns the Y location of the mouse at Z_Depth;Note - Only tested on (800x600) resolution, But should work on all resolutionsFunction MouseYTo3D#(Z_Depth#) Return Float(MouseY()- GraphicsHeight()/2)/GraphicsHeight()*(Z_Depth#* Float(GraphicsHeight()/ Float(GraphicsWidth()/2)))End Function;Creates a Quad;Axis = False :Creates a Quad along the XY Plain;Axis = True :Creates a Quad along the XZ PlainFunction CreateQuad%(Axis%=False,Parent%=0) Local o%,v%,Width#=1,Height#=1,Depth#=0,Container,s If Axis Then Height# = 0:Depth# = 1 container=CreateMesh(Parent) s=CreateSurface(container) v=AddVertex(s,- Width#,- Height#,- Depth# , 0.0,1.0) AddVertex  (s,  Width#,- Height#,- Depth# , 1.0,1.0) AddVertex  (s,- Width#,  Height#,  Depth# , 0.0,0.0) AddVertex  (s,  Width#,  Height#,  Depth# , 1.0,0.0) AddTriangle s,v+0,v+2,v+1 AddTriangle s,v+1,v+2,v+3 UpdateNormals container Return containerEnd Function`

Code :
Code (blitzbasic) Select
`...`