b2_edge_shape.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 "box2d/b2_edge_shape.h"
25 #include <new>
26 
27 void b2EdgeShape::SetOneSided(const b2Vec2& v0, const b2Vec2& v1, const b2Vec2& v2, const b2Vec2& v3)
28 {
29  m_vertex0 = v0;
30  m_vertex1 = v1;
31  m_vertex2 = v2;
32  m_vertex3 = v3;
33  m_oneSided = true;
34 }
35 
36 void b2EdgeShape::SetTwoSided(const b2Vec2& v1, const b2Vec2& v2)
37 {
38  m_vertex1 = v1;
39  m_vertex2 = v2;
40  m_oneSided = false;
41 }
42 
44 {
45  void* mem = allocator->Allocate(sizeof(b2EdgeShape));
46  b2EdgeShape* clone = new (mem) b2EdgeShape;
47  *clone = *this;
48  return clone;
49 }
50 
52 {
53  return 1;
54 }
55 
56 bool b2EdgeShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
57 {
58  B2_NOT_USED(xf);
59  B2_NOT_USED(p);
60  return false;
61 }
62 
63 // p = p1 + t * d
64 // v = v1 + s * e
65 // p1 + t * d = v1 + s * e
66 // s * e - t * d = p1 - v1
68  const b2Transform& xf, int32 childIndex) const
69 {
70  B2_NOT_USED(childIndex);
71 
72  // Put the ray into the edge's frame of reference.
73  b2Vec2 p1 = b2MulT(xf.q, input.p1 - xf.p);
74  b2Vec2 p2 = b2MulT(xf.q, input.p2 - xf.p);
75  b2Vec2 d = p2 - p1;
76 
77  b2Vec2 v1 = m_vertex1;
78  b2Vec2 v2 = m_vertex2;
79  b2Vec2 e = v2 - v1;
80 
81  // Normal points to the right, looking from v1 at v2
82  b2Vec2 normal(e.y, -e.x);
83  normal.Normalize();
84 
85  // q = p1 + t * d
86  // dot(normal, q - v1) = 0
87  // dot(normal, p1 - v1) + t * dot(normal, d) = 0
88  float numerator = b2Dot(normal, v1 - p1);
89  if (m_oneSided && numerator > 0.0f)
90  {
91  return false;
92  }
93 
94  float denominator = b2Dot(normal, d);
95 
96  if (denominator == 0.0f)
97  {
98  return false;
99  }
100 
101  float t = numerator / denominator;
102  if (t < 0.0f || input.maxFraction < t)
103  {
104  return false;
105  }
106 
107  b2Vec2 q = p1 + t * d;
108 
109  // q = v1 + s * r
110  // s = dot(q - v1, r) / dot(r, r)
111  b2Vec2 r = v2 - v1;
112  float rr = b2Dot(r, r);
113  if (rr == 0.0f)
114  {
115  return false;
116  }
117 
118  float s = b2Dot(q - v1, r) / rr;
119  if (s < 0.0f || 1.0f < s)
120  {
121  return false;
122  }
123 
124  output->fraction = t;
125  if (numerator > 0.0f)
126  {
127  output->normal = -b2Mul(xf.q, normal);
128  }
129  else
130  {
131  output->normal = b2Mul(xf.q, normal);
132  }
133  return true;
134 }
135 
136 void b2EdgeShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
137 {
138  B2_NOT_USED(childIndex);
139 
140  b2Vec2 v1 = b2Mul(xf, m_vertex1);
141  b2Vec2 v2 = b2Mul(xf, m_vertex2);
142 
143  b2Vec2 lower = b2Min(v1, v2);
144  b2Vec2 upper = b2Max(v1, v2);
145 
147  aabb->lowerBound = lower - r;
148  aabb->upperBound = upper + r;
149 }
150 
151 void b2EdgeShape::ComputeMass(b2MassData* massData, float density) const
152 {
153  B2_NOT_USED(density);
154 
155  massData->mass = 0.0f;
156  massData->center = 0.5f * (m_vertex1 + m_vertex2);
157  massData->I = 0.0f;
158 }
d
float mass
The mass of the shape, usually in kilograms.
Definition: b2_shape.h:36
T b2Min(T a, T b)
Definition: b2_math.h:626
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2_math.h:422
b2Vec2 p
Definition: b2_math.h:360
float b2Dot(const b2Vec2 &a, const b2Vec2 &b)
Perform the dot product on two vectors.
Definition: b2_math.h:395
b2Vec2 lowerBound
the lower vertex
Definition: b2_collision.h:220
b2Rot q
Definition: b2_math.h:361
f
float x
Definition: b2_math.h:128
float y
Definition: b2_math.h:128
XmlRpcServer s
Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
Definition: b2_collision.h:153
void ComputeMass(b2MassData *massData, float density) const override
b2Vec2 m_vertex0
Optional adjacent vertices. These are used for smooth collision.
Definition: b2_edge_shape.h:69
void ComputeAABB(b2AABB *aabb, const b2Transform &transform, int32 childIndex) const override
b2Vec2 center
The position of the shape&#39;s centroid relative to the shape&#39;s origin.
Definition: b2_shape.h:39
geometry_msgs::TransformStamped t
A 2D column vector.
Definition: b2_math.h:41
signed int int32
Definition: b2_types.h:28
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
void SetOneSided(const b2Vec2 &v0, const b2Vec2 &v1, const b2Vec2 &v2, const b2Vec2 &v3)
bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, const b2Transform &transform, int32 childIndex) const override
Implement b2Shape.
float m_radius
Definition: b2_shape.h:102
void * Allocate(int32 size)
Allocate memory. This will use b2Alloc if the size is larger than b2_maxBlockSize.
#define B2_NOT_USED(x)
Definition: b2_common.h:36
T b2Max(T a, T b)
Definition: b2_math.h:637
bool TestPoint(const b2Transform &transform, const b2Vec2 &p) const override
b2Vec2 m_vertex3
Definition: b2_edge_shape.h:69
float I
The rotational inertia of the shape about the local origin.
Definition: b2_shape.h:42
An axis aligned bounding box.
Definition: b2_collision.h:168
b2Vec2 b2MulT(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2_math.h:429
b2Vec2 m_vertex1
These are the edge vertices.
Definition: b2_edge_shape.h:66
b2Shape * Clone(b2BlockAllocator *allocator) const override
Implement b2Shape.
float Normalize()
Convert this vector into a unit vector. Returns the length.
Definition: b2_math.h:102
b2Vec2 m_vertex2
Definition: b2_edge_shape.h:66
bool m_oneSided
Uses m_vertex0 and m_vertex3 to create smooth collision.
Definition: b2_edge_shape.h:72
int32 GetChildCount() const override
This holds the mass data computed for a shape.
Definition: b2_shape.h:33
b2Vec2 upperBound
the upper vertex
Definition: b2_collision.h:221


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