Try it.
NOTE: Works only with Patch 3.1
`*******************************************************************************
`* Created: 16/02/2003 *
`* By: DMiTR0S dmitr0s@newmail.ru *
`*******************************************************************************
`*******************************************************************************
`* Free License Agreement. *
`* *
`* The user/developer agrees to include a visible credit to "Dmitry 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. *
`* *
`*******************************************************************************
`Declare variables
Dim ArrayOfRayColliders()
Global MatView As Integer
Global Ray As Integer
Global ObjectsUnderCursor As Integer
Global Vector As Integer
Global LineEnd As Integer
Global Distance# As Float
Global RayIntersectionPointX# As Float
Global RayIntersectionPointY# As Float
Global RayIntersectionPointZ# As Float
Vector=1
LineEnd=2
MatView=3
Ray=59999
LastSelectedObject=Ray
r=Make Matrix4(MatView)
r=Make Vector3(Vector)
r=Make Vector3(LineEnd)
`Setup environment
Set Display Mode 1024,768,16
Sync Rate 0
Sync On
Set Text Size 16
Set Text To Bold
`Make a ray
Make Object Box Ray, 1.0, 1.0, 10000.0
Position Object Ray, 0.0, 0.0, 0.0
Offset Limb Ray, 0, 0.0, 0.0, 5000.0
Hide Object Ray
`Create elements
For i=1 To 50
d=RND(10)
Make Object Sphere i, d
Position Object i, RND(200)-100, RND(100), RND(200)-100
Color Object i, RGB(0,255,0)
Next i
Position Camera 0.0, 70.0, -200.0
Hide Mouse
`Main loop
Do
Control Camera Using ArrowKeys 0, 20.0, 5.0
cay#=cay#+(Mousemovex()/2)
cax#=cax#+(Mousemovey()/2)
If cax#<-90.0 Then cax#=-90.0
If cax#>90.0 Then cax#=90.0
ax#=Curveangle(cax#,ax#,2)
ay#=Curveangle(cay#,ay#,2)
Rotate Camera Wrapvalue(ax#*-1),Wrapvalue(ay#),Wrapvalue(caz)
If Mouseclick()=1
SelectedObject=GetObjectUnderCursor()
If SelectedObject>0
Color Object LastSelectedObject, RGB(0,255,0)
Color Object SelectedObject, RGB(255,0,128)
LastSelectedObject=SelectedObject
Else
If LastSelectedObject>0 Then Color Object LastSelectedObject, RGB(0,255,0)
EndIf
EndIf
Ink RGB(255,255,0),RGB(0,0,0)
Set Cursor 10, 10: Print "Code by: ";: Ink RGB(255,0,0),RGB(0,0,0): Print "Dmitry Kuschev"
Ink RGB(255,255,0),RGB(0,0,0)
Text 10, 30, "E-mail: dmitr0s@newmail.ru"
Text 10, 50, "FPS "+Str$(Screen FPS())
Text 10, 70, "Object "+Str$(SelectedObject)
Ink RGB(128,255,128),RGB(0,0,0)
If SelectedObject Then Text 10, 90, "Object X="+Str$(Object Position X(SelectedObject))+" Y="+Str$(Object Position Y(SelectedObject))+" Z="+Str$(Object Position Z(SelectedObject))
Ink RGB(255,255,0),RGB(0,0,0)
Text 10, 110, "Amount of objects under cursor "+Str$(ObjectsCountUnderCursor())
Text 10, 130, "Distance to object "+Str$(GetRayDistanceToObject())
Text 10, 150, "Intersection point:"
Text 10, 170, "X = "+Str$(GetRayIntersectionPointX())
Text 10, 190, "Y = "+Str$(GetRayIntersectionPointY())
Text 10, 210, "Z = "+Str$(GetRayIntersectionPointZ())
cX#=Screen Width()/2
cY#=Screen Height()/2
cRadius#=10.0
Circle cX#, cY#, cRadius#
Ink RGB(255,0,0),RGB(0,0,0)
Dot cX#, cY#
Sync
Loop
End
Function GetObjectUnderCursor()
ClosestObject=0
ClosestDistance#=10000.0
OFnd=0
ObjectsUnderCursor=0
View Matrix4 MatView
r#=Inverse Matrix4(MatView, MatView)
Set Vector3 Vector, 0.0, 0.0, 10000.0
Transform Coords Vector3 LineEnd, Vector, MatView
Position Object Ray, Camera Position X(), Camera Position Y(), Camera Position Z()
Point Object Ray, X Vector3(LineEnd), Y Vector3(LineEnd), Z Vector3(LineEnd)
Set Object Collision On Ray
Repeat
Obj=Object Collision(Ray, 0)
If Obj>0
Distance#=Intersect Object(Obj, Camera Position X(), Camera Position Y(), Camera Position Z(), X Vector3(LineEnd), Y Vector3(LineEnd), Z Vector3(LineEnd))
If Distance#>0.0
Inc ObjectsUnderCursor
If Distance#<ClosestDistance#
ClosestDistance#=Distance#
ClosestObject=Obj
EndIf
EndIf
Set Object Collision Off Obj
Array Insert At Bottom ArrayOfRayColliders()
ArrayOfRayColliders(Array Count(ArrayOfRayColliders()))=Obj
Else
OFnd=1
EndIf
Until OFnd
Set Object Collision Off Ray
For i=1 To Array Count(ArrayOfRayColliders())
Set Object Collision On ArrayOfRayColliders(i)
Next i
Empty Array ArrayOfRayColliders()
Array Insert At Bottom ArrayOfRayColliders()
Set Vector3 Vector, ClosestDistance#*CameraspaceX#, ClosestDistance#*CameraspaceY#, ClosestDistance#*CameraspaceZ#
Transform Coords Vector3 LineEnd, Vector, MatView
If ClosestObject>0
RayIntersectionPointX#=X Vector3(LineEnd)
RayIntersectionPointY#=Y Vector3(LineEnd)
RayIntersectionPointZ#=Z Vector3(LineEnd)
Distance#=ClosestDistance#
Else
RayIntersectionPointX#=0.0
RayIntersectionPointY#=0.0
RayIntersectionPointZ#=0.0
Distance#=0.0
EndIf
EndFunction ClosestObject
Function GetRayIntersectionPointX()
EndFunction RayIntersectionPointX#
Function GetRayIntersectionPointY()
EndFunction RayIntersectionPointY#
Function GetRayIntersectionPointZ()
EndFunction RayIntersectionPointZ#
Function GetRayDistanceToObject()
EndFunction Distance#
Function ObjectsCountUnderCursor()
EndFunction ObjectsUnderCursor