util.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package tamias
  2. /*
  3. func J_MAX(constraint Constraint, dt Float) (Vect) {
  4. return Constraint.maxForce * dt
  5. }
  6. */
  7. func RelativeVelocity(a, b *Body, r1, r2 Vect) (Vect){
  8. v1_sum := a.v.Add(r1.Perp().Mult(a.w))
  9. v2_sum := b.v.Add(r2.Perp().Mult(b.w))
  10. return v2_sum.Sub(v1_sum)
  11. }
  12. func NormalRelativeVelocity(a, b *Body, r1, r2, n Vect) (Float) {
  13. return RelativeVelocity(a, b, r1, r2).Dot(n);
  14. }
  15. func ApplyImpulses (a, b *Body, r1, r2, j Vect) {
  16. a.ApplyImpulse(j.Neg() , r1)
  17. b.ApplyImpulse(j , r2)
  18. }
  19. func ApplyBiasImpulses(a, b *Body, r1, r2, j Vect) {
  20. a.ApplyBiasImpulse(j.Neg() , r1)
  21. b.ApplyBiasImpulse(j , r2)
  22. }
  23. func KScalar(a, b *Body, r1, r2, n Vect) (Float) {
  24. mass_sum := a.m_inv + b.m_inv;
  25. r1cn := r1.Cross(n)
  26. r2cn := r2.Cross(n)
  27. value := mass_sum + a.i_inv*r1cn*r1cn + b.i_inv*r2cn*r2cn
  28. Assert(value != 0.0, "Unsolvable collision or constraint.")
  29. return value
  30. }
  31. func KTensor(a, b *Body, r1, r2 Vect) (k1, k2 Vect) {
  32. // calculate mass matrix
  33. // If I wasn't lazy and wrote a proper matrix class, this wouldn't be so gross...
  34. m_sum := a.m_inv + b.m_inv
  35. // start with I*m_sum
  36. k11 := m_sum ; k12 := Float(0.0)
  37. k21 := Float(0.0) ; k22 := m_sum
  38. // add the influence from r1
  39. a_i_inv := a.i_inv;
  40. r1xsq := r1.X * r1.X * a_i_inv
  41. r1ysq := r1.Y * r1.Y * a_i_inv
  42. r1nxy := -r1.X * r1.Y * a_i_inv
  43. k11 += r1ysq; k12 += r1nxy
  44. k21 += r1nxy; k22 += r1xsq
  45. // add the influence from r2
  46. b_i_inv := b.i_inv;
  47. r2xsq := r2.X * r2.X * b_i_inv
  48. r2ysq := r2.Y * r2.Y * b_i_inv
  49. r2nxy := -r2.X * r2.Y * b_i_inv
  50. k11 += r2ysq; k12 += r2nxy
  51. k21 += r2nxy; k22 += r2xsq
  52. // invert
  53. determinant := k11*k22 - k12*k21
  54. Assert(determinant != 0.0, "Unsolvable constraint.")
  55. det_inv := 1.0 / determinant;
  56. k1 = V( k22*det_inv, -k12*det_inv)
  57. k2 = V(-k21*det_inv, k11*det_inv)
  58. return k1, k2
  59. }
  60. func (vr Vect) MultK(k1, k2 Vect) (Vect) {
  61. return V(vr.Dot(k1), vr.Dot(k2));
  62. }
  63. /*
  64. #define CP_DefineClassGetter(t) const cpConstraintClass * t##GetClass(){return (cpConstraintClass *)&klass;}
  65. #define J_MAX(constraint, dt) (((cpConstraint *)constraint)->maxForce*(dt))
  66. */