7
\$\begingroup\$

The idea is a user presses somewhere on the screen and the program will fire an object in a parabola shape from a defined start position that has it's vertex where the user touched the screen.

I have managed to use the two points I have to get the quadratic equation in vertex form, and converted it to standard form ok but I don't know how to get the values required for setLinearVelocity vx and vy in corona sdk.

Current code is:

-- given vertex and another point find the parabola formula in vertex form
-- y=a(x−h)2+k
local y, a, x, h, k, xh
h = event.x   
k = event.y
x = xStartPos
y = yStartPos

-- solve for a
xh = (x - h) * (x - h)
y = y + (k * -1)
a = y / xh

-- convert to quadratic formula
-- y = ax^2 + bx + c
local b, c
b = -2 * a * h
c = a * (h * h) + k

local vx, vy = -- calculate something here
proj:setLinearVelocity( vx,vy )
\$\endgroup\$
3
  • \$\begingroup\$ Hi, I started writing out an answer but halfway through got a bit confused about your approach. The problem was that if the start and end points are at the same height, i.e. yStartPos = event.y then you find a = 0 which means that you no longer have a parabola. Feel free to correct me. (I will have another crack at an answer tomorrow but also want to check if you are simulating some sort of gravity i.e. firing a cannonball, as I am not exactly sure what you are trying to do). \$\endgroup\$
    – Malrig
    Dec 14, 2015 at 23:04
  • \$\begingroup\$ Hi George - I do have gravity set to -9.8ms. I've been working in the problem today and maybe I can make it a bit clearer. With standard gravity set in box2d physics engine what linear velocity should I give to an object so that it starts from coords (60,260) and hits the vertex at (280,120) \$\endgroup\$
    – Ralph
    Dec 15, 2015 at 1:50
  • \$\begingroup\$ if yStartPos = event.y it will need to have a very large vx value, but it would still technically form a parabola ... actually maybe not - this could be a potential bug, I will force the user to select a event.y value above yStartPos \$\endgroup\$
    – Ralph
    Dec 15, 2015 at 1:53

2 Answers 2

9
\$\begingroup\$

Quick note: I don't know lua, however I am fairly good at maths/physics and please bear with me if I cover ideas you are already familiar with.

For any object under the force of gravity the horizontal component of the velocity, vx, will remain constant throughout the motion.

Parabolic Motion of a ball source: https://animationphysics.pbworks.com/f/1246396681/TossedBall_ArcTrack.jpg

In this picture the red dots are made at equal times, so the ball travels equal distances in the x direction every tick (so vx is constant) whereas it does not in the y direction.

The following does not answer your question directly but is useful so I have left it in

It is possible to solve the problem, given a starting position (x0, y0) and a final position (xf,yf) what velocity does the object need initially to reach the final point. This can be solved using equations of motion, defining (vx,vy) as initial velocities, T as the time to complete the motion and g as the acceleration due to gravity,

xf = x0 + vx*T               // This has no acceleration part as it is zero for x
yf = y0 + vy*T - g*T^2/2

solving these two equations gives

vy = vx * (yf - y0)/(xf - x) + g * (xf - x0)/(2 * vx).

Now this is not a complete solution as you know the motion is parabolic but it takes three points to define a parabola. So you are going to need to choose either the time it takes for the particle to reach its final spot or the velocity in the x direction.

Actual answer

You choose that the final point (xf,yf) to be the vertex of the parabola, this does in fact introduce a third constraint, as you mentioned in the comments, this time on the velocity in the y direction.

As you found in the linked question the vy required is given by,

vy = sqrt(2 * g * (yf - y0)).

You can then use the equation above for vy in terms of vx to solve for vx which gives,

vx = (xf - x0) * sqrt(g / (2 * (yf - y0))).
\$\endgroup\$
2
  • \$\begingroup\$ Thank you for your answer! The parabola in this case can be defined by two points as one of the points is the vertex. I found a half solution here math.stackexchange.com/questions/785375/… However I'm having some trouble translating it to the code... \$\endgroup\$
    – Ralph
    Dec 15, 2015 at 8:38
  • \$\begingroup\$ Ah I see, I misunderstood your question, I will edit my answer to reflect this. (For some reason my mind totally ignored the part where the final point is the vertex) \$\endgroup\$
    – Malrig
    Dec 15, 2015 at 8:56
0
\$\begingroup\$

George has answered this correctly - I just wanted to include the working code in case someone needs it in future. I did not need to convert the values to a quadratic equation in the end

The extra sums in the code such as dy * 30 is to convert between pixels and metres and back again, and were a major cause of being unable to convert the maths to code.

local dy = (yf - y0) * 30
local dx = (xf - x0) * 30
local t = 1 / display.fps
local a = t * t * -9.8


vy = math.sqrt(2 * a * dy) * display.fps * -1
vx = dx * math.sqrt(a / (2 * dy)) * display.fps


local startingVelocity = { x=vx,  y=vy}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .