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

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

Previous topic - Next topic

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,2
SeedRnd MilliSecs()
Global Camera = CreateCamera()
Depth1 = -10 ;Depth1 is the distance of the Camera to 0,0,0
PositionEntity Camera,0,0,Depth1
AmbientLight 255,255,255

tex = CreateTexture(128,128) ;Create Point Texture
SetBuffer TextureBuffer(tex)
Color 255,255,255
Line 0,126,126,126
Line 127,127,64,0
Line 64,0,0,127
Color 255,0,0
Line 64,0,64,127
SetBuffer BackBuffer()

Pivot = CreatePivot()
Point = CreateQuad(False,pivot) ;Create Point Quad
Depth2 = 5 ;Depth2 is the distance of the Point entity to 0,0,0
PositionEntity Point,5,0,Depth2
EntityTexture Point,tex

While 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"
Flip
Wend

;Returns the X location of the mouse at Z_Depth
;Note - Only tested on (800x600) resolution, But should work on all resolutions
Function 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 resolutions
Function 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 Plain
Function 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 container
End Function


Code :
Code (blitzbasic) Select
...

Comments :


_PJ_(Posted 1+ years ago)

Seems to work fine with other resolutions and aspect ratios.