pointtopoint.go 2.0 KB

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