b2CircleShape.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #include <Box2D/Collision/Shapes/b2CircleShape.h>
00020 #include <new>
00021 
00022 b2Shape* b2CircleShape::Clone(b2BlockAllocator* allocator) const
00023 {
00024         void* mem = allocator->Allocate(sizeof(b2CircleShape));
00025         b2CircleShape* clone = new (mem) b2CircleShape;
00026         *clone = *this;
00027         return clone;
00028 }
00029 
00030 int32 b2CircleShape::GetChildCount() const
00031 {
00032         return 1;
00033 }
00034 
00035 bool b2CircleShape::TestPoint(const b2Transform& transform, const b2Vec2& p) const
00036 {
00037         b2Vec2 center = transform.p + b2Mul(transform.q, m_p);
00038         b2Vec2 d = p - center;
00039         return b2Dot(d, d) <= m_radius * m_radius;
00040 }
00041 
00042 // Collision Detection in Interactive 3D Environments by Gino van den Bergen
00043 // From Section 3.1.2
00044 // x = s + a * r
00045 // norm(x) = radius
00046 bool b2CircleShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
00047                                                         const b2Transform& transform, int32 childIndex) const
00048 {
00049         B2_NOT_USED(childIndex);
00050 
00051         b2Vec2 position = transform.p + b2Mul(transform.q, m_p);
00052         b2Vec2 s = input.p1 - position;
00053         float32 b = b2Dot(s, s) - m_radius * m_radius;
00054 
00055         // Solve quadratic equation.
00056         b2Vec2 r = input.p2 - input.p1;
00057         float32 c =  b2Dot(s, r);
00058         float32 rr = b2Dot(r, r);
00059         float32 sigma = c * c - rr * b;
00060 
00061         // Check for negative discriminant and short segment.
00062         if (sigma < 0.0f || rr < b2_epsilon)
00063         {
00064                 return false;
00065         }
00066 
00067         // Find the point of intersection of the line with the circle.
00068         float32 a = -(c + b2Sqrt(sigma));
00069 
00070         // Is the intersection point on the segment?
00071         if (0.0f <= a && a <= input.maxFraction * rr)
00072         {
00073                 a /= rr;
00074                 output->fraction = a;
00075                 output->normal = s + a * r;
00076                 output->normal.Normalize();
00077                 return true;
00078         }
00079 
00080         return false;
00081 }
00082 
00083 void b2CircleShape::ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const
00084 {
00085         B2_NOT_USED(childIndex);
00086 
00087         b2Vec2 p = transform.p + b2Mul(transform.q, m_p);
00088         aabb->lowerBound.Set(p.x - m_radius, p.y - m_radius);
00089         aabb->upperBound.Set(p.x + m_radius, p.y + m_radius);
00090 }
00091 
00092 void b2CircleShape::ComputeMass(b2MassData* massData, float32 density) const
00093 {
00094         massData->mass = density * b2_pi * m_radius * m_radius;
00095         massData->center = m_p;
00096 
00097         // inertia about the local origin
00098         massData->I = massData->mass * (0.5f * m_radius * m_radius + b2Dot(m_p, m_p));
00099 }


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 22:08:34