friction.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // Friction is a friction constraint equation.
  10. type Friction struct {
  11. Equation
  12. rA *math32.Vector3 // World-oriented vector that goes from the center of bA to the contact point.
  13. rB *math32.Vector3 // World-oriented vector that starts in body j position and goes to the contact point.
  14. t *math32.Vector3 // Contact tangent
  15. }
  16. // NewFriction creates and returns a pointer to a new Friction equation object.
  17. // slipForce should be +-F_friction = +-mu * F_normal = +-mu * m * g
  18. func NewFriction(bodyA, bodyB IBody, slipForce float32) *Friction {
  19. fe := new(Friction)
  20. fe.rA = math32.NewVec3()
  21. fe.rB = math32.NewVec3()
  22. fe.t = math32.NewVec3()
  23. fe.Equation.initialize(bodyA, bodyB, -slipForce, slipForce)
  24. return fe
  25. }
  26. // ComputeB
  27. func (fe *Friction) ComputeB(h float32) float32 {
  28. // Calculate cross products
  29. rtA := math32.NewVec3().CrossVectors(fe.rA, fe.t)
  30. rtB := math32.NewVec3().CrossVectors(fe.rB, fe.t)
  31. // G = [-t -rtA t rtB]
  32. // And remember, this is a pure velocity constraint, g is always zero!
  33. fe.jeA.SetSpatial(fe.t.Clone().Negate())
  34. fe.jeA.SetRotational(rtA.Clone().Negate())
  35. fe.jeB.SetSpatial(fe.t.Clone())
  36. fe.jeB.SetRotational(rtB.Clone())
  37. var GW = fe.ComputeGW()
  38. var GiMf = fe.ComputeGiMf()
  39. return -GW*fe.b - h*GiMf
  40. }