collision_test.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2020 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 "box2d/box2d.h"
24 #include "doctest.h"
25 #include <stdio.h>
26 
27 // Unit tests for collision algorithms
28 DOCTEST_TEST_CASE("collision test")
29 {
30  SUBCASE("polygon mass data")
31  {
32  const b2Vec2 center(100.0f, -50.0f);
33  const float hx = 0.5f, hy = 1.5f;
34  const float angle1 = 0.25f;
35 
36  // Data from issue #422. Not used because the data exceeds accuracy limits.
37  //const b2Vec2 center(-15000.0f, -15000.0f);
38  //const float hx = 0.72f, hy = 0.72f;
39  //const float angle1 = 0.0f;
40 
41  b2PolygonShape polygon1;
42  polygon1.SetAsBox(hx, hy, center, angle1);
43 
44  const float absTol = 2.0f * b2_epsilon;
45  const float relTol = 2.0f * b2_epsilon;
46 
47  CHECK(b2Abs(polygon1.m_centroid.x - center.x) < absTol + relTol * b2Abs(center.x));
48  CHECK(b2Abs(polygon1.m_centroid.y - center.y) < absTol + relTol * b2Abs(center.y));
49 
50  b2Vec2 vertices[4];
51  vertices[0].Set(center.x - hx, center.y - hy);
52  vertices[1].Set(center.x + hx, center.y - hy);
53  vertices[2].Set(center.x - hx, center.y + hy);
54  vertices[3].Set(center.x + hx, center.y + hy);
55 
56  b2PolygonShape polygon2;
57  polygon2.Set(vertices, 4);
58 
59  CHECK(b2Abs(polygon2.m_centroid.x - center.x) < absTol + relTol * b2Abs(center.x));
60  CHECK(b2Abs(polygon2.m_centroid.y - center.y) < absTol + relTol * b2Abs(center.y));
61 
62  const float mass = 4.0f * hx * hy;
63  const float inertia = (mass / 3.0f) * (hx * hx + hy * hy) + mass * b2Dot(center, center);
64 
65  b2MassData massData1;
66  polygon1.ComputeMass(&massData1, 1.0f);
67 
68  CHECK(b2Abs(massData1.center.x - center.x) < absTol + relTol * b2Abs(center.x));
69  CHECK(b2Abs(massData1.center.y - center.y) < absTol + relTol * b2Abs(center.y));
70  CHECK(b2Abs(massData1.mass - mass) < 20.0f * (absTol + relTol * mass));
71  CHECK(b2Abs(massData1.I - inertia) < 40.0f * (absTol + relTol * inertia));
72 
73  b2MassData massData2;
74  polygon2.ComputeMass(&massData2, 1.0f);
75 
76  CHECK(b2Abs(massData2.center.x - center.x) < absTol + relTol * b2Abs(center.x));
77  CHECK(b2Abs(massData2.center.y - center.y) < absTol + relTol * b2Abs(center.y));
78  CHECK(b2Abs(massData2.mass - mass) < 20.0f * (absTol + relTol * mass));
79  CHECK(b2Abs(massData2.I - inertia) < 40.0f * (absTol + relTol * inertia));
80  }
81 }
float mass
The mass of the shape, usually in kilograms.
Definition: b2_shape.h:36
float b2Dot(const b2Vec2 &a, const b2Vec2 &b)
Perform the dot product on two vectors.
Definition: b2_math.h:395
f
float x
Definition: b2_math.h:128
float y
Definition: b2_math.h:128
#define SUBCASE
Definition: doctest.h:2447
b2Vec2 center
The position of the shape&#39;s centroid relative to the shape&#39;s origin.
Definition: b2_shape.h:39
A 2D column vector.
Definition: b2_math.h:41
void ComputeMass(b2MassData *massData, float density) const override
DOCTEST_TEST_CASE("collision test")
void SetAsBox(float hx, float hy)
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
float I
The rotational inertia of the shape about the local origin.
Definition: b2_shape.h:42
void Set(const b2Vec2 *points, int32 count)
#define CHECK
Definition: doctest.h:2471
T b2Abs(T a)
Definition: b2_math.h:610
#define b2_epsilon
Definition: b2_common.h:40
This holds the mass data computed for a shape.
Definition: b2_shape.h:33


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