MCPcopy
hub / github.com/g3n/engine / internalStep

Method internalStep

experimental/physics/simulation.go:381–500  ·  view source on GitHub ↗

TODO read https://gafferongames.com/post/fix_your_timestep/

(dt float32)

Source from the content-addressed store, hash-verified

379
380// TODO read https://gafferongames.com/post/fix_your_timestep/
381func (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

Callers 1

StepPlusMethod · 0.95

Calls 15

prunePairsMethod · 0.95
uniqueBodiesFromPairsMethod · 0.95
collisionMatrixTickMethod · 0.95
emitContactEventsMethod · 0.95
ApplySolutionMethod · 0.95
ClearForcesMethod · 0.95
BodyTypeMethod · 0.80
ApplyForceFieldMethod · 0.80
FindCollisionPairsMethod · 0.80
ShapeMethod · 0.80

Tested by

no test coverage detected