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