1 /*
2 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #include <Box2D/Dynamics/Contacts/b2Contact.h>
20 #include <Box2D/Dynamics/Contacts/b2CircleContact.h>
21 #include <Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.h>
22 #include <Box2D/Dynamics/Contacts/b2PolygonContact.h>
23 #include <Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.h>
24 #include <Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.h>
25 #include <Box2D/Dynamics/Contacts/b2ChainAndCircleContact.h>
26 #include <Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.h>
27 #include <Box2D/Dynamics/Contacts/b2ContactSolver.h>
28 
29 #include <Box2D/Collision/b2Collision.h>
30 #include <Box2D/Collision/b2TimeOfImpact.h>
31 #include <Box2D/Collision/Shapes/b2Shape.h>
32 #include <Box2D/Common/b2BlockAllocator.h>
33 #include <Box2D/Dynamics/b2Body.h>
34 #include <Box2D/Dynamics/b2Fixture.h>
35 #include <Box2D/Dynamics/b2World.h>
36 
37 b2ContactRegister b2Contact::s_registers[b2Shape::e_typeCount][b2Shape::e_typeCount];
38 bool b2Contact::s_initialized = false;
39 
InitializeRegisters()40 void b2Contact::InitializeRegisters()
41 {
42 	AddType(b2CircleContact::Create, b2CircleContact::Destroy, b2Shape::e_circle, b2Shape::e_circle);
43 	AddType(b2PolygonAndCircleContact::Create, b2PolygonAndCircleContact::Destroy, b2Shape::e_polygon, b2Shape::e_circle);
44 	AddType(b2PolygonContact::Create, b2PolygonContact::Destroy, b2Shape::e_polygon, b2Shape::e_polygon);
45 	AddType(b2EdgeAndCircleContact::Create, b2EdgeAndCircleContact::Destroy, b2Shape::e_edge, b2Shape::e_circle);
46 	AddType(b2EdgeAndPolygonContact::Create, b2EdgeAndPolygonContact::Destroy, b2Shape::e_edge, b2Shape::e_polygon);
47 	AddType(b2ChainAndCircleContact::Create, b2ChainAndCircleContact::Destroy, b2Shape::e_chain, b2Shape::e_circle);
48 	AddType(b2ChainAndPolygonContact::Create, b2ChainAndPolygonContact::Destroy, b2Shape::e_chain, b2Shape::e_polygon);
49 }
50 
AddType(b2ContactCreateFcn * createFcn,b2ContactDestroyFcn * destoryFcn,b2Shape::Type type1,b2Shape::Type type2)51 void b2Contact::AddType(b2ContactCreateFcn* createFcn, b2ContactDestroyFcn* destoryFcn,
52 						b2Shape::Type type1, b2Shape::Type type2)
53 {
54 	b2Assert(0 <= type1 && type1 < b2Shape::e_typeCount);
55 	b2Assert(0 <= type2 && type2 < b2Shape::e_typeCount);
56 
57 	s_registers[type1][type2].createFcn = createFcn;
58 	s_registers[type1][type2].destroyFcn = destoryFcn;
59 	s_registers[type1][type2].primary = true;
60 
61 	if (type1 != type2)
62 	{
63 		s_registers[type2][type1].createFcn = createFcn;
64 		s_registers[type2][type1].destroyFcn = destoryFcn;
65 		s_registers[type2][type1].primary = false;
66 	}
67 }
68 
Create(b2Fixture * fixtureA,int32 indexA,b2Fixture * fixtureB,int32 indexB,b2BlockAllocator * allocator)69 b2Contact* b2Contact::Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator)
70 {
71 	if (s_initialized == false)
72 	{
73 		InitializeRegisters();
74 		s_initialized = true;
75 	}
76 
77 	b2Shape::Type type1 = fixtureA->GetType();
78 	b2Shape::Type type2 = fixtureB->GetType();
79 
80 	b2Assert(0 <= type1 && type1 < b2Shape::e_typeCount);
81 	b2Assert(0 <= type2 && type2 < b2Shape::e_typeCount);
82 
83 	b2ContactCreateFcn* createFcn = s_registers[type1][type2].createFcn;
84 	if (createFcn)
85 	{
86 		if (s_registers[type1][type2].primary)
87 		{
88 			return createFcn(fixtureA, indexA, fixtureB, indexB, allocator);
89 		}
90 		else
91 		{
92 			return createFcn(fixtureB, indexB, fixtureA, indexA, allocator);
93 		}
94 	}
95 	else
96 	{
97 		return NULL;
98 	}
99 }
100 
Destroy(b2Contact * contact,b2BlockAllocator * allocator)101 void b2Contact::Destroy(b2Contact* contact, b2BlockAllocator* allocator)
102 {
103 	b2Assert(s_initialized == true);
104 
105 	if (contact->m_manifold.pointCount > 0)
106 	{
107 		contact->GetFixtureA()->GetBody()->SetAwake(true);
108 		contact->GetFixtureB()->GetBody()->SetAwake(true);
109 	}
110 
111 	b2Shape::Type typeA = contact->GetFixtureA()->GetType();
112 	b2Shape::Type typeB = contact->GetFixtureB()->GetType();
113 
114 	b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
115 	b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
116 
117 	b2ContactDestroyFcn* destroyFcn = s_registers[typeA][typeB].destroyFcn;
118 	destroyFcn(contact, allocator);
119 }
120 
b2Contact(b2Fixture * fA,int32 indexA,b2Fixture * fB,int32 indexB)121 b2Contact::b2Contact(b2Fixture* fA, int32 indexA, b2Fixture* fB, int32 indexB)
122 {
123 	m_flags = e_enabledFlag;
124 
125 	m_fixtureA = fA;
126 	m_fixtureB = fB;
127 
128 	m_indexA = indexA;
129 	m_indexB = indexB;
130 
131 	m_manifold.pointCount = 0;
132 
133 	m_prev = NULL;
134 	m_next = NULL;
135 
136 	m_nodeA.contact = NULL;
137 	m_nodeA.prev = NULL;
138 	m_nodeA.next = NULL;
139 	m_nodeA.other = NULL;
140 
141 	m_nodeB.contact = NULL;
142 	m_nodeB.prev = NULL;
143 	m_nodeB.next = NULL;
144 	m_nodeB.other = NULL;
145 
146 	m_toiCount = 0;
147 
148 	m_friction = b2MixFriction(m_fixtureA->m_friction, m_fixtureB->m_friction);
149 	m_restitution = b2MixRestitution(m_fixtureA->m_restitution, m_fixtureB->m_restitution);
150 }
151 
152 // Update the contact manifold and touching status.
153 // Note: do not assume the fixture AABBs are overlapping or are valid.
Update(b2ContactListener * listener)154 void b2Contact::Update(b2ContactListener* listener)
155 {
156 	b2Manifold oldManifold = m_manifold;
157 
158 	// Re-enable this contact.
159 	m_flags |= e_enabledFlag;
160 
161 	bool touching = false;
162 	bool wasTouching = (m_flags & e_touchingFlag) == e_touchingFlag;
163 
164 	bool sensorA = m_fixtureA->IsSensor();
165 	bool sensorB = m_fixtureB->IsSensor();
166 	bool sensor = sensorA || sensorB;
167 
168 	b2Body* bodyA = m_fixtureA->GetBody();
169 	b2Body* bodyB = m_fixtureB->GetBody();
170 	const b2Transform& xfA = bodyA->GetTransform();
171 	const b2Transform& xfB = bodyB->GetTransform();
172 
173 	// Is this contact a sensor?
174 	if (sensor)
175 	{
176 		const b2Shape* shapeA = m_fixtureA->GetShape();
177 		const b2Shape* shapeB = m_fixtureB->GetShape();
178 		touching = b2TestOverlap(shapeA, m_indexA, shapeB, m_indexB, xfA, xfB);
179 
180 		// Sensors don't generate manifolds.
181 		m_manifold.pointCount = 0;
182 	}
183 	else
184 	{
185 		Evaluate(&m_manifold, xfA, xfB);
186 		touching = m_manifold.pointCount > 0;
187 
188 		// Match old contact ids to new contact ids and copy the
189 		// stored impulses to warm start the solver.
190 		for (int32 i = 0; i < m_manifold.pointCount; ++i)
191 		{
192 			b2ManifoldPoint* mp2 = m_manifold.points + i;
193 			mp2->normalImpulse = 0.0f;
194 			mp2->tangentImpulse = 0.0f;
195 			b2ContactID id2 = mp2->id;
196 
197 			for (int32 j = 0; j < oldManifold.pointCount; ++j)
198 			{
199 				b2ManifoldPoint* mp1 = oldManifold.points + j;
200 
201 				if (mp1->id.key == id2.key)
202 				{
203 					mp2->normalImpulse = mp1->normalImpulse;
204 					mp2->tangentImpulse = mp1->tangentImpulse;
205 					break;
206 				}
207 			}
208 		}
209 
210 		if (touching != wasTouching)
211 		{
212 			bodyA->SetAwake(true);
213 			bodyB->SetAwake(true);
214 		}
215 	}
216 
217 	if (touching)
218 	{
219 		m_flags |= e_touchingFlag;
220 	}
221 	else
222 	{
223 		m_flags &= ~e_touchingFlag;
224 	}
225 
226 	if (wasTouching == false && touching == true && listener)
227 	{
228 		listener->BeginContact(this);
229 	}
230 
231 	if (wasTouching == true && touching == false && listener)
232 	{
233 		listener->EndContact(this);
234 	}
235 
236 	if (sensor == false && touching && listener)
237 	{
238 		listener->PreSolve(this, &oldManifold);
239 	}
240 }
241