Advertisement

Programming physics with formulas and equations

Started by September 14, 2020 09:48 PM
15 comments, last by taby 4 years, 2 months ago

You're welcome!

If you do decide to go the C++ route, definitely get The C++ Programming Language 4th Edition, or whatever is newest. There is also STL Tutorial and Reference Guide 2nd Edition. Together, you will learn all there is to know about how to become a proficient C++ programmer. I used to own these books, but I lost them. :( The book I have now is Professional C++, 5th Edition. Covers awesome stuff like multithreading.

Is it possible to use mathematical formulas and equations from real life physics and use it in my game through programming?

Yes! This is what every modern game engine does.

The problem, though, is that the formula is not contiguous. The ball, flying through the air, is affected by momentum, gravity, spin, and air resistance (which in turn might be affected by velocity and spin, depending on how detailed you want to make the model.)

But, as soon as the ball makes contact with the ground, or the net, or a racket, the physical interaction between the ball and those objects, needs to be considered. This causes a discontinuity in the simulation. You need to essentially stop the simulation of “ball flying on its own” and apply a simulation of “ball being hit by racket.” Until it leaves contact with the racket.

Further, for any object controlled by a player, every single frame of simulation may have a discontinuity, because the player could have added new input (moved the mouse / joystick, pressed a button, …)

So, what a physics engine does, is encode a bunch of physical equations (restitution, air drag, angular momentum, etc) togther with a bunch of geometric algebra equations (for whether a squished ball is in contact with a moving racket or not, etc,) and, for each simulation step, chooses which particular physical equations to apply, and simulates until the next interaction (or, more commonly, the next display frame.)

On a modern computer, simulating the air resistance from a rotating ball, and the deformation of the ball when it hits objects, and even the deformation of the tennis racket web of strings, is totally tractable, even on a lowish-end laptop. However, if you have, say, a hundred such balls, and a hundred such rackets, that lowish-end laptop wouldn't have enough CPU power to simulate all of that in real time. Hence, game physics that focus on “many objects” rather than “a few, very accurate objects,” end up taking shortcuts – balls don't deform, air drag is only dependent on velocity and a fixed constant, etc.

enum Bool { True, False, FileNotFound };
Advertisement

hplus0603, Thank you very much for your help, I recently started reading some books on physics (mainly those that recommended me on this topic) and I am already understanding better and trying to do some experiments inside Unity.

I think someone can close this topic now, I just wanted to thank everyone for their responses and book recommendations.

Emanuel Messias, R.d.S Brazil, São Paulo

Topics don't need closing! Your thanks is sufficient closure. When someone later has the same question, hopefully Google will oblige and point to this thread.

enum Bool { True, False, FileNotFound };

Also:

do you recommend a symplectic integrator, versus RK4

The problem with RK, is that you need the derivatives. And, for it to be energy conserving, you need the derivatives to be contiguous. But you don't know what the future will look like, because the player may affect the scene such that the “next step” is not going to be what it currently might look like! You also don't know whether the current step will affect the trajectory enough to just-hit or just-miss some collider in the way of the moving object.

I early in my career worked on an engine that used a “RK2 integrator!” But, actually, it derived the derivatives using finite differences in each time step, in a which made it equivalent to forward Euler. Spend four times the CPU cycles for none of the benefit! (At the time, I was not sure enough of myself to just go in and fix it, one way or the other, but looking back, I should have. Live and learn!)

Note that Symplectic first order is just Euler, which is not energy conserving unless you dampen it. Sumplectic second order is Verlet, which is energy preserving, and also has other nice properties, including efficiency of implemented code. I remember reading a good article by Jakobsen about the Verlet integrator used in the early Hitman games.

enum Bool { True, False, FileNotFound };

Nice. Thanks for reminiscing. Does symplectic order 4 have a nickname too?

All I know is that I simulated the orbit of Mercury around the Sun, using Euler integration, and it was not even close to accurate. Symplectic order 4 did the trick.

This topic is closed to new replies.

Advertisement