Skip to content

Commit d585b05

Browse files
committed
contact array conversion
1 parent c50dc96 commit d585b05

9 files changed

+65
-102
lines changed

src/body.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static void b2DestroyBodyContacts( b2World* world, b2Body* body, bool wakeBodies
160160
int contactId = edgeKey >> 1;
161161
int edgeIndex = edgeKey & 1;
162162

163-
b2Contact* contact = world->contactArray + contactId;
163+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId );
164164
edgeKey = contact->edges[edgeIndex].nextKey;
165165
b2DestroyContact( world, contact, wakeBodies );
166166
}
@@ -455,8 +455,7 @@ int b2Body_GetContactData( b2BodyId bodyId, b2ContactData* contactData, int capa
455455
int contactId = contactKey >> 1;
456456
int edgeIndex = contactKey & 1;
457457

458-
b2CheckIndex( world->contactArray, contactId );
459-
b2Contact* contact = world->contactArray + contactId;
458+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId );
460459

461460
// Is contact touching?
462461
if ( contact->flags & b2_contactTouchingFlag )

src/constraint_graph.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ void b2RemoveContactFromGraph( b2World* world, int bodyIdA, int bodyIdB, int col
203203

204204
// Fix moved contact
205205
int movedId = movedContactSim->contactId;
206-
b2CheckIndex( world->contactArray, movedId );
207-
b2Contact* movedContact = world->contactArray + movedId;
206+
b2Contact* movedContact = b2ContactArray_Get( &world->contactArray, movedId );
208207
B2_ASSERT( movedContact->setIndex == b2_awakeSet );
209208
B2_ASSERT( movedContact->colorIndex == colorIndex );
210209
B2_ASSERT( movedContact->localIndex == movedIndex );

src/contact.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,15 @@ void b2CreateContact( b2World* world, b2Shape* shapeA, b2Shape* shapeB )
229229

230230
// Create contact key and contact
231231
int contactId = b2AllocId( &world->contactIdPool );
232-
if ( contactId == b2Array( world->contactArray ).count )
232+
if ( contactId == world->contactArray.count )
233233
{
234-
b2Array_Push( world->contactArray, ( b2Contact ){ 0 } );
234+
b2ContactArray_Push( &world->contactArray, ( b2Contact ){ 0 } );
235235
}
236236

237237
int shapeIdA = shapeA->id;
238238
int shapeIdB = shapeB->id;
239239

240-
b2Contact* contact = world->contactArray + contactId;
240+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId );
241241
contact->contactId = contactId;
242242
contact->setIndex = setIndex;
243243
contact->colorIndex = B2_NULL_INDEX;
@@ -275,7 +275,7 @@ void b2CreateContact( b2World* world, b2Shape* shapeA, b2Shape* shapeB )
275275
int headContactKey = bodyA->headContactKey;
276276
if ( headContactKey != B2_NULL_INDEX )
277277
{
278-
b2Contact* headContact = world->contactArray + ( headContactKey >> 1 );
278+
b2Contact* headContact = b2ContactArray_Get( &world->contactArray, headContactKey >> 1 );
279279
headContact->edges[headContactKey & 1].prevKey = keyA;
280280
}
281281
bodyA->headContactKey = keyA;
@@ -292,7 +292,7 @@ void b2CreateContact( b2World* world, b2Shape* shapeA, b2Shape* shapeB )
292292
int headContactKey = bodyB->headContactKey;
293293
if ( bodyB->headContactKey != B2_NULL_INDEX )
294294
{
295-
b2Contact* headContact = world->contactArray + ( headContactKey >> 1 );
295+
b2Contact* headContact = b2ContactArray_Get( &world->contactArray, headContactKey >> 1 );
296296
headContact->edges[headContactKey & 1].prevKey = keyB;
297297
}
298298
bodyB->headContactKey = keyB;
@@ -364,14 +364,14 @@ void b2DestroyContact( b2World* world, b2Contact* contact, bool wakeBodies )
364364
// Remove from body A
365365
if ( edgeA->prevKey != B2_NULL_INDEX )
366366
{
367-
b2Contact* prevContact = world->contactArray + ( edgeA->prevKey >> 1 );
367+
b2Contact* prevContact = b2ContactArray_Get( &world->contactArray, edgeA->prevKey >> 1 );
368368
b2ContactEdge* prevEdge = prevContact->edges + ( edgeA->prevKey & 1 );
369369
prevEdge->nextKey = edgeA->nextKey;
370370
}
371371

372372
if ( edgeA->nextKey != B2_NULL_INDEX )
373373
{
374-
b2Contact* nextContact = world->contactArray + ( edgeA->nextKey >> 1 );
374+
b2Contact* nextContact = b2ContactArray_Get( &world->contactArray, edgeA->nextKey >> 1 );
375375
b2ContactEdge* nextEdge = nextContact->edges + ( edgeA->nextKey & 1 );
376376
nextEdge->prevKey = edgeA->prevKey;
377377
}
@@ -389,14 +389,14 @@ void b2DestroyContact( b2World* world, b2Contact* contact, bool wakeBodies )
389389
// Remove from body B
390390
if ( edgeB->prevKey != B2_NULL_INDEX )
391391
{
392-
b2Contact* prevContact = world->contactArray + ( edgeB->prevKey >> 1 );
392+
b2Contact* prevContact = b2ContactArray_Get( &world->contactArray, edgeB->prevKey >> 1 );
393393
b2ContactEdge* prevEdge = prevContact->edges + ( edgeB->prevKey & 1 );
394394
prevEdge->nextKey = edgeB->nextKey;
395395
}
396396

397397
if ( edgeB->nextKey != B2_NULL_INDEX )
398398
{
399-
b2Contact* nextContact = world->contactArray + ( edgeB->nextKey >> 1 );
399+
b2Contact* nextContact = b2ContactArray_Get( &world->contactArray, edgeB->nextKey >> 1 );
400400
b2ContactEdge* nextEdge = nextContact->edges + ( edgeB->nextKey & 1 );
401401
nextEdge->prevKey = edgeB->prevKey;
402402
}
@@ -430,8 +430,9 @@ void b2DestroyContact( b2World* world, b2Contact* contact, bool wakeBodies )
430430
int movedIndex = b2ContactSimArray_RemoveSwap( &set->contactsNew, contact->localIndex );
431431
if ( movedIndex != B2_NULL_INDEX )
432432
{
433-
b2ContactSim* movedContact = set->contactsNew.data + contact->localIndex;
434-
world->contactArray[movedContact->contactId].localIndex = contact->localIndex;
433+
b2ContactSim* movedContactSim = set->contactsNew.data + contact->localIndex;
434+
b2Contact* movedContact = b2ContactArray_Get( &world->contactArray, movedContactSim->contactId );
435+
movedContact->localIndex = contact->localIndex;
435436
}
436437
}
437438

src/island.c

+10-21
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ static void b2AddContactToIsland( b2World* world, int islandId, b2Contact* conta
9191
if ( island->headContact != B2_NULL_INDEX )
9292
{
9393
contact->islandNext = island->headContact;
94-
b2CheckIndex( world->contactArray, island->headContact );
95-
b2Contact* headContact = world->contactArray + island->headContact;
94+
b2Contact* headContact = b2ContactArray_Get( &world->contactArray, island->headContact);
9695
headContact->islandPrev = contact->contactId;
9796
}
9897

@@ -225,16 +224,14 @@ void b2UnlinkContact( b2World* world, b2Contact* contact )
225224

226225
if ( contact->islandPrev != B2_NULL_INDEX )
227226
{
228-
b2CheckIndex( world->contactArray, contact->islandPrev );
229-
b2Contact* prevContact = world->contactArray + contact->islandPrev;
227+
b2Contact* prevContact = b2ContactArray_Get( &world->contactArray, contact->islandPrev);
230228
B2_ASSERT( prevContact->islandNext == contact->contactId );
231229
prevContact->islandNext = contact->islandNext;
232230
}
233231

234232
if ( contact->islandNext != B2_NULL_INDEX )
235233
{
236-
b2CheckIndex( world->contactArray, contact->islandNext );
237-
b2Contact* nextContact = world->contactArray + contact->islandNext;
234+
b2Contact* nextContact = b2ContactArray_Get( &world->contactArray, contact->islandNext );
238235
B2_ASSERT( nextContact->islandPrev == contact->contactId );
239236
nextContact->islandPrev = contact->islandPrev;
240237
}
@@ -445,8 +442,7 @@ static void b2MergeIsland( b2World* world, b2Island* island )
445442
int contactId = island->headContact;
446443
while ( contactId != B2_NULL_INDEX )
447444
{
448-
b2CheckIndex( world->contactArray, contactId );
449-
b2Contact* contact = world->contactArray + contactId;
445+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId );
450446
contact->islandId = rootId;
451447
contactId = contact->islandNext;
452448
}
@@ -488,13 +484,11 @@ static void b2MergeIsland( b2World* world, b2Island* island )
488484
B2_ASSERT( island->tailContact != B2_NULL_INDEX && island->contactCount > 0 );
489485
B2_ASSERT( rootIsland->tailContact != B2_NULL_INDEX && rootIsland->contactCount > 0 );
490486

491-
b2CheckIndex( world->contactArray, rootIsland->tailContact );
492-
b2Contact* tailContact = world->contactArray + rootIsland->tailContact;
487+
b2Contact* tailContact = b2ContactArray_Get( &world->contactArray, rootIsland->tailContact );
493488
B2_ASSERT( tailContact->islandNext == B2_NULL_INDEX );
494489
tailContact->islandNext = island->headContact;
495490

496-
b2CheckIndex( world->contactArray, island->headContact );
497-
b2Contact* headContact = world->contactArray + island->headContact;
491+
b2Contact* headContact = b2ContactArray_Get( &world->contactArray, island->headContact );
498492
B2_ASSERT( headContact->islandPrev == B2_NULL_INDEX );
499493
headContact->islandPrev = rootIsland->tailContact;
500494

@@ -623,8 +617,6 @@ void b2SplitIsland( b2World* world, int baseId )
623617
int bodyCount = baseIsland->bodyCount;
624618

625619
b2Body* bodies = world->bodyArrayNew.data;
626-
b2Contact* contacts = world->contactArray;
627-
628620
b2StackAllocator* alloc = &world->stackAllocator;
629621

630622
// No lock is needed because I ensure the allocator is not used while this task is active.
@@ -652,7 +644,7 @@ void b2SplitIsland( b2World* world, int baseId )
652644
int nextContactId = baseIsland->headContact;
653645
while ( nextContactId != B2_NULL_INDEX )
654646
{
655-
b2Contact* contact = contacts + nextContactId;
647+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, nextContactId );
656648
contact->isMarked = false;
657649
nextContactId = contact->islandNext;
658650
}
@@ -726,8 +718,7 @@ void b2SplitIsland( b2World* world, int baseId )
726718
int contactId = contactKey >> 1;
727719
int edgeIndex = contactKey & 1;
728720

729-
b2CheckIndex( world->contactArray, contactId );
730-
b2Contact* contact = world->contactArray + contactId;
721+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId );
731722
B2_ASSERT( contact->contactId == contactId );
732723

733724
// Next key
@@ -769,8 +760,7 @@ void b2SplitIsland( b2World* world, int baseId )
769760
contact->islandId = islandId;
770761
if ( island->tailContact != B2_NULL_INDEX )
771762
{
772-
b2CheckIndex( world->contactArray, island->tailContact );
773-
b2Contact* tailContact = world->contactArray + island->tailContact;
763+
b2Contact* tailContact = b2ContactArray_Get( &world->contactArray, island->tailContact );
774764
tailContact->islandNext = contactId;
775765
}
776766
contact->islandPrev = island->tailContact;
@@ -927,8 +917,7 @@ void b2ValidateIsland( b2World* world, int islandId )
927917
int contactId = island->headContact;
928918
while ( contactId != B2_NULL_INDEX )
929919
{
930-
b2CheckIndex( world->contactArray, contactId );
931-
b2Contact* contact = world->contactArray + contactId;
920+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId );
932921
B2_ASSERT( contact->setIndex == island->setIndex );
933922
B2_ASSERT( contact->islandId == islandId );
934923
count += 1;

src/joint.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ static void b2DestroyContactsBetweenBodies( b2World* world, b2Body* bodyA, b2Bod
307307
int contactId = contactKey >> 1;
308308
int edgeIndex = contactKey & 1;
309309

310-
b2CheckIndex( world->contactArray, contactId );
311-
b2Contact* contact = world->contactArray + contactId;
310+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId );
312311
contactKey = contact->edges[edgeIndex].nextKey;
313312

314313
int otherEdgeIndex = edgeIndex ^ 1;

src/shape.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ void b2DestroyShapeInternal( b2World* world, b2Shape* shape, b2Body* body, bool
246246
int contactId = contactKey >> 1;
247247
int edgeIndex = contactKey & 1;
248248

249-
b2CheckIndex( world->contactArray, contactId );
250-
b2Contact* contact = world->contactArray + contactId;
249+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId );
251250
contactKey = contact->edges[edgeIndex].nextKey;
252251

253252
if ( contact->shapeIdA == shapeId || contact->shapeIdB == shapeId )
@@ -915,8 +914,7 @@ static void b2ResetProxy( b2World* world, b2Shape* shape, bool wakeBodies, bool
915914
int contactId = contactKey >> 1;
916915
int edgeIndex = contactKey & 1;
917916

918-
b2CheckIndex( world->contactArray, contactId );
919-
b2Contact* contact = world->contactArray + contactId;
917+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId );
920918
contactKey = contact->edges[edgeIndex].nextKey;
921919

922920
if ( contact->shapeIdA == shapeId || contact->shapeIdB == shapeId )
@@ -1274,8 +1272,7 @@ int b2Shape_GetContactData( b2ShapeId shapeId, b2ContactData* contactData, int c
12741272
int contactId = contactKey >> 1;
12751273
int edgeIndex = contactKey & 1;
12761274

1277-
b2CheckIndex( world->contactArray, contactId );
1278-
b2Contact* contact = world->contactArray + contactId;
1275+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId );
12791276

12801277
// Does contact involve this shape and is it touching?
12811278
if ( ( contact->shapeIdA == shapeId.index1 - 1 || contact->shapeIdB == shapeId.index1 - 1 ) &&

src/solver_set.c

+22-33
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ void b2WakeSolverSet( b2World* world, int setIndex )
4141
b2SolverSet* disabledSet = world->solverSetArray + b2_disabledSet;
4242

4343
b2Body* bodies = world->bodyArrayNew.data;
44-
b2Contact* contacts = world->contactArray;
4544

4645
int bodyCount = set->simsNew.count;
4746
for ( int i = 0; i < bodyCount; ++i )
@@ -69,8 +68,7 @@ void b2WakeSolverSet( b2World* world, int setIndex )
6968
int edgeIndex = contactKey & 1;
7069
int contactId = contactKey >> 1;
7170

72-
b2CheckIndex( contacts, contactId );
73-
b2Contact* contact = contacts + contactId;
71+
b2Contact* contact = b2ContactArray_Get(&world->contactArray, contactId);
7472

7573
contactKey = contact->edges[edgeIndex].nextKey;
7674

@@ -94,11 +92,10 @@ void b2WakeSolverSet( b2World* world, int setIndex )
9492
if ( movedLocalIndex != B2_NULL_INDEX )
9593
{
9694
// fix moved element
97-
b2ContactSim* movedContact = disabledSet->contactsNew.data + localIndex;
98-
int movedId = movedContact->contactId;
99-
b2CheckIndex( contacts, movedId );
100-
B2_ASSERT( contacts[movedId].localIndex == movedLocalIndex );
101-
contacts[movedId].localIndex = localIndex;
95+
b2ContactSim* movedContactSim = disabledSet->contactsNew.data + localIndex;
96+
b2Contact* movedContact = b2ContactArray_Get( &world->contactArray, movedContactSim->contactId );
97+
B2_ASSERT( movedContact->localIndex == movedLocalIndex );
98+
movedContact->localIndex = localIndex;
10299
}
103100
}
104101
}
@@ -109,7 +106,7 @@ void b2WakeSolverSet( b2World* world, int setIndex )
109106
for ( int i = 0; i < contactCount; ++i )
110107
{
111108
b2ContactSim* contactSim = set->contactsNew.data + i;
112-
b2Contact* contact = contacts + contactSim->contactId;
109+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactSim->contactId);
113110
B2_ASSERT( contact->flags & b2_contactTouchingFlag );
114111
B2_ASSERT( contactSim->simFlags & b2_simTouchingFlag );
115112
B2_ASSERT( contactSim->manifold.pointCount > 0 );
@@ -196,7 +193,6 @@ void b2TrySleepIsland( b2World* world, int islandId )
196193
// this shuffles around bodies in the awake set
197194
{
198195
b2SolverSet* disabledSet = world->solverSetArray + b2_disabledSet;
199-
b2Contact* contacts = world->contactArray;
200196
int bodyId = island->headBody;
201197
while ( bodyId != B2_NULL_INDEX )
202198
{
@@ -248,8 +244,7 @@ void b2TrySleepIsland( b2World* world, int islandId )
248244
int contactId = contactKey >> 1;
249245
int edgeIndex = contactKey & 1;
250246

251-
b2CheckIndex( contacts, contactId );
252-
b2Contact* contact = contacts + contactId;
247+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId);
253248

254249
B2_ASSERT( contact->setIndex == b2_awakeSet || contact->setIndex == b2_disabledSet );
255250
contactKey = contact->edges[edgeIndex].nextKey;
@@ -289,15 +284,14 @@ void b2TrySleepIsland( b2World* world, int islandId )
289284
b2ContactSim* disabledContactSim = b2ContactSimArray_Add( &disabledSet->contactsNew );
290285
memcpy( disabledContactSim, contactSim, sizeof( b2ContactSim ) );
291286

292-
int movedContactIndex = b2ContactSimArray_RemoveSwap( &awakeSet->contactsNew, localIndex );
293-
if ( movedContactIndex != B2_NULL_INDEX )
287+
int movedLocalIndex = b2ContactSimArray_RemoveSwap( &awakeSet->contactsNew, localIndex );
288+
if ( movedLocalIndex != B2_NULL_INDEX )
294289
{
295290
// fix moved element
296291
b2ContactSim* movedContactSim = awakeSet->contactsNew.data + localIndex;
297-
int movedId = movedContactSim->contactId;
298-
b2CheckIndex( contacts, movedId );
299-
B2_ASSERT( contacts[movedId].localIndex == movedContactIndex );
300-
contacts[movedId].localIndex = localIndex;
292+
b2Contact* movedContact = b2ContactArray_Get( &world->contactArray, movedContactSim->contactId );
293+
B2_ASSERT( movedContact->localIndex == movedLocalIndex );
294+
movedContact->localIndex = localIndex;
301295
}
302296
}
303297

@@ -308,12 +302,10 @@ void b2TrySleepIsland( b2World* world, int islandId )
308302
// move touching contacts
309303
// this shuffles contacts in the awake set
310304
{
311-
b2Contact* contacts = world->contactArray;
312305
int contactId = island->headContact;
313306
while ( contactId != B2_NULL_INDEX )
314307
{
315-
b2CheckIndex( contacts, contactId );
316-
b2Contact* contact = contacts + contactId;
308+
b2Contact* contact = b2ContactArray_Get( &world->contactArray, contactId);
317309
B2_ASSERT( contact->setIndex == b2_awakeSet );
318310
B2_ASSERT( contact->islandId == islandId );
319311
int colorIndex = contact->colorIndex;
@@ -329,23 +321,21 @@ void b2TrySleepIsland( b2World* world, int islandId )
329321
b2ClearBit( &color->bodySet, contact->edges[1].bodyId );
330322
}
331323

332-
int awakeContactIndex = contact->localIndex;
333-
b2ContactSim* awakeContactSim = b2ContactSimArray_Get( &color->contactSims, awakeContactIndex);
324+
int localIndex = contact->localIndex;
325+
b2ContactSim* awakeContactSim = b2ContactSimArray_Get( &color->contactSims, localIndex);
334326

335327
int sleepContactIndex = sleepSet->contactsNew.count;
336328
b2ContactSim* sleepContactSim = b2ContactSimArray_Add( &sleepSet->contactsNew );
337329
memcpy( sleepContactSim, awakeContactSim, sizeof( b2ContactSim ) );
338330

339-
int movedIndex = b2ContactSimArray_RemoveSwap( &color->contactSims, awakeContactIndex );
340-
if ( movedIndex != B2_NULL_INDEX )
331+
int movedLocalIndex = b2ContactSimArray_RemoveSwap( &color->contactSims, localIndex );
332+
if ( movedLocalIndex != B2_NULL_INDEX )
341333
{
342334
// fix moved element
343-
b2ContactSim* movedContactSim = color->contactSims.data + awakeContactIndex;
344-
int movedId = movedContactSim->contactId;
345-
b2CheckIndex( contacts, movedId );
346-
b2Contact* movedContact = contacts + movedId;
347-
B2_ASSERT( movedContact->localIndex == movedIndex );
348-
movedContact->localIndex = awakeContactIndex;
335+
b2ContactSim* movedContactSim = color->contactSims.data + localIndex;
336+
b2Contact* movedContact = b2ContactArray_Get( &world->contactArray, movedContactSim->contactId );
337+
B2_ASSERT( movedContact->localIndex == movedLocalIndex );
338+
movedContact->localIndex = localIndex;
349339
}
350340

351341
contact->setIndex = sleepSetId;
@@ -474,13 +464,12 @@ void b2MergeSolverSets( b2World* world, int setId1, int setId2 )
474464

475465
// transfer contacts
476466
{
477-
b2Contact* contacts = world->contactArray;
478467
int contactCount = set2->contactsNew.count;
479468
for ( int i = 0; i < contactCount; ++i )
480469
{
481470
b2ContactSim* contactSrc = set2->contactsNew.data + i;
482471

483-
b2Contact* contact = contacts + contactSrc->contactId;
472+
b2Contact* contact = b2ContactArray_Get(&world->contactArray, contactSrc->contactId);
484473
B2_ASSERT( contact->setIndex == setId2 );
485474
contact->setIndex = setId1;
486475
contact->localIndex = set1->contactsNew.count;

0 commit comments

Comments
 (0)