2
\$\begingroup\$

I'm wondering how to get all points (x,y) that constitute a given parabola. It's barely to simulate a jump of an object from point A to point B. I know: -The coordinate of point A -The coordinate of point B -The coordinate of the parabola's top

What i'm trying to do is to loop from start_x to dest_x to calculate the Y-coordinate at that moment, but I don't know how to do that part.

pixels = []
for x in start_x to dest_x
   y = (calculate the Y-coord here based on x )
   pixels.push( [x,y] )
end
return pixels

My math skills are not exceptional. Thank you.

\$\endgroup\$
  • \$\begingroup\$ you'll need a third point to specify the parabola, or the direction of the "gravity" and its strength. \$\endgroup\$ – ratchet freak Oct 15 '15 at 21:43
  • \$\begingroup\$ You're using too much data. If you know the parabola's top coordinate then you'll need one more point only and everything is done, but you have a second point too and this can lead you to undesired results \$\endgroup\$ – liggiorgio Oct 16 '15 at 0:14
1
\$\begingroup\$

In general, there is no parabola that goes through three given points. However, if you loosen the problem constraints and only define the Y coordinate of the parabola’s top and allow its X coordinate to be free, there is usually a solution.

Given A.x, A.y, B.x, B.y (A and B’s coordinates) and C.y (the parabola’s top), one can find a parabola formula of the form:

f(x) = C.y - u (x - v)²

One needs to find u and v in this formula. The constraints give us, after some maths:

k = sqrt((C.y - A.y) / (C.y - B.y))
v = (k B.x - A.x) / (k + 1)
u = (C.y - A.y) / (A.x - v)²

For instance if A = (0, 0), B = (10, 5) and C.y = 15:

k = sqrt((15 - 0) / (15 - 5)) = 1.2247448714
v = (1.2247448714 * 10 - 0) / (1.2247448714 + 1) = 5.505102572
u = (15 - 0) / (0 - 5.505102572)² = 0.494948974

Hence the formula:

f(x) = 30 - 0.494948974 * (x - 5.505102572)²

You can see this parabola going from (0,0) to (10,5) with a peak at Y = 15, as expected:

plot of the example parabloa

The following code should work:

pixels = []
k = sqrt((top_y - start_y) / (top_y - dest_y))
v = (k * dest_x - start_x) / (k + 1)
u = (top_y - start_y) / ((start_x - v) * (start_x - v))
for x in start_x to dest_x
   y = top_y - u * (x - v) * (x - v)
   pixels.push( [x,y] )
end
return pixels
\$\endgroup\$
  • \$\begingroup\$ Many thanks sam hocevar ! I'll let you know if it's applicable. \$\endgroup\$ – Wolfrevo stack Oct 16 '15 at 14:11
  • \$\begingroup\$ Unfortunately, i get some weird results. When the top_y is same as start_y, the movement becomes straight. It's normal. But when top_y is different, the movement become weirds. I don't know how to explain that. But after some investigations, il appears that the y at the beginning of the move is too inappropriate. It starts at 5838, for example. Whereas my start_y = ~200 and same for dest_y. \$\endgroup\$ – Wolfrevo stack Oct 17 '15 at 11:39
  • \$\begingroup\$ Please be more specific. Give the values for Ax, Ay, Bx, By, C.y, and what you get for k, u and v. Otherwise it’ll be difficult to tell what’s wrong. \$\endgroup\$ – sam hocevar Oct 17 '15 at 13:58
  • \$\begingroup\$ Ha sorry. Ax = 120; Ay = 258; Bx = 378; By = 264; Cy = 228; k = 0.92582009...; u = -85.852319..; v = 119,408867.... \$\endgroup\$ – Wolfrevo stack Oct 17 '15 at 16:22
  • \$\begingroup\$ Ah, but then Cy is smaller than Ay and By, so it’s no longer a “jump” above an object. If your Y axis goes downwards, you’ll need to use k = -sqrt(…) instead of k = sqrt(…). Also, check your code, because with the formulas above you should get k=0.912870929175277. \$\endgroup\$ – sam hocevar Oct 18 '15 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.