Friday, April 27, 2018

c# - Adding air drag to a golf ball trajectory equation


I'm developing a 2D golf game in VB.NET 2005, but I am stuck on how to implement air or wind drag that should affect the ball.


Already I have these equations for projectile:




  • $v_0$ for the initial velocity of a golfball when hit or fired


  • Vertical and horizontal components the velocity of the golfball: vx=v0cos(θ)vy=v0sin(θ)gt




  • Vertical and horizontal distance of golfball: x=v0cos(θ)ty=v0sin(θ)t(0.5)gt2




How do I add air drag to this equation to properly affect the velocity of the golf ball? I don't have any idea how to do it, has anyone worked with similar equations?




Answer



I'm not sure if there even exists a closed form for drag or wind, but it is quite easy to simulate in a step-wise fashion (like all the physics libraries do):




  1. set your initial condition:


    x,y,vx,vy(for t=0)




  2. update position:


    x=x+(vx×dt)y=x+(vy×dt)



    (where dt is the time elapsed since the last update, aka delta time)




  3. calculate these velocity helpers:


    v2=(vx)2+(vy)2|v|=v2


    (where $\lvert v \rvert$ represents the length of $v$)




  4. calculate drag force:


    fdrag=c×v2



    (where c is the coefficient of friction small!)




  5. accumulate forces:


    fx=(fdrag×vx|v|)fy=(fdrag×vy|v|)+(g×mass)


    (where $mass$ is the mass of your golf ball)




  6. update velocity:


    vx=vx+fx×dtmassvy=vy+fy×dtmass





That's basically Euler's Method for approximating those physics.




A bit more on how the simulation as requested in the comments:



  • The initial condition $(t = 0)$ in your case is


x=0y=0vx=v0×cos(θ)vy=v0×sin(θ)


It's basically the same as in your basic trajectory formula where every occurrence of t is replaced by 0.





  • The kinetic energy $KE = 0.5m(V^2) $ is valid for every $t$. See $v^2$ as in (3) above.




  • The potential energy $ PE = m \times g \times y $ is also always valid.




  • If you want to get the current $(x,y)$ for a given $t_1$, what you need to do is initialize the simulation for $t = 0$ and do small dt updates until $t = t_1$





  • If you already calculated $(x,y)$ for a $t_1$ and you want to know their values for a $t_2$ where $t_1 \lt t_2$, all you need to do is calculating those small dt update steps from $t_1$ to $t_2$




Pseudo-Code:


simulate(v0, theta, t1)
dt = 0.1
x = 0
y = 0
vx = v0 * cos(theta)

vy = v0 * sin(theta)
for (t = 0; t < t1; t += dt)
x += vx * dt
y += vy * dt
v_squared = vx * vx + vy * vy
v_length = sqrt(v_squared)
f_drag = c * v_squared
f_grav = g * mass
f_x = (-f_drag * vx / v_length)
f_y = (-f_drag * vy / v_length) + (-f_grav)

v_x += f_x * dt / mass
v_y += f_y * dt / mass
end for
return x, y
end simulate

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...