How can i get a parabolic path when two end coordinates given along with the maximum height. To draw on a canvas.
Printable View
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:
[code]
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)
[/code]
normalize it where Vx = 1, so divide all by Vx
[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.
[/code]
First, see where the ball touches the ground:
I remember this problem ...
[code]
P*t^2 + Q*t + R = 0,
where P=A/2Vx, Q = Vy/Vx, R = 0
[/code]
has the solution..
[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)
[/code]
The highest point is reached when the vertical speed reaches zero:
[code]
0 = A*t + Vy ==> t = -Vy/A
[/code]
Fill this into the original equation to get the highest/max height:
[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)
[/code]
So here are the max height and the distance you throw:
[code]
MAX_HEIGHT = (Vy^2)/(-2A)
MAX_DIST = Vx*t = (Vx * 2Vy)/(-A)
[/code]
Now, any combination of Vy and A (as long as Vy>0 and A<0) that give the given MAX_HEIGHT is valid.
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 [url]http://mathworld.wolfram.com/Parabola.html[/url] (but reverse the X and Y axis))
[B]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)[/B]
See if [url="http://discussion.forum.nokia.com/forum/showthread.php?t=141896"]this thread[/url] helps.
hehe,
why dont u make the game for us also ? ;)
seriuosly :
[I]i m trying to throw a ball based on force & direction (user have the option to select force and direction)
[/I]
Well the force u could set up a formula which is relational to the supplied formula for the distance :
[I]MAX_DIST = Vx*t = (Vx * 2Vy)/(-A)[/I]
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 ;)
[url]http://www.opencollege.com/simsim/php/ResourceManager.php?cmd=get_view&catID=1122&resID=485&rcatpath=Mathematics/Function%20Algebra/Parabolic%20trajectory[/url]
[url]http://en.wikipedia.org/wiki/Trajectory[/url]
[url]http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html#tra12[/url]
hundreds more after 10 secs of searching...
off-topic : didnt u have a B.Sc Mathematics ? this all should be common knowledge then ?