# To move the object (sprite) in all direction with constant speed in touch direction and to follow the touch path

I am developing this game in cocos2d. I want to move a object(airplane) in all direction with constant speed and it should move and flip in the direction of touch point and it should follow path make by touches like if we make a circle then the object should follow that path and it should flip so that it look like that its moving like real airplane, and i want it should move from one side of screen to other side and again it will come from other side and move towards other side and if we touch the screen then it should move in the touch direction. i have use the code to follow the path but its not working good in my coding object is not moving and not flipping its just appear like that we dragging any object from one position to other, Please any one help me to solve this issue any idea or sample code please provide so that i can make my object to move in all direction with touch.

Thanks in advance.

-
It's very hard to tell what your problem is. As it is, your question basically asks for: Write me some code that does what I want. – bummzack Feb 3 '12 at 10:02
First of all you need to clearly mention what is wrong with what you have tried. Show us some code. Secondly try to rephrase your question to make it readable. Leave a line between your sentences. Third read your question 3 times before posting it. You will quickly find the solution to your problem if you know it clearly. – Robin Feb 3 '12 at 11:13

## 1 Answer

As I explained in chat you have to use one of these algorithms:

``````target = touch.position - airplane.position;
target /= target.length();
target *= some_constant;
airplane.velocity += target;
if (airplane.velocity.length() > max_speed)
airplane.velocity *= max_speed / airplane.velocity.length();
airplane.angle = atan2(airplane.velocity.y,airplane.velocity.x);
``````

``````target = touch.position - airplane.position;
target_angle = atan2(target.y,target.x);
if (normalize_angle(target_angle - airplane.angle) < M_PI)
airplane.angle += constant;
else
airplane.angle -= constant;
airplane.velocity = max_speed * Vec2(cos(airplane.angle), sin(airplane.angle))
``````

and the `normalize_angle` function will convert its input value to a similar angle between 0 and 2*pi

-
Hi Gaget, above algo is for Box 2d or for cocos2d, beacouse i m developing my game in cocos2d and What "Target" point in above algo? – Anil gupta Feb 3 '12 at 17:04
will you please tell me max_speed , airplane.velocity , airplane.angle method how you got here? – Anil gupta Feb 3 '12 at 17:44