time_of_impact.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"
25 
26 class TimeOfImpact : public Test
27 {
28 public:
30  {
31  m_shapeA.SetAsBox(25.0f, 5.0f);
32  m_shapeB.SetAsBox(2.5f, 2.5f);
33  }
34 
35  static Test* Create()
36  {
37  return new TimeOfImpact;
38  }
39 
40  void Step(Settings& settings) override
41  {
42  Test::Step(settings);
43 
44  b2Sweep sweepA;
45  sweepA.c0.Set(24.0f, -60.0f);
46  sweepA.a0 = 2.95f;
47  sweepA.c = sweepA.c0;
48  sweepA.a = sweepA.a0;
49  sweepA.localCenter.SetZero();
50 
51  b2Sweep sweepB;
52  sweepB.c0.Set(53.474274f, -50.252514f);
53  sweepB.a0 = 513.36676f; // - 162.0f * b2_pi;
54  sweepB.c.Set(54.595478f, -51.083473f);
55  sweepB.a = 513.62781f; // - 162.0f * b2_pi;
56  sweepB.localCenter.SetZero();
57 
58  //sweepB.a0 -= 300.0f * b2_pi;
59  //sweepB.a -= 300.0f * b2_pi;
60 
61  b2TOIInput input;
62  input.proxyA.Set(&m_shapeA, 0);
63  input.proxyB.Set(&m_shapeB, 0);
64  input.sweepA = sweepA;
65  input.sweepB = sweepB;
66  input.tMax = 1.0f;
67 
68  b2TOIOutput output;
69 
70  b2TimeOfImpact(&output, &input);
71 
72  g_debugDraw.DrawString(5, m_textLine, "toi = %g", output.t);
74 
76  g_debugDraw.DrawString(5, m_textLine, "max toi iters = %d, max root iters = %d", b2_toiMaxIters, b2_toiMaxRootIters);
78 
79  b2Vec2 vertices[b2_maxPolygonVertices];
80 
81  b2Transform transformA;
82  sweepA.GetTransform(&transformA, 0.0f);
83  for (int32 i = 0; i < m_shapeA.m_count; ++i)
84  {
85  vertices[i] = b2Mul(transformA, m_shapeA.m_vertices[i]);
86  }
87  g_debugDraw.DrawPolygon(vertices, m_shapeA.m_count, b2Color(0.9f, 0.9f, 0.9f));
88 
89  b2Transform transformB;
90  sweepB.GetTransform(&transformB, 0.0f);
91 
92  //b2Vec2 localPoint(2.0f, -0.1f);
93 
94  for (int32 i = 0; i < m_shapeB.m_count; ++i)
95  {
96  vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]);
97  }
98  g_debugDraw.DrawPolygon(vertices, m_shapeB.m_count, b2Color(0.5f, 0.9f, 0.5f));
99 
100  sweepB.GetTransform(&transformB, output.t);
101  for (int32 i = 0; i < m_shapeB.m_count; ++i)
102  {
103  vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]);
104  }
105  g_debugDraw.DrawPolygon(vertices, m_shapeB.m_count, b2Color(0.5f, 0.7f, 0.9f));
106 
107  sweepB.GetTransform(&transformB, 1.0f);
108  for (int32 i = 0; i < m_shapeB.m_count; ++i)
109  {
110  vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]);
111  }
112  g_debugDraw.DrawPolygon(vertices, m_shapeB.m_count, b2Color(0.9f, 0.5f, 0.5f));
113 
114 #if 0
115  for (float t = 0.0f; t < 1.0f; t += 0.1f)
116  {
117  sweepB.GetTransform(&transformB, t);
118  for (int32 i = 0; i < m_shapeB.m_count; ++i)
119  {
120  vertices[i] = b2Mul(transformB, m_shapeB.m_vertices[i]);
121  }
122  g_debugDraw.DrawPolygon(vertices, m_shapeB.m_count, b2Color(0.9f, 0.5f, 0.5f));
123  }
124 #endif
125  }
126 
129 };
130 
131 static int testIndex = RegisterTest("Collision", "Time of Impact", TimeOfImpact::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 c0
Definition: b2_math.h:383
f
#define B2_API
Definition: b2_api.h:49
static Test * Create()
int32 m_textLine
Definition: test.h:127
B2_API void b2TimeOfImpact(b2TOIOutput *output, const b2TOIInput *input)
Definition: test.h:80
void Step(Settings &settings) override
Input parameters for b2TimeOfImpact.
b2PolygonShape m_shapeB
void SetZero()
Set this vector to all zeros.
Definition: b2_math.h:50
geometry_msgs::TransformStamped t
A 2D column vector.
Definition: b2_math.h:41
b2DistanceProxy proxyA
void DrawPolygon(const b2Vec2 *vertices, int32 vertexCount, const b2Color &color) override
Draw a closed polygon provided in CCW order.
Definition: draw.cpp:639
float a0
Definition: b2_math.h:384
b2Vec2 c
center world positions
Definition: b2_math.h:383
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)
b2DistanceProxy proxyB
B2_API int32 b2_toiMaxRootIters
b2Vec2 localCenter
local center of mass position
Definition: b2_math.h:382
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2Vec2 m_vertices[b2_maxPolygonVertices]
int32 m_textIncrement
Definition: test.h:135
Output parameters for b2TimeOfImpact.
static int testIndex
float a
world angles
Definition: b2_math.h:384
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
virtual void Step(Settings &settings)
Definition: test.cpp:278
void GetTransform(b2Transform *transform, float beta) const
Definition: b2_math.h:687
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2PolygonShape m_shapeA
B2_API int32 b2_toiMaxIters


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