TODO read https://gafferongames.com/post/fix_your_timestep/
(dt float32)
| 379 | |
| 380 | // TODO read https://gafferongames.com/post/fix_your_timestep/ |
| 381 | func (s *Simulation) internalStep(dt float32) { |
| 382 | |
| 383 | s.dt = dt |
| 384 | |
| 385 | // Apply force fields (only to dynamic bodies |
| 386 | for _, b := range s.bodies { |
| 387 | if b.BodyType() == object.Dynamic { |
| 388 | for _, ff := range s.forceFields { |
| 389 | pos := b.Position() |
| 390 | force := ff.ForceAt(&pos) |
| 391 | b.ApplyForceField(&force) |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // Find pairs of bodies that are potentially colliding (broadphase) |
| 397 | pairs := s.broadphase.FindCollisionPairs(s.bodies) |
| 398 | |
| 399 | // Remove some pairs before proceeding to narrowphase based on constraints' colConn property |
| 400 | // which specifies if constrained bodies should collide with one another |
| 401 | s.prunePairs(pairs) // TODO review/implement |
| 402 | |
| 403 | // Precompute world normals/edges only for convex bodies that will undergo narrowphase |
| 404 | for _, body := range s.uniqueBodiesFromPairs(pairs) { |
| 405 | if ch, ok := body.Shape().(*shape.ConvexHull); ok{ |
| 406 | ch.ComputeWorldFaceNormalsAndUniqueEdges(body.Quaternion()) |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // Switch collision matrices (to keep track of which collisions started/ended) |
| 411 | s.collisionMatrixTick() |
| 412 | |
| 413 | // Resolve collisions and generate contact and friction equations |
| 414 | contactEqs, frictionEqs := s.narrowphase.GenerateEquations(pairs) |
| 415 | |
| 416 | // Add all friction equations to solver |
| 417 | for i := 0; i < len(frictionEqs); i++ { |
| 418 | s.solver.AddEquation(frictionEqs[i]) |
| 419 | } |
| 420 | |
| 421 | // Add all contact equations to solver (and update some things) |
| 422 | for i := 0; i < len(contactEqs); i++ { |
| 423 | s.solver.AddEquation(contactEqs[i]) |
| 424 | s.updateSleepAndCollisionMatrix(contactEqs[i]) |
| 425 | } |
| 426 | |
| 427 | // Add all equations from user-added constraints to the solver |
| 428 | userAddedEquations := 0 |
| 429 | for i := 0; i < len(s.constraints); i++ { |
| 430 | s.constraints[i].Update() |
| 431 | eqs := s.constraints[i].Equations() |
| 432 | for j := 0; j < len(eqs); j++ { |
| 433 | userAddedEquations++ |
| 434 | s.solver.AddEquation(eqs[j]) |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // Emit events TODO implement |
no test coverage detected