• 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
abeylin

need an algorithm for following a path (curved line)?

11 posts in this topic

I am wondering how the game https://play.google.com/store/apps/details?id=com.namcowireless.flightcontrol&hl=en makes planes follow the path drawn by player.

 

Does anyone know of a simple algorithm to do this?

 

I am thinking of a game where there are predefined paths, which is kind of similar to the game I linked, but more like this:

sample.jpg

 

So I want to go from one box to another, but following the paths. Please help. The paths would not be dynamic in my game.

 

edit (not sure if it's better to add post or update original, so doing both):

The paths will not be in terms of any function. The paths are going to represent roads from one point to another. During level creation, I'd define them as makes sense for the level.

Edited by abeylin
1

Share this post


Link to post
Share on other sites

Are the points where you change from one curve to another known? (so that you can create an intermediary node)?

Are the curves logarithmic or not?

0

Share this post


Link to post
Share on other sites

Here is an explaination of legrange interpolation

http://mathforum.org/library/drmath/view/63984.html

 

Here's another that does a better job of describing how to actually compute the function

https://brilliant.org/assessment/techniques-trainer/lagrange-interpolation-formula/

 

 

These are slightly differen't than bezier curves, Bezier curves use control points, for which the final line does not pass through.

Legrange interpolation ensures that each point in the data set is in the final line.

 

These are good for lines where x is always increasing.

 To apply it to a line where x is not always increasing you have to convert your data set into a parameterized data set and interpolate the x and y components independently by t. 

I.E: f(t)=(fx(t),fy(t))

Get the lagrange interpolation formula for fx(t) and fy(t).

Edited by Paragon123
2

Share this post


Link to post
Share on other sites

Hi, I didn't go through links yet, but they are greately appreciated.

 

The paths will not be in terms of any function. The paths are going to represent roads from one point to another. During level creation, I'd define them as makes sense for the level.

0

Share this post


Link to post
Share on other sites

So, I got pretty interested in this topic b/c I was looking to do something similar.

So, it turns out that Interpolating the x and y components separately actually made the calculation simpler because I could choose which values of T to use.

So given a data set of at least four points (x1,y1,) (x2,y2) (x3,y3) (x4,y4), we separate it 

(0,x1) (1,x2) (2,x3) (3,x4)  and (0,y1) (0,y2) (0,y3) and (0,y4)

 

The function created with four points can always be represented as F(x)=ax^3+bx^2+cx+d, so we can store the function as a set of four values (a,b,c,d). 

To get a value at T=t

double Px(Tuple<double, double, double, double> coefficents, double t)
{
  double ret = coefficents.Item1 * Math.Pow(t, 3);
  ret += coefficents.Item2 * Math.Pow(t, 2);
  ret += coefficents.Item3 * t;
  ret += coefficents.Item4;
  return ret;
}

so the full polynomail is

    y1*1/((x1-x2)(x1-x3)(x1-x4))*(x-x2)(x-x3)(x-x4)

+  y2*1/((x2-x1)(x2-x3)(x2-x4))*(x-x1)(x-x3)(x-x4)

+  y3*1/((x3-x1)(x3-x2)(x3-x4))*(x-x1)(x-x2)(x-x4)

+ y4*1/((x4-x1)(x4-x2)(x4-x3))*(x-x1)(x-x2)(x-x3)

 

But, we already choose the X(T) values (0,1,2,3) so we can calculate that part and put it as a constant

   y1*-1/6*(x-1)(x-2)(x-3)

+ y2*1/2*(x-0)(x-2)(x-3)

+ y3*-1/2*(x-0)(x-1)(x-3)

+ y4*1/6*(x-0)(x-1)(x-2)

 

And we can expand to get

  y1* -1/6 *( x^3-6x^2+11x-6)

+y2* 1/2*(x^3-5x+6x)

+y3* -1/2*(x^3-4x^2+3x

+y4* 1/6*(x^3-3x^2+2x)

 

Then distribute to get

    -(1/6)y1x^3+y1x^2 -(11/6)y1x+1y1

+ (y2/2)x^3-(5/2)y2*x^2+3y2x

+ -(y3/2)x^3+2y3x^2-(3/2)y3x

+ (y4/6)x^3-(y4/2)x^2+(y4/3)x

 

Now we can collect the terms and it leaves us with the following (v1,v2,v3, and v4 are the points to parameterize by t):

Tuple<double, double, double, double> GetCoeffInT(double v1, double v2, double v3, double v4)
{
  double a = (-v1 / 6.0) + (v2 / 2.0) + (-v3 / 2.0) + (v4 / 6.0);
  double b = v1 - (5.0 / 2.0) * v2 + 2 * v3 - v4 / 2.0;
  double c = (-11.0 / 6.0) * v1 + 3.0 * v2 - (3.0 / 2.0) * v3 + v4 / 3.0;
  double d = v1;
  return new Tuple<double, double, double, double>(a, b, c, d);
}

Now we take our four X/Y cords and convert them to two sets of co-efficents 

Tuple<Tuple<double, double, double, double>,Tuple<double, double, double, double>> GetCoefficents(Point p1, Point p2, Point p3, Point p4)
{
  Tuple<double, double, double, double> xCoeff = GetCoeffInT(p1.X, p2.X, p3.X, p4.X);
  Tuple<double,double,double,double> yCoeff=GetCoeffInT(p1.Y,p2.Y,p3.Y,p4.Y);
  return new Tuple<Tuple<double, double, double, double>, Tuple<double, double, double, double>>(xCoeff, yCoeff);
}

The last part is to draw a line of n>4 data points

double step = .01;
for (int i = 3;i < DataPoints.Count; i++) 
{
int ii = i;
  var v = GetCoefficents(DataPoints[ii-3], DataPoints[ii-2], DataPoints[ii-1], DataPoints[ii]);
  //On the first segment draw from T=0 to T=2
  //On the last segment draw from T=1 to T=3
  //On every other segment draw from T=1 to T=2
  for (double t = (i==3?0:1); t <= (i==DataPoints.Count-1?3.0:2.0); t += step) 
  {
    Point p1 = new Point((int)Px(v.Item1, t - step), (int)Px(v.Item2, t - step));
    Point p2 = new Point((int)Px(v.Item1, t), (int)Px(v.Item2, t));
    g.DrawLine(Pens.Green, p1, p2);
   }
}

Even more interesting is that is it trivial to expand this to any number of dimentions. 

2

Share this post


Link to post
Share on other sites
For comparison, Catmull Rom, taken from here
/// <summary>
/// Return a point on the curve between P1 and P2 with P0 and P4 describing curvature, at
/// the normalized distance t.
/// </summary>
 
public static Vector3 PointOnCurve(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
	Vector3 result = new Vector3();
 
	float t0 = ((-t + 2f) * t - 1f) * t * 0.5f;
	float t1 = (((3f * t - 5f) * t) * t + 2f) * 0.5f;
	float t2 = ((-3f * t + 4f) * t + 1f) * t * 0.5f;
	float t3 = ((t - 1f) * t * t) * 0.5f;
 
	result.x = p0.x * t0 + p1.x * t1 + p2.x * t2 + p3.x * t3;
	result.y = p0.y * t0 + p1.y * t1 + p2.y * t2 + p3.y * t3;
	result.z = p0.z * t0 + p1.z * t1 + p2.z * t2 + p3.z * t3;
 
	return result;
}


 
1

Share this post


Link to post
Share on other sites

What is the difference between Catmull-Rom and legrange? I read somewhere that Catmull-Rom gives you "Local control" while lerange does not, but I am not 100% certain what they mean by that? Also, The artical you pointed out mentions "The location can be calulated... assuming uniform spacing of control points", does this mean that D(p1,p2)=D(p2,p3)=D(p3,p4) is a requirement?  Is it able to create curves that double back on it's self?

0

Share this post


Link to post
Share on other sites

I am not sure what he means by that statement, it isn't true, the points do not need to be equidistant from one another.  I'm not sure what you mean by a curve that doubles back on itself, but Catmull-Rom is local in that if you have multiple points on your spline, if you move the 4th point, it will not affect the curve anywhere but with the lines that are attached to that point.  I'm not that familiar with legrange, but I'm guessing moving one point would require a recalculation of the entire curve?

 

(This has a nice interactive example: http://www.ibiblio.org/e-notes/Splines/cardinal.html)

1

Share this post


Link to post
Share on other sites

I would also go with Catmull-Rom. Its a cubic interpolation curve that is connected of more Hermite curves. I think its similar to Lagrange curve. You can get the same Lagrange curve by manipulating coefficients.

 

Bezier curve does not pass trough all of control points or nodes. But if you choose to go with it maybe it would be better to go with B-Splines instead or with NURBS (Non-Uniform Rational Splines)

Edited by ryt
1

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