Are particle systems used in games?
From the Unreal Engine documentation
Will physics simulations always be imperfect?
Never say never, but....
Are physics engines usually built separately from graphics engines?
This varies historically. It is possible to build them separately, whether separate ones are used or not tends to fluctuate.
Can we adjust explicit Euler to avoid deviations?
Yes, this leads to other so-called time integration techniques like:
Why is cloth so hard to simulate?
Cloth is almost-inextensible.
If we had exactly rigid cloth (e.g. Mylar) or very stretchy cloth (think spandex but way stretchier) this would be easy.
What's the difference in speed between 2D and 3D simulations?
Almost nothing*.
* terms and conditions apply
void draw() {
background(210);
ellipse(250, y, r, r);
accel = gravity;
vel += accel;
vel -= coef_fric * vel;
y += vel;
if (y > (height - r)) {
vel = -(coef_rest * vel);
y = height - r; // Massive hack
}
}
You want to make your simulation as principled as possible.
But at some point, you'll find that the principle breaks down, or is too expensive to enforce.
This is an area of active research. Common ideas include:
Yes. They're less familiar and pack more details into fewer lines of code.
That's really about it.
There are also a few pitfalls you have to be aware of, like v1.add(v2)
modifying v1.
Equations that describe motions over time.
Newton's Second Law
This is not solvable exactly on a computer for any interesting set of forces.
But we don't have a way to express "infinitely small amount" on a computer!
We need some way to discretize the simulation into timesteps and decide what happens in each timestep.
One of the methods often used in practice: it is not much more accurate, but it's still simple and doesn't suffer energy growth like the explicit Euler method does.
where \( \vec{a} \) is the acceleration vector.
Other common methods: Runge-Kutta methods, Midpoint Euler
Our particle has a mass of \(m\).
The particle experiences a force of \(\vec{f}\).
Solution: we need to divide the force by the mass to get the acceleration.
class Particle {
float x;
float vx;
float mass;
// ...
void advanceTime(float force, float dt){
float accel = force / this.mass;
this.x += this.vx * dt;
this.vx += accel * dt;
}
The force a spring exerts on its endpoints is based on:
Hooke's Law tells us that the force is \( F = -k x\).
By Svjo - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=25398333
float y = 100.0;
float vy = 0.0;
float m = 1.0;
float ry = 250;
float ks = 1.0;
float dt = 0.1;
void setup() {
size(500, 500);
rectMode(CENTER);
}
void draw() {
background(210);
fill(0);
ellipse(200, ry, 50, 50);
float f = -(ks * (y - ry));
float a = f/m;
vy = vy + dt*a;
y += dt*vy;
line(200,ry,200,y);
rect(200, y, 100, 20);
}
Question: what does ry
represent in this code?
This spring system oscillates forever (the spring doesn't dissipate any energy).
That's not necessarily bad!
Sometimes, we want vibrations to "damp" out, i.e. get weaker and weaker until they stop.
How can we introduce damping into our spring?
Spring force: tries to restore spring to its resting length.
Damping force: acts against motion of the spring
float y;
float vy;
float m = 1.0;
float ry = 250;
float ks = 0.1;
float kd = 0.1;
void setup() {
size(500, 500);
}
void draw() {
background(210);
float f = -((ks * (y - ry)) + kd*vy);
float a = f/m;
vy = vy + a;
y += vy;
rect(200, y, 100, 20);
}
All our springs so far have been zero restlength. Realistic springs have restlength of greater than zero.
Sequences of springs can simulate:
Networks of springs can simulate cloth.
Timestep 0
Timestep 1
Timestep 2
Timestep 3
Timestep 4
We can enter a cycle where our forces spiral out of control.
Will always happen with a sufficiently large timestep, though things like the integrator and spring stiffness will play a role.
float y;
float vy;
float m = 1.0;
float ry = 250;
float ks = 100.0;
float dt = 2.0;
void setup() {
size(500, 500);
}
void draw() {
background(210);
float f = -(ks * (y - ry)) + 0.05;
float a = f/m;
vy = vy + dt*a;
y += dt*vy;
rect(200, y, 100, 20);
}
Absolutely. Most physics is controlled by the complexity of your numeric code
PVector
s work similarly in 3D?Yes. In fact, all PVectors are implicitly 3D (but the third component is zeroed out and ignored when you use them in a 2D fashion).
I might recommend using the .x, .y, and .z members of the vector.
draw()
is only a special name when it's at the top level of your Processing program.
// This function is special. It is
// called in a loop and there can
// only be one function named this
void draw(){
}
// None of these methods are special.
// As far as Processing cares, they're
// just ordinary methods, and could
// be named anything--the name "draw"
// is totally coincidental.
class A {
void draw(){}
}
class B {
void draw(){}
void draw(int x){}
}
Examples: smoke, water, fire, clouds, dust, galaxies, etc.
Generally, there will be three "stages" in a particle system, though these can be thought more as decisions to make than as stages-in-time.
Fireworks
Snowstorm
Particle
Additional rules (usually written as methods) can make the behavior of our Particle
class more complex and interesting:
Linked from https://en.wikipedia.org/wiki/Particle_system#/media/File:Particle_Emitter.jpg