polygon_collision.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 
25 class PolygonCollision : public Test
26 {
27 public:
29  {
30  {
31  m_polygonA.SetAsBox(0.2f, 0.4f);
32  m_transformA.Set(b2Vec2(0.0f, 0.0f), 0.0f);
33  }
34 
35  {
36  m_polygonB.SetAsBox(0.5f, 0.5f);
37  m_positionB.Set(19.345284f, 1.5632932f);
38  m_angleB = 1.9160721f;
40  }
41  }
42 
43  static Test* Create()
44  {
45  return new PolygonCollision;
46  }
47 
48  void Step(Settings& settings) override
49  {
50  B2_NOT_USED(settings);
51 
52  b2Manifold manifold;
54 
55  b2WorldManifold worldManifold;
57 
58  g_debugDraw.DrawString(5, m_textLine, "point count = %d", manifold.pointCount);
60 
61  {
62  b2Color color(0.9f, 0.9f, 0.9f);
64  for (int32 i = 0; i < m_polygonA.m_count; ++i)
65  {
67  }
69 
70  for (int32 i = 0; i < m_polygonB.m_count; ++i)
71  {
73  }
75  }
76 
77  for (int32 i = 0; i < manifold.pointCount; ++i)
78  {
79  g_debugDraw.DrawPoint(worldManifold.points[i], 4.0f, b2Color(0.9f, 0.3f, 0.3f));
80  }
81 
82  Test::Step(settings);
83  }
84 
85  void Keyboard(int key) override
86  {
87  switch (key)
88  {
89  case GLFW_KEY_A:
90  m_positionB.x -= 0.1f;
91  break;
92 
93  case GLFW_KEY_D:
94  m_positionB.x += 0.1f;
95  break;
96 
97  case GLFW_KEY_S:
98  m_positionB.y -= 0.1f;
99  break;
100 
101  case GLFW_KEY_W:
102  m_positionB.y += 0.1f;
103  break;
104 
105  case GLFW_KEY_Q:
106  m_angleB += 0.1f * b2_pi;
107  break;
108 
109  case GLFW_KEY_E:
110  m_angleB -= 0.1f * b2_pi;
111  break;
112  }
113 
115  }
116 
119 
122 
124  float m_angleB;
125 };
126 
127 static int testIndex = RegisterTest("Geometry", "Polygon Collision", PolygonCollision::Create);
b2PolygonShape m_polygonA
void Initialize(const b2Manifold *manifold, const b2Transform &xfA, float radiusA, const b2Transform &xfB, float radiusB)
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2_math.h:422
b2Vec2 points[b2_maxManifoldPoints]
world contact point (point of intersection)
Definition: b2_collision.h:127
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
float y
Definition: b2_math.h:128
int32 m_textLine
Definition: test.h:127
void Set(const b2Vec2 &position, float angle)
Set this based on the position and angle.
Definition: b2_math.h:354
Definition: test.h:80
#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
void DrawPolygon(const b2Vec2 *vertices, int32 vertexCount, const b2Color &color) override
Draw a closed polygon provided in CCW order.
Definition: draw.cpp:639
b2PolygonShape m_polygonB
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)
float m_radius
Definition: b2_shape.h:102
#define B2_NOT_USED(x)
Definition: b2_common.h:36
#define GLFW_KEY_S
Definition: glfw3.h:396
void Step(Settings &settings) override
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
#define GLFW_KEY_W
Definition: glfw3.h:400
b2Vec2 m_vertices[b2_maxPolygonVertices]
int32 m_textIncrement
Definition: test.h:135
int32 pointCount
the number of manifold points
Definition: b2_collision.h:112
#define GLFW_KEY_D
Definition: glfw3.h:381
#define b2_pi
Definition: b2_common.h:41
void Keyboard(int key) override
static Test * Create()
B2_API void b2CollidePolygons(b2Manifold *manifold, const b2PolygonShape *polygonA, const b2Transform &xfA, const b2PolygonShape *polygonB, const b2Transform &xfB)
Compute the collision manifold between two polygons.
static int testIndex
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
This is used to compute the current state of a contact manifold.
Definition: b2_collision.h:116


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