 00:00
00:00 As : Closest Proximity (ai)

3,237 Views | 16 Replies

As : Closest Proximity (ai) 2006-05-19 15:03:50

AS: Main

FlashTroops

In this tutorial, I will explain a code that will allow your to calculate which movieclip happens to be the closest in descending named order (ball1, ball2, ball3, etc…) either duplicated or otherwise. This can extremely useful for AI purposes in a variety of games. I will not be introducing any new concepts that have not already been in tutorial in AS: Main, so I will link you to tut’s needed to understand the code and only explain things already covered lightly.

Full Code:

distance=0
xDist=0
yDist=0
}
onClipEvent (enterFrame) {
for (i=0; i<10; i++) {
xDist = _x-_root["ball"+i]._x;
yDist = _y-_root["ball"+i]._y;
distance = Math.sqrt(Math.pow(xDist, 2)+Math.pow(yDist, 2));
if (distance< searchRadius) {
trace(i)
}
}
}

Working File

Now for the breakdown.

totalDist = 0;
distance=0
xDist=0
yDist=0
}

This is just setting up our variables. I don’t think any explaining is needed there.

This is exactly what it sounds like. It is your search radius. I will explain its further importance a little later.

for (i=0; i<10; i++) {

This is our loop that we will be using for checking the movie clips. I will pink to a tut explaining how they work in the end.

xDist = _x-_root["ball"+i]._x;
yDist = _y-_root["ball"+i]._y;

These distances are what we will be using with the distance formula to find out exactly how far each movieclip is away from your main MC.

distance = Math.sqrt(Math.pow(xDist, 2)+Math.pow(yDist, 2));

This what calculates the distance of each MC. Some of you may recognize if from a math class, it is the basic distance formula used in all math.

Math.sqrt() = Square Root.
Math.pow() = To the power of.

The 2 I have in the Math.pow() function is the power, whether it be squared (2), or cubed (3), and any higher.

if (distance< searchRadius) {
}

Our search radius is, at the moment, 1000. What this does is checks if the total distance of the movie clip is less then the current radius. If it is, then it must be closer. I then set the radius to how close that paticular movie clip may be. Now for the logic behind all of this.

The first radius is 1000. The loop checks to see if ball1’s distance is less then that radius. Ball1 will always be under the initial radius. Now that it is under the radius, we check to see if ball2 is under that radius. Now remember, that radius is no longer 1000, it is however close ball1 is to you. So, if ball2 happens to be closer then ball1, then the radius is changed back to the distance of ball2. If it is not closer, then the radius remains what it was (distance of ball1). This cycle goes through until the closest ball is found.

The one flaw with this code is that it traces more then just the closest ball. It will first trace ball1, and if ball2 is closer, then ball2 is traced, and if ball2 happens to be the closest of all of the MC’s, then ball2 is traced for the rest of the script. But ball1 is traced for the one split second, which may cause problems in some scripts. It works fine for my working file, as we moves toward the MC, which is the purpose of many AI, which I is why I still find this script useful.

Hopefully someone understood this and hopefully someone finds it useful. I for one will be using it in my next small project, which probably wont get done like all my other games, but I will get the engine done at least. Here is the script I used on my working file:

xSpeed = 0;
ySpeed = 0;
playerDist = 0;
speed = 5;
}
onClipEvent (enterFrame) {
_x -= xSpeed;
_y -= ySpeed;
playerDist = 1000;
for (i=0; i<10; i++) {
xDist = _x-_root["ball"+i]._x;
yDist = _y-_root["ball"+i]._y;
distance = Math.sqrt(Math.pow(xDist, 2)+Math.pow(yDist, 2));
if (distance<playerDist) {
_root["ball"+i].thing = true;
playerDist = distance;
xDist = _x-_root["ball"+i]._x;
yDist = _y-_root["ball"+i]._y;
angle = Math.atan2(yDist, xDist)/(Math.PI/180);
xSpeed = Math.cos(angle*(Math.PI/180))*speed;
ySpeed = Math.sin(angle*(Math.PI/180))*speed;
if (this.hitTest(_root["ball"+i])) {
xSpeed = 0;
ySpeed = 0;
_root["ball"+i].swapDepths(9999);
_root["ball"+i].removeMovieClip();
}
}
}
}

So add my code to a movie clip, then make other movie clips with the name ball1, ball2, ball3, and so on. If you want more then 10 balls, change the 10 in the loop to a higher number. Here are some other tuts that might help explaining other parts of my code:

Loops and Conditions by BleeBlap

AI by Vengence

Collision with duplicated MC's by SpamBurger

Response to As : Closest Proximity (ai) 2006-05-19 15:07:59

yeah, i was looking for a code that did this. Thanks.

Response to As : Closest Proximity (ai) 2006-05-19 15:30:34

At 5/19/06 03:04 PM, -Chaz- wrote: Wow, nice tutorial. Could make for some pretty unique games.

Thanks. Thats really what im hoping for. It can be useful for many different kinds of games.

At 5/19/06 03:07 PM, RyanPridgeon wrote: yeah, i was looking for a code that did this. Thanks.

I aim to please.

Response to As : Closest Proximity (ai) 2006-05-19 15:33:23

ooooooooooo nice tutorial :) ill have a proper read over it now.

Response to As : Closest Proximity (ai) 2006-05-19 16:52:40

Great tutorial! This will be great for puzzle games. For instance, maybe you can set a path and it would follow the path that you said depending on where the points are =) That would be interesting.

Good job!

Response to As : Closest Proximity (ai) 2006-05-19 17:02:00

WOW.

Friggin great, i have no idea how this hadnt come to my mind before. =D great tutorial!

Response to As : Closest Proximity (ai) 2006-05-19 17:02:39

Ooh damn it, just today I finished a very similiar engine (does the same thing), and it took me quite a lot of time to fix all the bugs. Too bad you didn't post this before, good job. Response to As : Closest Proximity (ai) 2006-05-19 17:03:13

Nice, but you don't need to find the square root of it if you're just comparing them, i'll save processing power, little as may be.

wtfbbqhax

Response to As : Closest Proximity (ai) 2006-05-19 17:08:27

At 5/19/06 05:03 PM, fwe wrote: Nice, but you don't need to find the square root of it if you're just comparing them,

Good point, it's like having all the values / 5 if all of them are divided by 5 then there won't be any difference to compare them if you remove it. It can be useful to actually know what the distance is though. Response to As : Closest Proximity (ai) 2006-05-19 17:10:33

At 5/19/06 05:08 PM, -Toast- wrote:
At 5/19/06 05:03 PM, fwe wrote: Nice, but you don't need to find the square root of it if you're just comparing them,
Good point, it's like having all the values / 5 if all of them are divided by 5 then there won't be any difference to compare them if you remove it. It can be useful to actually know what the distance is though.

You guys also forgot that in order to control the radius at which you search at, you need to know it all in the correct pixels. So yah, there are many reason why you would need to know the distance in pixels, but ofcourse it is not completely needed.

Thanks for liking it guys. I will make some more sometime.

Response to As : Closest Proximity (ai) 2006-05-19 18:39:28

Nice code. Now edit to to make a pathfinding engin.

Response to As : Closest Proximity (ai) 2006-05-19 18:52:09

At 5/19/06 06:39 PM, pyro111 wrote: Nice code. Now edit to to make a pathfinding engin.

Im not really interested in that. I made this tut because i am making a game that uses the code. I might look into pathfinding here soon though. Dont have a clue how to do it at the moment though.

Response to As : Closest Proximity (ai) 2006-05-19 18:57:35

Neato. I just recently wrote a similar algorithm to find the most secluded spawn point for a respawning fighter. tip top. Response to As : Closest Proximity (ai) 2006-05-19 19:10:28

At 5/19/06 06:57 PM, NegativeONE wrote: Neato. I just recently wrote a similar algorithm to find the most secluded spawn point for a respawning fighter. tip top.

Got the thumbs up from neg. Must be a good tut. : )

And what would this new algorithm be for? Simple engine or upcomming project???

Response to As : Closest Proximity (ai) 2006-05-19 19:15:14

At 5/19/06 07:10 PM, 23450 wrote: And what would this new algorithm be for? Simple engine or upcomming project???

S'a big one. A brawler. More infos on my site ;) Response to As : Closest Proximity (ai) 2006-05-19 19:18:05

Wow, some pretty cool stuff you have there. This would make for a very innovative game. Well, it would have, had you not made this thread. Nice work!

wew