Quadratic Equation
Hey all,
I´m working on a soccer shootout game. The ball is at the bottom of the screen and the goal is at the "back".
When the user clicks the ball i check the percentage of left and top from the center and make the ball go to the same percentage of left and top of the goal. But the animation is a little rough. I want to make a more curve animation. That´s why i need to find a way to represent y-axis in function of x-axis through a time t so the ball make a more nice movement.
I have the following info:
gtop - goal top y
gbot - goal bottom y
gleft - goal left x
gright - goal right x
ballx - ball center x
bally - ball center y
balltop - ball top y
ballbot - ball bottom y
ballleft - ball left x
ballright - ball right x
Just to make it clearer. This is what it looks like:
|----------------------------|
| |
| |
| |
| |
| |
O
then the user clicks the ball and it makes it´s way to the goal in t seconds.
Thanx for the help,
Bernardo Heynemann
Developer @ Nigraph Tech
Veni, Vidi, Vici
(Julius Caesar)
Veni, Vidi, Vici(Julius Caesar)
What kind of curve? Why would a kicked soccer ball curve? (I know that air friction can do that, but not in a goal kick)
Use the
Cédric
Use the
and
tags to do your ASCII art (your goal diagram). Cédric
It must do a curve because it goes higher til the place where it enters the goal and then lose altitude. If you know what i mean... I want to make it as real as possible...And a straight line from the ball to where it goes is not really real. I tried it.
The diagram is:
Thanx anyway,
Bernardo Heynemann
Developer @ Nigraph Tech
The diagram is:
|---------------------------|| || || || || | 0
Thanx anyway,
Bernardo Heynemann
Developer @ Nigraph Tech
Veni, Vidi, Vici(Julius Caesar)
distance = velocity * time
Therefore:
xDistance = cos(Angle)*Velocity*Time
yDistance = sin(Angle)*Velocity*Time - g*Time
(g == accelleration due to gravity)
This is a lot easier to work with than using a quadratic. This is how we did all of our 2D movement problems in my physics class (high school). If you want to add in air resistance I think it you should subtract it from the x, and add it to the y. I''m not sure of this though, I just thought of it now. I don''t know how you would calculate air resistance, though.
Proceeding on a brutal rampage is the obvious choice.
Therefore:
xDistance = cos(Angle)*Velocity*Time
yDistance = sin(Angle)*Velocity*Time - g*Time
(g == accelleration due to gravity)
This is a lot easier to work with than using a quadratic. This is how we did all of our 2D movement problems in my physics class (high school). If you want to add in air resistance I think it you should subtract it from the x, and add it to the y. I''m not sure of this though, I just thought of it now. I don''t know how you would calculate air resistance, though.
Proceeding on a brutal rampage is the obvious choice.
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
Actually, the paramtetric equation is:
x(t) = cos(A) * V * t
y(t) = sin(A) * V * t - g/2 * t^2
V is the velocity, A is the angle at which it is kicked, g is the acceleration due to gravity (which can be arbitrary in a game as long as it looks good), t is the time, and (x, y) is the ball''s position. However, most of the time, you''re only going to want to know the tiny deltas (i.e. changes) between frames. Call Dt the difference in time between the frames (1/60 for 60fps, etc.), and call Dx and Dy the x and y differences. Suppose Xv and Yv are the current x- and y-velocities, and Dxv and Dyv are the changes in x- and y-velocities. When the ball is first kicked:
Xv = cos(A) * V
Yv = sin(A) * V
Then after that:
Dxv / Df = -k * Xv
Dyv / Df = -k * Yv - g
Dx = Xv
Dy = Yv
k is the air resistance coefficient. When there is no air resistance (i.e. space or a friction-less environment), k = 0. Otherwise, k > 0. Typically, really small values of k work well (i.e. less than 0.1). The larger k is, the more air resistance there is. Anyways, to implement this in code:
x(t) = cos(A) * V * t
y(t) = sin(A) * V * t - g/2 * t^2
V is the velocity, A is the angle at which it is kicked, g is the acceleration due to gravity (which can be arbitrary in a game as long as it looks good), t is the time, and (x, y) is the ball''s position. However, most of the time, you''re only going to want to know the tiny deltas (i.e. changes) between frames. Call Dt the difference in time between the frames (1/60 for 60fps, etc.), and call Dx and Dy the x and y differences. Suppose Xv and Yv are the current x- and y-velocities, and Dxv and Dyv are the changes in x- and y-velocities. When the ball is first kicked:
Xv = cos(A) * V
Yv = sin(A) * V
Then after that:
Dxv / Df = -k * Xv
Dyv / Df = -k * Yv - g
Dx = Xv
Dy = Yv
k is the air resistance coefficient. When there is no air resistance (i.e. space or a friction-less environment), k = 0. Otherwise, k > 0. Typically, really small values of k work well (i.e. less than 0.1). The larger k is, the more air resistance there is. Anyways, to implement this in code:
// Global variables:float X = 0.0f, Y = 0.0f, Xv = 50.0f, Yv = 50.0f; // These values are just fillers. You would set up the real values earlierDWORD lastTime = timeGetTime();float airResistance = 0.05f;float gravity = 1.0f;// This is inside your main game loop:DWORD currentTime = timeGetTime();DWORD Df = currentTime - lastTime;Xv += (-airResistance * Xv) * (double)Df;Yv += (-airResistance * Yv - gravity) * (double)Df;X += Xv;Y += Yv;lastTime = timeGetTime();
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement