solver.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package physics implements a basic physics engine.
  5. package solver
  6. import (
  7. "github.com/g3n/engine/physics/equation"
  8. "time"
  9. "github.com/g3n/engine/math32"
  10. )
  11. // ISolver is the interface type for all constraint solvers.
  12. type ISolver interface {
  13. // Solve should return the number of iterations performed
  14. Solve(frameDelta time.Duration, nBodies int) int
  15. }
  16. // Solution represents a solver solution
  17. type Solution struct {
  18. VelocityDeltas []math32.Vector3
  19. AngularVelocityDeltas []math32.Vector3
  20. }
  21. // Constraint equation solver base class.
  22. type Solver struct {
  23. equations []*equation.Equation // All equations to be solved
  24. }
  25. // AddEquation adds an equation to the solver.
  26. func (s *Solver) AddEquation(eq *equation.Equation) {
  27. s.equations = append(s.equations, eq)
  28. }
  29. // RemoveEquation removes the specified equation from the solver.
  30. // Returns true if found, false otherwise.
  31. func (s *Solver) RemoveEquation(eq *equation.Equation) bool {
  32. for pos, current := range s.equations {
  33. if current == eq {
  34. copy(s.equations[pos:], s.equations[pos+1:])
  35. s.equations[len(s.equations)-1] = nil
  36. s.equations = s.equations[:len(s.equations)-1]
  37. return true
  38. }
  39. }
  40. return false
  41. }
  42. // ClearEquations removes all equations from the solver.
  43. func (s *Solver) ClearEquations() {
  44. s.equations = s.equations[0:0]
  45. }