constraint.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 constraint implements physics constraints.
  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. Equations() []equation.IEquation
  21. CollideConnected() bool
  22. BodyA() IBody
  23. BodyB() IBody
  24. }
  25. // Constraint base struct.
  26. type Constraint struct {
  27. equations []equation.IEquation // Equations to be solved in this constraint
  28. bodyA IBody
  29. bodyB IBody
  30. colConn bool // Set to true if you want the bodies to collide when they are connected.
  31. }
  32. // NewConstraint creates and returns a pointer to a new Constraint object.
  33. //func NewConstraint(bodyA, bodyB IBody, colConn, wakeUpBodies bool) *Constraint {
  34. //
  35. // c := new(Constraint)
  36. // c.initialize(bodyA, bodyB, colConn, wakeUpBodies)
  37. // return c
  38. //}
  39. func (c *Constraint) initialize(bodyA, bodyB IBody, colConn, wakeUpBodies bool) {
  40. c.bodyA = bodyA
  41. c.bodyB = bodyB
  42. c.colConn = colConn // true
  43. if wakeUpBodies { // true
  44. if bodyA != nil {
  45. bodyA.WakeUp()
  46. }
  47. if bodyB != nil {
  48. bodyB.WakeUp()
  49. }
  50. }
  51. }
  52. // AddEquation adds an equation to the constraint.
  53. func (c *Constraint) AddEquation(eq equation.IEquation) {
  54. c.equations = append(c.equations, eq)
  55. }
  56. // Equations returns the constraint's equations.
  57. func (c *Constraint) Equations() []equation.IEquation {
  58. return c.equations
  59. }
  60. func (c *Constraint) CollideConnected() bool {
  61. return c.colConn
  62. }
  63. func (c *Constraint) BodyA() IBody {
  64. return c.bodyA
  65. }
  66. func (c *Constraint) BodyB() IBody {
  67. return c.bodyB
  68. }
  69. // SetEnable sets the enabled flag of the constraint equations.
  70. func (c *Constraint) SetEnabled(state bool) {
  71. for i := range c.equations {
  72. c.equations[i].SetEnabled(state)
  73. }
  74. }