Beginner help: Making a curved path for object in 2D.
Members - Reputation: 214
Posted 09 May 2012 - 05:41 PM
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!
Crossbones+ - Reputation: 2403
Posted 09 May 2012 - 06:14 PM
If you just want any curve, then pick something really simple like a parabola.
Members - Reputation: 214
Posted 09 May 2012 - 07:05 PM
Members - Reputation: 766
Posted 09 May 2012 - 09:25 PM
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.
Edited by taby, 09 May 2012 - 11:05 PM.