b2CircleShape.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
20 #include <new>
21 
23 {
24  void* mem = allocator->Allocate(sizeof(b2CircleShape));
25  b2CircleShape* clone = new (mem) b2CircleShape;
26  *clone = *this;
27  return clone;
28 }
29 
31 {
32  return 1;
33 }
34 
36 {
37  b2Vec2 center = transform.p + b2Mul(transform.q, m_p);
38  b2Vec2 d = p - center;
39  return b2Dot(d, d) <= m_radius * m_radius;
40 }
41 
42 // Collision Detection in Interactive 3D Environments by Gino van den Bergen
43 // From Section 3.1.2
44 // x = s + a * r
45 // norm(x) = radius
47  const b2Transform& transform, int32 childIndex) const
48 {
49  B2_NOT_USED(childIndex);
50 
51  b2Vec2 position = transform.p + b2Mul(transform.q, m_p);
52  b2Vec2 s = input.p1 - position;
53  float32 b = b2Dot(s, s) - m_radius * m_radius;
54 
55  // Solve quadratic equation.
56  b2Vec2 r = input.p2 - input.p1;
57  float32 c = b2Dot(s, r);
58  float32 rr = b2Dot(r, r);
59  float32 sigma = c * c - rr * b;
60 
61  // Check for negative discriminant and short segment.
62  if (sigma < 0.0f || rr < b2_epsilon)
63  {
64  return false;
65  }
66 
67  // Find the point of intersection of the line with the circle.
68  float32 a = -(c + b2Sqrt(sigma));
69 
70  // Is the intersection point on the segment?
71  if (0.0f <= a && a <= input.maxFraction * rr)
72  {
73  a /= rr;
74  output->fraction = a;
75  output->normal = s + a * r;
76  output->normal.Normalize();
77  return true;
78  }
79 
80  return false;
81 }
82 
83 void b2CircleShape::ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const
84 {
85  B2_NOT_USED(childIndex);
86 
87  b2Vec2 p = transform.p + b2Mul(transform.q, m_p);
88  aabb->lowerBound.Set(p.x - m_radius, p.y - m_radius);
89  aabb->upperBound.Set(p.x + m_radius, p.y + m_radius);
90 }
91 
92 void b2CircleShape::ComputeMass(b2MassData* massData, float32 density) const
93 {
94  massData->mass = density * b2_pi * m_radius * m_radius;
95  massData->center = m_p;
96 
97  // inertia about the local origin
98  massData->I = massData->mass * (0.5f * m_radius * m_radius + b2Dot(m_p, m_p));
99 }
GLboolean GLboolean GLboolean GLboolean a
d
float32 b2Dot(const b2Vec2 &a, const b2Vec2 &b)
Perform the dot product on two vectors.
Definition: b2Math.h:405
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2Math.h:432
b2Vec2 p
Definition: b2Math.h:371
CvPoint2D32f * center
void ComputeMass(b2MassData *massData, float32 density) const
#define b2_pi
Definition: b2Settings.h:40
b2Vec2 lowerBound
the lower vertex
Definition: b2Collision.h:214
b2Rot q
Definition: b2Math.h:372
const GLfloat * c
GLenum GLenum GLenum input
float32 I
The rotational inertia of the shape about the local origin.
Definition: b2Shape.h:36
Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
Definition: b2Collision.h:147
#define B2_NOT_USED(x)
Definition: b2Settings.h:26
#define b2_epsilon
Definition: b2Settings.h:39
#define b2Sqrt(x)
Definition: b2Math.h:48
b2Vec2 center
The position of the shape&#39;s centroid relative to the shape&#39;s origin.
Definition: b2Shape.h:33
float32 m_radius
Definition: b2Shape.h:93
A circle shape.
Definition: b2CircleShape.h:25
A 2D column vector.
Definition: b2Math.h:52
void ComputeAABB(b2AABB *aabb, const b2Transform &transform, int32 childIndex) const
GLuint GLenum GLenum transform
b2Vec2 m_p
Position.
Definition: b2CircleShape.h:62
GLdouble s
signed int int32
Definition: b2Settings.h:31
b2Shape * Clone(b2BlockAllocator *allocator) const
Implement b2Shape.
float32 fraction
Definition: b2Collision.h:158
float32 mass
The mass of the shape, usually in kilograms.
Definition: b2Shape.h:30
void * Allocate(int32 size)
Allocate memory. This will use b2Alloc if the size is larger than b2_maxBlockSize.
float sigma
bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, const b2Transform &transform, int32 childIndex) const
Implement b2Shape.
float32 maxFraction
Definition: b2Collision.h:150
GLfloat GLfloat p
float32 y
Definition: b2Math.h:139
An axis aligned bounding box.
Definition: b2Collision.h:162
GLdouble GLdouble GLdouble r
GLdouble GLdouble GLdouble b
float32 x
Definition: b2Math.h:139
float32 Normalize()
Convert this vector into a unit vector. Returns the length.
Definition: b2Math.h:113
void Set(float32 x_, float32 y_)
Set this vector to some specified coordinates.
Definition: b2Math.h:64
bool TestPoint(const b2Transform &transform, const b2Vec2 &p) const
Implement b2Shape.
This holds the mass data computed for a shape.
Definition: b2Shape.h:27
int32 GetChildCount() const
float float32
Definition: b2Settings.h:35
b2Vec2 upperBound
the upper vertex
Definition: b2Collision.h:215
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51