File indexing completed on 2025-08-03 03:49:54
0001 /* 0002 * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com 0003 * 0004 * This software is provided 'as-is', without any express or implied 0005 * warranty. In no event will the authors be held liable for any damages 0006 * arising from the use of this software. 0007 * Permission is granted to anyone to use this software for any purpose, 0008 * including commercial applications, and to alter it and redistribute it 0009 * freely, subject to the following restrictions: 0010 * 1. The origin of this software must not be misrepresented; you must not 0011 * claim that you wrote the original software. If you use this software 0012 * in a product, an acknowledgment in the product documentation would be 0013 * appreciated but is not required. 0014 * 2. Altered source versions must be plainly marked as such, and must not be 0015 * misrepresented as being the original software. 0016 * 3. This notice may not be removed or altered from any source distribution. 0017 */ 0018 0019 #include <Box2D/Collision/Shapes/b2CircleShape.h> 0020 #include <new> 0021 using namespace std; 0022 0023 b2Shape* b2CircleShape::Clone(b2BlockAllocator* allocator) const 0024 { 0025 void* mem = allocator->Allocate(sizeof(b2CircleShape)); 0026 b2CircleShape* clone = new (mem) b2CircleShape; 0027 *clone = *this; 0028 return clone; 0029 } 0030 0031 int32 b2CircleShape::GetChildCount() const 0032 { 0033 return 1; 0034 } 0035 0036 bool b2CircleShape::TestPoint(const b2Transform& transform, const b2Vec2& p) const 0037 { 0038 b2Vec2 center = transform.position + b2Mul(transform.R, m_p); 0039 b2Vec2 d = p - center; 0040 return b2Dot(d, d) <= m_radius * m_radius; 0041 } 0042 0043 // Collision Detection in Interactive 3D Environments by Gino van den Bergen 0044 // From Section 3.1.2 0045 // x = s + a * r 0046 // norm(x) = radius 0047 bool b2CircleShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, 0048 const b2Transform& transform, int32 childIndex) const 0049 { 0050 B2_NOT_USED(childIndex); 0051 0052 b2Vec2 position = transform.position + b2Mul(transform.R, m_p); 0053 b2Vec2 s = input.p1 - position; 0054 qreal b = b2Dot(s, s) - m_radius * m_radius; 0055 0056 // Solve quadratic equation. 0057 b2Vec2 r = input.p2 - input.p1; 0058 qreal c = b2Dot(s, r); 0059 qreal rr = b2Dot(r, r); 0060 qreal sigma = c * c - rr * b; 0061 0062 // Check for negative discriminant and short segment. 0063 if (sigma < 0.0f || rr < b2_epsilon) 0064 { 0065 return false; 0066 } 0067 0068 // Find the point of intersection of the line with the circle. 0069 qreal a = -(c + b2Sqrt(sigma)); 0070 0071 // Is the intersection point on the segment? 0072 if (0.0f <= a && a <= input.maxFraction * rr) 0073 { 0074 a /= rr; 0075 output->fraction = a; 0076 output->normal = s + a * r; 0077 output->normal.Normalize(); 0078 return true; 0079 } 0080 0081 return false; 0082 } 0083 0084 void b2CircleShape::ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const 0085 { 0086 B2_NOT_USED(childIndex); 0087 0088 b2Vec2 p = transform.position + b2Mul(transform.R, m_p); 0089 aabb->lowerBound.Set(p.x - m_radius, p.y - m_radius); 0090 aabb->upperBound.Set(p.x + m_radius, p.y + m_radius); 0091 } 0092 0093 void b2CircleShape::ComputeMass(b2MassData* massData, qreal density) const 0094 { 0095 massData->mass = density * b2_pi * m_radius * m_radius; 0096 massData->center = m_p; 0097 0098 // inertia about the local origin 0099 massData->I = massData->mass * (0.5f * m_radius * m_radius + b2Dot(m_p, m_p)); 0100 }