rotationalmotor.go 2.0 KB

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