arbiter.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package tamias
  2. // Determines how fast penetrations resolve themselves.
  3. var BIAS_COEF = Float(0.1)
  4. // Amount of allowed penetration. Used to reduce vibrating contacts.
  5. var COLLISION_SLOP = Float(0.1)
  6. // Data structure for contact points.
  7. type Contact struct {
  8. // Contact point and normal.
  9. P, N Vect
  10. // Penetration distance.
  11. Dist Float
  12. // Calculated by cpArbiterPreStep().
  13. r1, r2 Vect
  14. nMass, tMass, bounce Float
  15. // Persistant contact information.
  16. jnAcc, jtAcc, jBias Float
  17. bias Float
  18. // Hash value used to (mostly) uniquely identify a contact.
  19. Hash HashValue
  20. }
  21. type ArbiterState int
  22. const (
  23. ArbiterStateNormal = ArbiterState(0)
  24. ArbiterStateFirstColl = ArbiterState(1)
  25. ArbiterStateIgnore = ArbiterState(2)
  26. )
  27. // Data structure for tracking collisions between shapes.
  28. type Arbiter struct {
  29. // Information on the contact points between the objects.
  30. numContacts int
  31. contacts []Contact
  32. // The two shapes involved in the collision.
  33. // These variables are NOT in the order defined by the collision handler.
  34. private_a, private_b *Shape
  35. // Calculated before calling the pre-solve collision handler
  36. // Override them with custom values if you want specialized behavior
  37. e Float
  38. u Float
  39. // Used for surface_v calculations, implementation may change
  40. surface_vr Vect
  41. // Time stamp of the arbiter. (from cpSpace)
  42. stamp int
  43. handler * CollisionHandler
  44. // Are the shapes swapped in relation to the collision handler?
  45. swappedColl bool
  46. state ArbiterState
  47. }
  48. func (con *Contact) Init(p, n Vect, dist Float, hash HashValue) (*Contact) {
  49. con.P = p
  50. con.N = n
  51. con.Dist = dist
  52. con.jnAcc = Float(0.0)
  53. con.jtAcc = Float(0.0)
  54. con.jBias = Float(0.0)
  55. con.Hash = hash
  56. return con;
  57. }
  58. func (arb *Arbiter) TotalImpulse() (Vect) {
  59. contacts := arb.contacts
  60. sum := VZERO
  61. count := arb.numContacts
  62. for i:=0 ; i < count ; i++ {
  63. con := &contacts[i]
  64. sum = sum.Add(con.N.Mult(con.jnAcc))
  65. }
  66. return sum;
  67. }
  68. func (arb *Arbiter) TotalImpulseWithFriction() (Vect) {
  69. contacts := arb.contacts
  70. sum := VZERO
  71. count := arb.numContacts
  72. for i:=0 ; i < count ; i++ {
  73. con := &contacts[i]
  74. sum = sum.Add(con.N.Rotate(V(con.jnAcc, con.jtAcc)))
  75. }
  76. return sum;
  77. }
  78. func ContactsEstimateCrushingImpulse(contacts []Contact, numContacts int) (Float) {
  79. fsum := Float(0.0);
  80. vsum := VZERO;
  81. for i:=0; i<numContacts; i++ {
  82. con := &contacts[i]
  83. j := con.N.Rotate(V(con.jnAcc, con.jtAcc))
  84. fsum += j.Length()
  85. vsum = vsum.Add(j)
  86. }
  87. vmag := vsum.Length()
  88. return (Float(1.0) - vmag/fsum)
  89. }
  90. func (arb *Arbiter) Ignore() {
  91. arb.state = ArbiterStateIgnore
  92. }
  93. func ArbiterAlloc() (* Arbiter) {
  94. return &Arbiter{}
  95. }
  96. func (arb *Arbiter) Init(a *Shape, b *Shape) (* Arbiter) {
  97. arb.numContacts = 0
  98. arb.contacts = nil
  99. arb.private_a = a
  100. arb.private_b = b
  101. arb.stamp = -1;
  102. arb.state = ArbiterStateFirstColl
  103. return arb
  104. }
  105. func ArbiterNew(a, b *Shape) (*Arbiter) {
  106. return ArbiterAlloc().Init(a, b);
  107. }
  108. func (arb *Arbiter) Destroy() {
  109. // if(arb.contacts) { arb.contacts = nil }
  110. }
  111. func (arb *Arbiter) Free() {
  112. arb.Destroy()
  113. arb = nil // garbage collected
  114. }
  115. func (arb *Arbiter) Update(contacts []Contact, numContacts int,
  116. handler *CollisionHandler, a *Shape, b *Shape) {
  117. // Arbiters without contact data may exist if a collision function rejected the collision.
  118. if arb.contacts != nil {
  119. // Iterate over the possible pairs to look for hash value matches.
  120. for i:=0; i < arb.numContacts; i++ {
  121. old := &arb.contacts[i]
  122. for j:=0; j<numContacts; j++ {
  123. new_contact := &contacts[j];
  124. // This could trigger false positives, but is fairly unlikely nor serious if it does.
  125. if new_contact.Hash == old.Hash {
  126. // Copy the persistant contact information.
  127. new_contact.jnAcc = old.jnAcc;
  128. new_contact.jtAcc = old.jtAcc;
  129. }
  130. }
  131. }
  132. // cpfree(arb.contacts);
  133. }
  134. arb.contacts = contacts;
  135. arb.numContacts = numContacts;
  136. arb.handler = handler;
  137. arb.swappedColl = (a.collision_type != handler.a);
  138. arb.e = a.e * b.e;
  139. arb.u = a.u * b.u;
  140. arb.surface_vr = a.surface_v.Sub(b.surface_v)
  141. // For collisions between two similar primitive types, the order could have been swapped.
  142. arb.private_a = a; arb.private_b = b;
  143. }
  144. func (arb *Arbiter) PreStep(dt_inv Float) {
  145. shapea := arb.private_a;
  146. shapeb := arb.private_b;
  147. a := shapea.Body;
  148. b := shapeb.Body;
  149. for i:=0; i<arb.numContacts; i++ {
  150. con := &arb.contacts[i];
  151. // Calculate the offsets.
  152. con.r1 = con.P.Sub(a.p);
  153. con.r2 = con.P.Sub(b.p);
  154. // Calculate the mass normal and mass tangent.
  155. con.nMass = Float(1.0) / KScalar(a, b, con.r1, con.r2, con.N)
  156. con.tMass = Float(1.0) / KScalar(a, b, con.r1, con.r2, con.N.Perp())
  157. aidmin := Float(0.0).Min(con.Dist + COLLISION_SLOP)
  158. // Calculate the target bias velocity.
  159. con.bias = -BIAS_COEF*dt_inv*aidmin;
  160. con.jBias = 0.0;
  161. // Calculate the target bounce velocity.
  162. con.bounce = NormalRelativeVelocity(a, b, con.r1, con.r2, con.N)*arb.e;
  163. //cpvdot(con.n, cpvsub(v2, v1))*e;
  164. }
  165. }
  166. func (arb *Arbiter) ApplyCachedImpulse() {
  167. shapea := arb.private_a
  168. shapeb := arb.private_b
  169. arb.u = shapea.u * shapeb.u
  170. arb.surface_vr = shapeb.surface_v.Sub(shapea.surface_v)
  171. a := shapea.Body
  172. b := shapeb.Body
  173. for i:=0 ; i<arb.numContacts ; i++ {
  174. con := &arb.contacts[i]
  175. aid := con.N.Rotate(V(con.jnAcc, con.jtAcc))
  176. ApplyImpulses(a, b, con.r1, con.r2, aid)
  177. }
  178. }
  179. func (arb *Arbiter) ApplyImpulse(eCoef Float) {
  180. a := arb.private_a.Body
  181. b := arb.private_b.Body
  182. for i:=0 ; i<arb.numContacts ; i++ {
  183. con := &arb.contacts[i]
  184. n := con.N
  185. r1 := con.r1
  186. r2 := con.r2
  187. // Calculate the relative bias velocities.
  188. vb1 := a.v_bias.Add(r1.Perp().Mult(a.w_bias))
  189. vb2 := b.v_bias.Add(r2.Perp().Mult(b.w_bias))
  190. vbn := vb2.Sub(vb1).Dot(n)
  191. // Calculate and clamp the bias impulse.
  192. jbn := (con.bias - vbn)*con.nMass
  193. jbnOld := con.jBias
  194. con.jBias = jbnOld + jbn.Max(Float(0.0))
  195. jbn = con.jBias - jbnOld
  196. // Apply the bias impulse.
  197. ApplyBiasImpulses(a, b, r1, r2, n.Mult(jbn))
  198. // Calculate the relative velocity.
  199. vr := RelativeVelocity(a, b, r1, r2)
  200. vrn := vr.Dot(n)
  201. // Calculate and clamp the normal impulse.
  202. jn := -(con.bounce*eCoef + vrn)*con.nMass
  203. jnOld := con.jnAcc
  204. con.jnAcc = jnOld + jn.Max(Float(0.0))
  205. jn = con.jnAcc - jnOld
  206. // Calculate the relative tangent velocity.
  207. vrt := vr.Add(arb.surface_vr).Dot(n.Perp())
  208. // Calculate and clamp the friction impulse.
  209. jtMax := arb.u*con.jnAcc
  210. jt := -vrt*con.tMass
  211. jtOld := con.jtAcc
  212. con.jtAcc = (jtOld + jt).Clamp(-jtMax, jtMax)
  213. jt = con.jtAcc - jtOld
  214. // Apply the final impulse.
  215. ApplyImpulses(a, b, r1, r2, n.Rotate(V(jn, jt)))
  216. }
  217. }
  218. func (arb *Arbiter) GetShapes() ( a *Shape, b *Shape) {
  219. if arb.swappedColl {
  220. return arb.private_b, arb.private_a
  221. }
  222. return arb.private_a, arb.private_b
  223. }
  224. func (arb *Arbiter) IsFirstContact() (bool) {
  225. return arb.state == ArbiterStateFirstColl
  226. }
  227. func (arb * Arbiter) GetNormal(i int) (Vect) {
  228. n := arb.contacts[i].N;
  229. if arb.swappedColl {
  230. return n.Neg()
  231. }
  232. return n
  233. }
  234. func (arb * Arbiter) GetPoint(i int) (Vect) {
  235. p := arb.contacts[i].P;
  236. return p
  237. }