b2EdgeShape.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006-2010 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/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 // p = p1 + t * d
00051 // v = v1 + s * e
00052 // p1 + t * d = v1 + s * e
00053 // s * e - t * d = p1 - v1
00054 bool b2EdgeShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
00055                                                         const b2Transform& xf, int32 childIndex) const
00056 {
00057         B2_NOT_USED(childIndex);
00058 
00059         // Put the ray into the edge's frame of reference.
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         // q = p1 + t * d
00071         // dot(normal, q - v1) = 0
00072         // dot(normal, p1 - v1) + t * dot(normal, d) = 0
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         // q = v1 + s * r
00090         // s = dot(q - v1, r) / dot(r, r)
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 }


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