Lecture 3: Simulation Loop

28 Aug 2024


§ The Loop

The basic simulation loop that we'll use in this class is to integrate, check for collisions, determine and resolve them, repeat.

Simulation Loop

  • Set initial conditions
  • tt \leftarrow 0
  •  
  • while t<tmaxt < t_{\mathrm{max}} do
    • TT \leftarrow min(h,Tdispt)\min(h, T_{\mathrm{disp}} - t)
    • a\mathbf{a} \leftarrow 1mFtotal\frac{1}{m} \mathbf{F}_{\mathrm{total}}
    • xnew,vnew\mathbf{x}_{\mathrm{new}}, \mathbf{v}_{\mathrm{new}} \leftarrow Integrate(x,v,T\mathbf{x}, \mathbf{v}, T)
    •  
    • if DetectCollision(x,v,xnew,vnew\mathbf{x}, \mathbf{v}, \mathbf{x}_{\mathrm{new}}, \mathbf{v}_{\mathrm{new}}) then
      • ff \leftarrow fractional timestep of collision
      • xc,vc\mathbf{x}_{c}, \mathbf{v}_{c} \leftarrow Integrate(x,v,T\mathbf{x}, \mathbf{v}, T)
      • v\mathbf{v} \leftarrow CollisionResponse(xc,vc\mathbf{x}_{c}, \mathbf{v}_{c})
      • x\mathbf{x} \leftarrow xc\mathbf{x}_c
    • else
      • x\mathbf{x} \leftarrow xnew\mathbf{x}_{\mathrm{new}}
    •  
    • tt \leftarrow t+Tt + T
    •  
    • if t>Tdispt > T_{\mathrm{disp}} then
      • render and output frame
      • update TdispT_{\mathrm{disp}}

§ Resting Situations

We want to stop the simulation for an object whenever it comes to rest; this saves on computational resources, avoids numerical issues, and jitter effects.

For an object to come to rest, it must satisfy all of the following

  1. vδ1\|\mathbf{v}\| \leq \delta_1; the speed of the object is less than some small threshold
  2. There exists a surface ss such that dist(s,x)δ2\mathrm{dist}(s, \mathbf{x}) \leq \delta_2; i.e. it touches a some surface
  3. Acceleration must be towards the touching surface, an^<0.\mathbf{a}\cdot\hat{\mathbf{n}} < 0.
  4. Friction must overcome the tangential acceleration, aμa\|\mathbf{a}_\parallel\| \leq \mu\|\mathbf{a}_\perp\|

§ Numerical Error

Roundoff error grows and propagates as we chain computations.

We won't get into the specifics in this course, but some guidelines are to

  • use tolerances; do not check if a=?ba \stackrel{?}{=} b but rather ab?ε,\|a - b\| \stackrel{?}{\leq} \varepsilon, and to
  • not assume transitivity; e.g. equality (A=B)(B=C)  ̸ ⁣ ⁣ ⁣    A=C.(A = B) \wedge (B = C) \;\not\!\!\!\implies A = C.