Finding the closest

Blitz3D Forums/Blitz3D Programming/Finding the closest

Jerome Squalor(Posted 2007) [#1]
ok I'm not completely sure how to explain tis buti'm gonna try...

I am making a 3D pong game.
I added powerups
One of the powerups creates a new ball
When there are to many balls the computer paddle can't figure out the right one to follow

I want it to follow the closest one to it.
How do i find the closest one of a bunch of types?(Or the biggest, fastest, slowest, farthest, etc....)


big10p(Posted 2007) [#2]
EntityDistance.

I wouldn't just base the AI on distance, though. You'll probably wan't to consider the direction the ball is travelling, and it's velocity.


Jerome Squalor(Posted 2007) [#3]
what do you mean EntityDistance?


big10p(Posted 2007) [#4]
It's the blitz3D command to find the distance between 2 entities.


Beaker(Posted 2007) [#5]
Or..
Function Distance2D(x1#,y1#,x2#,y2#)
Local dx#=x1-x2
Local dy#=y1-y2
Return Sqr(dx*dx + dy*dy)
End Function



Jerome Squalor(Posted 2007) [#6]
I know what the command does, i just don't get how i can use it to accomplish what explained


big10p(Posted 2007) [#7]
Use it to find the distance between the bat entity and the ball entity/entities?


Jerome Squalor(Posted 2007) [#8]
Can you post some code for how you would do it, because i seriously have no clue.


H&K(Posted 2007) [#9]
targetball = null
Distance = 100000
for ball = firstball to lastball
if entitydistance(ball,bat)<Distance
Distance = entitydistance(ball,bat)
TargetBall = ball
endif
next


Tom(Posted 2007) [#10]
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