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/b2EdgeShape.h>
00020 #include <new>
00021
00022 void b2EdgeShape::Set(const b2Vec2& v1, const b2Vec2& v2)
00023 {
00024 m_vertex1 = v1;
00025 m_vertex2 = v2;
00026 m_hasVertex0 = false;
00027 m_hasVertex3 = false;
00028 }
00029
00030 b2Shape* b2EdgeShape::Clone(b2BlockAllocator* allocator) const
00031 {
00032 void* mem = allocator->Allocate(sizeof(b2EdgeShape));
00033 b2EdgeShape* clone = new (mem) b2EdgeShape;
00034 *clone = *this;
00035 return clone;
00036 }
00037
00038 int32 b2EdgeShape::GetChildCount() const
00039 {
00040 return 1;
00041 }
00042
00043 bool b2EdgeShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
00044 {
00045 B2_NOT_USED(xf);
00046 B2_NOT_USED(p);
00047 return false;
00048 }
00049
00050
00051
00052
00053
00054 bool b2EdgeShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
00055 const b2Transform& xf, int32 childIndex) const
00056 {
00057 B2_NOT_USED(childIndex);
00058
00059
00060 b2Vec2 p1 = b2MulT(xf.q, input.p1 - xf.p);
00061 b2Vec2 p2 = b2MulT(xf.q, input.p2 - xf.p);
00062 b2Vec2 d = p2 - p1;
00063
00064 b2Vec2 v1 = m_vertex1;
00065 b2Vec2 v2 = m_vertex2;
00066 b2Vec2 e = v2 - v1;
00067 b2Vec2 normal(e.y, -e.x);
00068 normal.Normalize();
00069
00070
00071
00072
00073 float32 numerator = b2Dot(normal, v1 - p1);
00074 float32 denominator = b2Dot(normal, d);
00075
00076 if (denominator == 0.0f)
00077 {
00078 return false;
00079 }
00080
00081 float32 t = numerator / denominator;
00082 if (t < 0.0f || input.maxFraction < t)
00083 {
00084 return false;
00085 }
00086
00087 b2Vec2 q = p1 + t * d;
00088
00089
00090
00091 b2Vec2 r = v2 - v1;
00092 float32 rr = b2Dot(r, r);
00093 if (rr == 0.0f)
00094 {
00095 return false;
00096 }
00097
00098 float32 s = b2Dot(q - v1, r) / rr;
00099 if (s < 0.0f || 1.0f < s)
00100 {
00101 return false;
00102 }
00103
00104 output->fraction = t;
00105 if (numerator > 0.0f)
00106 {
00107 output->normal = -b2Mul(xf.q, normal);
00108 }
00109 else
00110 {
00111 output->normal = b2Mul(xf.q, normal);
00112 }
00113 return true;
00114 }
00115
00116 void b2EdgeShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
00117 {
00118 B2_NOT_USED(childIndex);
00119
00120 b2Vec2 v1 = b2Mul(xf, m_vertex1);
00121 b2Vec2 v2 = b2Mul(xf, m_vertex2);
00122
00123 b2Vec2 lower = b2Min(v1, v2);
00124 b2Vec2 upper = b2Max(v1, v2);
00125
00126 b2Vec2 r(m_radius, m_radius);
00127 aabb->lowerBound = lower - r;
00128 aabb->upperBound = upper + r;
00129 }
00130
00131 void b2EdgeShape::ComputeMass(b2MassData* massData, float32 density) const
00132 {
00133 B2_NOT_USED(density);
00134
00135 massData->mass = 0.0f;
00136 massData->center = 0.5f * (m_vertex1 + m_vertex2);
00137 massData->I = 0.0f;
00138 }