Cocos2d-x tips on rotating and moving

Source: Internet
Author: User

Cocos2d-x tips on rotating and moving

Are you sleepy? Well, tell you a joke ~


A pair of emotions went to play from the forest, and were caught by the diners. The diners are in a good mood, saying that if you want to live, they will eat the opposite side's stool. On their way back, the women could not help but stop and sat down on the stone head and cried. The man grabbed her shoulder. Women don't go over and say, you don't love me, or else you won't be pulling so much.


(Can you ?)


========================================================== ==========================================================


In general, we cannot avoid rotating or bullet-fired tasks in games,For example, for anti-tower games, we need to determine where the enemy is going, where the turret is going, after turning, and then launching a bullet in one direction.(Is one direction rather than one point, such as defending the radish, the bullet passes through the monster and continues to fly in that direction until it is removed from the screen ),The following is a brief analysis of the implementation process, which involves a little bit of mathematical knowledge of plane vectors.

(Pay attention to constant speed)

1. Rotating: rotating at a constant speed toward a certain point

2. Launch: Let the bullet move at a constant speed toward a certain point


We will implement the rotation function step by step:


Well, now let's assume that there is A bit of A and B in the plane, A is the turret, and B is the enemy. Now we need to turn turret A in the direction of enemy B, because the turret is placed in the upward directionThe rotation angle is α.,


First, we can create enemies and towers.

// Enemy auto enemy = Sprite: create ("enemy.png"); enemy-> setPostion (Point (100,200); this-> addChild (enemy ); // tower auto tower = Sprite: create ("tower.png"); tower-> setPostion (Point (200,100); this-> addChild (tower );

Then let the tower rotate to target the enemy,Just to make it look (huh ?)

// Rotate the tower to target the enemy. // The shooting direction vector Point shootVector = enemy-> getPosition ()-tower-> getPosition (); // vector standardization (that is, vector length is 1) Point normalizedVector = ccpNormalize (shootVector); // calculates the float radians = atan2 (normalizedVector. y,-normalizedVector. x); // converts radians to degrees float degree = CC_RADIANS_TO_DEGREES (radians); // for constant speed rotation, we need to set the speed. Here we assume the rotation speed is 2 π (rad/s) float rotateSpeed = 2 * M_PI; // The rotation time of 1 radian is float rotate_1rad_time = 1/rotateSpeed; // The rotation duration is float rotateDuration = fabs (radians * rotate_1rad_time ); // finally execute the rotate _ sprite-> runAction (RotateTo: create (rotateDuration, degree-90 ));

Note that

(1) Assume that point A is A tower and B is an enemy. Then the vector shootVector = OB-OA = AB

(2) atan2 (y, x) is an arc tangent function. It calculates the angle between the point (x, y) and the square direction of the x axis, and returns the radian value of the angle.

(3) the angle calculated by degree is actually the angle between the angle and the positive direction of the X axis.

(4) due to the turret direction up, the [rotation angle α] = degree-90


If you can't remember what it is, it doesn't matter. Let's look at the assumptions and figures below (I can't remember it anymore)

Assume tan (α) = y/x, then α = arctan (y/x)


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHN0cm9uZz7Q/fill + PC9wPgo8cD682cno09DL/fill + fill = "http://www.2cto.com/uploadfile/Collfiles/20140626/2014062609121973.jpg" alt = "\">


Let's first create enemies, towers, and bullets.


// Enemy auto enemy = Sprite: create ("enemy.png"); enemy-> setPostion (Point (100,200); this-> addChild (enemy ); // tower auto tower = Sprite: create ("tower.png"); tower-> setPostion (Point (200,100); this-> addChild (tower); // bullet, auto tower = Sprite: create ("bullet.png"); tower-> setPostion (Point (200,100); this-> addChild (tower );

Then, this time we really shot it.(Again)


// Shooting direction vector Point shootVector = enemy-> getPosition ()-bullet-> getPosition (); // vector standardization (that is, vector length is 1) point normalizedVector = ccpNormalize (shootVector); // move the length vector Point overShootVector = normalizedVector * 900; // beyond the screen Point offScreenPoint = bullet-> getPosition () + overShootVector; // assume the speed is 500 (pix/s) float moveSpeed = 500; // The moving time float moveDuration = overShootVector/moveSpeed; // execute the design auto move = MoveTo :: create (moveDuration, offScreenPoint); CallFunc * moveDone = CallFunc: create (CC_CALLBACK_0 (shootFinish, this, bullet); bullet-> runAction (Sequence: create (move, moveDone, NULL ));
Remove bullets after shooting

// Remove void HelloWorld: shootFinish (Node * pNode) {Sprite * bullet = (Sprite *) pNode; if (bullet! = NULL) bullet-> stopAllActions (); this-> removeChild (bullet );}

A little explanation:

(1) shootVector is the vector AB.

(2) overShootVector = (AB vector Standardization) × 900 to obtain the AC. For example, if the resolution you set is 800x400, you can useStandardized vector × your maximum resolution is a little larger, so that the vector will go beyond the screen and the length is fixed.

(3) then calculate the offScreenPoint (point C) to be moved Based on the vector OC = OA + AC ).

(4) set the speed to a certain extent, so the time = length/speed.


========================================================== ==================


In fact, there is nothing to do. It's just a small tutorial ..


Reprinted please indicate the source: http://blog.csdn.net/shun_fzll/article/details/34430045






Related Article

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.