• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
theagentd

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

14 posts in this topic

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

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

 

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.

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0