**0**

# Beginner help: Making a curved path for object in 2D.

Started by dacypher22, May 09 2012 05:41 PM

5 replies to this topic

###
#1
Members - Reputation: **214**

Posted 09 May 2012 - 05:41 PM

I am not sure if I should post this here or in the beginner section. I have search extensively, but am wondering if I just don't know what the name of the solution I am looking for is.

Anyway, I am trying to make an object (a ball) move from point A to point B, but curve along a path to its destination. This is all in 2D. I am familiar with trig (just completed it in college last fall semester), but I have seemed to have a problem converting that knowledge to a game. I did some looking around, and most of the topics related to Bezier curves, which I am pretty sure I don't need, and hermite curves, which may be what I need. Is there a more simple way to do this? I always want the ball to end up at point B, but I want to allow the player to select how wide the curve will be, up to a point.

Does anyone know of any tutorials for this, or just good general 2D game math tutorials? Most everything I have come across is for 3D, which I know contains a lot of good info for 2D, but really clouds things up, particularly when I don't know what is relevant to me or not. Thank you in advance!

Anyway, I am trying to make an object (a ball) move from point A to point B, but curve along a path to its destination. This is all in 2D. I am familiar with trig (just completed it in college last fall semester), but I have seemed to have a problem converting that knowledge to a game. I did some looking around, and most of the topics related to Bezier curves, which I am pretty sure I don't need, and hermite curves, which may be what I need. Is there a more simple way to do this? I always want the ball to end up at point B, but I want to allow the player to select how wide the curve will be, up to a point.

Does anyone know of any tutorials for this, or just good general 2D game math tutorials? Most everything I have come across is for 3D, which I know contains a lot of good info for 2D, but really clouds things up, particularly when I don't know what is relevant to me or not. Thank you in advance!

###
#2
Crossbones+ - Reputation: **2403**

Posted 09 May 2012 - 06:14 PM

You need to be more specific. There are an infinite number of 2D curves that pass through two points so to decide on a type of curve you need to think about what kind of curve you want and how to specify it in terms of mathematical constraints. For example, do you need the ball to start out in a particular direction, end up moving in a particular direction, and also pass through a particular point along the way?

If you just want any curve, then pick something really simple like a parabola.

If you just want any curve, then pick something really simple like a parabola.

###
#3
Members - Reputation: **214**

Posted 09 May 2012 - 07:05 PM

Oh, no the points it passes through are not important. Only where it starts, where it ends, and the amount of curvature (defined in the game by the number of times the player presses left or right before launching the ball). So a parabola sounds good. But I don't really know how to convert that to a path the ball can follow in the game. I guess I am kind of suffering on the

*applications*of trig and other math types, particularly for games. I always used y^2 = 4ax to make a parabola. But I don't really know where to go from that, or if that is even the right parabola formula, since I know there are several. I guess I don't know how to take that and actually work with it in terms of an angle, starting location and ending location. Thanks!###
#4
Members - Reputation: **766**

Posted 09 May 2012 - 09:25 PM

Here are some equations about the parabola in general:

http://en.wikipedia....abola#Equations

For fun, try plotting this somewhat arbitrary equation on wolframalpha.com with various parameters... Where d is the distance between the start and end points on the x axis, and h is the height of the "parabola" on the y axis:

y = a(-x^2 + b) = -ax^2 + h

b = (d/2)^2

a = h/b

For instance -- if my math is right -- where d = 8 and h = 5, you get the equation y = (5/4^2)*(-x^2 + 4^2) = -(5/4^2)*x^2 + 5. This "parabola" reaches a maximum height of y = 5. This "parabola" intersects with the y = 0 line at x = -4 and 4, which gives a total distance of 8. Hope that gives you some help. You can use the derivative of the equation to calculate the slope-angle of the "parabola" at the starting point -- that is, the derivative dy/dx = -2ax gives a slope of 2.5 for x = -4. Note that converting between slope and angle is given by A = arctan(slope), which in this case is A = arctan(2.5) = 68.19 degrees for x = -4. For fun, note that as the height h goes to infinity, the angle should go to 90 degrees. Likewise as h goes to 0, the angle should go to 0. Now, on the other hand if you start with a desired angle A and a desired d (which seems to be your case), then you can reverse calculate h from those by doing some algebra to get something like h = 1/4 * d * tan(A). Have fun, and of course be aware that tan/arctan generally work in radians, where I worked in degrees here. No big deal. Also, of course your start point and end point might not be centred around the x origin, but that's just a matter of translation before and after the calculations.

If you really want to get involved with angles and this is all getting more complicated than desired / not true enough to a parabola for you, then maybe the plain old SUVAT equations are what you want.

https://sentynel.com.../equations.html

http://www.physicsfo...m/dynamics.html

http://en.wikipedia....abola#Equations

For fun, try plotting this somewhat arbitrary equation on wolframalpha.com with various parameters... Where d is the distance between the start and end points on the x axis, and h is the height of the "parabola" on the y axis:

y = a(-x^2 + b) = -ax^2 + h

b = (d/2)^2

a = h/b

For instance -- if my math is right -- where d = 8 and h = 5, you get the equation y = (5/4^2)*(-x^2 + 4^2) = -(5/4^2)*x^2 + 5. This "parabola" reaches a maximum height of y = 5. This "parabola" intersects with the y = 0 line at x = -4 and 4, which gives a total distance of 8. Hope that gives you some help. You can use the derivative of the equation to calculate the slope-angle of the "parabola" at the starting point -- that is, the derivative dy/dx = -2ax gives a slope of 2.5 for x = -4. Note that converting between slope and angle is given by A = arctan(slope), which in this case is A = arctan(2.5) = 68.19 degrees for x = -4. For fun, note that as the height h goes to infinity, the angle should go to 90 degrees. Likewise as h goes to 0, the angle should go to 0. Now, on the other hand if you start with a desired angle A and a desired d (which seems to be your case), then you can reverse calculate h from those by doing some algebra to get something like h = 1/4 * d * tan(A). Have fun, and of course be aware that tan/arctan generally work in radians, where I worked in degrees here. No big deal. Also, of course your start point and end point might not be centred around the x origin, but that's just a matter of translation before and after the calculations.

If you really want to get involved with angles and this is all getting more complicated than desired / not true enough to a parabola for you, then maybe the plain old SUVAT equations are what you want.

https://sentynel.com.../equations.html

http://www.physicsfo...m/dynamics.html

**Edited by taby, 09 May 2012 - 11:05 PM.**

###
#5
Crossbones+ - Reputation: **779**

Posted 09 May 2012 - 09:51 PM

Bezier curves are really nice curves and easy to implement.

More can be found here: http://en.wikipedia.org/wiki/B%C3%A9zier_curve

More technical definition can be found on Wolfram Mathworld: http://mathworld.wolfram.com/BezierCurve.html

More can be found here: http://en.wikipedia.org/wiki/B%C3%A9zier_curve

More technical definition can be found on Wolfram Mathworld: http://mathworld.wolfram.com/BezierCurve.html

what