Building a Physics Engine with C++ and Simulating Machines
Summary
TLDRIn this video, the creator unveils a physics engine developed over weeks, initially part of a larger project but now a standalone codebase for future reuse. The engine simulates physical principles using a system state object, force generators, time discretization, and a differential equation solver. It employs Euler's method and the more advanced Runge-Kutta method for stability. The creator demonstrates basic mass-spring systems, cloth simulation, and repulsion forces in a game environment. The video also covers constraint-based physics, showcasing complex systems like double and triple pendulums, and the importance of spring stiffness. The creator's homemade screen capture program is highlighted, emphasizing the video's focus on physics simulation for game development.
Takeaways
- 🛠️ The video introduces a custom physics engine developed by the creator for use in game projects.
- 🔧 Initially, the physics engine was a small part of a larger project but was later developed into a standalone codebase for future reuse.
- 🎮 The physics engine is essential for simulating physical reality in games, as showcased with a sentient cereal box affected by gravity and reaction forces.
- 📐 The engine uses a system state object to track object properties like position, velocity, and orientation.
- 💨 Force generators are employed to apply forces such as gravity or springs to objects within the scene.
- ⏱️ Time discretization is crucial for simulating continuous time values in a digital environment, with smaller time steps leading to more accurate results.
- 🔢 The differential equation solver, such as Euler's method, is used to update an object's position and velocity, simulating acceleration.
- 🚀 An improved solver, the Runge-Kutta method (RK4), offers greater accuracy and stability over Euler's method, requiring fewer iterations for the same simulation quality.
- 📹 The creator also developed a custom screen capture program integrated into the demo for high-quality video encoding without lag.
- 🔗 Constraints are used to simulate more complex physical interactions, such as maintaining a circle's position or simulating a rolling object.
- 🔍 The physics engine's constraint solver uses a matrix equation to find the forces needed to satisfy multiple constraints simultaneously.
- 🤖 The video includes demos of the physics engine simulating various systems, including mass-spring systems, pendulums, and complex mechanical assemblies.
Q & A
What was the original purpose of the physics engine mentioned in the video?
-The physics engine was initially intended to be a small part of a larger project, but the creator found it useful enough to be developed into its own code base for future reuse in similar physics simulations.
Why is simulating physics important in game development?
-Simulating physics is crucial in game development because it allows games to approximate some form of physical reality, making the gameplay more realistic and engaging for players.
What is a 'system state object' in the context of a physics engine?
-A system state object is a simple structure that tracks various properties of objects in the simulation, such as position, velocity, and orientation.
What is the role of a 'force generator' in a physics engine?
-A force generator is responsible for applying forces to objects in the scene, such as gravity or spring forces, and is essential for simulating realistic interactions between objects.
Why is time discretization necessary in computer simulations?
-Time discretization is necessary because computers cannot handle continuous values of time. By breaking the simulation into discrete time steps, it becomes computationally manageable.
What is Euler's method, and how is it used in physics simulations?
-Euler's method is a numerical approach for solving differential equations. It is used to update an object's position and velocity in a simulation by adding the product of velocity and time step to the position, and the product of acceleration and time step to the velocity.
What are the limitations of Euler's method in sensitive systems?
-Euler's method can introduce significant errors in sensitive systems, causing the simulation to become unstable or 'explode'. It requires very small time steps to maintain accuracy, which can be computationally expensive.
What is the Runge-Kutta method (RK4), and how does it improve upon Euler's method?
-The Runge-Kutta method, specifically RK4, is a more advanced numerical technique that takes a weighted average of four points instead of a single point like Euler's method. This results in a more stable and accurate simulation with fewer iterations required.
What is a 'constraint' in the context of a physics engine, and how is it used?
-A constraint in a physics engine is a function that evaluates to zero only when a physical constraint is satisfied. The goal is to apply forces in such a way that all constraint functions are zero, ensuring realistic behavior under the constraints.
What is the purpose of the 'differential equation solver' in a physics engine?
-The differential equation solver is responsible for updating the state of the system over time by solving differential equations that describe the motion of objects under the influence of applied forces.
How does the video creator handle the complexity of simulating more advanced systems like a double pendulum?
-The creator uses a constraint solver system, which involves setting up and solving matrix equations to find the forces required to satisfy all constraints simultaneously, allowing for the simulation of more complex and realistic systems.
Outlines
🛠️ Physics Engine Development
The creator introduces a physics engine they've developed over several weeks, initially intended as a small part of a larger project but later realized to be useful on its own. The engine simulates basic physical principles like gravity and reaction forces, as demonstrated by a sentient cereal box in a game. The physics are not sophisticated but serve as a foundation. The video explains the necessity of physics in games to approximate physical reality and the creator's journey to build a more accurate physics engine, starting with a system state object to track object properties and a force generator for applying forces like gravity or springs. The importance of time discretization in simulations for computer applications is discussed, and Euler's method is introduced as a basic numerical approach for solving differential equations, though it's acknowledged to be error-prone and computationally expensive.
🔍 Advanced Simulation Techniques
The video delves into the use of the Runge-Kutta 4th order method (RK4) for improving simulation accuracy and performance, allowing for a more stable simulation with fewer iterations per second. The creator shares a basic interface with demos using springs and masses to illustrate the engine's capabilities. They also discuss the creation of a custom screen capture program due to issues with OBS, which integrates directly with the demo for smooth video encoding. The creator demonstrates the mass-spring system's energy conservation and its application in simulating cloth, as well as the use of force generators for simple game physics, like repulsion forces and AI movement. However, the limitations of the spring-based approach are acknowledged, leading to the introduction of constraints for more complex simulations.
🔧 Implementing Constraints in Physics Simulations
The video explains the concept of constraints in physics simulations, which are functions that evaluate to zero when a physical constraint is satisfied. The creator discusses the process of finding reaction forces to satisfy multiple constraints simultaneously, which can be formulated as a matrix equation. Gaussian elimination is used to solve this equation, though more advanced algorithms may be necessary for future projects. The implementation of constraints as classes with their own calculation methods is described, along with the creation of a master Jacobian matrix for solving the constraints. The video showcases demos using the constraint solver system, including a double pendulum and a triple pendulum, highlighting the importance of numerical stability and accuracy in simulations. The creator also discusses the challenges of implementing rolling constraints and the importance of matching spring stiffness to specific applications. The video concludes with a demo of a complex system with many moving parts, emphasizing the use of triangles to create rigid structural members, similar to real-world machine design.
Mindmap
Keywords
💡Physics Engine
💡System State Object
💡Force Generator
💡Discretization
💡Differential Equation Solver
💡Euler's Method
💡RK4 (Runge-Kutta 4th Order Method)
💡Constraints
💡Jacobi Matrix
💡Gaussian Elimination
💡Optimization
Highlights
Introduction of a physics engine developed for a larger project but found to be useful by itself.
The physics engine is designed to be reusable for future projects requiring similar physics simulations.
Explanation of the necessity for physics simulation in games to approximate physical reality.
Demonstration of a sentient cereal box game affected by gravity and reaction forces.
The physics implemented are not sophisticated but functional, highlighting the need for a more advanced engine.
Introduction of a system state object to track object properties in a physics engine.
Description of a force generator for applying forces like gravity or springs to objects in the scene.
Time discretization in simulations to make continuous real-world time compatible with digital systems.
The use of time steps in simulations and their relation to video frame lengths.
Introduction of the differential equation solver, a fundamental component of a physics engine.
Explanation of Euler's method for solving differential equations with its limitations.
Introduction of the Runge-Kutta method (RK4) as an improvement over Euler's method.
Demonstration of a basic mass-spring system and its energy conservation properties.
Use of mass-spring systems to simulate cloth in games with its limitations.
Application of force generators to create simple game physics like repulsion forces.
Introduction of constraints in physics engines to simulate more complex and accurate physical interactions.
Explanation of how constraints are implemented and solved in a physics engine using matrix equations.
Description of the process of solving for constraint forces using Gaussian elimination.
Demonstration of various demos using the constraint solver system, including a double pendulum.
Discussion on the importance of matching spring stiffness to the application in simulations.
Showcasing complex system simulations using triangles to create rigid structural members.
Acknowledgment of the non-optimized code for clarity and ease of debugging.
Teaser for future projects using the physics engine and ongoing work on a serial adventure game.
Transcripts
hey what's up guys and welcome back to
another video
what you see on the screen right now in
front of you is a physics engine that
i've been working on for the past few
weeks and it was originally meant to be
a small part of a bigger project
but i realized that it was actually kind
of useful by itself so i took all the
physics code
and i made it into its own code base and
now anytime that i need similar physics
in the future i can just reuse this
whole thing all right so most games need
a way of simulating physics because i
mean most games tend to approximate some
sort of physical reality and here in the
real world we are obviously bound by
certain physical principles
so here's my own game which approximates
the very real situation of a sentient
cereal box
and as you can see he is affected by
forces like gravity and the reaction
forces from the furniture that he's
standing on
the physics i implemented for this game
is uh
not particularly sophisticated though
it works but it isn't suitable for
simulating precise machines which is
actually what i needed for this project
so i set out building a real physics
engine
the first and most basic element that i
needed is a system state object
this is a simple structure which tracks
various object properties like position
velocity and orientation
next we need a way to apply forces to
the objects in the scene this is handled
by something called a force generator
and force generators can be things like
gravity or springs and the math behind
them is fairly straightforward
next we need a way to discretize time
and by discretization i mean that in the
real world we usually regard time as
being a continuous value so in other
words you can subdivide it infinitely
computers can't really do continuous
values very well so we need to break our
simulation down into time steps
every time we run the simulation we
advance time by the time step
for games the time step is often just
the length of each video frame but you
can actually run the simulation multiple
times per frame which further subdivides
your time step
and the usual rule is that the smaller
the time step is the more accurate your
simulation results will be
the final component of a basic physics
engine is called the differential
equation solver now this sounds like a
complex term but it actually isn't
really that complicated if you've
written a basic game before or some sort
of simulation you've actually probably
already written a differential equation
solver you might be familiar with this
sequence of events for an object that is
accelerating
so first you add velocity multiplied by
the time step to the object's position
and then you add acceleration multiplied
by the time step to the object's
velocity
and in some sense this has the effect of
basically updating the position every
frame by moving it by the object's
current velocity and you obviously have
to update the velocity depending on how
fast that object is
accelerating the simple process is
actually a numerical approach for
solving a differential equation and this
particular approach is called euler's
method
euler's method isn't really that great
though
for sensitive systems like what you see
on the screen the error can actually be
fairly significant and the simulation
just explodes immediately
you can improve the accuracy by reducing
the time step since euler's method
becomes more accurate as the time step
becomes smaller but as you can see to
get good results we need to run the
simulation 600 000 times per second it's
a fairly simple simulation so we can get
away with it without taking a hit to our
frame rate but for most scenes this
isn't really going to work
instead we can use a more clever
technique called rk4 or the classic
runga kuda method i have left a link in
the description of this video if you
want to learn more about it or you can
check out my own implementation which is
on the screen here
essentially we take a weighted average
of four points instead of just a single
point like with euler's method
this results in a much more stable
simulation and ultimately better
performance as you can see i can run the
same simulation with only 300 iterations
per second as opposed to six hundred
thousand
this is actually all you need to
simulate some basic systems and to show
this i created this basic interface with
my game engine and a few demos which
only use springs and masses
a side note
and this is totally unrelated but obs
which is the screen capture program that
i usually use drove me absolutely insane
while i was trying to make this video it
kept lagging and missing frames even
though my simulation was running
smoothly
so i just wrote my own version which is
built directly into this demo
every frame it takes what's on the
screen and sends it to a video encoder
that is running in a separate thread
and i also made this into its own
project as well and it's a simple
interface with just a few functions and
allows you to write high quality mp4
files from a c plus application so check
that out if you're interested one
benefit of this is that regardless of
the real time frame rate the video is
always smooth with a stable frame rate
of your choosing
anyway
this is a basic mass spring system
suspended from a fixed point and one
interesting thing to note is how the
total energy of the system doesn't
change which means that the simulation
is at least somewhat stable
if energy grows without putting any
energy in then we know that something
has to be broken
we can use a mass spring system to
simulate cloth as well and while it's
not perfect it performs fairly well all
things considered
we can also use force generators to
implement simple game physics so in this
case i made a repulsion force that acts
on these blobs and keeps them from
moving too close together and i also
added a simple ai that enables them to
move around and explore
it looks a bit bouncy but for some games
that might actually be a good thing
we can't actually use springs to
simulate everything though
even a simple double pendulum using
stiff springs is pretty jittery and the
more complex the system becomes the more
unworkable this approach is
instead we can use something called
constraints
a constraint in the mathematical sense
is just a function that evaluates to
zero only when a physical constraint is
satisfied so for example let's say that
we want to constrain this circle so that
its x position is three
and the constraint function is simply c
equals x minus three and notice how when
the circle is actually in the right
place c is equal to zero and everywhere
else c is non-zero
the goal of the physics engine is to try
to position and apply forces to objects
such that every single constraint
function is zero
now most scenes are going to have a lot
of constraints in them and we need to
find reaction forces that will satisfy
all of these constraints at the same
time
we can actually restate this problem as
a matrix equation and the math is a
little involved so i won't go into it
too much in detail here but check the
description for links to the full
derivation
essentially it all comes down to this
sort of scary looking equation
the right side is a vector with known
values
j times w times the transpose of j is a
matrix with known values and lambda is a
vector with unknown values
a more standard way of writing this
would be a times lambda equals b and by
solving for lambda we can find all the
forces required to satisfy all
constraints at the same time
there are a lot of different algorithms
to solve this equation and some are more
complex than others
i just implemented gaussian elimination
which works well enough for my purposes
but i might have to switch to using
something a bit more advanced later on
the actual constraints are implemented
as classes
and they each have their own calculate
method which calculates j and the time
derivative of j based on their own
constraint functions
and for those that are mathematically
inclined j is the jacobian of the
constraint function and it's basically a
matrix of derivatives with respect to
all of our system state parameters
we then take all of these values and
combine them all into a single master
jacobian matrix
then we pass the left and right side of
this equation to the linear equation
solver to find lambda
then it's just a matter of applying
these constraint forces to each rigid
body and then our differential equation
solver will take care of the rest by the
way i know that this code is not
particularly optimized it's mainly just
written for clarity and there's a lot of
room for optimization and unfortunately
if i did optimize it it'd be a lot less
readable and more difficult to debug i'm
not an expert
so i just wanted to keep things easy for
me if i put anyone to sleep with that
mathematical explanation i'm sorry i'm
trying to find a compromise here
but anyway here are some demos using
this constraint solver system
first we have the obligatory double
pendulum system
and just having this is going to get me
millions of views on this video i'm sure
we can also simulate a triple pendulum
which is quite a bit more chaotic than a
simple double pendulum you can see how
the numerical error accumulates and
starts to mysteriously increase the
total energy of the system if we left
this running for a very long time and i
mean like a very long time like many
hours maybe even days
the simulation would eventually break
down
but that can be pretty easily prevented
by just dampening the motion of
everything slightly or just decreasing
the time step to get a more accurate
simulation
now this demo
is by far my favorite but also cause me
the most headaches the mathematics
behind things like this rolling
constraint can be very tedious
um as you can see and i spent many hours
chasing down arithmetic errors in the
code it's not difficult math per se
it's just basic differentiation but
small mistakes can be really hard to
track down
so that just added to the fun i guess
here's another demo where we can see a
vivid demonstration of why it's
important to match spring stiffness to a
particular application
the spring is the same in both systems
but its reaction to the system frequency
is noticeably different
we can also simulate more complex
systems like this with a lot of moving
parts in them
and notice how you can make rigid
structural members using triangles
similar to how you design a real machine
and that's pretty much all i wanted to
show today i hope you guys got some
value out of this video uh make sure to
subscribe if you want to see what i end
up using this physics engine for i'm
pretty excited about that project and i
think it'll be very interesting by the
way i am still working on my serial
adventure game so expect a devlog for
that in the near future
and alright thanks for watching everyone
and i'll see you guys next time
浏览更多相关视频
SIGGRAPH 2024 Technical Papers Trailer
Fisika Kelas 10 | Hakikat Fisika dan Prosedur Ilmiah
Simple Pendulum Motion Derived Using Torque and the Small Angle Approximation
RAIZ CUADRADA INVERSA RÁPIDA: El algoritmo "mágico" del videojuego Quake III
I built 10 AI Tools, and am giving it for free!
The future of game development... has no game engine?
5.0 / 5 (0 votes)