Finding the closest

Blitz3D Forums/Blitz3D Programming/Finding the closest

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

You should have a record of all your bats, using a type will do:
(untested! :P)

Type tBall
 Field entity
End Type


Then you'd create your bat & balls:
bat = LoadMesh("myBat.x")
Local ball.tBall
For i = 1 to 3
 ball = New tBall
 ball\entity = CreateShpere()
Next


Now just call the function in your game, passing it the 'bat' entity
Function NearestBall(bat)

  Local ball.tBall
  Local nearestBall.tBall
  Local nearestDist#

  ;Assume first tBall instance is the nearest
  nearestBall = First tBall
  nearestDistance = EntityDistance(bat,nearestBall\entity)

  For ball = Each tBall
    If ball <> nearestBall
      dist = EntityDistance(bat,ball\entity)
      If dist < nearestDist
        nearestBall = ball
        nearestDist = dist
      End If
    End If
  Next

  Return nearestBall\entity
End Function