Cocos2d-x a little trick on rotation and movement

Source: Internet
Author: User

Are you sleepy, uh, telling a joke to cheer you up?


A pair of feelings partners to play from the forest, the cannibals caught. The cannibal leader is in a good mood, saying that if you want to live, you eat the stool from the side. On their way back, the woman finally stopped and sat down on the stone and began to cry. The man held her shoulder. Woman to face, faint said: You do not love me, otherwise just you will not pull so much.


(Can you hehe?) )


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


Generally in the game we can not avoid processing rotation or bullets, such as the Tower defense game, we need to determine where the enemy to go, turret on where to go, turn over and then in a direction fired bullets ( is a direction instead of a point, such as the protection of radish, The bullet continues to fly in that direction through the monster until it is removed from the screen, and the following is a simple analysis of the implementation process, which involves a little bit of mathematical knowledge of the plane vectors.

(Note constant speed)

1. Rotate: Rotate in a constant direction towards a point

2. Launch: Let the bullet move at a constant speed in the direction of a point


Let's step through the implementation of the rotation function first:


Well, now assuming the plane is a bit a and dot b,a is the turret, B is the enemy, now we need to make the direction of turret A to the enemy B rotation, because the turret is placed in the direction upward, so we want to rotate the angle of α,


Now first, we 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 We let the Tower spin aimed at the enemy, just to be able to shoot it in the face (huh? )

Gental in the direction of the enemy//shooting direction vector point shootvector = enemy->getposition ()-tower->getposition ();//vector normalization (i.e. vector length 1) point Normalizedvector = Ccpnormalize (shootvector);//Calculate the rotational radian of float radians = atan2 (normalizedvector.y,-normalizedvector.x) ;//convert radians to angle float degree = cc_radians_to_degrees (RADIANS);//constant rotation requires us to set the speed, where the rotational speed is assumed to be 2π (rad/s) Float rotatespeed = 2 * m_ pi;//the time to rotate 1 radians is float rotate_1rad_time = 1/rotatespeed;//So the length of rotation is float rotateduration = fabs (radians * rotate_1rad_ time);//Final Execution of Rotary _sprite->runaction (Rotateto::create (rotateduration,degree-90));

need to be careful.

(1) False setpoint A is the tower and B is the enemy. then vector shootvector = Ob-oa = AB

(2) atan2 (y,x) is the inverse tangent function, which calculates the angle between the point (X, y) and the positive direction of the x axis, and returns the Radian value of the angle.

(3) So the angle of the degree figure is actually the angle of the positive direction of the X-axis

(4) because the turret direction upward, so "angle of rotation α" = degree-90


If you can't think of anything is anyway cut, that's okay, look at the following assumptions and graphs (I can't remember what I can do)

Assuming tan (α) = y/x, there is α= arctan (y/x)


After the rotation, we then realize the shooting function:


Assuming there are towers, bullets and enemies, position, we need to shoot the bullets from position A at a constant speed along the ab direction to C (point C outside the screen)



We'll start by creating 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);//Bullets, and tower in one position auto Tower = sprite::create ("Bullet.png"); Tower->setpostion (Point (200,100)); This->addchild (tower);

And then this time we really shot it in the face (again hehe)


Firing direction vector Point shootvector = enemy->getposition ()-bullet->getposition ();//vector normalization (i.e. vector length 1) point Normalizedvector = Ccpnormalize (shootvector);//move length vector point overshootvector = normalizedvector * 900;//out of screen Offscreenpoint = bullet->getposition () + overshootvector;//assumed speed is (pix/s) float movespeed = 500;//Move Time float Moveduration = overshootvector/movespeed;//execution 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 the bullets after the shot is over

remove void Helloworld::shootfinish (node* pnode) {    sprite* bullet = (sprite*) Pnode at the end of the shot;    if (bullet! = NULL)        bullet->stopallactions ();        This->removechild (bullet);}

To explain a little bit:

(1) shootvector is vector ab.

(2) Overshootvector = (ab vector normalization) x900 is obtained AC. For example, if you set the resolution to 400 x, then you can use the normalized vector x your maximum resolution is a little larger, this way the vector will be outside the screen and fixed length.

(3) then according to the vector oc = OA + AC, figure out the point to move to Offscreenpoint (that is, point C).

(4) Set the speed, length must be, so time = length/speed.


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


In fact, there is nothing, purely small white tutorial.


Reprint Please specify source: http://blog.csdn.net/shun_fzll/article/details/34430045






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.