Rem *******************************************
Rem Object Selection In 3D Space With 2D Mouse
Rem By TDK_Man - November 2002
Rem Method 1 - Simple Version For Primitives
Rem *******************************************

Rem This method is a very basic method which does not use maths to calculate
Rem the object clicked on. Instead, it changes the colour of each object in turn
Rem until it detects that the object under the mouse has changed colour.

Rem The routines assume that the objects are white, so you need to store the colour
Rem of your objects in your program so you can restore them correctly. (Line 47)

Rem It is not the most refined method of doing this, but it is very simple and
Rem with a little modification can be used in many situations.

Rem Note: Can be a little slow with large numbers of objects and occasionally you
Rem may need to select an object twice.

Gosub Landscape

Do
  Mx=MouseX(): My=MouseY(): MClick=MouseClick()
  If MClick=1 Then Gosub Find
  Sync
  Ink RGB(255,255,255),0
  T$="X: "+Str$(Mx)+"  Y: "+Str$(My)+"  "+"  Object Selected: "+Str$(ObjSelected)+"          "
  Text 0,580,T$
  Sync
Loop

Find:
  StartCol=Point(Mx,My)
  For N=1 To NumObjects
    Color Object N,RGB(255,255,255)
  Next N
  For N=1 To NumObjects
    Color Object N,RGB(254,254,254)
    NowCol=Point(Mx,My)
    If StartCol<>NowCol
      ObjSelected=N
      Gosub Bounce
      N=51
    Else
      Rem If your objects are not white, you need to use the varaible used to store
      Rem the object's current colour here
      Color Object N,RGB(255,255,255)
    Endif
  Next N
  Sync
Return

Bounce:
  For N2=0 To 200 Step 10
    Position Object ObjSelected, Object Position X(ObjSelected) ,N2,Object Position Z(ObjSelected)
    Sync
  Next N2
  For N2=200 To 0 Step -10
    Position Object ObjSelected, Object Position X(ObjSelected) ,N2,Object Position Z(ObjSelected)
    Sync
  Next N2
Return

Landscape:
  Set Display Mode 800,600,16
  Sync On
  Sync Rate 0
  Set Camera View 0,0,800,580
  Set Camera Range 1,10000
  Set Text Opaque
  Randomize Timer()
  AutoCam Off
  Make Matrix 1,5000,5000,50,50
  CREATE BITMAP 1, 128, 128
  CLS RGB(0,100,0)
  For N=1 To 500
    Ink RGB(0,Rnd(200)+55,0),0
    Dot Rnd(128),Rnd(128)
  Next N
  Get Image 1,0,0,128,128
  PREPARE MATRIX TEXTURE 1,1,1,1
  Delete Bitmap 1
  Position Camera 2500,1000,-1500
  Point Camera 2500,0,2500
  NumObjects=30
  For N=1 to NumObjects
    ObType=Rnd(2)
    If ObType=0 Then Make Object Sphere N,300
    If ObType=1 Then Make Object Cube N,300
    If ObType=2 Then Make Object Cylinder N,300
    Position Object N,RND(4500)+250,0,RND(4500)+250
  Next N
Return
