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

Started by
13 comments, last by theagentd 9 years, 11 months ago

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)?

Advertisement
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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?

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.)

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.

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.

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.

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.

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.

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.

This topic is closed to new replies.

Advertisement