Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00043
00044
00045
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
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
00062 if (sigma < 0.0f || rr < b2_epsilon)
00063 {
00064 return false;
00065 }
00066
00067
00068 float32 a = -(c + b2Sqrt(sigma));
00069
00070
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
00098 massData->I = massData->mass * (0.5f * m_radius * m_radius + b2Dot(m_p, m_p));
00099 }