**0**

# Maintaining speed with touch path

###
#1
Members - Reputation: **107**

Posted 13 March 2012 - 11:09 AM

###
#3
Moderators - Reputation: **39078**

Posted 13 March 2012 - 05:03 PM

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

###
#4
Members - Reputation: **107**

Posted 14 March 2012 - 10:38 AM

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?

###
#5
Members - Reputation: **154**

Posted 15 March 2012 - 11:59 AM

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

###
#6
Members - Reputation: **2143**

Posted 15 March 2012 - 01:53 PM

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.