Finding the closest
Blitz3D Forums/Blitz3D Programming/Finding the closest
| ||
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....) |
| ||
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. |
| ||
what do you mean EntityDistance? |
| ||
It's the blitz3D command to find the distance between 2 entities. |
| ||
Or..Function Distance2D(x1#,y1#,x2#,y2#) Local dx#=x1-x2 Local dy#=y1-y2 Return Sqr(dx*dx + dy*dy) End Function |
| ||
I know what the command does, i just don't get how i can use it to accomplish what explained |
| ||
Use it to find the distance between the bat entity and the ball entity/entities? |
| ||
Can you post some code for how you would do it, because i seriously have no clue. |
| ||
targetball = null Distance = 100000 for ball = firstball to lastball if entitydistance(ball,bat)<Distance Distance = entitydistance(ball,bat) TargetBall = ball endif next |
| ||
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 |