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 
19 #include <Box2D/Collision/Shapes/b2CircleShape.h>
20 #include <new>
21 using namespace std;
22 
Clone(b2BlockAllocator * allocator) const23 b2Shape* b2CircleShape::Clone(b2BlockAllocator* allocator) const
24 {
25 	void* mem = allocator->Allocate(sizeof(b2CircleShape));
26 	b2CircleShape* clone = new (mem) b2CircleShape;
27 	*clone = *this;
28 	return clone;
29 }
30 
GetChildCount() const31 int32 b2CircleShape::GetChildCount() const
32 {
33 	return 1;
34 }
35 
TestPoint(const b2Transform & transform,const b2Vec2 & p) const36 bool b2CircleShape::TestPoint(const b2Transform& transform, const b2Vec2& p) const
37 {
38 	b2Vec2 center = transform.p + b2Mul(transform.q, m_p);
39 	b2Vec2 d = p - center;
40 	return b2Dot(d, d) <= m_radius * m_radius;
41 }
42 
43 // Collision Detection in Interactive 3D Environments by Gino van den Bergen
44 // From Section 3.1.2
45 // x = s + a * r
46 // norm(x) = radius
RayCast(b2RayCastOutput * output,const b2RayCastInput & input,const b2Transform & transform,int32 childIndex) const47 bool b2CircleShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
48 							const b2Transform& transform, int32 childIndex) const
49 {
50 	B2_NOT_USED(childIndex);
51 
52 	b2Vec2 position = transform.p + b2Mul(transform.q, m_p);
53 	b2Vec2 s = input.p1 - position;
54 	float32 b = b2Dot(s, s) - m_radius * m_radius;
55 
56 	// Solve quadratic equation.
57 	b2Vec2 r = input.p2 - input.p1;
58 	float32 c =  b2Dot(s, r);
59 	float32 rr = b2Dot(r, r);
60 	float32 sigma = c * c - rr * b;
61 
62 	// Check for negative discriminant and short segment.
63 	if (sigma < 0.0f || rr < b2_epsilon)
64 	{
65 		return false;
66 	}
67 
68 	// Find the point of intersection of the line with the circle.
69 	float32 a = -(c + b2Sqrt(sigma));
70 
71 	// Is the intersection point on the segment?
72 	if (0.0f <= a && a <= input.maxFraction * rr)
73 	{
74 		a /= rr;
75 		output->fraction = a;
76 		output->normal = s + a * r;
77 		output->normal.Normalize();
78 		return true;
79 	}
80 
81 	return false;
82 }
83 
ComputeAABB(b2AABB * aabb,const b2Transform & transform,int32 childIndex) const84 void b2CircleShape::ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const
85 {
86 	B2_NOT_USED(childIndex);
87 
88 	b2Vec2 p = transform.p + b2Mul(transform.q, m_p);
89 	aabb->lowerBound.Set(p.x - m_radius, p.y - m_radius);
90 	aabb->upperBound.Set(p.x + m_radius, p.y + m_radius);
91 }
92 
ComputeMass(b2MassData * massData,float32 density) const93 void b2CircleShape::ComputeMass(b2MassData* massData, float32 density) const
94 {
95 	massData->mass = density * b2_pi * m_radius * m_radius;
96 	massData->center = m_p;
97 
98 	// inertia about the local origin
99 	massData->I = massData->mass * (0.5f * m_radius * m_radius + b2Dot(m_p, m_p));
100 }
101