SyntaxBomb - Indie Coders

Languages & Coding => Blitz Code Archives => 3D Graphics - Maths => Topic started by: BlitzBot on June 29, 2017, 12:28:43 AM

Title: [bb] Mouse XY to 3D XY by MusicianKool [ 1+ years ago ]
Post by: BlitzBot on June 29, 2017, 12:28:43 AM
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
  1.  
  2. Graphics3D 800,600,32,2
  3. SeedRnd MilliSecs()
  4. Global Camera = CreateCamera()
  5. Depth1 = -10    ;Depth1 is the distance of the Camera to 0,0,0
  6. PositionEntity Camera,0,0,Depth1
  7. AmbientLight 255,255,255
  8.  
  9. tex = CreateTexture(128,128) ;Create Point Texture
  10. SetBuffer TextureBuffer(tex)
  11. Color 255,255,255
  12. Line 0,126,126,126
  13. Line 127,127,64,0
  14. Line 64,0,0,127
  15. Color 255,0,0
  16. Line 64,0,64,127
  17. SetBuffer BackBuffer()
  18.  
  19. Pivot = CreatePivot()
  20. Point = CreateQuad(False,pivot) ;Create Point Quad
  21. Depth2 = 5              ;Depth2 is the distance of the Point entity to 0,0,0
  22. PositionEntity Point,5,0,Depth2
  23. EntityTexture Point,tex
  24.  
  25. While Not KeyHit(1)
  26.  
  27.         TurnEntity Pivot,0,0,1  ;turn the parent Pivot of Point
  28.        
  29.         y# = EntityY(Point,1)   ;Get the Y location of Point
  30.         x# = EntityX(Point,1)   ;Get the X location of Point
  31.        
  32.         If MouseHit(1) Then depth2 = depth2 - 1
  33.         If MouseHit(2) Then depth2 = depth2 + 1
  34.         PositionEntity Point,5,0,depth2 ;Position Point closer or farther away
  35.        
  36.         ;Get the angle from Mouse position to point position
  37.         a# = 90+ -ATan2((MouseYTo3D#(Depth1-depth2)-y),(MouseXTo3D#(Depth1-Depth2))-x)
  38.        
  39.         ;Rotate Point
  40.         RotateEntity Point,0,0,-a,True
  41.        
  42.         RenderWorld
  43.         Color 255,255,255
  44.         Locate 0,0
  45.         Print "Move mouse around, the quad will always point to the mouse"
  46.         Print "LeftMouse button to bring quad closer to camera"
  47.         Print "RightMouse button to take quad farther from camera"
  48.         Flip
  49. Wend
  50.  
  51. ;Returns the X location of the mouse at Z_Depth
  52. ;Note - Only tested on (800x600) resolution, But should work on all resolutions
  53. Function MouseXTo3D#(Z_Depth#)
  54.         Return Float-(MouseX()- GraphicsWidth()/2)/GraphicsWidth()*(Z_Depth#* Float(GraphicsWidth()/ Float(GraphicsWidth()/2)))
  55. End Function
  56. ;Returns the Y location of the mouse at Z_Depth
  57. ;Note - Only tested on (800x600) resolution, But should work on all resolutions
  58. Function MouseYTo3D#(Z_Depth#)
  59.         Return Float(MouseY()- GraphicsHeight()/2)/GraphicsHeight()*(Z_Depth#* Float(GraphicsHeight()/ Float(GraphicsWidth()/2)))
  60. End Function
  61.  
  62.  
  63. ;Creates a Quad
  64. ;Axis = False   :Creates a Quad along the XY Plain
  65. ;Axis = True    :Creates a Quad along the XZ Plain
  66. Function CreateQuad%(Axis%=False,Parent%=0)
  67.         Local o%,v%,Width#=1,Height#=1,Depth#=0,Container,s
  68.         If Axis Then Height# = 0:Depth# = 1
  69.         container=CreateMesh(Parent)
  70.         s=CreateSurface(container)
  71.         v=AddVertex(s,- Width#,- Height#,- Depth# , 0.0,1.0)
  72.         AddVertex  (s,  Width#,- Height#,- Depth# , 1.0,1.0)
  73.         AddVertex  (s,- Width#,  Height#,  Depth# , 0.0,0.0)
  74.         AddVertex  (s,  Width#,  Height#,  Depth# , 1.0,0.0)
  75.         AddTriangle s,v+0,v+2,v+1
  76.         AddTriangle s,v+1,v+2,v+3
  77.         UpdateNormals container
  78.         Return container
  79. End Function
  80.  

Code :
Code: BlitzBasic
  1. ...

Comments :


_PJ_(Posted 1+ years ago)

 Seems to work fine with other resolutions and aspect ratios.

SimplePortal 2.3.6 © 2008-2014, SimplePortal