How can i get a parabolic path when two end coordinates given along with the maximum height. To draw on a canvas.
How can i get a parabolic path when two end coordinates given along with the maximum height. To draw on a canvas.
Do you want the curve that you get when throwing a ball into the air?:
OK... let's dreg up some math-stuff from highschool :-)
Then, the equation for that could be written as:
normalize it where Vx = 1, so divide all by VxCode:x = Vx*t y = (.5A)*t^2 + Vy*t t = time in secones. Vx = initial horizontal speed (m/s) Vy = initial vertical speed (upwards in m/s) A = Gravity (A < 0, works against Vy) (m/s^2)
First, see where the ball touches the ground:Code:x = t y = (A/2Vx)*t^2 + (Vy/Vx)*t This (y) is the graph you want to draw. Read the rest of the post to figure out the values for A, Vx, Vy and the maximum value for t.
I remember this problem ...
has the solution..Code:P*t^2 + Q*t + R = 0, where P=A/2Vx, Q = Vy/Vx, R = 0
Code:y = 0 when t = (-Q/2P) +/- square_root(Q^2 - 4PR) / (2P) y = 0 when t = (-Q/2P) +/- square_root(Q^2) / (2P) y = 0 when t = (-Q/2P) +/- Q/(2P) y = 0 when t = 0 or t = -Q/P = -(Vy/Vx)/(A/2Vx) Min Distance time: t = 0 Max Distance time: t = 2Vy/(-A)
The highest point is reached when the vertical speed reaches zero:
Fill this into the original equation to get the highest/max height:Code:0 = A*t + Vy ==> t = -Vy/A
So here are the max height and the distance you throw:Code:t = -Vy/A y = (.5A)*t^2 + Vy*t y = (.5A)*(-Vy^2/A^2) - Vy*Vy/A = -(Vy^2/2A) Max Height: y = Vy^2/(-2A)
Now, any combination of Vy and A (as long as Vy>0 and A<0) that give the given MAX_HEIGHT is valid.Code:MAX_HEIGHT = (Vy^2)/(-2A) MAX_DIST = Vx*t = (Vx * 2Vy)/(-A)
Use this combination in MAX_DIST and find a suitable value for Vx to get the given MAX_DIST.
I hope i have this right... highschool has been while.. :-)
(See also http://mathworld.wolfram.com/Parabola.html (but reverse the X and Y axis))
Last edited by aspaans; 2004-03-03 at 16:48.
how to impliment it in game canvas ?
can u give one sample code .
because i also got stuck with this issue.....
i m trying to throw a ball based on force & direction (user have the option to select force and direction)
See if this thread helps.
hehe,
why dont u make the game for us also ?
seriuosly :
i m trying to throw a ball based on force & direction (user have the option to select force and direction)
Well the force u could set up a formula which is relational to the supplied formula for the distance :
MAX_DIST = Vx*t = (Vx * 2Vy)/(-A)
u might want to say that twice the used force twice the distance or make up an own proportion between the two variables...
And about the direction, which obviously translates to an angle.. U can use this angle to deifine the height reached by the ball. Unfortunately u cant use the supplied formulae because (if im not mistaken!) it is for throwing a ball in the air without defining an angle...
and how to design it on the canvas : well u got documentation for that, I guess u'd like to draw an arc so give al ook at the graphics.drawarc method...
anyways google is ur best friend :
look for parabolic trajectory
http://www.opencollege.com/simsim/ph...c%20trajectory
http://en.wikipedia.org/wiki/Trajectory
http://hyperphysics.phy-astr.gsu.edu...raj.html#tra12
hundreds more after 10 secs of searching...
off-topic : didnt u have a B.Sc Mathematics ? this all should be common knowledge then ?
Last edited by Tiger79; 2008-09-05 at 09:51.