constraint.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 constraint
  6. import (
  7. "github.com/g3n/engine/physics/equation"
  8. "github.com/g3n/engine/math32"
  9. )
  10. type IBody interface {
  11. equation.IBody
  12. WakeUp()
  13. VectorToWorld(*math32.Vector3) math32.Vector3
  14. PointToLocal(*math32.Vector3) math32.Vector3
  15. VectorToLocal(*math32.Vector3) math32.Vector3
  16. Quaternion() *math32.Quaternion
  17. }
  18. type IConstraint interface {
  19. Update() // Update all the equations with data.
  20. }
  21. // Constraint base struct.
  22. type Constraint struct {
  23. equations []*equation.Equation // Equations to be solved in this constraint
  24. bodyA IBody
  25. bodyB IBody
  26. //id
  27. collideConnected bool // Set to true if you want the bodies to collide when they are connected.
  28. }
  29. // NewConstraint creates and returns a pointer to a new Constraint object.
  30. func NewConstraint(bodyA, bodyB IBody, collideConnected, wakeUpBodies bool) *Constraint {
  31. c := new(Constraint)
  32. c.initialize(bodyA, bodyB, collideConnected, wakeUpBodies)
  33. return c
  34. }
  35. func (c *Constraint) initialize(bodyA, bodyB IBody, collideConnected, wakeUpBodies bool) {
  36. c.bodyA = bodyA
  37. c.bodyB = bodyB
  38. c.collideConnected = collideConnected // true
  39. if wakeUpBodies { // true
  40. if bodyA != nil {
  41. bodyA.WakeUp()
  42. }
  43. if bodyB != nil {
  44. bodyB.WakeUp()
  45. }
  46. }
  47. }
  48. // AddEquation adds an equation to the constraint.
  49. func (c *Constraint) AddEquation(eq *equation.Equation) {
  50. c.equations = append(c.equations, eq)
  51. }
  52. // SetEnable sets the enabled flag of the constraint equations.
  53. func (c *Constraint) SetEnabled(state bool) {
  54. for i := range c.equations {
  55. c.equations[i].SetEnabled(state)
  56. }
  57. }