shape_cast.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "test.h"
24 #include "box2d/b2_distance.h"
25 
26 class ShapeCast : public Test
27 {
28 public:
29  enum
30  {
32  };
33 
35  {
36 #if 1
37  m_vAs[0].Set(-0.5f, 1.0f);
38  m_vAs[1].Set(0.5f, 1.0f);
39  m_vAs[2].Set(0.0f, 0.0f);
40  m_countA = 3;
42 
43  m_vBs[0].Set(-0.5f, -0.5f);
44  m_vBs[1].Set(0.5f, -0.5f);
45  m_vBs[2].Set(0.5f, 0.5f);
46  m_vBs[3].Set(-0.5f, 0.5f);
47  m_countB = 4;
49 
50  m_transformA.p.Set(0.0f, 0.25f);
52  m_transformB.p.Set(-4.0f, 0.0f);
54  m_translationB.Set(8.0f, 0.0f);
55 #elif 0
56  m_vAs[0].Set(0.0f, 0.0f);
57  m_countA = 1;
58  m_radiusA = 0.5f;
59 
60  m_vBs[0].Set(0.0f, 0.0f);
61  m_countB = 1;
62  m_radiusB = 0.5f;
63 
64  m_transformA.p.Set(0.0f, 0.25f);
66  m_transformB.p.Set(-4.0f, 0.0f);
68  m_translationB.Set(8.0f, 0.0f);
69 #else
70  m_vAs[0].Set(0.0f, 0.0f);
71  m_vAs[1].Set(2.0f, 0.0f);
72  m_countA = 2;
74 
75  m_vBs[0].Set(0.0f, 0.0f);
76  m_countB = 1;
77  m_radiusB = 0.25f;
78 
79  // Initial overlap
80  m_transformA.p.Set(0.0f, 0.0f);
82  m_transformB.p.Set(-0.244360745f, 0.05999358f);
84  m_translationB.Set(0.0f, 0.0399999991f);
85 #endif
86  }
87 
88  static Test* Create()
89  {
90  return new ShapeCast;
91  }
92 
93  void Step(Settings& settings) override
94  {
95  Test::Step(settings);
96 
97  b2ShapeCastInput input;
100  input.transformA = m_transformA;
101  input.transformB = m_transformB;
103 
104  b2ShapeCastOutput output;
105  bool hit = b2ShapeCast(&output, &input);
106 
107  b2Transform transformB2;
108  transformB2.q = m_transformB.q;
109  transformB2.p = m_transformB.p + output.lambda * input.translationB;
110 
111  b2DistanceInput distanceInput;
112  distanceInput.proxyA.Set(m_vAs, m_countA, m_radiusA);
113  distanceInput.proxyB.Set(m_vBs, m_countB, m_radiusB);
114  distanceInput.transformA = m_transformA;
115  distanceInput.transformB = transformB2;
116  distanceInput.useRadii = false;
117  b2SimplexCache simplexCache;
118  simplexCache.count = 0;
119  b2DistanceOutput distanceOutput;
120 
121  b2Distance(&distanceOutput, &simplexCache, &distanceInput);
122 
123  g_debugDraw.DrawString(5, m_textLine, "hit = %s, iters = %d, lambda = %g, distance = %g",
124  hit ? "true" : "false", output.iterations, output.lambda, distanceOutput.distance);
126 
127  b2Vec2 vertices[b2_maxPolygonVertices];
128 
129  for (int32 i = 0; i < m_countA; ++i)
130  {
131  vertices[i] = b2Mul(m_transformA, m_vAs[i]);
132  }
133 
134  if (m_countA == 1)
135  {
136  g_debugDraw.DrawCircle(vertices[0], m_radiusA, b2Color(0.9f, 0.9f, 0.9f));
137  }
138  else
139  {
140  g_debugDraw.DrawPolygon(vertices, m_countA, b2Color(0.9f, 0.9f, 0.9f));
141  }
142 
143  for (int32 i = 0; i < m_countB; ++i)
144  {
145  vertices[i] = b2Mul(m_transformB, m_vBs[i]);
146  }
147 
148  if (m_countB == 1)
149  {
150  g_debugDraw.DrawCircle(vertices[0], m_radiusB, b2Color(0.5f, 0.9f, 0.5f));
151  }
152  else
153  {
154  g_debugDraw.DrawPolygon(vertices, m_countB, b2Color(0.5f, 0.9f, 0.5f));
155  }
156 
157  for (int32 i = 0; i < m_countB; ++i)
158  {
159  vertices[i] = b2Mul(transformB2, m_vBs[i]);
160  }
161 
162  if (m_countB == 1)
163  {
164  g_debugDraw.DrawCircle(vertices[0], m_radiusB, b2Color(0.5f, 0.7f, 0.9f));
165  }
166  else
167  {
168  g_debugDraw.DrawPolygon(vertices, m_countB, b2Color(0.5f, 0.7f, 0.9f));
169  }
170 
171  if (hit)
172  {
173  b2Vec2 p1 = output.point;
174  g_debugDraw.DrawPoint(p1, 10.0f, b2Color(0.9f, 0.3f, 0.3f));
175  b2Vec2 p2 = p1 + output.normal;
176  g_debugDraw.DrawSegment(p1, p2, b2Color(0.9f, 0.3f, 0.3f));
177  }
178  }
179 
182  float m_radiusA;
183 
186  float m_radiusB;
187 
191 };
192 
193 static int testIndex = RegisterTest("Collision", "Shape Cast", ShapeCast::Create);
#define b2_polygonRadius
Definition: b2_common.h:74
b2Transform m_transformB
Definition: shape_cast.cpp:189
b2Transform transformB
Definition: b2_distance.h:107
void Set(const b2Shape *shape, int32 index)
Definition: b2_distance.cpp:32
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2_math.h:422
static int testIndex
Definition: shape_cast.cpp:193
b2Vec2 p
Definition: b2_math.h:360
b2DistanceProxy proxyA
Definition: b2_distance.h:104
float m_radiusB
Definition: shape_cast.cpp:186
b2Rot q
Definition: b2_math.h:361
f
void DrawPoint(const b2Vec2 &p, float size, const b2Color &color) override
Draw a point.
Definition: draw.cpp:766
void SetIdentity()
Set to the identity rotation.
Definition: b2_math.h:308
int32 m_textLine
Definition: test.h:127
static Test * Create()
Definition: shape_cast.cpp:88
Definition: test.h:80
b2Vec2 m_vBs[b2_maxPolygonVertices]
Definition: shape_cast.cpp:184
b2DistanceProxy proxyA
Definition: b2_distance.h:78
Input parameters for b2ShapeCast.
Definition: b2_distance.h:102
b2Vec2 m_translationB
Definition: shape_cast.cpp:190
void Step(Settings &settings) override
Definition: shape_cast.cpp:93
A 2D column vector.
Definition: b2_math.h:41
B2_API bool b2ShapeCast(b2ShapeCastOutput *output, const b2ShapeCastInput *input)
void DrawPolygon(const b2Vec2 *vertices, int32 vertexCount, const b2Color &color) override
Draw a closed polygon provided in CCW order.
Definition: draw.cpp:639
signed int int32
Definition: b2_types.h:28
Color for debug drawing. Each value has the range [0,1].
Definition: b2_draw.h:30
int32 m_countA
Definition: shape_cast.cpp:181
void DrawCircle(const b2Vec2 &center, float radius, const b2Color &color) override
Draw a circle.
Definition: draw.cpp:674
b2Transform transformA
Definition: b2_distance.h:80
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2Transform transformB
Definition: b2_distance.h:81
b2Transform transformA
Definition: b2_distance.h:106
int32 m_textIncrement
Definition: test.h:135
Output results for b2ShapeCast.
Definition: b2_distance.h:112
b2Vec2 m_vAs[b2_maxPolygonVertices]
Definition: shape_cast.cpp:180
B2_API void b2Distance(b2DistanceOutput *output, b2SimplexCache *cache, const b2DistanceInput *input)
int32 m_countB
Definition: shape_cast.cpp:185
Output for b2Distance.
Definition: b2_distance.h:86
float m_radiusA
Definition: shape_cast.cpp:182
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
#define b2_maxPolygonVertices
Definition: b2_settings.h:53
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
b2Transform m_transformA
Definition: shape_cast.cpp:188
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2DistanceProxy proxyB
Definition: b2_distance.h:105
b2DistanceProxy proxyB
Definition: b2_distance.h:79
void DrawSegment(const b2Vec2 &p1, const b2Vec2 &p2, const b2Color &color) override
Draw a line segment.
Definition: draw.cpp:742


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21