[Ai-steering programming] 10. Community Behavior

Source: Internet
Author: User
Tags flock

10. Community Behavior

Look at the geese, look at it, look at it, wait for a while in the "S" shape, wait for a while in the "B" shape, how cute !!


Haha, just kidding. The Community behavior in this chapter is not so intelligent, and it can discharge "SB". I don't need to explain the community behavior here!


10.1
Purpose



Implement community behavior like Dayan


10.2
Implementation



L calculate the average location averageposition and average speed averagevelocity


Lseek average position and average speed, not too far or too close


10.3
Discussion



L calculate the average location averageposition and average speed averagevelocity


Walking in a community as a whole, ah, it's a little far away from a, hurry up and catch up with B, it's a little far away from B, and then a little more to B, huh? How can I stay so close to C? I can't stay away from C, or even hit C... How fast am I going? Let's go at the speed of a. No. A's going too fast and will drop C. It's not going to be done at the speed of B. It will be caught up by C...


Through the above ideas, we should probably know how the locomotive was changed;


The locomotive should be in the average position of A, B, and C, so that it will not be too far away from a or too close to C; so the speed, according to a, B, the average speed of C. this will neither be left behind nor caught up.


Well, let's see how the code is implemented. The first is to define the average variable:

// Average speed
VaR averagevelocity: vector2d = _ velocity. Clone ();
// Average position
VaR averageposition: vector2d = new vector2d ();
// Number of objects visible to the locomotive


Then calculate the average variable:

// Collect average Variables
Averagevelocity = averagevelocity. Add (vehicle. velocity );
Averageposition = averageposition. Add (vehicle. position );


Seek average position and average speed, not too far or too close


The average position and the average speed are fixed, just as this position starts at the average speed.

If (insightcount> 0 ){
// Average the average variable
Averagevelocity = averagevelocity. Divide (insightcount );
Averageposition = averageposition. Divide (insightcount );
Seek (averageposition );
_ Steerforce. Add (averagevelocity. substract (_ velocity ));
}


But it cannot be too close, otherwise it will still collide:

If (tooclose (DIST )){
// If it is too close to C, avoid
Flee (vehicle. position );
}


Okay. Is it easy? It is simpler than <advanced_actionscript_animation>. Haha, you can understand it in a silly way. Well, let's take a look at the code changes after learning this chapter. The first is the flock method:



Public Function flock (vehicles: array): void {
// Average speed
VaR averagevelocity: vector2d = _ velocity. Clone ();
// Average position
VaR averageposition: vector2d = new vector2d ();
// Number of objects visible to the locomotive
VaR insightcount: Int = 0;

For (var I: Int = 0; I <vehicles. length; I ++ ){
// All locomotives are stored in the vehicles array.
VaR vehicle: vehicle = vehicles [I] as vehicle;
// Distance from the current locomotive object
VaR Dist: Number = _ position. dist (vehicle. position );

If (vehicle! = This & insight (vehicle, DIST) {// If the locomotive is not in the current car's field of view, continue to run
// Collect average Variables
Averagevelocity = averagevelocity. Add (vehicle. velocity );
Averageposition = averageposition. Add (vehicle. position );

If (tooclose (DIST )){
// If it is too close to C, avoid
Flee (vehicle. position );
}
Insightcount ++;
}
}
If (insightcount> 0 ){
// Average the average variable
Averagevelocity = averagevelocity. Divide (insightcount );
Averageposition = averageposition. Divide (insightcount );
Seek (averageposition );
_ Steerforce. Add (averagevelocity. substract (_ velocity ));
}
}


The following insight and tooclose are included. I don't want to explain that you can understand them too! Hey, I'm so lazy again.

Public Function insight (vehicle: vehicle, distance: Number): Boolean {
// If the field of view is no longer in the specified range, false is returned.
If (distance> _ insightdist) return false;
// If this algorithm has read the wander chapter, it will not be difficult to understand and it will not be repeated here
VaR heading: vector2d = _ velocity. Clone (). normalize ();
VaR difference: vector2d = vehicle. position. substract (_ Position );
VaR dotprod: Number = difference. dotprod (heading );
If (dotprod <0) return false;
Return true;
}
Public Function tooclose (distance: Number): Boolean {
// Whether it is too close
Return distance <_ tooclosedist;
}
}


Document flock class:

Package
{
Import flash. display. graphics;
Import flash. display. Sprite;
Import flash. Events. event;
Import flash. Events. mouseevent;
Import flash. Text. textfield;

Import ladeng6666.steering. vector2d;
Import ladeng6666.steering. vehicle;
Import ladeng6666.steering. targetobj;

Public class flock extends Sprite
{
Private var vehiclenum: Int = 30;
Private var vehiclelist: array;

Public Function flock ()
{
Vehiclelist = new array ();
For (var I: Number = 0; I <vehiclenum; I ++ ){
VaR vehicle: vehicle = new vehicle (0 xffffff * Math. Random ());
Vehicle. Position = new vector2d (stage. stagewidth * Math. Random (), stage. stageheight * Math. Random ());
Vehicle. velocity = new vector2d (math. Random () * 20-10, math. Random () * 20-10 );
Vehicle. edgebehavior = vehicle. Bounce;
Vehiclelist. Push (vehicle );
Addchild (vehicle );
}

Conifgevent ();
}
Private function conifgevent (): void {
Addeventlistener (event. enter_frame, enterframehandler );
}
// Update the locomotive Location Based on Frame Rate
Private function enterframehandler (E: Event): void {
For (var j: Number = 0; j <vehiclenum; j ++ ){
Vehiclelist [J]. Flock (vehiclelist );
Vehiclelist [J]. Update ();
}
}
}
}
 


Source code of this chapter:





Chapter10.rar


(53.71 KB)

Downloads: 442010-1-5

Download silver 1 two

Okay, it's the end,
I am exhausted, but I still get a lot of results. Here I am just a beginner about AI in the game, and there is a lot of information to learn on the Internet, here we recommend a book <artificial intelligence for games> By Elsevier. The code in the book is written in C ++, but the article is very detailed and easy to understand, if you are interested, you can learn about the download link of the book as follows:

Http://www.brsbox.com/filebox/down/fc/1ad7dc9f53f48c883156af245e18df5f





Now, I am bin Laden, English name ladeng6666, focusing on as3 games and visual editing. You are welcome to come to my blog for watering and join our group for communication! It's an advertisement.
Hey, hey!




Blog

:
Http://blog.sina.com.cn/ladeng6666



Q group: 43806024

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.