• Create Account

# Algorithm from going from point X to point Y with specific velocities

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

### #1theagentd  Members   -  Reputation: 551

Like
0Likes
Like

Posted 04 May 2014 - 06:34 PM

Hello.

I'm trying to come up with an algorithm for moving around objects in my 2D world. Given an object at position P0(x0, y0) with velocity V0(vx0, vy0), I want to find out exactly how I need to accelerate (how much and in which direction) to reach a new position P1, and when the object reaches P1(x1, y1) it needs to have a specific velocity V1(vx1, vy1).

Could this possibly be solved by calculating bezier curves or something like that? How would I detect and handle a situation where the object cannot accelerate fast enough to be able to follow the bezier curve (e.g. impossible movement)?

### #2ApochPiQ  Moderators   -  Reputation: 15068

Like
0Likes
Like

Posted 05 May 2014 - 03:24 AM

How much mathematics training do you have? This is a relatively straightforward problem in vector calculus, but that may or may not be a useful approach depending on how familiar you are with the basic principles.
Maker of Machinery

### #3Álvaro  Crossbones+   -  Reputation: 12921

Like
0Likes
Like

Posted 05 May 2014 - 04:23 AM

How much mathematics training do you have? This is a relatively straightforward problem in vector calculus, but that may or may not be a useful approach depending on how familiar you are with the basic principles.

Hmmm... Perhaps we understood the problem differently, but it looks to me like this is a problem in planning, and not a trivial one.

But the problem needs to be specified in more detail. Can this object have a maximum acceleration? Does it have a limited turn radius?

### #4Patrick B  Members   -  Reputation: 199

Like
0Likes
Like

Posted 05 May 2014 - 05:34 AM

Such animation systems have been a standard part of Flash and, more recently, JavaScript so you should have plenty of sample and well-tested code to look at. In Flash, animations from point A to point B with certain dynamic velocities are called Tweens and are typically comprised of two parts: the code that applies the tween to the target display object (and reports on progress), and the equation that accelerates, decelerates, or otherwise calculates the motion (or more correctly, the X,Y position of the object at a specific point in time -- what will X,Y be at N milliseconds/frames?). Beziers are just one type of curve that can be applied -- others include exponential, quadratic, sinusoidal, etc.

One of the more established and robust libraries for this is the Greensock library (TweenLite or TweenMax). In jQuery, there's the "animate" function which does the same thing, except of course if operates on HTML DOM objects -- but the concepts are the same.

One thing to keep in mind as that you will probably want to include some sort of reporting mechanism: a callback or event whenever the position is updated, and one more when the animation completes. This will allow you to expand on the animations beyond simply animations (collision detection, Z-axis support, etc.)

Edited by Patrick B, 05 May 2014 - 06:01 AM.

### #5theagentd  Members   -  Reputation: 551

Like
0Likes
Like

Posted 05 May 2014 - 06:56 AM

How much mathematics training do you have? This is a relatively straightforward problem in vector calculus, but that may or may not be a useful approach depending on how familiar you are with the basic principles.

Hmmm... Perhaps we understood the problem differently, but it looks to me like this is a problem in planning, and not a trivial one.

But the problem needs to be specified in more detail. Can this object have a maximum acceleration? Does it have a limited turn radius?

I took a class in linear algebra at my university, if that's what you mean.

As Álvaro said, the problem is relatively complex in my opinion. The objects are part of a simulation, so I wish to be able to send them to different points in the world AND I want them to have a specific velocity when they get there. The objects have a specified maximum acceleration, but for simplicity we can say that they have an infinite angular velocity and acceleration. This is not meant to be used for rendering so that it just looks cool; it's meant to generate an accurate and possible path for the objects.

### #6Álvaro  Crossbones+   -  Reputation: 12921

Like
1Likes
Like

Posted 05 May 2014 - 07:56 AM

If you only care about it looking cool, you can find a cubic parametric curve that does what you want.

x(t) = (1-3*t2+2*t3) * x0 + (3*t2-2*t3) * x1 + (t-2*t2+t3) * vx0 + (-t2+t3) * vx1
y(t) = (1-3*t2+2*t3) * y0 + (3*t2-2*t3) * y1 + (t-2*t2+t3) * vy0 + (-t2+t3) * vy1

This will take your object from its current position and velocity at time t=0 to the desired one at time t=1. This curve is the smoothest possible, in the sense that it minimizes the integral of the square of the acceleration.

Edited by Álvaro, 05 May 2014 - 08:02 AM.

### #7theagentd  Members   -  Reputation: 551

Like
0Likes
Like

Posted 05 May 2014 - 08:07 AM

My experiments with Bezier curves didn't work out very well. The acceleration varies way too much. In that sense, a cubic parametric curve sounds much better if it is better at keeping a constant acceleration. I'll try it out!

I will eventually have to handle movement under external forces (magnetism and gravity mostly). Is it possible to extend this to in some way take such things into consideration? I realize that I'll probably need a numeric solution.

Edited by theagentd, 05 May 2014 - 08:16 AM.

### #8Tutorial Doctor  Members   -  Reputation: 1570

Like
0Likes
Like

Posted 05 May 2014 - 09:32 AM

Yeah. I've been researching the same thing and I went with a cubic parametric curve. I am working on making it straightforward and adaptable. If I finish it before this post expires, I will post the solution here.

They call me the Tutorial Doctor.

### #9ferrous  Members   -  Reputation: 1919

Like
0Likes
Like

Posted 05 May 2014 - 09:42 AM

My experiments with Bezier curves didn't work out very well. The acceleration varies way too much. In that sense, a cubic parametric curve sounds much better if it is better at keeping a constant acceleration. I'll try it out!

I will eventually have to handle movement under external forces (magnetism and gravity mostly). Is it possible to extend this to in some way take such things into consideration? I realize that I'll probably need a numeric solution.

It sounds like you're attempting to do motion planning of spaceships.

This in guy had a similar question, but with Lua:  http://stackoverflow.com/questions/2560817/2d-trajectory-planning-of-a-spaceship-with-physics

It's a fairly hard problem, even just in 2d.

### #10Tutorial Doctor  Members   -  Reputation: 1570

Like
0Likes
Like

Posted 05 May 2014 - 09:53 AM

One more thing. If the cubic parametric curve is describing the position of an object, at a specific time, then the first derivative of that curve describes the velocity at that time, and the second derivative describes the acceleration at that specific time.

You could use polar coordinates instead of Cartesian coordinates.

They call me the Tutorial Doctor.

### #11theagentd  Members   -  Reputation: 551

Like
0Likes
Like

Posted 05 May 2014 - 04:50 PM

I'm a bit confused now. My cubic curves have turned out identical to my bezier curves.

I've made a small test program which allows me to specify 4 points to create a path. For the bezier curve, these 4 points [(x0, y0), ..., (x3, y3)] are treated as control points and simply used as inputs to the bezier function. Excuse the slightly messy code...

	private float bezierX(float t){
float omt = 1 - t; //One minus T

float x01 = mix(x0, x1, t, omt);
float x12 = mix(x1, x2, t, omt);
float x23 = mix(x2, x3, t, omt);

float x012 = mix(x01, x12, t, omt);
float x123 = mix(x12, x23, t, omt);

float x0123 = mix(x012, x123, t, omt);

return x0123;
}

private float bezierY(float t){
float omt = 1 - t; //One minus T

float y01 = mix(y0, y1, t, omt);
float y12 = mix(y1, y2, t, omt);
float y23 = mix(y2, y3, t, omt);

float y012 = mix(y01, y12, t, omt);
float y123 = mix(y12, y23, t, omt);

float y0123 = mix(y012, y123, t, omt);

return y0123;
}


Now, a bezier curve has the following derivation:

	private float bezierDX(float t){
float omt = 1 - t; //One minus T

return 3*omt*omt*(x1 - x0) + 6*omt*t*(x2-x1) + 3*t*t*(x3-x2);
}

private float bezierDY(float t){
float omt = 1 - t; //One minus T

return 3*omt*omt*(y1 - y0) + 6*omt*t*(y2-y1) + 3*t*t*(y3-y2);
}


Therefore the initial velocity (velocity at t = 0) is obviously

(3*(x1 - x0), 3*(y1 - y0))

and the final velocity (velocity at t = 1) is

(3*(x3 - x2), 3*(y3 - y2))

Then we can create a cubic parametric curve with the same initial and final velocity (note the values for f3 and f4):

	private float cubicX(float t){

float t2 = t*t;
float t3 = t2*t;

float f1 = x0 * (1 - 3*t2 + 2*t3);
float f2 = x3 * (3*t2 - 2*t3);
float f3 = (x1-x0)*3 * (t - 2*t2 + t3);
float f4 = (x3-x2)*3 * (-t2+t3);

return f1 + f2 + f3 + f4;
}

private float cubicY(float t){

float t2 = t*t;
float t3 = t2*t;

float f1 = y0 * (1 - 3*t2 + 2*t3);
float f2 = y3 * (3*t2 - 2*t3);
float f3 = (y1-y0)*3 * (t - 2*t2 + t3);
float f4 = (y3-y2)*3 * (-t2+t3);

return f1 + f2 + f3 + f4;
}


Now, the twist here is that these two curves are 100% identical! Assuming I've done everything right so far, I guess I'll just go with cubic parametric curves since it's easier to specify the initial and final velocity.

The problem then is that these cubic curves seem to have widely varying acceleration, just like bezier curves have (since they're identical). This is very problematic since my objects have a maximum acceleration. My guess here is that this can be solved by changing the time given to follow the curve. So by default T goes from 0 to 1, so we'll say that this is in seconds. An object that's going to move 1000 meters in 1 second is going to have to accelerate insanely fast at the beginning to get there in time and then brake extremely fast as well so that it ends up with the correct velocity. If I instead give function 2 seconds to get there, (assuming I've got this right) we need to multiply the initial velocity by 2 since time is passing half as quickly now, but the acceleration becomes 1/4th of the original value.

Does this all make sense?

Edited by theagentd, 05 May 2014 - 04:51 PM.

### #12Doublefris  Members   -  Reputation: 302

Like
0Likes
Like

Posted 05 May 2014 - 05:01 PM

Perhaps it would be obtain the maxiumum of the acceleration curve by differentation, call it T and divide the curve by (maxAcceleration / T), so that the maximum acceleration will match the object's.

Now I'm not sure, but because integrals are linear you could then simply stretch out the t parameter like you said by the inverse (T/maxAcceleration).

Edited by Doublefris, 05 May 2014 - 05:01 PM.

### #13theagentd  Members   -  Reputation: 551

Like
0Likes
Like

Posted 05 May 2014 - 05:49 PM

Yes, the curves I'm getting are starting to make sense. There are many interesting scenarios where the algorithm produces some extremely "smart" paths, like where it loops around to be able to gather enough speed without having to accelerate as quickly.

I believe my main problem then is to handle this when under the influence of external forces. Are there any good approaches here?

### #14Álvaro  Crossbones+   -  Reputation: 12921

Like
0Likes
Like

Posted 05 May 2014 - 08:13 PM

Perhaps you can give us some idea of what you are actually trying to do. What are these objects you talk about? How do they move? What kind of external forces are we talking about? Can they be predicted in advance? These details matter.

Chances are you need to think about this as a control problem, and control theory is not easy. This problem is low-dimensional enough that you might be able to do a good job by discretizing the situation and then using dynamic programming to find an optimal strategy.

### #15theagentd  Members   -  Reputation: 551

Like
0Likes
Like

Posted 06 May 2014 - 06:15 AM

As Ferrous predicted, I am indeed trying to plan motion for spaceships. External forces would pretty much exclusively be gravity wells (planets, stars). For now all gravity wells are stationary. These are predictable as they only depend on the current position of the object, but to plan/predict the motion of an object I'd most likely need to integrate (e.g. simulate it exactly as I would when actually moving the object).

My biggest problem so far when solving similar problems by iteratively attempting to improve the results is that it's hard to measure the degree of success certain parameters produced.

- Do I terminate the simulation after a set time and measure how off I am from the target position and velocity? I don't know the optimal time.

- Do I terminate the simulation when I get close enough to the target? What if it was going to loop around and get a much better result then?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS