b2Contact.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/Dynamics/Contacts/b2Contact.h>
00020 #include <Box2D/Dynamics/Contacts/b2CircleContact.h>
00021 #include <Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.h>
00022 #include <Box2D/Dynamics/Contacts/b2PolygonContact.h>
00023 #include <Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.h>
00024 #include <Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.h>
00025 #include <Box2D/Dynamics/Contacts/b2ChainAndCircleContact.h>
00026 #include <Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.h>
00027 #include <Box2D/Dynamics/Contacts/b2ContactSolver.h>
00028 
00029 #include <Box2D/Collision/b2Collision.h>
00030 #include <Box2D/Collision/b2TimeOfImpact.h>
00031 #include <Box2D/Collision/Shapes/b2Shape.h>
00032 #include <Box2D/Common/b2BlockAllocator.h>
00033 #include <Box2D/Dynamics/b2Body.h>
00034 #include <Box2D/Dynamics/b2Fixture.h>
00035 #include <Box2D/Dynamics/b2World.h>
00036 
00037 b2ContactRegister b2Contact::s_registers[b2Shape::e_typeCount][b2Shape::e_typeCount];
00038 bool b2Contact::s_initialized = false;
00039 
00040 void b2Contact::InitializeRegisters()
00041 {
00042         AddType(b2CircleContact::Create, b2CircleContact::Destroy, b2Shape::e_circle, b2Shape::e_circle);
00043         AddType(b2PolygonAndCircleContact::Create, b2PolygonAndCircleContact::Destroy, b2Shape::e_polygon, b2Shape::e_circle);
00044         AddType(b2PolygonContact::Create, b2PolygonContact::Destroy, b2Shape::e_polygon, b2Shape::e_polygon);
00045         AddType(b2EdgeAndCircleContact::Create, b2EdgeAndCircleContact::Destroy, b2Shape::e_edge, b2Shape::e_circle);
00046         AddType(b2EdgeAndPolygonContact::Create, b2EdgeAndPolygonContact::Destroy, b2Shape::e_edge, b2Shape::e_polygon);
00047         AddType(b2ChainAndCircleContact::Create, b2ChainAndCircleContact::Destroy, b2Shape::e_chain, b2Shape::e_circle);
00048         AddType(b2ChainAndPolygonContact::Create, b2ChainAndPolygonContact::Destroy, b2Shape::e_chain, b2Shape::e_polygon);
00049 }
00050 
00051 void b2Contact::AddType(b2ContactCreateFcn* createFcn, b2ContactDestroyFcn* destoryFcn,
00052                                                 b2Shape::Type type1, b2Shape::Type type2)
00053 {
00054         b2Assert(0 <= type1 && type1 < b2Shape::e_typeCount);
00055         b2Assert(0 <= type2 && type2 < b2Shape::e_typeCount);
00056         
00057         s_registers[type1][type2].createFcn = createFcn;
00058         s_registers[type1][type2].destroyFcn = destoryFcn;
00059         s_registers[type1][type2].primary = true;
00060 
00061         if (type1 != type2)
00062         {
00063                 s_registers[type2][type1].createFcn = createFcn;
00064                 s_registers[type2][type1].destroyFcn = destoryFcn;
00065                 s_registers[type2][type1].primary = false;
00066         }
00067 }
00068 
00069 b2Contact* b2Contact::Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator)
00070 {
00071         if (s_initialized == false)
00072         {
00073                 InitializeRegisters();
00074                 s_initialized = true;
00075         }
00076 
00077         b2Shape::Type type1 = fixtureA->GetType();
00078         b2Shape::Type type2 = fixtureB->GetType();
00079 
00080         b2Assert(0 <= type1 && type1 < b2Shape::e_typeCount);
00081         b2Assert(0 <= type2 && type2 < b2Shape::e_typeCount);
00082         
00083         b2ContactCreateFcn* createFcn = s_registers[type1][type2].createFcn;
00084         if (createFcn)
00085         {
00086                 if (s_registers[type1][type2].primary)
00087                 {
00088                         return createFcn(fixtureA, indexA, fixtureB, indexB, allocator);
00089                 }
00090                 else
00091                 {
00092                         return createFcn(fixtureB, indexB, fixtureA, indexA, allocator);
00093                 }
00094         }
00095         else
00096         {
00097                 return NULL;
00098         }
00099 }
00100 
00101 void b2Contact::Destroy(b2Contact* contact, b2BlockAllocator* allocator)
00102 {
00103         b2Assert(s_initialized == true);
00104 
00105         b2Fixture* fixtureA = contact->m_fixtureA;
00106         b2Fixture* fixtureB = contact->m_fixtureB;
00107 
00108         if (contact->m_manifold.pointCount > 0 &&
00109                 fixtureA->IsSensor() == false &&
00110                 fixtureB->IsSensor() == false)
00111         {
00112                 fixtureA->GetBody()->SetAwake(true);
00113                 fixtureB->GetBody()->SetAwake(true);
00114         }
00115 
00116         b2Shape::Type typeA = fixtureA->GetType();
00117         b2Shape::Type typeB = fixtureB->GetType();
00118 
00119         b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
00120         b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
00121 
00122         b2ContactDestroyFcn* destroyFcn = s_registers[typeA][typeB].destroyFcn;
00123         destroyFcn(contact, allocator);
00124 }
00125 
00126 b2Contact::b2Contact(b2Fixture* fA, int32 indexA, b2Fixture* fB, int32 indexB)
00127 {
00128         m_flags = e_enabledFlag;
00129 
00130         m_fixtureA = fA;
00131         m_fixtureB = fB;
00132 
00133         m_indexA = indexA;
00134         m_indexB = indexB;
00135 
00136         m_manifold.pointCount = 0;
00137 
00138         m_prev = NULL;
00139         m_next = NULL;
00140 
00141         m_nodeA.contact = NULL;
00142         m_nodeA.prev = NULL;
00143         m_nodeA.next = NULL;
00144         m_nodeA.other = NULL;
00145 
00146         m_nodeB.contact = NULL;
00147         m_nodeB.prev = NULL;
00148         m_nodeB.next = NULL;
00149         m_nodeB.other = NULL;
00150 
00151         m_toiCount = 0;
00152 
00153         m_friction = b2MixFriction(m_fixtureA->m_friction, m_fixtureB->m_friction);
00154         m_restitution = b2MixRestitution(m_fixtureA->m_restitution, m_fixtureB->m_restitution);
00155 
00156         m_tangentSpeed = 0.0f;
00157 }
00158 
00159 // Update the contact manifold and touching status.
00160 // Note: do not assume the fixture AABBs are overlapping or are valid.
00161 void b2Contact::Update(b2ContactListener* listener)
00162 {
00163         b2Manifold oldManifold = m_manifold;
00164 
00165         // Re-enable this contact.
00166         m_flags |= e_enabledFlag;
00167 
00168         bool touching = false;
00169         bool wasTouching = (m_flags & e_touchingFlag) == e_touchingFlag;
00170 
00171         bool sensorA = m_fixtureA->IsSensor();
00172         bool sensorB = m_fixtureB->IsSensor();
00173         bool sensor = sensorA || sensorB;
00174 
00175         b2Body* bodyA = m_fixtureA->GetBody();
00176         b2Body* bodyB = m_fixtureB->GetBody();
00177         const b2Transform& xfA = bodyA->GetTransform();
00178         const b2Transform& xfB = bodyB->GetTransform();
00179 
00180         // Is this contact a sensor?
00181         if (sensor)
00182         {
00183                 const b2Shape* shapeA = m_fixtureA->GetShape();
00184                 const b2Shape* shapeB = m_fixtureB->GetShape();
00185                 touching = b2TestOverlap(shapeA, m_indexA, shapeB, m_indexB, xfA, xfB);
00186 
00187                 // Sensors don't generate manifolds.
00188                 m_manifold.pointCount = 0;
00189         }
00190         else
00191         {
00192                 Evaluate(&m_manifold, xfA, xfB);
00193                 touching = m_manifold.pointCount > 0;
00194 
00195                 // Match old contact ids to new contact ids and copy the
00196                 // stored impulses to warm start the solver.
00197                 for (int32 i = 0; i < m_manifold.pointCount; ++i)
00198                 {
00199                         b2ManifoldPoint* mp2 = m_manifold.points + i;
00200                         mp2->normalImpulse = 0.0f;
00201                         mp2->tangentImpulse = 0.0f;
00202                         b2ContactID id2 = mp2->id;
00203 
00204                         for (int32 j = 0; j < oldManifold.pointCount; ++j)
00205                         {
00206                                 b2ManifoldPoint* mp1 = oldManifold.points + j;
00207 
00208                                 if (mp1->id.key == id2.key)
00209                                 {
00210                                         mp2->normalImpulse = mp1->normalImpulse;
00211                                         mp2->tangentImpulse = mp1->tangentImpulse;
00212                                         break;
00213                                 }
00214                         }
00215                 }
00216 
00217                 if (touching != wasTouching)
00218                 {
00219                         bodyA->SetAwake(true);
00220                         bodyB->SetAwake(true);
00221                 }
00222         }
00223 
00224         if (touching)
00225         {
00226                 m_flags |= e_touchingFlag;
00227         }
00228         else
00229         {
00230                 m_flags &= ~e_touchingFlag;
00231         }
00232 
00233         if (wasTouching == false && touching == true && listener)
00234         {
00235                 listener->BeginContact(this);
00236         }
00237 
00238         if (wasTouching == true && touching == false && listener)
00239         {
00240                 listener->EndContact(this);
00241         }
00242 
00243         if (sensor == false && touching && listener)
00244         {
00245                 listener->PreSolve(this, &oldManifold);
00246         }
00247 }


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