# Maintaining speed with touch path

This topic is 2469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm currently making a game similar to the airport controller games already available but I'm having a bit of difficulty keeping the same speed with a path. When the plane is initialised, it is given a velX and velY value that changes the position of the plane, but when the user touches the plane, a vector of points is created that set the position of the plane. My question is, how can I keep the plane moving at the same speed as it was set, instead of just moving from point to point at whatever speed the path is created?

##### Share on other sites
Forgot to mention, if the plane is moved after being held down, points are collected and added to this vector.

##### Share on other sites
Whatever you do will end up being very specific to your game logic.

Perhaps a rolling average or weighted spline of point distances to come up with your velocity ? Or if the velocity should be kept current, you would not need to do anything other than follow the curve.

##### Share on other sites
Sorry I think I was a bit unclear about my intentions. My plane has a velX and velY value that is added to the position to move it. When a path is created I'm having an issue moving the plane along to the next point. I tried :

double theta = Math.atan2(pathPoints.firstElement().y-posY, pathPoints.firstElement().x - posX);

velX=(float) (magnitude * Math.sin(theta));
velY=(float) (magnitude * Math.cos(theta));

but this results in the plane moving in a straight line away from the point, in the opposite direction. I also tried doing posY-velY but this resulted in the plane flying in a circle. What am I doing wrong?

##### Share on other sites
 double theta = Math.atan2(nextY - posY, nextX - posX); //calculate the angle of the next coordinate relative to the current position velX = magnitude*Math.cos(theta); //calculate the velocity of X given using the angle of theta and magnitude of the total velocity velY = magnitude*Math.sin(theta); //calculate the velocity of Y given using the angle of theta and magnitude of the total velocity currX+=velX*(timeElapsed); //add the velocity to the currentX currY+=velY*(timeElapsed); //add the velocity to the currentY 

##### Share on other sites

Sorry I think I was a bit unclear about my intentions. My plane has a velX and velY value that is added to the position to move it. When a path is created I'm having an issue moving the plane along to the next point. I tried :

double theta = Math.atan2(pathPoints.firstElement().y-posY, pathPoints.firstElement().x - posX);

velX=(float) (magnitude * Math.sin(theta));
velY=(float) (magnitude * Math.cos(theta));

but this results in the plane moving in a straight line away from the point, in the opposite direction. I also tried doing posY-velY but this resulted in the plane flying in a circle. What am I doing wrong?

With this code snippet, you are doing a redundant euclidean -> polar -> euclidean conversion. It is simpler and faster to just work with 2D vectors as follows:
 // Unnormalized direction vector to move to. velX = pathPoints.firstElement().x - posX; velY = pathPoints.firstElement().y - posY; float length = velX*velX + velY*velY; float desiredVelocity = x; if (length <= desiredVelocity*desiredVelocity) { // distance to target is shorter than our velocity. We have arrived to our target, what to do? // Immediately snap to target, or take next waypoint instead? return; } // Scale the velocity to the desired length. length = 1.0f / Sqrt(length); velX *= length * desiredVelocity; velY *= length * desiredVelocity; 

However, if I understand correctly, you are moving your plane through a set of waypoints. In that case, just going linearly from waypoint to waypoint will look jaggy, and instead, you will be looking to use a smooth curve with arc length parametrization. At least, that's what the flight control games on Android and iPad are doing.

1. 1
2. 2
Rutin
20
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633763
• Total Posts
3013728
×