distance_test.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 DistanceTest : public Test
27 {
28 public:
30  {
31  {
33  m_transformA.p.Set(0.0f, -0.2f);
34  m_polygonA.SetAsBox(10.0f, 0.2f);
35  }
36 
37  {
38  m_positionB.Set(12.017401f, 0.13678508f);
39  m_angleB = -0.0109265f;
41 
42  m_polygonB.SetAsBox(2.0f, 0.1f);
43  }
44  }
45 
46  static Test* Create()
47  {
48  return new DistanceTest;
49  }
50 
51  void Step(Settings& settings) override
52  {
53  Test::Step(settings);
54 
55  b2DistanceInput input;
56  input.proxyA.Set(&m_polygonA, 0);
57  input.proxyB.Set(&m_polygonB, 0);
58  input.transformA = m_transformA;
59  input.transformB = m_transformB;
60  input.useRadii = true;
61  b2SimplexCache cache;
62  cache.count = 0;
63  b2DistanceOutput output;
64  b2Distance(&output, &cache, &input);
65 
66  g_debugDraw.DrawString(5, m_textLine, "distance = %g", output.distance);
68 
69  g_debugDraw.DrawString(5, m_textLine, "iterations = %d", output.iterations);
71 
72  {
73  b2Color color(0.9f, 0.9f, 0.9f);
75  for (int32 i = 0; i < m_polygonA.m_count; ++i)
76  {
78  }
80 
81  for (int32 i = 0; i < m_polygonB.m_count; ++i)
82  {
84  }
86  }
87 
88  b2Vec2 x1 = output.pointA;
89  b2Vec2 x2 = output.pointB;
90 
91  b2Color c1(1.0f, 0.0f, 0.0f);
92  g_debugDraw.DrawPoint(x1, 4.0f, c1);
93 
94  b2Color c2(1.0f, 1.0f, 0.0f);
95  g_debugDraw.DrawPoint(x2, 4.0f, c2);
96  }
97 
98  void Keyboard(int key) override
99  {
100  switch (key)
101  {
102  case GLFW_KEY_A:
103  m_positionB.x -= 0.1f;
104  break;
105 
106  case GLFW_KEY_D:
107  m_positionB.x += 0.1f;
108  break;
109 
110  case GLFW_KEY_S:
111  m_positionB.y -= 0.1f;
112  break;
113 
114  case GLFW_KEY_W:
115  m_positionB.y += 0.1f;
116  break;
117 
118  case GLFW_KEY_Q:
119  m_angleB += 0.1f * b2_pi;
120  break;
121 
122  case GLFW_KEY_E:
123  m_angleB -= 0.1f * b2_pi;
124  break;
125  }
126 
128  }
129 
131  float m_angleB;
132 
137 };
138 
139 static int testIndex = RegisterTest("Geometry", "Distance Test", DistanceTest::Create);
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
b2Vec2 p
Definition: b2_math.h:360
b2PolygonShape m_polygonA
f
void DrawPoint(const b2Vec2 &p, float size, const b2Color &color) override
Draw a point.
Definition: draw.cpp:766
float x
Definition: b2_math.h:128
static Test * Create()
float y
Definition: b2_math.h:128
int32 m_textLine
Definition: test.h:127
void SetIdentity()
Set this to the identity transform.
Definition: b2_math.h:347
void Set(const b2Vec2 &position, float angle)
Set this based on the position and angle.
Definition: b2_math.h:354
Definition: test.h:80
b2DistanceProxy proxyA
Definition: b2_distance.h:78
int32 iterations
number of GJK iterations used
Definition: b2_distance.h:91
#define GLFW_KEY_E
Definition: glfw3.h:382
A 2D column vector.
Definition: b2_math.h:41
#define GLFW_KEY_A
Definition: glfw3.h:378
b2Vec2 pointB
closest point on shapeB
Definition: b2_distance.h:89
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
void SetAsBox(float hx, float hy)
b2Vec2 pointA
closest point on shapeA
Definition: b2_distance.h:88
b2Transform m_transformB
static int testIndex
#define GLFW_KEY_S
Definition: glfw3.h:396
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
#define GLFW_KEY_W
Definition: glfw3.h:400
b2PolygonShape m_polygonB
b2Vec2 m_vertices[b2_maxPolygonVertices]
void Step(Settings &settings) override
int32 m_textIncrement
Definition: test.h:135
#define GLFW_KEY_D
Definition: glfw3.h:381
B2_API void b2Distance(b2DistanceOutput *output, b2SimplexCache *cache, const b2DistanceInput *input)
#define b2_pi
Definition: b2_common.h:41
void Keyboard(int key) override
Output for b2Distance.
Definition: b2_distance.h:86
b2Transform m_transformA
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
#define GLFW_KEY_Q
Definition: glfw3.h:394
#define b2_maxPolygonVertices
Definition: b2_settings.h:53
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2DistanceProxy proxyB
Definition: b2_distance.h:79


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