/flash/Box2D/Dynamics/Contacts/b2CircleContact.as

https://github.com/GunioRobot/box2d2-js · ActionScript · 141 lines · 98 code · 19 blank · 24 comment · 7 complexity · ae1d7ab374ec8c8371258f8329c7cda3 MD5 · raw file

  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. package Box2D.Dynamics.Contacts{
  19. import Box2D.Collision.Shapes.*;
  20. import Box2D.Collision.*;
  21. import Box2D.Dynamics.*;
  22. import Box2D.Common.*;
  23. import Box2D.Common.Math.*;
  24. public class b2CircleContact extends b2Contact
  25. {
  26. static public function Create(shape1:b2Shape, shape2:b2Shape, allocator:*):b2Contact{
  27. return new b2CircleContact(shape1, shape2);
  28. }
  29. static public function Destroy(contact:b2Contact, allocator:*) : void{
  30. //
  31. }
  32. public function b2CircleContact(shape1:b2Shape, shape2:b2Shape){
  33. super(shape1, shape2);
  34. m_manifold = m_manifolds[0];
  35. //b2Settings.b2Assert(m_shape1.m_type == b2Shape.e_circleShape);
  36. //b2Settings.b2Assert(m_shape2.m_type == b2Shape.e_circleShape);
  37. m_manifold.pointCount = 0;
  38. var point:b2ManifoldPoint = m_manifold.points[0];
  39. point.normalImpulse = 0.0;
  40. point.tangentImpulse = 0.0;
  41. }
  42. //~b2CircleContact() {}
  43. static private const s_evalCP:b2ContactPoint = new b2ContactPoint();
  44. public override function Evaluate(listener:b2ContactListener) : void{
  45. var v1:b2Vec2;
  46. var v2:b2Vec2;
  47. var mp0:b2ManifoldPoint;
  48. var b1:b2Body = m_shape1.m_body;
  49. var b2:b2Body = m_shape2.m_body;
  50. //b2Manifold m0;
  51. //memcpy(&m0, &m_manifold, sizeof(b2Manifold));
  52. // TODO: make sure this is completely necessary
  53. m0.Set(m_manifold);
  54. b2Collision.b2CollideCircles(m_manifold, m_shape1 as b2CircleShape, b1.m_xf, m_shape2 as b2CircleShape, b2.m_xf);
  55. var cp:b2ContactPoint = s_evalCP;
  56. cp.shape1 = m_shape1;
  57. cp.shape2 = m_shape2;
  58. cp.friction = m_friction;
  59. cp.restitution = m_restitution;
  60. if (m_manifold.pointCount > 0)
  61. {
  62. m_manifoldCount = 1;
  63. var mp:b2ManifoldPoint = m_manifold.points[ 0 ];
  64. if (m0.pointCount == 0)
  65. {
  66. mp.normalImpulse = 0.0;
  67. mp.tangentImpulse = 0.0;
  68. if (listener)
  69. {
  70. cp.position = b1.GetWorldPoint(mp.localPoint1);
  71. v1 = b1.GetLinearVelocityFromLocalPoint(mp.localPoint1);
  72. v2 = b2.GetLinearVelocityFromLocalPoint(mp.localPoint2);
  73. cp.velocity.Set(v2.x - v1.x, v2.y - v1.y);
  74. cp.normal.SetV(m_manifold.normal);
  75. cp.separation = mp.separation;
  76. cp.id.key = mp.id._key;
  77. listener.Add(cp);
  78. }
  79. } else
  80. {
  81. mp0 = m0.points[ 0 ];
  82. mp.normalImpulse = mp0.normalImpulse;
  83. mp.tangentImpulse = mp0.tangentImpulse;
  84. if (listener)
  85. {
  86. cp.position = b1.GetWorldPoint(mp.localPoint1);
  87. v1 = b1.GetLinearVelocityFromLocalPoint(mp.localPoint1);
  88. v2 = b2.GetLinearVelocityFromLocalPoint(mp.localPoint2);
  89. cp.velocity.Set(v2.x - v1.x, v2.y - v1.y);
  90. cp.normal.SetV(m_manifold.normal);
  91. cp.separation = mp.separation;
  92. cp.id.key = mp.id._key;
  93. listener.Persist(cp);
  94. }
  95. }
  96. }
  97. else
  98. {
  99. m_manifoldCount = 0;
  100. if (m0.pointCount > 0 && listener)
  101. {
  102. mp0 = m0.points[ 0 ];
  103. cp.position = b1.GetWorldPoint(mp0.localPoint1);
  104. v1 = b1.GetLinearVelocityFromLocalPoint(mp0.localPoint1);
  105. v2 = b2.GetLinearVelocityFromLocalPoint(mp0.localPoint2);
  106. cp.velocity.Set(v2.x - v1.x, v2.y - v1.y);
  107. cp.normal.SetV(m0.normal);
  108. cp.separation = mp0.separation;
  109. cp.id.key = mp0.id._key;
  110. listener.Remove(cp);
  111. }
  112. }
  113. }
  114. public override function GetManifolds():Array
  115. {
  116. return m_manifolds;
  117. }
  118. private var m_manifolds:Array = [new b2Manifold()];
  119. public var m_manifold:b2Manifold;
  120. private var m0:b2Manifold = new b2Manifold();
  121. };
  122. }