Are you sleepy, huh? I'll tell you a joke.
A pair of feelings partners to play from the forest. Caught by cannibals. The cannibals are in a very good mood. If you pretend to live, you eat the stool of the side. On the way back to them. The woman finally couldn't help but stop. 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 the processing of rotation or bullets, such as the Tower defense game, we need to infer where the enemy to go, where the turret to turn, turn over and then in a direction to fire bullets ( is a direction instead of a point, for example, to defend the radish, The bullet continued to fly in that direction through the monster. Until the flight to the screen is removed), to simply analyze the implementation of the process, need to involve a little bit of plane vector mathematical knowledge.
(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 if the plane is a bit a and dot b,a is the turret. B is the enemy. Now we need to rotate the turret A in the direction of enemy B, as the turret is placed upward, so we have to rotate the angle to α,
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 rotate to aim 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. Here if the rotational speed is 2π (rad/s) Float rotatespeed = 2 * m_pi;//then rotate 1 radians the time is float rotate_1rad_time = 1/rotatespeed;//So the length of rotation is float RO Tateduration = fabs (radians * rotate_1rad_time);//Last Run rotation _sprite->runaction (rotateto::create (Rotateduration, degree-90));
need to pay attention to
(1) If point A is a tower, B is the enemy. then vector shootvector = Ob-oa = AB
(2) atan2 (y,x) is the inverse tangent function. 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 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 if and figure (again I can not think of it only hehe)
If tan (α) = y/x, then there is α= arctan (y/x)
After the rotation, we then realize the shooting function:
If there is a tower. Bullets and enemies, position, we need to shoot the bullets from position A to C (C point outside the screen) at a constant speed along the ab direction
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;//if speed is (pix/s) float movespeed = 500;//Move Time float Moveduration = overshootvector/movespeed;//Run 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. Let's say you set the resolution to 400 X. Then you can use the normalized vector x your maximum resolution A little larger, this way the vector will be outside the screen and the length is fixed.
(3) then based on 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 a small white tutorial.
Reprint Please specify source: http://blog.csdn.net/shun_fzll/article/details/34430045
Cocos2d-x a little trick on rotation and movement