-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
259 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// SPDX-FileCopyrightText: 2023 Erin Catto | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "graph.h" | ||
|
||
#include "allocate.h" | ||
#include "array.h" | ||
#include "body.h" | ||
#include "contact.h" | ||
#include "core.h" | ||
#include "shape.h" | ||
#include "stack_allocator.h" | ||
#include "world.h" | ||
|
||
#include <stdbool.h> | ||
|
||
void b2CreateGraph(b2Graph* graph, int32_t bodyCapacity, int32_t contactCapacity) | ||
{ | ||
bodyCapacity = B2_MAX(bodyCapacity, 8); | ||
contactCapacity = B2_MAX(contactCapacity, 8); | ||
|
||
for (int32_t i = 0; i < b2_graphColorCount; ++i) | ||
{ | ||
b2GraphColor* color = graph->colors + i; | ||
color->bodySet = b2CreateBitSet(bodyCapacity); | ||
b2SetBitCountAndClear(&color->bodySet, bodyCapacity); | ||
|
||
color->contactArray = b2CreateArray(sizeof(int32_t), contactCapacity); | ||
} | ||
} | ||
|
||
void b2DestroyGraph(b2Graph* graph) | ||
{ | ||
for (int32_t i = 0; i < b2_graphColorCount; ++i) | ||
{ | ||
b2GraphColor* color = graph->colors + i; | ||
b2DestroyBitSet(&color->bodySet); | ||
b2DestroyArray(color->contactArray, sizeof(int32_t)); | ||
} | ||
} | ||
|
||
void b2AddContactToGraph(b2World* world, b2Graph* graph, b2Contact* contact) | ||
{ | ||
B2_ASSERT(contact->colorContactIndex == B2_NULL_INDEX); | ||
B2_ASSERT(contact->colorIndex == B2_NULL_INDEX); | ||
|
||
int32_t bodyIndexA = contact->edges[0].bodyIndex; | ||
int32_t bodyIndexB = contact->edges[1].bodyIndex; | ||
|
||
b2BodyType typeA = world->bodies[bodyIndexA].type; | ||
b2BodyType typeB = world->bodies[bodyIndexB].type; | ||
|
||
if (typeA == b2_dynamicBody && typeB == b2_dynamicBody) | ||
{ | ||
for (int32_t i = 0; i < b2_graphColorCount; ++i) | ||
{ | ||
b2GraphColor* color = graph->colors + i; | ||
if (b2GetBit(&color->bodySet, bodyIndexA) || b2GetBit(&color->bodySet, bodyIndexB)) | ||
{ | ||
continue; | ||
} | ||
|
||
b2SetBit(&color->bodySet, bodyIndexA); | ||
b2SetBit(&color->bodySet, bodyIndexB); | ||
|
||
contact->colorContactIndex = b2Array(color->contactArray).count; | ||
b2Array_Push(color->contactArray, contact->object.index); | ||
contact->colorIndex = i; | ||
break; | ||
} | ||
} | ||
else if (typeA == b2_dynamicBody) | ||
{ | ||
for (int32_t i = 0; i < b2_graphColorCount; ++i) | ||
{ | ||
b2GraphColor* color = graph->colors + i; | ||
if (b2GetBit(&color->bodySet, bodyIndexA)) | ||
{ | ||
continue; | ||
} | ||
|
||
b2SetBit(&color->bodySet, bodyIndexA); | ||
|
||
contact->colorContactIndex = b2Array(color->contactArray).count; | ||
b2Array_Push(color->contactArray, contact->object.index); | ||
contact->colorIndex = i; | ||
break; | ||
} | ||
} | ||
else if (typeB == b2_dynamicBody) | ||
{ | ||
for (int32_t i = 0; i < b2_graphColorCount; ++i) | ||
{ | ||
b2GraphColor* color = graph->colors + i; | ||
if (b2GetBit(&color->bodySet, bodyIndexB)) | ||
{ | ||
continue; | ||
} | ||
|
||
b2SetBit(&color->bodySet, bodyIndexB); | ||
|
||
contact->colorContactIndex = b2Array(color->contactArray).count; | ||
b2Array_Push(color->contactArray, contact->object.index); | ||
contact->colorIndex = i; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void b2RemoveContactFromGraph(b2World* world, b2Graph* graph, b2Contact* contact) | ||
{ | ||
if (contact->colorIndex == B2_NULL_INDEX) | ||
{ | ||
return; | ||
} | ||
|
||
B2_ASSERT(0 <= contact->colorIndex && contact->colorIndex < b2_graphColorCount); | ||
int32_t bodyIndexA = contact->edges[0].bodyIndex; | ||
int32_t bodyIndexB = contact->edges[1].bodyIndex; | ||
|
||
b2BodyType typeA = world->bodies[bodyIndexA].type; | ||
b2BodyType typeB = world->bodies[bodyIndexB].type; | ||
|
||
b2GraphColor* color = graph->colors + contact->colorIndex; | ||
|
||
int32_t colorContactIndex = contact->colorContactIndex; | ||
b2Array_RemoveSwap(color->contactArray, colorContactIndex); | ||
if (colorContactIndex < b2Array(color->contactArray).count) | ||
{ | ||
// Fix index on swapped contact | ||
int32_t swappedContactIndex = color->contactArray[colorContactIndex]; | ||
world->contacts[swappedContactIndex].colorContactIndex = colorContactIndex; | ||
} | ||
|
||
if (typeA == b2_dynamicBody && typeB == b2_dynamicBody) | ||
{ | ||
B2_ASSERT(b2GetBit(&color->bodySet, bodyIndexA) && b2GetBit(&color->bodySet, bodyIndexB)); | ||
|
||
b2ClearBit(&color->bodySet, bodyIndexA); | ||
b2ClearBit(&color->bodySet, bodyIndexB); | ||
} | ||
else if (typeA == b2_dynamicBody) | ||
{ | ||
B2_ASSERT(b2GetBit(&color->bodySet, bodyIndexA)); | ||
|
||
b2ClearBit(&color->bodySet, bodyIndexA); | ||
} | ||
else if (typeB == b2_dynamicBody) | ||
{ | ||
B2_ASSERT(b2GetBit(&color->bodySet, bodyIndexB)); | ||
|
||
b2ClearBit(&color->bodySet, bodyIndexB); | ||
} | ||
} | ||
|
||
void b2SolveGraph(b2World* world, b2Graph* graph) | ||
{ | ||
B2_MAYBE_UNUSED(world); | ||
B2_MAYBE_UNUSED(graph); | ||
} |
Oops, something went wrong.