Skip to content
This repository
Browse code

FIX use of box2d svn trunk

  • Loading branch information...
commit 1999f25d73645f783842b8da6b1c23736dede589 1 parent 0c47576
PoUpA PoUpA authored

Showing 101 changed files with 9,500 additions and 2,488 deletions. Show diff stats Hide diff stats

  1. +1 1  application/Makefile
  2. +17 4 application/lib/box2d/Box2D.h
  3. +43 41 application/lib/box2d/Collision/Shapes/b2CircleShape.cpp
  4. +40 37 application/lib/box2d/Collision/Shapes/b2CircleShape.h
  5. +184 0 application/lib/box2d/Collision/Shapes/b2EdgeShape.cpp
  6. +194 0 application/lib/box2d/Collision/Shapes/b2EdgeShape.h
  7. +221 184 application/lib/box2d/Collision/Shapes/b2PolygonShape.cpp
  8. +56 89 application/lib/box2d/Collision/Shapes/b2PolygonShape.h
  9. +1 149 application/lib/box2d/Collision/Shapes/b2Shape.cpp
  10. +43 202 application/lib/box2d/Collision/Shapes/b2Shape.h
  11. +274 3 application/lib/box2d/Collision/b2BroadPhase.cpp
  12. +13 1 application/lib/box2d/Collision/b2BroadPhase.h
  13. +17 17 application/lib/box2d/Collision/b2CollideCircle.cpp
  14. +363 0 application/lib/box2d/Collision/b2CollideEdge.cpp
  15. +40 54 application/lib/box2d/Collision/b2CollidePoly.cpp
  16. +69 1 application/lib/box2d/Collision/b2Collision.cpp
  17. +63 26 application/lib/box2d/Collision/b2Collision.h
  18. +478 272 application/lib/box2d/Collision/b2Distance.cpp
  19. +192 0 application/lib/box2d/Collision/b2Distance.h
  20. +322 0 application/lib/box2d/Collision/b2DynamicTree.cpp
  21. +266 0 application/lib/box2d/Collision/b2DynamicTree.h
  22. +1 1  application/lib/box2d/Collision/b2PairManager.cpp
  23. +1 1  application/lib/box2d/Collision/b2PairManager.h
  24. +404 22 application/lib/box2d/Collision/b2TimeOfImpact.cpp
  25. +41 0 application/lib/box2d/Collision/b2TimeOfImpact.h
  26. +3 1 application/lib/box2d/Common/b2BlockAllocator.cpp
  27. +1 1  application/lib/box2d/Common/b2BlockAllocator.h
  28. +23 26 application/lib/box2d/Common/b2Math.cpp
  29. +189 71 application/lib/box2d/Common/b2Math.h
  30. +2 2 application/lib/box2d/Common/b2Settings.cpp
  31. +31 22 application/lib/box2d/Common/b2Settings.h
  32. +1 1  application/lib/box2d/Common/b2StackAllocator.cpp
  33. +1 1  application/lib/box2d/Common/b2StackAllocator.h
  34. +43 26 application/lib/box2d/Dynamics/Contacts/b2CircleContact.cpp
  35. +7 5 application/lib/box2d/Dynamics/Contacts/b2CircleContact.h
  36. +46 42 application/lib/box2d/Dynamics/Contacts/b2Contact.cpp
  37. +27 27 application/lib/box2d/Dynamics/Contacts/b2Contact.h
  38. +489 117 application/lib/box2d/Dynamics/Contacts/b2ContactSolver.cpp
  39. +9 8 application/lib/box2d/Dynamics/Contacts/b2ContactSolver.h
  40. +140 0 application/lib/box2d/Dynamics/Contacts/b2EdgeAndCircleContact.cpp
  41. +50 0 application/lib/box2d/Dynamics/Contacts/b2EdgeAndCircleContact.h
  42. +7 1 application/lib/box2d/Dynamics/Contacts/b2NullContact.h
  43. +44 29 application/lib/box2d/Dynamics/Contacts/b2PolyAndCircleContact.cpp
  44. +7 5 application/lib/box2d/Dynamics/Contacts/b2PolyAndCircleContact.h
  45. +174 0 application/lib/box2d/Dynamics/Contacts/b2PolyAndEdgeContact.cpp
  46. +47 0 application/lib/box2d/Dynamics/Contacts/b2PolyAndEdgeContact.h
  47. +43 27 application/lib/box2d/Dynamics/Contacts/b2PolyContact.cpp
  48. +7 5 application/lib/box2d/Dynamics/Contacts/b2PolyContact.h
  49. +119 0 application/lib/box2d/Dynamics/Controllers/b2BuoyancyController.cpp
  50. +102 0 application/lib/box2d/Dynamics/Controllers/b2BuoyancyController.h
  51. +46 0 application/lib/box2d/Dynamics/Controllers/b2ConstantAccelController.cpp
  52. +54 0 application/lib/box2d/Dynamics/Controllers/b2ConstantAccelController.h
  53. +47 0 application/lib/box2d/Dynamics/Controllers/b2ConstantForceController.cpp
  54. +54 0 application/lib/box2d/Dynamics/Controllers/b2ConstantForceController.h
  55. +110 0 application/lib/box2d/Dynamics/Controllers/b2Controller.cpp
  56. +133 0 application/lib/box2d/Dynamics/Controllers/b2Controller.h
  57. +70 0 application/lib/box2d/Dynamics/Controllers/b2GravityController.cpp
  58. +59 0 application/lib/box2d/Dynamics/Controllers/b2GravityController.h
  59. +72 0 application/lib/box2d/Dynamics/Controllers/b2TensorDampingController.cpp
  60. +69 0 application/lib/box2d/Dynamics/Controllers/b2TensorDampingController.h
  61. +10 7 application/lib/box2d/Dynamics/Joints/b2DistanceJoint.cpp
  62. +3 3 application/lib/box2d/Dynamics/Joints/b2DistanceJoint.h
  63. +27 23 application/lib/box2d/Dynamics/Joints/b2GearJoint.cpp
  64. +4 4 application/lib/box2d/Dynamics/Joints/b2GearJoint.h
  65. +12 0 application/lib/box2d/Dynamics/Joints/b2Joint.cpp
  66. +18 7 application/lib/box2d/Dynamics/Joints/b2Joint.h
  67. +559 0 application/lib/box2d/Dynamics/Joints/b2LineJoint.cpp
  68. +168 0 application/lib/box2d/Dynamics/Joints/b2LineJoint.h
  69. +40 33 application/lib/box2d/Dynamics/Joints/b2MouseJoint.cpp
  70. +7 12 application/lib/box2d/Dynamics/Joints/b2MouseJoint.h
  71. +330 221 application/lib/box2d/Dynamics/Joints/b2PrismaticJoint.cpp
  72. +10 13 application/lib/box2d/Dynamics/Joints/b2PrismaticJoint.h
  73. +42 44 application/lib/box2d/Dynamics/Joints/b2PulleyJoint.cpp
  74. +6 11 application/lib/box2d/Dynamics/Joints/b2PulleyJoint.h
  75. +208 133 application/lib/box2d/Dynamics/Joints/b2RevoluteJoint.cpp
  76. +6 8 application/lib/box2d/Dynamics/Joints/b2RevoluteJoint.h
  77. +82 68 application/lib/box2d/Dynamics/b2Body.cpp
  78. +167 32 application/lib/box2d/Dynamics/b2Body.h
  79. +65 67 application/lib/box2d/Dynamics/b2ContactManager.cpp
  80. +3 3 application/lib/box2d/Dynamics/b2ContactManager.h
  81. +93 0 application/lib/box2d/Dynamics/b2EdgeChain.cpp
  82. +229 0 application/lib/box2d/Dynamics/b2EdgeChain.h
  83. +237 0 application/lib/box2d/Dynamics/b2Fixture.cpp
  84. +789 0 application/lib/box2d/Dynamics/b2Fixture.h
  85. +129 66 application/lib/box2d/Dynamics/b2Island.cpp
  86. +20 4 application/lib/box2d/Dynamics/b2Island.h
  87. +296 133 application/lib/box2d/Dynamics/b2World.cpp
  88. +89 24 application/lib/box2d/Dynamics/b2World.h
  89. +19 8 application/lib/box2d/Dynamics/b2WorldCallbacks.cpp
  90. +15 10 application/lib/box2d/Dynamics/b2WorldCallbacks.h
  91. +1 1  application/polukili.pnproj
  92. +1 0  application/source/Actor.cpp
  93. +1 0  application/source/Actor.h
  94. +3 3 application/source/ActorAction.h
  95. +3 2 application/source/Bullets/Bullet.cpp
  96. +1 1  application/source/Bullets/Bullet.h
  97. +2 2 application/source/Ennemies/Bee.cpp
  98. +3 3 application/source/Level.cpp
  99. +95 19 application/source/Players/Player.cpp
  100. +37 1 application/source/Players/Player.h
  101. BIN  polukili/data/cross_hair.png
2  application/Makefile
@@ -22,7 +22,7 @@ include $(DEVKITPPC)/wii_rules
22 22 #---------------------------------------------------------------------------------
23 23 TARGET := ../polukili/boot
24 24 BUILD := build
25   -SOURCES := source source/Bullets source/Ennemies source/Items source/Pets source/Players lib/box2d/Collision lib/box2d/Collision/Shapes lib/box2d/Common lib/box2d/Dynamics lib/box2d/Dynamics/Contacts lib/box2d/Dynamics/Joints
  25 +SOURCES := source source/Bullets source/Ennemies source/Items source/Pets source/Players lib/box2d/Collision lib/box2d/Collision/Shapes lib/box2d/Common lib/box2d/Dynamics lib/box2d/Dynamics/Contacts lib/box2d/Dynamics/Controllers lib/box2d/Dynamics/Joints
26 26 DATA :=
27 27 TEXTURES := textures
28 28 INCLUDES := source lib/box2d
21 application/lib/box2d/Box2D.h
... ... @@ -1,5 +1,5 @@
1 1 /*
2   -* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  2 +* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
3 3 *
4 4 * This software is provided 'as-is', without any express or implied
5 5 * warranty. In no event will the authors be held liable for any damages
@@ -35,18 +35,31 @@ For discussion please visit http://www.box2d.org/forum
35 35
36 36 #include "Collision/Shapes/b2CircleShape.h"
37 37 #include "Collision/Shapes/b2PolygonShape.h"
  38 +#include "Collision/Shapes/b2EdgeShape.h"
38 39 #include "Collision/b2BroadPhase.h"
  40 +#include "Collision/b2Distance.h"
  41 +#include "Collision/b2DynamicTree.h"
  42 +#include "Collision/b2TimeOfImpact.h"
  43 +#include "Dynamics/b2Body.h"
  44 +#include "Dynamics/b2EdgeChain.h"
  45 +#include "Dynamics/b2Fixture.h"
39 46 #include "Dynamics/b2WorldCallbacks.h"
40 47 #include "Dynamics/b2World.h"
41   -#include "Dynamics/b2Body.h"
42 48
43 49 #include "Dynamics/Contacts/b2Contact.h"
44 50
45 51 #include "Dynamics/Joints/b2DistanceJoint.h"
  52 +#include "Dynamics/Joints/b2GearJoint.h"
  53 +#include "Dynamics/Joints/b2LineJoint.h"
46 54 #include "Dynamics/Joints/b2MouseJoint.h"
47 55 #include "Dynamics/Joints/b2PrismaticJoint.h"
48   -#include "Dynamics/Joints/b2RevoluteJoint.h"
49 56 #include "Dynamics/Joints/b2PulleyJoint.h"
50   -#include "Dynamics/Joints/b2GearJoint.h"
  57 +#include "Dynamics/Joints/b2RevoluteJoint.h"
  58 +
  59 +#include "Dynamics/Controllers/b2BuoyancyController.h"
  60 +#include "Dynamics/Controllers/b2ConstantForceController.h"
  61 +#include "Dynamics/Controllers/b2ConstantAccelController.h"
  62 +#include "Dynamics/Controllers/b2GravityController.h"
  63 +#include "Dynamics/Controllers/b2TensorDampingController.h"
51 64
52 65 #endif
84 application/lib/box2d/Collision/Shapes/b2CircleShape.cpp
... ... @@ -1,5 +1,5 @@
1 1 /*
2   -* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  2 +* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
3 3 *
4 4 * This software is provided 'as-is', without any express or implied
5 5 * warranty. In no event will the authors be held liable for any damages
@@ -18,28 +18,9 @@
18 18
19 19 #include "b2CircleShape.h"
20 20
21   -b2CircleShape::b2CircleShape(const b2ShapeDef* def)
22   -: b2Shape(def)
23   -{
24   - b2Assert(def->type == e_circleShape);
25   - const b2CircleDef* circleDef = (const b2CircleDef*)def;
26   -
27   - m_type = e_circleShape;
28   - m_localPosition = circleDef->localPosition;
29   - m_radius = circleDef->radius;
30   -}
31   -
32   -void b2CircleShape::UpdateSweepRadius(const b2Vec2& center)
33   -{
34   - // Update the sweep radius (maximum radius) as measured from
35   - // a local center point.
36   - b2Vec2 d = m_localPosition - center;
37   - m_sweepRadius = d.Length() + m_radius - b2_toiSlop;
38   -}
39   -
40 21 bool b2CircleShape::TestPoint(const b2XForm& transform, const b2Vec2& p) const
41 22 {
42   - b2Vec2 center = transform.position + b2Mul(transform.R, m_localPosition);
  23 + b2Vec2 center = transform.position + b2Mul(transform.R, m_p);
43 24 b2Vec2 d = p - center;
44 25 return b2Dot(d, d) <= m_radius * m_radius;
45 26 }
@@ -48,20 +29,21 @@ bool b2CircleShape::TestPoint(const b2XForm& transform, const b2Vec2& p) const
48 29 // From Section 3.1.2
49 30 // x = s + a * r
50 31 // norm(x) = radius
51   -bool b2CircleShape::TestSegment(const b2XForm& transform,
  32 +b2SegmentCollide b2CircleShape::TestSegment(const b2XForm& transform,
52 33 float32* lambda,
53 34 b2Vec2* normal,
54 35 const b2Segment& segment,
55 36 float32 maxLambda) const
56 37 {
57   - b2Vec2 position = transform.position + b2Mul(transform.R, m_localPosition);
  38 + b2Vec2 position = transform.position + b2Mul(transform.R, m_p);
58 39 b2Vec2 s = segment.p1 - position;
59 40 float32 b = b2Dot(s, s) - m_radius * m_radius;
60 41
61 42 // Does the segment start inside the circle?
62 43 if (b < 0.0f)
63 44 {
64   - return false;
  45 + *lambda = 0;
  46 + return b2_startsInsideCollide;
65 47 }
66 48
67 49 // Solve quadratic equation.
@@ -73,7 +55,7 @@ bool b2CircleShape::TestSegment(const b2XForm& transform,
73 55 // Check for negative discriminant and short segment.
74 56 if (sigma < 0.0f || rr < B2_FLT_EPSILON)
75 57 {
76   - return false;
  58 + return b2_missCollide;
77 59 }
78 60
79 61 // Find the point of intersection of the line with the circle.
@@ -86,35 +68,55 @@ bool b2CircleShape::TestSegment(const b2XForm& transform,
86 68 *lambda = a;
87 69 *normal = s + a * r;
88 70 normal->Normalize();
89   - return true;
  71 + return b2_hitCollide;
90 72 }
91 73
92   - return false;
  74 + return b2_missCollide;
93 75 }
94 76
95 77 void b2CircleShape::ComputeAABB(b2AABB* aabb, const b2XForm& transform) const
96 78 {
97   - b2Vec2 p = transform.position + b2Mul(transform.R, m_localPosition);
  79 + b2Vec2 p = transform.position + b2Mul(transform.R, m_p);
98 80 aabb->lowerBound.Set(p.x - m_radius, p.y - m_radius);
99 81 aabb->upperBound.Set(p.x + m_radius, p.y + m_radius);
100 82 }
101 83
102   -void b2CircleShape::ComputeSweptAABB(b2AABB* aabb, const b2XForm& transform1, const b2XForm& transform2) const
  84 +
  85 +void b2CircleShape::ComputeMass(b2MassData* massData, float32 density) const
103 86 {
104   - b2Vec2 p1 = transform1.position + b2Mul(transform1.R, m_localPosition);
105   - b2Vec2 p2 = transform2.position + b2Mul(transform2.R, m_localPosition);
106   - b2Vec2 lower = b2Min(p1, p2);
107   - b2Vec2 upper = b2Max(p1, p2);
  87 + massData->mass = density * b2_pi * m_radius * m_radius;
  88 + massData->center = m_p;
108 89
109   - aabb->lowerBound.Set(lower.x - m_radius, lower.y - m_radius);
110   - aabb->upperBound.Set(upper.x + m_radius, upper.y + m_radius);
  90 + // inertia about the local origin
  91 + massData->I = massData->mass * (0.5f * m_radius * m_radius + b2Dot(m_p, m_p));
111 92 }
112 93
113   -void b2CircleShape::ComputeMass(b2MassData* massData) const
  94 +float32 b2CircleShape::ComputeSubmergedArea( const b2Vec2& normal,
  95 + float32 offset,
  96 + const b2XForm& xf,
  97 + b2Vec2* c) const
114 98 {
115   - massData->mass = m_density * b2_pi * m_radius * m_radius;
116   - massData->center = m_localPosition;
117   -
118   - // inertia about the local origin
119   - massData->I = massData->mass * (0.5f * m_radius * m_radius + b2Dot(m_localPosition, m_localPosition));
  99 + b2Vec2 p = b2Mul(xf,m_p);
  100 + float32 l = -(b2Dot(normal,p) - offset);
  101 + if(l<-m_radius+B2_FLT_EPSILON){
  102 + //Completely dry
  103 + return 0;
  104 + }
  105 + if(l>m_radius){
  106 + //Completely wet
  107 + *c = p;
  108 + return b2_pi*m_radius*m_radius;
  109 + }
  110 +
  111 + //Magic
  112 + float32 r2 = m_radius*m_radius;
  113 + float32 l2 = l*l;
  114 + //TODO: write b2Sqrt to handle fixed point case.
  115 + float32 area = r2 * (asin(l/m_radius) + b2_pi/2.0f)+ l * b2Sqrt(r2 - l2);
  116 + float32 com = -2.0f/3.0f*pow(r2-l2,1.5f)/area;
  117 +
  118 + c->x = p.x + normal.x * com;
  119 + c->y = p.y + normal.y * com;
  120 +
  121 + return area;
120 122 }
77 application/lib/box2d/Collision/Shapes/b2CircleShape.h
... ... @@ -1,5 +1,5 @@
1 1 /*
2   -* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  2 +* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
3 3 *
4 4 * This software is provided 'as-is', without any express or implied
5 5 * warranty. In no event will the authors be held liable for any damages
@@ -21,29 +21,17 @@
21 21
22 22 #include "b2Shape.h"
23 23
24   -/// This structure is used to build circle shapes.
25   -struct b2CircleDef : public b2ShapeDef
26   -{
27   - b2CircleDef()
28   - {
29   - type = e_circleShape;
30   - localPosition.SetZero();
31   - radius = 1.0f;
32   - }
33   -
34   - b2Vec2 localPosition;
35   - float32 radius;
36   -};
37   -
38 24 /// A circle shape.
39 25 class b2CircleShape : public b2Shape
40 26 {
41 27 public:
  28 + b2CircleShape() { m_type = b2_circleShape; }
  29 +
42 30 /// @see b2Shape::TestPoint
43 31 bool TestPoint(const b2XForm& transform, const b2Vec2& p) const;
44 32
45 33 /// @see b2Shape::TestSegment
46   - bool TestSegment( const b2XForm& transform,
  34 + b2SegmentCollide TestSegment( const b2XForm& transform,
47 35 float32* lambda,
48 36 b2Vec2* normal,
49 37 const b2Segment& segment,
@@ -52,41 +40,56 @@ class b2CircleShape : public b2Shape
52 40 /// @see b2Shape::ComputeAABB
53 41 void ComputeAABB(b2AABB* aabb, const b2XForm& transform) const;
54 42
55   - /// @see b2Shape::ComputeSweptAABB
56   - void ComputeSweptAABB( b2AABB* aabb,
57   - const b2XForm& transform1,
58   - const b2XForm& transform2) const;
59   -
60 43 /// @see b2Shape::ComputeMass
61   - void ComputeMass(b2MassData* massData) const;
  44 + void ComputeMass(b2MassData* massData, float32 density) const;
62 45
63   - /// Get the local position of this circle in its parent body.
64   - const b2Vec2& GetLocalPosition() const;
  46 + /// @see b2Shape::ComputeSubmergedArea
  47 + float32 ComputeSubmergedArea( const b2Vec2& normal,
  48 + float32 offset,
  49 + const b2XForm& xf,
  50 + b2Vec2* c) const;
65 51
66   - /// Get the radius of this circle.
67   - float32 GetRadius() const;
  52 + /// @see b2Shape::ComputeSweepRadius
  53 + float32 ComputeSweepRadius(const b2Vec2& pivot) const;
68 54
69   -private:
  55 + /// Get the supporting vertex index in the given direction.
  56 + int32 GetSupport(const b2Vec2& d) const;
70 57
71   - friend class b2Shape;
  58 + /// Get the supporting vertex in the given direction.
  59 + const b2Vec2& GetSupportVertex(const b2Vec2& d) const;
72 60
73   - b2CircleShape(const b2ShapeDef* def);
  61 + /// Get the vertex count.
  62 + int32 GetVertexCount() const { return 1; }
74 63
75   - void UpdateSweepRadius(const b2Vec2& center);
  64 + /// Get a vertex by index. Used by b2Distance.
  65 + const b2Vec2& GetVertex(int32 index) const;
76 66
77   - // Local position in parent body
78   - b2Vec2 m_localPosition;
79   - float32 m_radius;
  67 + /// Position
  68 + b2Vec2 m_p;
80 69 };
81 70
82   -inline const b2Vec2& b2CircleShape::GetLocalPosition() const
  71 +inline int32 b2CircleShape::GetSupport(const b2Vec2 &d) const
  72 +{
  73 + B2_NOT_USED(d);
  74 + return 0;
  75 +}
  76 +
  77 +inline const b2Vec2& b2CircleShape::GetSupportVertex(const b2Vec2 &d) const
  78 +{
  79 + B2_NOT_USED(d);
  80 + return m_p;
  81 +}
  82 +
  83 +inline const b2Vec2& b2CircleShape::GetVertex(int32 index) const
83 84 {
84   - return m_localPosition;
  85 + B2_NOT_USED(index);
  86 + b2Assert(index == 0);
  87 + return m_p;
85 88 }
86 89
87   -inline float32 b2CircleShape::GetRadius() const
  90 +inline float32 b2CircleShape::ComputeSweepRadius(const b2Vec2& pivot) const
88 91 {
89   - return m_radius;
  92 + return b2Distance(m_p, pivot);
90 93 }
91 94
92 95 #endif
184 application/lib/box2d/Collision/Shapes/b2EdgeShape.cpp
... ... @@ -0,0 +1,184 @@
  1 +/*
  2 +* Copyright (c) 2006-2009 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 +
  19 +#include "b2EdgeShape.h"
  20 +
  21 +b2EdgeShape::b2EdgeShape()
  22 +{
  23 + m_type = b2_edgeShape;
  24 + m_radius = b2_polygonRadius;
  25 + m_prevEdge = NULL;
  26 + m_nextEdge = NULL;
  27 +}
  28 +
  29 +b2EdgeShape::~b2EdgeShape()
  30 +{
  31 + if (m_prevEdge)
  32 + {
  33 + m_prevEdge->m_nextEdge = NULL;
  34 + }
  35 +
  36 + if (m_nextEdge)
  37 + {
  38 + m_nextEdge->m_prevEdge = NULL;
  39 + }
  40 +}
  41 +
  42 +void b2EdgeShape::Set(const b2Vec2& v1, const b2Vec2& v2)
  43 +{
  44 + m_v1 = v1;
  45 + m_v2 = v2;
  46 +
  47 + m_direction = m_v2 - m_v1;
  48 + m_length = m_direction.Normalize();
  49 + m_normal.Set(m_direction.y, -m_direction.x);
  50 +
  51 + m_cornerDir1 = m_normal;
  52 + m_cornerDir2 = -1.0f * m_normal;
  53 +}
  54 +
  55 +bool b2EdgeShape::TestPoint(const b2XForm& transform, const b2Vec2& p) const
  56 +{
  57 + B2_NOT_USED(transform);
  58 + B2_NOT_USED(p);
  59 + return false;
  60 +}
  61 +
  62 +b2SegmentCollide b2EdgeShape::TestSegment(const b2XForm& transform,
  63 + float32* lambda,
  64 + b2Vec2* normal,
  65 + const b2Segment& segment,
  66 + float32 maxLambda) const
  67 +{
  68 + b2Vec2 r = segment.p2 - segment.p1;
  69 + b2Vec2 v1 = b2Mul(transform, m_v1);
  70 + b2Vec2 d = b2Mul(transform, m_v2) - v1;
  71 + b2Vec2 n = b2Cross(d, 1.0f);
  72 +
  73 + const float32 k_slop = 100.0f * B2_FLT_EPSILON;
  74 + float32 denom = -b2Dot(r, n);
  75 +
  76 + // Cull back facing collision and ignore parallel segments.
  77 + if (denom > k_slop)
  78 + {
  79 + // Does the segment intersect the infinite line associated with this segment?
  80 + b2Vec2 b = segment.p1 - v1;
  81 + float32 a = b2Dot(b, n);
  82 +
  83 + if (0.0f <= a && a <= maxLambda * denom)
  84 + {
  85 + float32 mu2 = -r.x * b.y + r.y * b.x;
  86 +
  87 + // Does the segment intersect this segment?
  88 + if (-k_slop * denom <= mu2 && mu2 <= denom * (1.0f + k_slop))
  89 + {
  90 + a /= denom;
  91 + n.Normalize();
  92 + *lambda = a;
  93 + *normal = n;
  94 + return b2_hitCollide;
  95 + }
  96 + }
  97 + }
  98 +
  99 + return b2_missCollide;
  100 +}
  101 +
  102 +void b2EdgeShape::ComputeAABB(b2AABB* aabb, const b2XForm& transform) const
  103 +{
  104 + b2Vec2 v1 = b2Mul(transform, m_v1);
  105 + b2Vec2 v2 = b2Mul(transform, m_v2);
  106 +
  107 + b2Vec2 r(m_radius, m_radius);
  108 + aabb->lowerBound = b2Min(v1, v2) - r;
  109 + aabb->upperBound = b2Max(v1, v2) + r;
  110 +}
  111 +
  112 +void b2EdgeShape::ComputeMass(b2MassData* massData, float32 density) const
  113 +{
  114 + B2_NOT_USED(density);
  115 +
  116 + massData->mass = 0.0f;
  117 + massData->center = m_v1;
  118 + massData->I = 0.0f;
  119 +}
  120 +
  121 +void b2EdgeShape::SetPrevEdge(b2EdgeShape* edge, const b2Vec2& cornerDir, bool convex)
  122 +{
  123 + m_prevEdge = edge;
  124 + m_cornerDir1 = cornerDir;
  125 + m_cornerConvex1 = convex;
  126 +}
  127 +
  128 +void b2EdgeShape::SetNextEdge(b2EdgeShape* edge, const b2Vec2& cornerDir, bool convex)
  129 +{
  130 + m_nextEdge = edge;
  131 + m_cornerDir2 = cornerDir;
  132 + m_cornerConvex2 = convex;
  133 +}
  134 +
  135 +float32 b2EdgeShape::ComputeSubmergedArea( const b2Vec2& normal,
  136 + float32 offset,
  137 + const b2XForm& xf,
  138 + b2Vec2* c) const
  139 +{
  140 + //Note that v0 is independent of any details of the specific edge
  141 + //We are relying on v0 being consistent between multiple edges of the same body
  142 + b2Vec2 v0 = offset * normal;
  143 + //b2Vec2 v0 = xf.position + (offset - b2Dot(normal, xf.position)) * normal;
  144 +
  145 + b2Vec2 v1 = b2Mul(xf, m_v1);
  146 + b2Vec2 v2 = b2Mul(xf, m_v2);
  147 +
  148 + float32 d1 = b2Dot(normal, v1) - offset;
  149 + float32 d2 = b2Dot(normal, v2) - offset;
  150 +
  151 + if (d1 > 0.0f)
  152 + {
  153 + if (d2 > 0.0f)
  154 + {
  155 + return 0.0f;
  156 + }
  157 + else
  158 + {
  159 + v1 = -d2 / (d1 - d2) * v1 + d1 / (d1 - d2) * v2;
  160 + }
  161 + }
  162 + else
  163 + {
  164 + if (d2 > 0.0f)
  165 + {
  166 + v2 = -d2 / (d1 - d2) * v1 + d1 / (d1 - d2) * v2;
  167 + }
  168 + else
  169 + {
  170 + //Nothing
  171 + }
  172 + }
  173 +
  174 + // v0,v1,v2 represents a fully submerged triangle
  175 + float32 k_inv3 = 1.0f / 3.0f;
  176 +
  177 + // Area weighted centroid
  178 + *c = k_inv3 * (v0 + v1 + v2);
  179 +
  180 + b2Vec2 e1 = v1 - v0;
  181 + b2Vec2 e2 = v2 - v0;
  182 +
  183 + return 0.5f * b2Cross(e1, e2);
  184 +}
194 application/lib/box2d/Collision/Shapes/b2EdgeShape.h
... ... @@ -0,0 +1,194 @@
  1 +/*
  2 +* Copyright (c) 2006-2009 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 +
  19 +#ifndef B2_EDGE_SHAPE_H
  20 +#define B2_EDGE_SHAPE_H
  21 +
  22 +#include "b2Shape.h"
  23 +
  24 +/// A circle shape.
  25 +class b2EdgeShape : public b2Shape
  26 +{
  27 +public:
  28 + b2EdgeShape();
  29 + ~b2EdgeShape();
  30 +
  31 + /// Initialize this edge using the two vertices.
  32 + void Set(const b2Vec2& v1, const b2Vec2& v2);
  33 +
  34 + /// @see b2Shape::TestPoint
  35 + bool TestPoint(const b2XForm& transform, const b2Vec2& p) const;
  36 +
  37 + /// @see b2Shape::TestSegment
  38 + b2SegmentCollide TestSegment( const b2XForm& transform,
  39 + float32* lambda,
  40 + b2Vec2* normal,
  41 + const b2Segment& segment,
  42 + float32 maxLambda) const;
  43 +
  44 + /// @see b2Shape::ComputeAABB
  45 + void ComputeAABB(b2AABB* aabb, const b2XForm& transform) const;
  46 +
  47 + /// @see b2Shape::ComputeMass
  48 + void ComputeMass(b2MassData* massData, float32 density) const;
  49 +
  50 + /// @warning This only gives a consistent and sensible answer when when summed over a body only contains loops of edges
  51 + /// @see b2Shape::ComputeSubmergedArea
  52 + float32 ComputeSubmergedArea( const b2Vec2& normal,
  53 + float32 offset,
  54 + const b2XForm& xf,
  55 + b2Vec2* c) const;
  56 +
  57 + /// @see b2Shape::ComputeSweepRadius
  58 + float32 ComputeSweepRadius(const b2Vec2& pivot) const;
  59 +
  60 + /// Linear distance from vertex1 to vertex2:
  61 + float32 GetLength() const;
  62 +
  63 + /// Local position of vertex in parent body
  64 + const b2Vec2& GetVertex1() const;
  65 +
  66 + /// Local position of vertex in parent body
  67 + const b2Vec2& GetVertex2() const;
  68 +
  69 + /// Perpendicular unit vector point, pointing from the solid side to the empty side:
  70 + const b2Vec2& GetNormalVector() const;
  71 +
  72 + /// Parallel unit vector, pointing from vertex1 to vertex2:
  73 + const b2Vec2& GetDirectionVector() const;
  74 +
  75 + const b2Vec2& GetCorner1Vector() const;
  76 +
  77 + const b2Vec2& GetCorner2Vector() const;
  78 +
  79 + bool Corner1IsConvex() const;
  80 +
  81 + bool Corner2IsConvex() const;
  82 +
  83 + /// Get the supporting vertex index in the given direction.
  84 + int32 GetSupport(const b2Vec2& d) const;
  85 +
  86 + /// Get the supporting vertex in the given direction.
  87 + const b2Vec2& GetSupportVertex(const b2Vec2& d) const;
  88 +
  89 + /// Get the vertex count.
  90 + int32 GetVertexCount() const { return 2; }
  91 +
  92 + /// Get a vertex by index. Used by b2Distance.
  93 + const b2Vec2& GetVertex(int32 index) const;
  94 +
  95 + /// Get the next edge in the chain.
  96 + b2EdgeShape* GetNextEdge() const;
  97 +
  98 + /// Get the previous edge in the chain.
  99 + b2EdgeShape* GetPrevEdge() const;
  100 +
  101 + void SetPrevEdge(b2EdgeShape* edge, const b2Vec2& cornerDir, bool convex);
  102 + void SetNextEdge(b2EdgeShape* edge, const b2Vec2& cornerDir, bool convex);
  103 +
  104 + b2Vec2 m_v1;
  105 + b2Vec2 m_v2;
  106 +
  107 + float32 m_length;
  108 +
  109 + b2Vec2 m_normal;
  110 +
  111 + b2Vec2 m_direction;
  112 +
  113 + // Unit vector halfway between m_direction and m_prevEdge.m_direction:
  114 + b2Vec2 m_cornerDir1;
  115 +
  116 + // Unit vector halfway between m_direction and m_nextEdge.m_direction:
  117 + b2Vec2 m_cornerDir2;
  118 +
  119 + bool m_cornerConvex1;
  120 + bool m_cornerConvex2;
  121 +
  122 + b2EdgeShape* m_nextEdge;
  123 + b2EdgeShape* m_prevEdge;
  124 +};
  125 +
  126 +inline float32 b2EdgeShape::GetLength() const
  127 +{
  128 + return m_length;
  129 +}
  130 +
  131 +inline const b2Vec2& b2EdgeShape::GetVertex1() const
  132 +{
  133 + return m_v1;
  134 +}
  135 +
  136 +inline const b2Vec2& b2EdgeShape::GetVertex2() const
  137 +{
  138 + return m_v2;
  139 +}
  140 +
  141 +inline const b2Vec2& b2EdgeShape::GetNormalVector() const
  142 +{
  143 + return m_normal;
  144 +}
  145 +
  146 +inline const b2Vec2& b2EdgeShape::GetDirectionVector() const
  147 +{
  148 + return m_direction;
  149 +}
  150 +
  151 +inline const b2Vec2& b2EdgeShape::GetCorner1Vector() const
  152 +{
  153 + return m_cornerDir1;
  154 +}
  155 +
  156 +inline const b2Vec2& b2EdgeShape::GetCorner2Vector() const
  157 +{
  158 + return m_cornerDir2;
  159 +}
  160 +
  161 +inline int32 b2EdgeShape::GetSupport(const b2Vec2& d) const
  162 +{
  163 + return b2Dot(m_v1, d) > b2Dot(m_v2, d) ? 0 : 1;
  164 +}
  165 +
  166 +inline const b2Vec2& b2EdgeShape::GetSupportVertex(const b2Vec2& d) const
  167 +{
  168 + return b2Dot(m_v1, d) > b2Dot(m_v2, d) ? m_v1 : m_v2;
  169 +}
  170 +
  171 +inline const b2Vec2& b2EdgeShape::GetVertex(int32 index) const
  172 +{
  173 + b2Assert(0 <= index && index < 2);
  174 + return (&m_v1)[index];
  175 +}
  176 +
  177 +inline bool b2EdgeShape::Corner1IsConvex() const
  178 +{
  179 + return m_cornerConvex1;
  180 +}
  181 +
  182 +inline bool b2EdgeShape::Corner2IsConvex() const
  183 +{
  184 + return m_cornerConvex2;
  185 +}
  186 +
  187 +inline float32 b2EdgeShape::ComputeSweepRadius(const b2Vec2& pivot) const
  188 +{
  189 + float32 ds1 = b2DistanceSquared(m_v1, pivot);
  190 + float32 ds2 = b2DistanceSquared(m_v2, pivot);
  191 + return b2Sqrt(b2Max(ds1, ds2));
  192 +}
  193 +
  194 +#endif
405 application/lib/box2d/Collision/Shapes/b2PolygonShape.cpp
... ... @@ -1,6 +1,5 @@
1   -
2 1 /*
3   -* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  2 +* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
4 3 *
5 4 * This software is provided 'as-is', without any express or implied
6 5 * warranty. In no event will the authors be held liable for any damages
@@ -19,25 +18,42 @@
19 18
20 19 #include "b2PolygonShape.h"
21 20
22   -void b2PolygonDef::SetAsBox(float32 hx, float32 hy)
  21 +void b2PolygonShape::SetAsBox(float32 hx, float32 hy)
23 22 {
24   - vertexCount = 4;
25   - vertices[0].Set(-hx, -hy);
26   - vertices[1].Set( hx, -hy);
27   - vertices[2].Set( hx, hy);
28   - vertices[3].Set(-hx, hy);
  23 + m_vertexCount = 4;
  24 + m_vertices[0].Set(-hx, -hy);
  25 + m_vertices[1].Set( hx, -hy);
  26 + m_vertices[2].Set( hx, hy);
  27 + m_vertices[3].Set(-hx, hy);
  28 + m_normals[0].Set(0.0f, -1.0f);
  29 + m_normals[1].Set(1.0f, 0.0f);
  30 + m_normals[2].Set(0.0f, 1.0f);
  31 + m_normals[3].Set(-1.0f, 0.0f);
  32 + m_centroid.SetZero();
29 33 }
30 34
31   -void b2PolygonDef::SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle)
  35 +void b2PolygonShape::SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle)
32 36 {
33   - SetAsBox(hx, hy);
  37 + m_vertexCount = 4;
  38 + m_vertices[0].Set(-hx, -hy);
  39 + m_vertices[1].Set( hx, -hy);
  40 + m_vertices[2].Set( hx, hy);
  41 + m_vertices[3].Set(-hx, hy);
  42 + m_normals[0].Set(0.0f, -1.0f);
  43 + m_normals[1].Set(1.0f, 0.0f);
  44 + m_normals[2].Set(0.0f, 1.0f);
  45 + m_normals[3].Set(-1.0f, 0.0f);
  46 + m_centroid = center;
  47 +
34 48 b2XForm xf;
35 49 xf.position = center;
36 50 xf.R.Set(angle);
37 51
38   - for (int32 i = 0; i < vertexCount; ++i)
  52 + // Transform vertices and normals.
  53 + for (int32 i = 0; i < m_vertexCount; ++i)
39 54 {
40   - vertices[i] = b2Mul(xf, vertices[i]);
  55 + m_vertices[i] = b2Mul(xf, m_vertices[i]);
  56 + m_normals[i] = b2Mul(xf.R, m_normals[i]);
41 57 }
42 58 }
43 59
@@ -87,69 +103,15 @@ static b2Vec2 ComputeCentroid(const b2Vec2* vs, int32 count)
87 103 return c;
88 104 }
89 105
90   -// http://www.geometrictools.com/Documentation/MinimumAreaRectangle.pdf
91   -static void ComputeOBB(b2OBB* obb, const b2Vec2* vs, int32 count)
92   -{
93   - b2Assert(count <= b2_maxPolygonVertices);
94   - b2Vec2 p[b2_maxPolygonVertices + 1];
95   - for (int32 i = 0; i < count; ++i)
96   - {
97   - p[i] = vs[i];
98   - }
99   - p[count] = p[0];
100   -
101   - float32 minArea = B2_FLT_MAX;
102   -
103   - for (int32 i = 1; i <= count; ++i)
104   - {
105   - b2Vec2 root = p[i-1];
106   - b2Vec2 ux = p[i] - root;
107   - float32 length = ux.Normalize();
108   - b2Assert(length > B2_FLT_EPSILON);
109   - b2Vec2 uy(-ux.y, ux.x);
110   - b2Vec2 lower(B2_FLT_MAX, B2_FLT_MAX);
111   - b2Vec2 upper(-B2_FLT_MAX, -B2_FLT_MAX);
112   -
113   - for (int32 j = 0; j < count; ++j)
114   - {
115   - b2Vec2 d = p[j] - root;
116   - b2Vec2 r;
117   - r.x = b2Dot(ux, d);
118   - r.y = b2Dot(uy, d);
119   - lower = b2Min(lower, r);
120   - upper = b2Max(upper, r);
121   - }
122   -
123   - float32 area = (upper.x - lower.x) * (upper.y - lower.y);
124   - if (area < 0.95f * minArea)
125   - {
126   - minArea = area;
127   - obb->R.col1 = ux;
128   - obb->R.col2 = uy;
129   - b2Vec2 center = 0.5f * (lower + upper);
130   - obb->center = root + b2Mul(obb->R, center);
131   - obb->extents = 0.5f * (upper - lower);
132   - }
133   - }
134   -
135   - b2Assert(minArea < B2_FLT_MAX);
136   -}
137   -
138   -b2PolygonShape::b2PolygonShape(const b2ShapeDef* def)
139   - : b2Shape(def)
  106 +void b2PolygonShape::Set(const b2Vec2* vertices, int32 count)
140 107 {
141   - b2Assert(def->type == e_polygonShape);
142   - m_type = e_polygonShape;
143   - const b2PolygonDef* poly = (const b2PolygonDef*)def;
144   -
145   - // Get the vertices transformed into the body frame.
146   - m_vertexCount = poly->vertexCount;
147   - b2Assert(3 <= m_vertexCount && m_vertexCount <= b2_maxPolygonVertices);
  108 + b2Assert(3 <= count && count <= b2_maxPolygonVertices);
  109 + m_vertexCount = count;
148 110
149 111 // Copy vertices.
150 112 for (int32 i = 0; i < m_vertexCount; ++i)
151 113 {
152   - m_vertices[i] = poly->vertices[i];
  114 + m_vertices[i] = vertices[i];
153 115 }
154 116
155 117 // Compute normals. Ensure the edges have non-zero length.
@@ -164,82 +126,34 @@ b2PolygonShape::b2PolygonShape(const b2ShapeDef* def)
164 126 }
165 127
166 128 #ifdef _DEBUG
167   - // Ensure the polygon is convex.
  129 + // Ensure the polygon is convex and the interior
  130 + // is to the left of each edge.
168 131 for (int32 i = 0; i < m_vertexCount; ++i)
169 132 {
  133 + int32 i1 = i;
  134 + int32 i2 = i + 1 < m_vertexCount ? i + 1 : 0;
  135 + b2Vec2 edge = m_vertices[i2] - m_vertices[i1];
  136 +
170 137 for (int32 j = 0; j < m_vertexCount; ++j)
171 138 {
172 139 // Don't check vertices on the current edge.
173   - if (j == i || j == (i + 1) % m_vertexCount)
  140 + if (j == i1 || j == i2)
174 141 {
175 142 continue;
176 143 }
177 144
178   - // Your polygon is non-convex (it has an indentation).
179   - // Or your polygon is too skinny.
180   - float32 s = b2Dot(m_normals[i], m_vertices[j] - m_vertices[i]);
181   - b2Assert(s < -b2_linearSlop);
182   - }
183   - }
  145 + b2Vec2 r = m_vertices[j] - m_vertices[i1];
184 146
185   - // Ensure the polygon is counter-clockwise.
186   - for (int32 i = 1; i < m_vertexCount; ++i)
187   - {
188   - float32 cross = b2Cross(m_normals[i-1], m_normals[i]);
189   -
190   - // Keep asinf happy.
191   - cross = b2Clamp(cross, -1.0f, 1.0f);
192   -
193   - // You have consecutive edges that are almost parallel on your polygon.
194   - float32 angle = asinf(cross);
195   - b2Assert(angle > b2_angularSlop);
  147 + // Your polygon is non-convex (it has an indentation) or
  148 + // has colinear edges.
  149 + float32 s = b2Cross(edge, r);
  150 + b2Assert(s > 0.0f);
  151 + }
196 152 }
197 153 #endif
198 154
199 155 // Compute the polygon centroid.
200   - m_centroid = ComputeCentroid(poly->vertices, poly->vertexCount);
201   -
202   - // Compute the oriented bounding box.
203   - ComputeOBB(&m_obb, m_vertices, m_vertexCount);
204   -
205   - // Create core polygon shape by shifting edges inward.
206   - // Also compute the min/max radius for CCD.
207   - for (int32 i = 0; i < m_vertexCount; ++i)
208   - {
209   - int32 i1 = i - 1 >= 0 ? i - 1 : m_vertexCount - 1;
210   - int32 i2 = i;
211   -
212   - b2Vec2 n1 = m_normals[i1];
213   - b2Vec2 n2 = m_normals[i2];
214   - b2Vec2 v = m_vertices[i] - m_centroid;;
215   -
216   - b2Vec2 d;
217   - d.x = b2Dot(n1, v) - b2_toiSlop;
218   - d.y = b2Dot(n2, v) - b2_toiSlop;
219   -
220   - // Shifting the edge inward by b2_toiSlop should
221   - // not cause the plane to pass the centroid.
222   -
223   - // Your shape has a radius/extent less than b2_toiSlop.
224   - b2Assert(d.x >= 0.0f);
225   - b2Assert(d.y >= 0.0f);
226   - b2Mat22 A;
227   - A.col1.x = n1.x; A.col2.x = n1.y;
228   - A.col1.y = n2.x; A.col2.y = n2.y;
229   - m_coreVertices[i] = A.Solve(d) + m_centroid;
230   - }
231   -}
232   -
233   -void b2PolygonShape::UpdateSweepRadius(const b2Vec2& center)
234   -{
235   - // Update the sweep radius (maximum radius) as measured from
236   - // a local center point.
237   - m_sweepRadius = 0.0f;
238   - for (int32 i = 0; i < m_vertexCount; ++i)
239   - {
240   - b2Vec2 d = m_coreVertices[i] - center;
241   - m_sweepRadius = b2Max(m_sweepRadius, d.Length());
242   - }
  156 + m_centroid = ComputeCentroid(m_vertices, m_vertexCount);
243 157 }
244 158
245 159 bool b2PolygonShape::TestPoint(const b2XForm& xf, const b2Vec2& p) const
@@ -258,7 +172,7 @@ bool b2PolygonShape::TestPoint(const b2XForm& xf, const b2Vec2& p) const
258 172 return true;
259 173 }
260 174
261   -bool b2PolygonShape::TestSegment(
  175 +b2SegmentCollide b2PolygonShape::TestSegment(
262 176 const b2XForm& xf,
263 177 float32* lambda,
264 178 b2Vec2* normal,
@@ -280,28 +194,37 @@ bool b2PolygonShape::TestSegment(
280 194 float32 numerator = b2Dot(m_normals[i], m_vertices[i] - p1);
281 195 float32 denominator = b2Dot(m_normals[i], d);
282 196
283   - // Note: we want this predicate without division:
284   - // lower < numerator / denominator, where denominator < 0
285   - // Since denominator < 0, we have to flip the inequality:
286   - // lower < numerator / denominator <==> denominator * lower > numerator.
287   -
288   - if (denominator < 0.0f && numerator < lower * denominator)
289   - {
290   - // Increase lower.
291   - // The segment enters this half-space.
292   - lower = numerator / denominator;
293   - index = i;
  197 + if (denominator == 0.0f)
  198 + {
  199 + if (numerator < 0.0f)
  200 + {
  201 + return b2_missCollide;
  202 + }
294 203 }
295   - else if (denominator > 0.0f && numerator < upper * denominator)
  204 + else
296 205 {
297   - // Decrease upper.
298   - // The segment exits this half-space.
299   - upper = numerator / denominator;
  206 + // Note: we want this predicate without division:
  207 + // lower < numerator / denominator, where denominator < 0
  208 + // Since denominator < 0, we have to flip the inequality:
  209 + // lower < numerator / denominator <==> denominator * lower > numerator.
  210 + if (denominator < 0.0f && numerator < lower * denominator)
  211 + {
  212 + // Increase lower.
  213 + // The segment enters this half-space.
  214 + lower = numerator / denominator;
  215 + index = i;
  216 + }
  217 + else if (denominator > 0.0f && numerator < upper * denominator)
  218 + {
  219 + // Decrease upper.
  220 + // The segment exits this half-space.
  221 + upper = numerator / denominator;
  222 + }
300 223 }
301 224
302 225 if (upper < lower)
303 226 {
304   - return false;
  227 + return b2_missCollide;
305 228 }
306 229 }
307 230
@@ -311,34 +234,31 @@ bool b2PolygonShape::TestSegment(
311 234 {
312 235 *lambda = lower;
313 236 *normal = b2Mul(xf.R, m_normals[index]);
314   - return true;
  237 + return b2_hitCollide;
315 238 }
316 239
317   - return false;
  240 + *lambda = 0;
  241 + return b2_startsInsideCollide;
318 242 }
319 243
320 244 void b2PolygonShape::ComputeAABB(b2AABB* aabb, const b2XForm& xf) const
321 245 {
322   - b2Mat22 R = b2Mul(xf.R, m_obb.R);
323   - b2Mat22 absR = b2Abs(R);
324   - b2Vec2 h = b2Mul(absR, m_obb.extents);
325   - b2Vec2 position = xf.position + b2Mul(xf.R, m_obb.center);
326   - aabb->lowerBound = position - h;
327   - aabb->upperBound = position + h;
328   -}
  246 + b2Vec2 lower = b2Mul(xf, m_vertices[0]);
  247 + b2Vec2 upper = lower;