Box2D: /build/box2d/src/box2d-2.4.1/include/box2d/b2_distance.h Source File

| Box2D 2.4.1 A 2D physics engine for games

Loading...
Searching...
No Matches
b2_distance.h
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#ifndef B2_DISTANCE_H
24#define B2_DISTANCE_H
25
26#include "b2_api.h"
27#include "b2_math.h"
28
29class b2Shape;
30
33 struct B2_API b2DistanceProxy
34{
35 b2DistanceProxy() : m_vertices(nullptr), m_count(0), m_radius(0.0f) {}
36
39 void Set(const b2Shape* shape, int32 index);
40
43 void Set(const b2Vec2* vertices, int32 count, float radius);
44
46 int32 GetSupport(const b2Vec2& d) const;
47
49 const b2Vec2& GetSupportVertex(const b2Vec2& d) const;
50
52 int32 GetVertexCount() const;
53
55 const b2Vec2& GetVertex(int32 index) const;
56
57 b2Vec2 m_buffer[2];
58 const b2Vec2* m_vertices;
59 int32 m_count;
60 float m_radius;
61};
62
65 struct B2_API b2SimplexCache
66{
67 float metric;
68 uint16 count;
69 uint8 indexA[3];
70 uint8 indexB[3];
71};
72
76 struct B2_API b2DistanceInput
77{
78 b2DistanceProxy proxyA;
79 b2DistanceProxy proxyB;
80 b2Transform transformA;
81 b2Transform transformB;
82 bool useRadii;
83};
84
86 struct B2_API b2DistanceOutput
87{
88 b2Vec2 pointA;
89 b2Vec2 pointB;
90 float distance;
91 int32 iterations;
92};
93
97B2_API void b2Distance(b2DistanceOutput* output,
98 b2SimplexCache* cache,
99 const b2DistanceInput* input);
100
102 struct B2_API b2ShapeCastInput
103{
104 b2DistanceProxy proxyA;
105 b2DistanceProxy proxyB;
106 b2Transform transformA;
107 b2Transform transformB;
108 b2Vec2 translationB;
109};
110
112 struct B2_API b2ShapeCastOutput
113{
114 b2Vec2 point;
115 b2Vec2 normal;
116 float lambda;
117 int32 iterations;
118};
119
122B2_API bool b2ShapeCast(b2ShapeCastOutput* output, const b2ShapeCastInput* input);
123
125
126 inline int32 b2DistanceProxy::GetVertexCount() const
127{
128 return m_count;
129}
130
131 inline const b2Vec2& b2DistanceProxy::GetVertex(int32 index) const
132{
133 b2Assert(0 <= index && index < m_count);
134 return m_vertices[index];
135}
136
137 inline int32 b2DistanceProxy::GetSupport(const b2Vec2& d) const
138{
139 int32 bestIndex = 0;
140 float bestValue = b2Dot(m_vertices[0], d);
141 for (int32 i = 1; i < m_count; ++i)
142 {
143 float value = b2Dot(m_vertices[i], d);
144 if (value > bestValue)
145 {
146 bestIndex = i;
147 bestValue = value;
148 }
149 }
150
151 return bestIndex;
152}
153
154 inline const b2Vec2& b2DistanceProxy::GetSupportVertex(const b2Vec2& d) const
155{
156 int32 bestIndex = 0;
157 float bestValue = b2Dot(m_vertices[0], d);
158 for (int32 i = 1; i < m_count; ++i)
159 {
160 float value = b2Dot(m_vertices[i], d);
161 if (value > bestValue)
162 {
163 bestIndex = i;
164 bestValue = value;
165 }
166 }
167
168 return m_vertices[bestIndex];
169}
170
171#endif
b2Shape
Definition b2_shape.h:49
b2DistanceInput
Definition b2_distance.h:77
b2DistanceOutput
Output for b2Distance.
Definition b2_distance.h:87
b2DistanceOutput::pointA
b2Vec2 pointA
closest point on shapeA
Definition b2_distance.h:88
b2DistanceOutput::pointB
b2Vec2 pointB
closest point on shapeB
Definition b2_distance.h:89
b2DistanceOutput::iterations
int32 iterations
number of GJK iterations used
Definition b2_distance.h:91
b2DistanceProxy
Definition b2_distance.h:34
b2DistanceProxy::GetSupportVertex
const b2Vec2 & GetSupportVertex(const b2Vec2 &d) const
Get the supporting vertex in the given direction.
Definition b2_distance.h:154
b2DistanceProxy::GetSupport
int32 GetSupport(const b2Vec2 &d) const
Get the supporting vertex index in the given direction.
Definition b2_distance.h:137
b2DistanceProxy::Set
void Set(const b2Vec2 *vertices, int32 count, float radius)
b2DistanceProxy::Set
void Set(const b2Shape *shape, int32 index)
b2DistanceProxy::GetVertex
const b2Vec2 & GetVertex(int32 index) const
Get a vertex by index. Used by b2Distance.
Definition b2_distance.h:131
b2DistanceProxy::GetVertexCount
int32 GetVertexCount() const
Get the vertex count.
Definition b2_distance.h:126
b2ShapeCastInput
Input parameters for b2ShapeCast.
Definition b2_distance.h:103
b2ShapeCastOutput
Output results for b2ShapeCast.
Definition b2_distance.h:113
b2SimplexCache
Definition b2_distance.h:66
b2SimplexCache::metric
float metric
length or area
Definition b2_distance.h:67
b2Transform
Definition b2_math.h:339
b2Vec2
A 2D column vector.
Definition b2_math.h:42