Be a Supporter!

As : Closest Proximity (ai)

  • 2,514 Views
  • 16 Replies
New Topic Respond to this Topic
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
As : Closest Proximity (ai) May. 19th, 2006 @ 03:03 PM Reply

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:

onClipEvent (load) {
distance=0
searchRadius =0
xDist=0
yDist=0
}
onClipEvent (enterFrame) {
searchRadius = 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< searchRadius) {
searchRadius = distance;
trace(i)
}
}
}

Working File

Now for the breakdown.

onClipEvent (load) {
totalDist = 0;
distance=0
xDist=0
yDist=0
}

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

searchRadius = 1000;

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) {
searchRadius = distance;
}

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:

onClipEvent (load) {
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


BBS Signature
Chaz
Chaz
  • Member since: Sep. 27, 2005
  • Offline.
Forum Stats
Member
Level 23
Game Developer
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 03:04 PM Reply

Wow, nice tutorial. Could make for some pretty unique games.


BBS Signature
RyanPridgeon
RyanPridgeon
  • Member since: Dec. 7, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 03:07 PM Reply

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


I make flashes because I can.
PM me for anything flash or web related or visit my blog!!

BBS Signature
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 03:30 PM Reply

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.


BBS Signature
reelbigcheese
reelbigcheese
  • Member since: Aug. 12, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 03:33 PM Reply

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

TrueDarkness
TrueDarkness
  • Member since: Aug. 31, 2004
  • Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 04:52 PM Reply

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!

Blaze
Blaze
  • Member since: Aug. 4, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 05:02 PM Reply

WOW.

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

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 05:02 PM Reply

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.


BBS Signature
fwe
fwe
  • Member since: Jul. 24, 2003
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 05:03 PM Reply

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

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 05:08 PM Reply

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.


BBS Signature
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 05:10 PM Reply

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.


BBS Signature
Pyromaniac
Pyromaniac
  • Member since: Jan. 14, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 06:39 PM Reply

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

23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 06:52 PM Reply

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.


BBS Signature
NegativeONE
NegativeONE
  • Member since: Feb. 15, 2003
  • Offline.
Forum Stats
Member
Level 49
Game Developer
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 06:57 PM Reply

Neato. I just recently wrote a similar algorithm to find the most secluded spawn point for a respawning fighter. tip top.


BBS Signature
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 07:10 PM Reply

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???


BBS Signature
NegativeONE
NegativeONE
  • Member since: Feb. 15, 2003
  • Offline.
Forum Stats
Member
Level 49
Game Developer
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 07:15 PM Reply

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 ;)


BBS Signature
Trunks
Trunks
  • Member since: Jul. 31, 2005
  • Offline.
Forum Stats
Member
Level 22
Musician
Response to As : Closest Proximity (ai) May. 19th, 2006 @ 07:18 PM Reply

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!


Check out my band by clicking my sig!

BBS Signature