1

I'd like to start writing and incorporating my own 3D physics engine in to my game engine, and after doing a little research in to what one might need to have a firm grasp of before doing such a thing it started to seem a little daunting.

I've found that apparently Classical Mechanics (Goldstein and friends) is a great book on classical/Newtonian mechanics which is where I think I would like to end up, but I'm sure that if I started diving in to it I would be overwhelmed, not understand much, and ultimately lose motivation.

I think my intuition for mathematics in general is okay but I never really applied myself very much in school until it was too late and I didn't have the foundations to carry me. I have heard that mathematical methods (I'm not even confident I know what that is) and calculus are good to know before diving in to classical mechanics but it would really help to gather opinions on what the best route forward is. I'm in no rush and I'm eager to learn, but I'm just worried about reading the wrong material or material I'm not ready for and then being put off.

Qmechanic
  • 201,751
  • 5
    Aren’t there dozens of open-source physics engines? If you don’t know physics or math, why do you think you could or should write a better one? Would it not be better to focus your energy on the game and not on the engine? – G. Smith Nov 20 '20 at 17:45
  • @G.Smith Ah good question. It's all for fun. Like I said I'm in no rush, so the parts of the engine that I find interesting I would like to try and write myself, i.e: rendering, audio, text, physics, math utilities, etc. – georgeous Nov 20 '20 at 18:01
  • Assuming 2 hours of daily effort: learn differential calculus and Newton's mechanics means from 2 to 6 years of effort (it depends of your initial level on basic math). Sorry for the bad news. – pasaba por aqui Nov 20 '20 at 18:44
  • One place to start is to dive into an open-source physics engine and see what it does... – Jon Custer Nov 20 '20 at 18:50
  • @JonCuster: if the engine is well documented, these docs will need theoretical physics to be understood. If it is not, inverse engineering of the code could need years of effort. – pasaba por aqui Nov 20 '20 at 19:02
  • @pasabaporaqui - or you spend years learning all of physics and then dive into the code, needing years of effort... A nice mix of an intro physics text and some code spelunking is likely a good middle ground. – Jon Custer Nov 20 '20 at 19:09
  • @Jon I think you're right that it will take a mixture. I am a programmer by trade so I think I will be able to suss out how to get started and where to go for that side of the problem. It's really just the mathematical study I feel unsure about. As I mentioned in other comments I think I framed this question poorly, and I think that a better question would be something like "I'm interested in learning about bodys in motion, forces, collision, etc. and I have little academic background in mathematics." – georgeous Nov 20 '20 at 19:17
  • @georgeous There is a book "Game Engine Architecture" by Gregory (from Naughty Dog studios, I believe) which covers physics engine implementation in detail. For specifics about engines rather than general physics (including the computational shortcuts), this would likely be your best bet. – Richard Myers Nov 20 '20 at 20:56
  • @sammy Not quite but thank you. – georgeous Nov 20 '20 at 22:22
  • @Richard I have the second edition on my shelf and yes the chapter on physics is a good overview of the design of a physics simulation system for a game engine. It's great for - as you said - learning about the more practical ways you might implement the physics for 3D games but I am more interested in the fundamental mechanics that these more computational-friendly solutions are derived from. – georgeous Nov 20 '20 at 22:32
  • Actually, the physics is relatively simple - optimising the collision detection algorithm (based on geometric methods) is probably the largest time sink, followed by combatting numerical inaccuracies. – Nihar Karve Nov 21 '20 at 02:53

1 Answers1

3

The answer depends greatly on what you want to do with this physics engine. To make a "reasonable" one, $F=ma$ and $\tau=I\omega$ are really all you need.

The answer gets squirley from there.

First off, most game physics engines are horrible physics emulators. This is because their goal is not to accurately predict what will happen in a system. Their goal is to make the player feel like the physics is real while staying in the confines of the processing power available to the engine. Quite often the physics is flat out wrong -- not by accident but by intent. Often the wrong physics is far easier to compute while still being "good enough" for human eye balls.

Other physic engines are more accurate. It should be no surprise that flight simulators tend to get the physics of flight more correct than other engines! If you are making a flight simulator, you probably will want to brush up on your aerodynamic physics. But even here, you will find assumptions made to simplify things. Most flight simulators are not very accurate on the precipice of a stall, where the fluid dynamics over the wing can get complicated enough to require computational fluid dynamics (CFD), which does not get done well in real time!

The most common thread in all of these engines will not be physics based at all, but practical. You will compute the physics using floating point numbers, and there are numeric issues such as roundoff that arise from doing this repeatedly. You will spend time making sure that your systems don't blow up when this occurs (I've had more than one infinite loop occur because I can have two finite floating point numbers, $a$ and $b$, such that $b>0$ and find that $a+b=a$ because floating point addition is approximate.) You will also find many issues arise with the finite numeric techniques you have to use because computers can't use real numbers and continuous operations. More than one game puzzle has been defeated by a player that figures out how to move something fast enough to break the collision detection algorithm and walk right through a wall.

If you really want to build an engine (and I'd second G. Smith's comment about using an open source one), I'd look into how physicists and engineers structure their problems to avoid these numeric instabilities.

Cort Ammon
  • 48,357
  • 1
    +1 - great answer. Finding the sweet spot between accuracy and speed is definitely the biggest problem in practice. A physics engine that can simulate the path of every snowflake but takes an hour to render each frame may be just what you need for movie animation but is no use in a game engine. – gandalf61 Nov 20 '20 at 17:56
  • @CortAmmon Thank you for that. I see what you're saying. As gandalf mentioned in the end it will be about finding that sweet spot, but my worry is that I have no idea how to find that because I have no understand of the "accuracy" end of the spectrum. I should probably have mentioned that I am also just interested in learning classical mechanics in general, so I'm not too hung up on the most practical way I can go about writing a physics engine. :-) – georgeous Nov 20 '20 at 18:08
  • "The most common thread in all of these engines will not be physics based at all": downvote. If a person that recognizes near zero level in physics and maths says he wants to write a "physics simulator", really you think the biggest problem he will have is float numbers precision ? – pasaba por aqui Nov 20 '20 at 18:14
  • @georgeous what kind of physics do you think you would be incorporating? Most games are just rigid body propagation, and every now and then they have something simple like ragdoll physics. For game engines in general, I'd recommend reading about collision detection and a flock model like Boids. I think those two topics would help you better identify what questions you really would like to ask – Cort Ammon Nov 20 '20 at 18:14
  • 2
    @pasabaporaqui As someone who has programmed them, yes. Yes I do. The physics can be learned from papers easy enough. But numeric issues plague even the best physics simulators. (I'm going to add an edit to point out that roundoff is just one of the numeric issues I was thinking of. I don't think I made that clear) I think I can explain all of the physics in most modern games in about 10 minutes with a whiteboard. The implementation of said physics... that's where it gets tricky. – Cort Ammon Nov 20 '20 at 18:16
  • @CortAmmon Okay I think I framed my question poorly (first time, sorry). While my end goal is to write a real-time physics engine I would much prefer to do so with a firm understanding of the classical mechanics that the engine code would be derived from, rather than learn just enough to get me by. I guess the physics engine context was used by me to give people an idea of my motivation for wanting to learn this stuff. – georgeous Nov 20 '20 at 18:19
  • Assuming 2 hours of daily effort: basic math, differential calculus, Newton's mechanics, ... : from 2 to 6 years study (it depends on start point). Compare it with the study of numerical methods, 4 months (1 week for float numbers issues in computation). – pasaba por aqui Nov 20 '20 at 18:22
  • 1
    @pasabaporaqui Given that georgeous has pointed out that their goal is to learn physics, you are probably right. When it comes to game engines you do not need anywhere close to that much time on studying the underlying math and physics, but the practical implications of coding will always be present. Most game engine topics I've seen take a few minutes to understand the basic principle, and then tons of research to understand all the numeric hacks that were required to make it actually work. – Cort Ammon Nov 20 '20 at 18:34
  • 1
    @CortAmmon In my research I saw that most people claimed writing a physics engine is non-trivial mainly for the reasons that the prerequisite mathematical knowledge can be quite a lot and there was less mention about the technical gotchas (though I'm sure that's certainly a hurdle). It didn't seem as trivival as you're making it sound. Collision detection is one thing, but then collision resolution with multiple bodys seems like a whole different game. Maybe this kind of stuff is trivial to you but for me I wouldn't know where to start. – georgeous Nov 20 '20 at 19:22
  • @georgeous Interesting. It'd be interesting to look at what features they are thinking of in a physics engine. Many engines don't do anything that requires more than basic algebra, though understanding why that is sufficient may require differential equations. From a physics perspective, an engine such as Unreal doesn't really do much that isn't taught in highschool physics. From a computer science and a math perspective, it can do quite a lot more, but the physics is really just glorified F=ma. – Cort Ammon Nov 20 '20 at 20:14
  • Take the smoke demo from NVidia as an example. That requires very little physics. You have to learn Navier Stokes, which are just a few equations. But solving those equations requires a great deal of math (not necessarily physics) to construct an iterative solver like in this paper. But the thing that made that paper worthy was not the math. It was that it handled all... – Cort Ammon Nov 20 '20 at 20:19
  • ... of the computer science juggling required to reformulate that solver in a way that fit the hardware. So lots of differential equations and numeric shenanigans... but just a few physics equations. – Cort Ammon Nov 20 '20 at 20:19
  • Kerbal Space Program doesn't really have any physics you didn't learn in high school, stitched together with calculus and differential equations. But the mathematical and computer science challenges with constructing that engine are daunting! – Cort Ammon Nov 20 '20 at 20:25
  • @CortAmmon Some really interesting points and the smoke demo example is great. I mentioned to Jon earlier that perhaps a better way to phrase my question might be "I'm interested in learning about bodys in motion, forces, collision, etc. and I have little academic background in mathematics." I think I have to admit this was not a great question! But this discourse has definitely helped a lot. Would I be able to pick your brains in chat about something a bit more general? – georgeous Nov 20 '20 at 20:39
  • @georgeous Sure, if you can get it into chat mode. Sometimes its hard to get the stack exchange algrotihm to offer the "convert to chat" button. – Cort Ammon Nov 20 '20 at 20:41
  • On a related note, I would recommend putting the most energy into understanding vectors and caluclus. They are some of the favorite tools found in physics,so they keep showing up again and again, everwhere you look. – Cort Ammon Nov 20 '20 at 20:48
  • @CortAmmon I actually forgot SE doesn't have private chat so nevermind :-) Calculus keeps popping up so it sounds like that is a good place to start. I'm well enough versed in vectors for now so hopefully that at least will be of some aid. – georgeous Nov 20 '20 at 22:34
  • "most game physics engines are horrible physics emulators." As they say on TVTropes, Reality Is Unrealistic. ;) – PM 2Ring Nov 21 '20 at 01:41
  • Learning how to do numerical integration correctly on a computer definitely takes some time. In simple cases, you can get away with using the naive Euler method, but generally you need something that converges faster, and is less prone to problems caused by the inevitable rounding errors. And for physics, you generally want your system to obey the law of conservation of energy, so you need a symplectic integrator like Leapfrog or Verlet. – PM 2Ring Nov 21 '20 at 01:51