hinge.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // Hinge constraint.
  11. // Think of it as a door hinge.
  12. // It tries to keep the door in the correct place and with the correct orientation.
  13. type Hinge struct {
  14. PointToPoint
  15. axisA *math32.Vector3 // Rotation axis, defined locally in bodyA.
  16. axisB *math32.Vector3 // Rotation axis, defined locally in bodyB.
  17. rotEq1 *equation.Rotational
  18. rotEq2 *equation.Rotational
  19. motorEq *equation.RotationalMotor
  20. }
  21. // NewHinge creates and returns a pointer to a new Hinge constraint object.
  22. func NewHinge(bodyA, bodyB IBody, pivotA, pivotB, axisA, axisB *math32.Vector3, maxForce float32) *Hinge {
  23. hc := new(Hinge)
  24. hc.initialize(bodyA, bodyB, pivotA, pivotB, maxForce)
  25. hc.axisA = axisA
  26. hc.axisB = axisB
  27. hc.axisA.Normalize()
  28. hc.axisB.Normalize()
  29. hc.rotEq1 = equation.NewRotational(bodyA, bodyB, maxForce)
  30. hc.rotEq2 = equation.NewRotational(bodyA, bodyB, maxForce)
  31. hc.motorEq = equation.NewRotationalMotor(bodyA, bodyB, maxForce)
  32. hc.motorEq.SetEnabled(false) // Not enabled by default
  33. hc.equations = append(hc.equations, &hc.rotEq1.Equation)
  34. hc.equations = append(hc.equations, &hc.rotEq2.Equation)
  35. hc.equations = append(hc.equations, &hc.motorEq.Equation)
  36. return hc
  37. }
  38. func (hc *Hinge) SetMotorEnabled(state bool) {
  39. hc.motorEq.SetEnabled(state)
  40. }
  41. func (hc *Hinge) SetMotorSpeed(speed float32) {
  42. hc.motorEq.SetTargetSpeed(speed)
  43. }
  44. func (hc *Hinge) SetMotorMaxForce(maxForce float32) {
  45. hc.motorEq.SetMaxForce(maxForce)
  46. hc.motorEq.SetMinForce(-maxForce)
  47. }
  48. // Update updates the equations with data.
  49. func (hc *Hinge) Update() {
  50. hc.PointToPoint.Update()
  51. // Get world axes
  52. quatA := hc.bodyA.Quaternion()
  53. quatB := hc.bodyB.Quaternion()
  54. worldAxisA := hc.axisA.Clone().ApplyQuaternion(quatA)
  55. worldAxisB := hc.axisB.Clone().ApplyQuaternion(quatB)
  56. t1, t2 := worldAxisA.RandomTangents()
  57. hc.rotEq1.SetAxisA(t1)
  58. hc.rotEq2.SetAxisA(t2)
  59. hc.rotEq1.SetAxisB(worldAxisB)
  60. hc.rotEq2.SetAxisB(worldAxisB)
  61. if hc.motorEq.Enabled() {
  62. hc.motorEq.SetAxisA(hc.axisA.Clone().ApplyQuaternion(quatA))
  63. hc.motorEq.SetAxisB(hc.axisB.Clone().ApplyQuaternion(quatB))
  64. }
  65. }