I've scored the internet for sources and have found a lot of useful information, but they are math sites trying t开发者_如何学Co tell me how to solve what angle an object has to be at to reach y location. However, I'm trying to run a simulation, and haven't found any solid equations that can be implemented to code to simulate a parabolic curve. Can those with some knowledge of physics help me on this?
While Benny's answer is good, especially in its generality, you can solve your problem exactly rather than using finite integration steps. The equation you want is:
s = u*t + 0.5*a*t^2;
Look here for an explanation of where this comes from.
Here s is the displacement, u is the initial speed, a is the acceleration and t is time. This equation is only 1 dimensional, but can be easily used for your problem. All you need to do is split the motion of your projectile into two components: one parallel to your acceleration and one perpendicular. If we let Sx describe the displacement in the x direction and Sy the displacement in the y direction we get:
Sx = Ux*t + 0.5*Ax*t;
Sy = Uy*t + 0.5*Ay*t;
Now in your particular example Ax is 0 as the only acceleration is due to gravity, which is in the y direction, ie Ay = -g. The minus comes from the fact that gravity will be acting in the opposite direction to the original motion of the object. Ux and Uy come from simple trigonometry:
Ux = U*cos(angle);
Uy = U*sin(angle);
Putting this all together you get two equations describing where the projectile will be at a time t after being launched, relative to its starting position:
Sx = U*cos(angle)*t;
Sy = U*sin(angle)*t - 0.5*g*t^2;
Don't use the equations for position. Instead, use the equations for velocity. Calculate the new velocity each loop of your simulation from the object's old velocity and apply it to your object. You will need to know the elapsed time between each loop of the simulation. Of course, this works for vertical or horizontal velocity.
v_new = v_old + acceleration * delta_time (from wikipedia)
Then apply:
position_new = position_old + v_new * delta_time;
You can use a simple acceleration of -9.8 m/s
(don't forget that "down" on the screen is really an increase in the vertical position! So you can use +9.8
for simplicity). Or you could get fancy and add variable acceleration (for example, from wind, if you are also modeling the horizontal motion of the object).
Basically, the acceleration you apply is based on the sum of forces applied to the object (force of gravity, friction, jet propulsion, etc.).
F_final = F1 + F2 + ... + Fn
The following can help you with that.
If you are modeling a force applied to the object, first break the force into it's horizontal and vertical components using:
F_horiz = F * sin( angle )
F_vert = F * cos( angle ) where angle is the angle between the force and the horizontal.
Then calculate the acceleration from the force using:
a = F / mass
(I give all credit for this knowledge to my first programming experience: GORILLA.BAS =) )
Some definitions:
x = x-coordinate (horizontal)
y = y-coordinate (vertical)
Vx = x-velocity
Vy = y-veloctiy
t = time
A = initial angle
V0 = intial velocity
g = acceleration due to gravity
Some equations:
Vx = V0*cos(A)
Vy = V0*sin(A) - g*t
x = V0*cos(A)*t
y = V0*sin(A)*t - (1/2)*g*t^2
Heres a nice library that might help you
http://sites.google.com/site/physics2d/
I haven't looked into it too much to be honest, I came across it in Scott Whitlock's code project article.
http://www.codeproject.com/KB/WPF/SoapBoxCorePinBallDemo.aspx
精彩评论