Ok, I did a bit of work creating a object selection program which looks for a mouse release event at the closest object under the mouse. Pick Object does all the main work; all I have done is created a global variable for you to check which object the player has selected.
This can be used in your program by calling the
HandleObjectMouseSelection function in your loop.
`` Mouse select info
#CONSTANT MOUSE_SELECT_BUTTON 1
#CONSTANT MOUSE_UP = 1
#CONSTANT MOUSE_DOWN = 2
` Make the backdrop dark
Color Backdrop 0x000000
` Make some objects
For I = 1 to 50
If Rnd(1)
Make Object Cube I, 2 + ( Rnd(2) * 5 )
Else
Make Object Sphere I, 3 + ( Rnd(2) * 4 )
Endif
Move Object I, Rnd(100)-50
Move Object Right I, Rnd(100)-50
Color Object I, Rgb( 55 + Rnd(200), 55 + Rnd(200), 55 + Rnd(200))
Next I
` Selected Object
Global SelectedObject = 0
` Global Mouse Select State
Global MouseSelectState = 0
` Setup scene
Make Matrix 1, 150, 150, 25, 25
Position Matrix 1, -75, -10, -75
Position Camera 0, 25, -40
Point Camera 0, 0, 0
Sync On
Sync Rate 60
` Run the loop
Do
` Display information
Text 0, 0, "Selected Object: " + Str$(SelectedObject) + " | FPS: " + Str$( Screen Fps() )
` Handle mouse click selection when the button is released. Deal with objects 1 to 50
HandleObjectMouseSelection( 1, 50 )
` Allow the user to move around
Control Camera Using Arrowkeys 0, 1, 1
` Update
Sync
Loop
Function HandleObjectMouseSelection( iFirstObject, iLastObject )
` Check for object selection if the correct mouse button has been released
If MouseSelectState = MOUSE_UP
MouseSelectState = 0
iCheckObject = Pick Object( MouseX(), MouseY(), iFirstObject, iLastObject )
If SelectedObject > 0 Then Set Object Wireframe SelectedObject, 0
SelectedObject = iCheckObject
If SelectedObject > 0 Then Set Object Wireframe SelectedObject, 1
Else
` Check if the user is pressing down on the selection button
If MouseClick() && MOUSE_SELECT_BUTTON
` Store the mouse-down event
MouseSelectState = MOUSE_DOWN
Else
` Store a mouse-up event for one loop only
If MouseSelectState = MOUSE_DOWN Then MouseSelectState = MOUSE_UP
Endif
EndIf
EndFunction
