2
\$\begingroup\$

I'm working on a game in LOVE2D using Lua, where you pilot a drilling vehicle. The idea is that as you drill into different surfaces, you speed up/slow down depending on how dense that surface is. I'm simulating it using drag forces, something like this:

engineForce = engineForce + userInput * dt -- increase the engine speed based on user input
dragForce = .25 * dragCoefficient * speed^2 -- a simple implementation of the drag equation
acceleration = engineForce - dragForce
speed = speed + acceleration*dt

What's supposed to happen is velocity will increase until dragForce = engineForce and it maintains speed. This mostly works except occasionally it gets into a weird feedback loop where speed overshoots slightly (not sure if it's due to a rounding error or what) so drag becomes stronger than the engine causing it to decelerate. But because it decelerated, drag decreases and now the engine is stronger than drag, causing it to accelerate again. And again it will overshoot the target, causing the process to repeat again.

Does anyone know best practices for programming physics like this?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

in case anyone's curious, I think I figured it out. I calculated a "terminal velocity" for a given engineForce, that is the speed where drag and the engine would balance out (in this case that's sqrt(4*engineForce/dragCoefficient)) and cap your velocity at that. So far, it seems to work!

\$\endgroup\$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .