pointtopoint.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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
  5. import (
  6. "github.com/g3n/engine/experimental/physics/equation"
  7. "github.com/g3n/engine/math32"
  8. )
  9. // PointToPoint is an offset constraint.
  10. // Connects two bodies at the specified offset points.
  11. type PointToPoint struct {
  12. Constraint
  13. pivotA *math32.Vector3 // Pivot, defined locally in bodyA.
  14. pivotB *math32.Vector3 // Pivot, defined locally in bodyB.
  15. eqX *equation.Contact
  16. eqY *equation.Contact
  17. eqZ *equation.Contact
  18. }
  19. // NewPointToPoint creates and returns a pointer to a new PointToPoint constraint object.
  20. func NewPointToPoint(bodyA, bodyB IBody, pivotA, pivotB *math32.Vector3, maxForce float32) *PointToPoint {
  21. ptpc := new(PointToPoint)
  22. ptpc.initialize(bodyA, bodyB, pivotA, pivotB, maxForce)
  23. return ptpc
  24. }
  25. func (ptpc *PointToPoint) initialize(bodyA, bodyB IBody, pivotA, pivotB *math32.Vector3, maxForce float32) {
  26. ptpc.Constraint.initialize(bodyA, bodyB, true, true)
  27. ptpc.pivotA = pivotA // default is zero vec3
  28. ptpc.pivotB = pivotB // default is zero vec3
  29. ptpc.eqX = equation.NewContact(bodyA, bodyB, -maxForce, maxForce)
  30. ptpc.eqY = equation.NewContact(bodyA, bodyB, -maxForce, maxForce)
  31. ptpc.eqZ = equation.NewContact(bodyA, bodyB, -maxForce, maxForce)
  32. ptpc.eqX.SetNormal(&math32.Vector3{1, 0, 0})
  33. ptpc.eqY.SetNormal(&math32.Vector3{0, 1, 0})
  34. ptpc.eqZ.SetNormal(&math32.Vector3{0, 0, 1})
  35. ptpc.AddEquation(&ptpc.eqX.Equation)
  36. ptpc.AddEquation(&ptpc.eqY.Equation)
  37. ptpc.AddEquation(&ptpc.eqZ.Equation)
  38. }
  39. // Update updates the equations with data.
  40. func (ptpc *PointToPoint) Update() {
  41. // Rotate the pivots to world space
  42. xRi := ptpc.pivotA.Clone().ApplyQuaternion(ptpc.bodyA.Quaternion())
  43. xRj := ptpc.pivotA.Clone().ApplyQuaternion(ptpc.bodyA.Quaternion())
  44. ptpc.eqX.SetRA(xRi)
  45. ptpc.eqX.SetRB(xRj)
  46. ptpc.eqY.SetRA(xRi)
  47. ptpc.eqY.SetRB(xRj)
  48. ptpc.eqZ.SetRA(xRi)
  49. ptpc.eqZ.SetRB(xRj)
  50. }