`*******************************************************************************
`* Selecting a 3d object with a mouseclick *
`* Code by: DMiTR0S dmitr0s@newmail.ru *
`*******************************************************************************
`*******************************************************************************
`* Free License Agreement. *
`* *
`* The user/developer agrees to include a visible credit to "Dmitri Kuschev" *
`* within your programs documentation and or web site.šš *
`* *
`* The user/developer agrees to NOT offer the this source code for Sale. *
`* *
`* Providing the user/Developer agrees in full to the previous Free *
`* License Agreement clauses, the user/developer may use this source *
`* code FREELY in all their products, commercial or otherwise.š *
`* *
`*******************************************************************************
ViewMatrix=1
ProjectionMatrix=2
Vector=3
r=Make Matrix4(ViewMatrix)
r=Make Matrix4(ProjectionMatrix)
r=Make Vector3(Vector)
Make Object Sphere 1, 10.0
For i=10 To 50
Make Object Cube i, 10.0
Position Object i, RND(200)-100, RND(100), RND(200)-100
Next i
Position Camera 0, 50, -200
Sync Rate 0
Sync On
Do
If Mouseclick()=2
cy#=cy#+MousemoveY()
Move Camera MousemoveX()
Position Camera Camera Position X(), cy#, Camera Position Z()
EndIf
2DTo3D(Vector, ViewMatrix, ProjectionMatrix, 1000)
Position Object 1, X Vector3(Vector), Y Vector3(Vector), Z Vector3(Vector)
If MouseClick()&&1
For check=10 To 50
Coll#=Intersect Object(check, Camera Position X(), Camera Position Y(), Camera Position Z(), X Vector3(Vector), Y Vector3(Vector), Z Vector3(Vector))
If Coll# > 0.0
Text 10, 30, "Hitting object "+Str$(check)
Text 10, 50, "Distance to object: "+Str$(Coll#)
Color Object check,RGB(255,0,0)
Else
Color Object check,RGB(255,255,255)
Endif
Next check
Endif
Text 10, 10, "X = "+Str$(X Vector3(Vector))+" Y = "+Str$(Y Vector3(Vector))+" Z = "+Str$(Z Vector3(Vector))
Sync
Loop
Function 2DTo3D(VectorResult, ViewMatrix, ProjectionMatrix, Range#)
X#=2.0*(Mousex()*1.0)/(Screen Width()*1.0)-1.0
Y#=1.0-2.0*(MouseY()*1.0)/(Screen Height()*1.0)
Set Vector3 VectorResult, X#, Y#, 1.0
View Matrix4 ViewMatrix
Projection Matrix4 ProjectionMatrix
r=Inverse Matrix4(ViewMatrix, ViewMatrix)
r=Inverse Matrix4(ProjectionMatrix, ProjectionMatrix)
Transform Coords Vector3 VectorResult, VectorResult, ProjectionMatrix
Normalize Vector3 VectorResult, VectorResult
Multiply Vector3 VectorResult, Range#
Transform Coords Vector3 VectorResult, VectorResult, ViewMatrix
EndFunction