b2_circle_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_circle_shape.h"
25 
26 #include <new>
27 
29 {
30  void* mem = allocator->Allocate(sizeof(b2CircleShape));
31  b2CircleShape* clone = new (mem) b2CircleShape;
32  *clone = *this;
33  return clone;
34 }
35 
37 {
38  return 1;
39 }
40 
41 bool b2CircleShape::TestPoint(const b2Transform& transform, const b2Vec2& p) const
42 {
43  b2Vec2 center = transform.p + b2Mul(transform.q, m_p);
44  b2Vec2 d = p - center;
45  return b2Dot(d, d) <= m_radius * m_radius;
46 }
47 
48 // Collision Detection in Interactive 3D Environments by Gino van den Bergen
49 // From Section 3.1.2
50 // x = s + a * r
51 // norm(x) = radius
53  const b2Transform& transform, int32 childIndex) const
54 {
55  B2_NOT_USED(childIndex);
56 
57  b2Vec2 position = transform.p + b2Mul(transform.q, m_p);
58  b2Vec2 s = input.p1 - position;
59  float b = b2Dot(s, s) - m_radius * m_radius;
60 
61  // Solve quadratic equation.
62  b2Vec2 r = input.p2 - input.p1;
63  float c = b2Dot(s, r);
64  float rr = b2Dot(r, r);
65  float sigma = c * c - rr * b;
66 
67  // Check for negative discriminant and short segment.
68  if (sigma < 0.0f || rr < b2_epsilon)
69  {
70  return false;
71  }
72 
73  // Find the point of intersection of the line with the circle.
74  float a = -(c + b2Sqrt(sigma));
75 
76  // Is the intersection point on the segment?
77  if (0.0f <= a && a <= input.maxFraction * rr)
78  {
79  a /= rr;
80  output->fraction = a;
81  output->normal = s + a * r;
82  output->normal.Normalize();
83  return true;
84  }
85 
86  return false;
87 }
88 
89 void b2CircleShape::ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const
90 {
91  B2_NOT_USED(childIndex);
92 
93  b2Vec2 p = transform.p + b2Mul(transform.q, m_p);
94  aabb->lowerBound.Set(p.x - m_radius, p.y - m_radius);
95  aabb->upperBound.Set(p.x + m_radius, p.y + m_radius);
96 }
97 
98 void b2CircleShape::ComputeMass(b2MassData* massData, float density) const
99 {
100  massData->mass = density * b2_pi * m_radius * m_radius;
101  massData->center = m_p;
102 
103  // inertia about the local origin
104  massData->I = massData->mass * (0.5f * m_radius * m_radius + b2Dot(m_p, m_p));
105 }
d
float mass
The mass of the shape, usually in kilograms.
Definition: b2_shape.h:36
#define b2Sqrt(x)
Definition: b2_math.h:37
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2_math.h:422
b2Vec2 p
Definition: b2_math.h:360
void ComputeMass(b2MassData *massData, float density) const override
int32 GetChildCount() const override
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
bool TestPoint(const b2Transform &transform, const b2Vec2 &p) const override
Implement b2Shape.
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
bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, 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
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
signed int int32
Definition: b2_types.h:28
b2Vec2 m_p
Position.
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
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 ComputeAABB(b2AABB *aabb, const b2Transform &transform, int32 childIndex) const override
#define b2_pi
Definition: b2_common.h:41
An axis aligned bounding box.
Definition: b2_collision.h:168
float Normalize()
Convert this vector into a unit vector. Returns the length.
Definition: b2_math.h:102
b2Shape * Clone(b2BlockAllocator *allocator) const override
Implement b2Shape.
#define b2_epsilon
Definition: b2_common.h:40
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