5 replies to this topic

Posted 13 March 2012 - 11:09 AM

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?

Posted 13 March 2012 - 11:10 AM

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

Posted 13 March 2012 - 05:03 PM

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.

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.

Posted 14 March 2012 - 10:38 AM

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?

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?

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

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.

Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!