Cocos2d-x "Thunder War" (6) Intelligent enemy aircraft AI attack--flight path algorithm design and implementation (i)

Source: Internet
Author: User
Tags addchild

Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka

This paper is to realize the following and parallel flight of a series of aircraft in aircraft games. And on the net to find some COCOS2DX developed flight games, all only found some simple intelligent enemy aircraft. Basically there is no AI, so the game is too boring to play. Then went to find the enemy aircraft flight path related data, found that the relevant is very few. Think about it or design it yourself!

Aircraft game design, intelligent flight path design and smart bullet design absolutely a flying class game is the core of good or bad. Enemy aircraft intelligence is also sub-level. Boss Machine will not say, and flight games because of its particularity, but also often have the kind of a series of enemy aircraft. This can be divided into the following two kinds:

Follow: same position, same flight path, different start time, usually by time interval.

And fly: Different locations, same run path, same start time.

The effect is as follows:

To follow, not yet make a collision judgment

And fly, yet do collision judgment

Cocos2d-x Version: 3.4

Engineering Environment: VS30213

One, follow the flight

In the following flight, a simple follow-up route is a straight line, such as from left to right, from one corner to another. This approach is simple and not very difficult. In the actual game development, it is very rare to see this kind of following flight, more or change the curve. And this curve is usually a Bezier curve. Aircraft not only to fly, or real-time angle changes, so that more simulation of the real game scene!

1.1 Overview of the Bezier curve
Bezier curves are mathematical curves applied to two-dimensional graphics applications. The definition of a curve has four points: a starting point, a terminating point (also called an anchor point), and two separate intermediate points. Sliding two intermediate points, the shape of the Bezier curve changes.

P0,P1,P2,P34 points are defined in planar or in three-dimensional space with three quadratic Bezier curves. The curve starts at p 0 toward P 1 and comes from P 2 to p3. Generally do not go through P1 or P2; These two points are just there to provide direction information. The spacing between p0 and p1 determines how long the curve moves toward the p2 direction before turning to p3.


P0 starting point, P3 is the end point, P1,p2 is the control point

1.2 Game Apps
We may need to simulate the trajectory of a missile or arrow in the game, which can be easily simulated with a Bezier cocos2d-x.
Cocos2d-x provides us with two action Bezierby and Bezierto, which are simple to use and only need to populate the structure:

Set Bezier parameters Ccbezierconfig tr0;tr0.endposition = Vec2 (0, 10);//end point tr0.controlpoint_1 = VEC2 (250, 300);// Control Point 1tr0.controlpoint_2 = VEC2 (180, 150);//Control point 2actioninterval* Bezierforward = Bezierto::create (3.F, TR0);//Create a running Bezier curve

we just need to provide two control points and an end location, so here's the note
Ccbezier This action is the starting point of the current position, two control points and end points are offset from the starting point
such as: Tr0.endposition = CCP (280,240), is offset from the starting point

1.3 Code

This is just to validate the algorithm, so the code has not been packaged separately into classes, and it has not packaged the images into plist or spritebatchnode to optimize memory. The author intends to the enemy aircraft flight path and the design of the enemy bullets, then unified to optimize the memory!

Add a timer to the GameMain.h:

void EnemyBuild1 (float dt);//Follow
then it is GameMain.cpp's init () function to open the timing, here set every 0.5

Every 0.5S call once schedule (Schedule_selector (gamemain::enemybuild1), 0.5f);
Finally, it is achieved:

void GameMain :: EnemyBuild1 (float dt) {Size winsize = director::getinstance ()->getwinsize (); Auto Spriteplane = Sprite::create (" Spriteplane->setrotation (Air3.png), Spriteplane->setposition (VEC2 (0,400)); Spriteplane->setscale ( 0.25); This->addchild (Spriteplane);//Set Bezier parameters ccbezierconfig tr0;tr0.endposition = Vec2 (0, 10);// End tr0.controlpoint_1 = VEC2 (250, 300);//Control Point 1tr0.controlpoint_2 = VEC2 (180, 150);//Control point 2actioninterval* Bezierforward =     Bezierto::create (3.F, TR0);//Create a running Bezier actioninterval *forwardby = rotateby::create (3.f,180); The second parameter: If a positive number is clockwise, otherwise counterclockwise spawn* Spawn = Spawn::create (Bezierforward, forwardby,null);//Create a composition action//Perform a function callback after the aircraft executes the action, Call the Remove airplane function Auto Actiondone = callfuncn::create (Cc_callback_1 (Gamemain::enemyremove, this));//continuous action sequence* sequence = Sequence::create (Spawn,actiondone, NULL); Spriteplane->runaction (Sequence);} 
Don't look at the code, there is a lot of content involved in it!

The function where the plane is removed:

void Gamemain::enemyremove (node* pnode) {if (NULL = = Pnode) {return;} sprite* plane = (sprite*) pnode;this->removechild (plane,true);}

Remember to define it first in GameMain.h.

void Enemyremove (node* pnode);

Finally, it runs, the effect is as follows:



Second, and fly flight

And fly is simpler than that because it is the same path method. And generally do not take into account the problem of angular rotation. Most of the game is left and right to fly or up and down and fly. Just set up a few planes on a line, then set the flight path. The last execution is, the following directly to see the code bar, the comments are very detailed, there is a need to directly take the past, it is convenient to expand their own, the image name changed to good. Remember, there are no memory optimizations here, and there are two ways to do this. One is the image made Plist, the other is to do with Spritebatchnode. Here in, I recommend the former, but it is best to wait until the game is all developed and then to get it.

First GameMain.h Add timer:

void EnemyBuild2 (float dt);//And Fly

Turn on the timer:

    Every 3S call once    schedule (Schedule_selector (gamemain::enemybuild2), 3.0f);

Finally, it is achieved:

void Gamemain::enemybuild2 (float dt) {Size winsize = director::getinstance ()->getwinsize (); Point origin = Director::getinstance ()->getvisibleorigin ();//Generate Sprite Auto spritePlane1 = sprite::create ("Air4.png"); Auto SpritePlane2 = sprite::create ("air4.png"); auto SpritePlane3 = Sprite::create ("Air4.png");//Get elf width and height float height = Spriteplane1->getcontentsize (). Height;float width = spriteplane1->getcontentsize (). width;// Rotation angle spriteplane1->setrotation (spriteplane2->setrotation); spriteplane3->setrotation (180);// Set zoom//spriteplane1->setscale (0.3);//spriteplane2->setscale (0.3);//spriteplane3->setscale (0.3);// Set Position spriteplane1->setposition (Vec2 (width, winsize.height + height)) spriteplane2->setposition (VEC2 ( WINSIZE.WIDTH/2, Winsize.height-height)); Spriteplane3->setposition (VEC2 (winsize.width-width, WinSize.height + height);//Add Sprite This->addchild (SPRITEPLANE1) to the layer, this->addchild (spritePlane2); This->addchild ( SPRITEPLANE3);//Calculate flight time float flyvelocity=200;//running speed, you can control the pixels per second, float Flylen = winsize.height;float realflyduration = flylen/flyvelocity;//actual flight time// The distance and time of the bullet run from the plane to the bottom of the screen auto actionMove1 = Moveby::create (realflyduration, point (0,-winsize.height-height)); auto ActionMove2 = Moveby::create (realflyduration, point (0,-winsize.height-height)); Auto ActionMove3 = Moveby::create ( Realflyduration, point (0,-winsize.height-height));//function callback after the execution of the bullet, call remove bullet function Auto Actiondone = callfuncn::create (cc_ Callback_1 (Gamemain::enemyremove, this));//continuous action sequence* Sequence1 = Sequence::create (actionMove1, Actiondone, NULL) ; sequence* Sequence2 = sequence::create (ActionMove2, Actiondone, NULL); sequence* sequence3 = sequence::create (ActionMove3, Actiondone, NULL);//aircraft start running Spriteplane1->runaction (Sequence1) ; Spriteplane2->runaction (Sequence2); spriteplane3->runaction (SEQUENCE3);}

Look at the effect:



Have not yet done collision detection, you can see. The enemy planes were generated and moving according to our requirements.

This is the group of flying groups of two ways, you can also fly and follow together. Can generate a number of different aircraft paths. I'll explain this in the back, and I'll be here before today. It is best to encapsulate the enemy aircraft, which is not yet implemented. You can change the function by yourself if you need it! Finally, put another picture!

Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka

Cocos2d-x "Thunder War" (6) Intelligent enemy aircraft AI attack--flight path algorithm design and implementation (i)

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.