rotationalmotor.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 equation
  6. import (
  7. "github.com/g3n/engine/math32"
  8. )
  9. // RotationalMotor is a rotational motor constraint equation.
  10. // Tries to keep the relative angular velocity of the bodies to a given value.
  11. type RotationalMotor struct {
  12. Equation
  13. axisA *math32.Vector3 // World oriented rotational axis
  14. axisB *math32.Vector3 // World oriented rotational axis
  15. targetSpeed float32 // Target speed
  16. }
  17. // NewRotationalMotor creates and returns a pointer to a new RotationalMotor equation object.
  18. func NewRotationalMotor(bodyA, bodyB IBody, maxForce float32) *RotationalMotor {
  19. re := new(RotationalMotor)
  20. re.Equation.initialize(bodyA, bodyB, -maxForce, maxForce)
  21. return re
  22. }
  23. // SetAxisA sets the axis of body A.
  24. func (ce *RotationalMotor) SetAxisA(axisA *math32.Vector3) {
  25. ce.axisA = axisA
  26. }
  27. // AxisA returns the axis of body A.
  28. func (ce *RotationalMotor) AxisA() math32.Vector3 {
  29. return *ce.axisA
  30. }
  31. // SetAxisB sets the axis of body B.
  32. func (ce *RotationalMotor) SetAxisB(axisB *math32.Vector3) {
  33. ce.axisB = axisB
  34. }
  35. // AxisB returns the axis of body B.
  36. func (ce *RotationalMotor) AxisB() math32.Vector3 {
  37. return *ce.axisB
  38. }
  39. // SetTargetSpeed sets the target speed.
  40. func (ce *RotationalMotor) SetTargetSpeed(speed float32) {
  41. ce.targetSpeed = speed
  42. }
  43. // TargetSpeed returns the target speed.
  44. func (ce *RotationalMotor) TargetSpeed() float32 {
  45. return ce.targetSpeed
  46. }
  47. // ComputeB
  48. func (re *RotationalMotor) ComputeB(h float32) float32 {
  49. // g = 0
  50. // gdot = axisA * wi - axisB * wj
  51. // gdot = G * W = G * [vi wi vj wj]
  52. // =>
  53. // G = [0 axisA 0 -axisB]
  54. re.jeA.SetRotational(re.axisA.Clone())
  55. re.jeB.SetRotational(re.axisB.Clone().Negate())
  56. GW := re.ComputeGW() - re.targetSpeed
  57. GiMf := re.ComputeGiMf()
  58. return -GW*re.b - h*GiMf
  59. }