Skip to content

Commit

Permalink
refactor: encapsulate islang flag in Body
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Feb 17, 2024
1 parent 35dc473 commit 8197d51
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author Daniel Murphy
*/
public final class Body {
static final int e_islandFlag = 0x0001;
private static final int e_islandFlag = 0x0001;

private static final int e_awakeFlag = 0x0002;
private static final int e_autoSleepFlag = 0x0004;
Expand Down Expand Up @@ -939,6 +939,26 @@ public void setBullet(boolean flag) {
}
}

boolean isIslandFlagOff() {
return (m_flags & e_islandFlag) == 0;
}

boolean isIslandFlagOn() {
return (m_flags & e_islandFlag) != 0;
}

boolean isIslandFlagOn2() {
return (m_flags & e_islandFlag) == e_islandFlag;
}

void setIslandFlag(boolean flag) {
if (flag) {
m_flags |= e_islandFlag;
} else {
m_flags &= ~e_islandFlag;
}
}

/**
* You can disable sleeping on this body.
* If you disable sleeping, the body will be woken.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ private void report(ContactVelocityConstraint[] constraints) {
void resetFlagsAndSynchronizeBroadphaseProxies() {
for (int i = 0; i < bodyCount; ++i) {
Body body = bodies[i];
body.m_flags &= ~Body.e_islandFlag;
body.setIslandFlag(false);

if (body.getType() != BodyType.DYNAMIC) {
continue;
Expand All @@ -550,7 +550,7 @@ void postSolveCleanup() {
// Allow static bodies to participate in other islands.
Body b = bodies[i];
if (b.getType() == BodyType.STATIC) {
b.m_flags &= ~Body.e_islandFlag;
b.setIslandFlag(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private void solve(TimeStep step) {

// Clear all the island flags.
for (Body b : bodies) {
b.m_flags &= ~Body.e_islandFlag;
b.setIslandFlag(false);
}
for (Contact c = contactManager.contactList; c != null; c = c.m_next) {
c.m_flags &= ~Contact.ISLAND_FLAG;
Expand All @@ -324,7 +324,7 @@ private void solve(TimeStep step) {
}

for (Body seed : bodies) {
if ((seed.m_flags & Body.e_islandFlag) == Body.e_islandFlag) {
if (seed.isIslandFlagOn2()) {
continue;
}

Expand All @@ -341,7 +341,7 @@ private void solve(TimeStep step) {
island.clear();
int stackCount = 0;
stack[stackCount++] = seed;
seed.m_flags |= Body.e_islandFlag;
seed.setIslandFlag(true);

// Perform a depth first search (DFS) on the constraint graph.
while (stackCount > 0) {
Expand Down Expand Up @@ -386,13 +386,13 @@ private void solve(TimeStep step) {
Body other = ce.other;

// Was the other body already added to this island?
if ((other.m_flags & Body.e_islandFlag) == Body.e_islandFlag) {
if (other.isIslandFlagOn2()) {
continue;
}

assert stackCount < stackSize;
stack[stackCount++] = other;
other.m_flags |= Body.e_islandFlag;
other.setIslandFlag(true);
}

// Search all joints connect to this body.
Expand All @@ -411,13 +411,13 @@ private void solve(TimeStep step) {
island.add(je.joint);
je.joint.m_islandFlag = true;

if ((other.m_flags & Body.e_islandFlag) == Body.e_islandFlag) {
if (other.isIslandFlagOn2()) {
continue;
}

assert stackCount < stackSize;
stack[stackCount++] = other;
other.m_flags |= Body.e_islandFlag;
other.setIslandFlag(true);
}
}
island.solve(step, gravity, allowSleep);
Expand All @@ -428,7 +428,7 @@ private void solve(TimeStep step) {
// Synchronize fixtures, check for out of range bodies.
for (Body b : bodies) {
// If a body was not in an island then it did not move.
if ((b.m_flags & Body.e_islandFlag) == 0) {
if (b.isIslandFlagOff()) {
continue;
}

Expand Down Expand Up @@ -459,7 +459,7 @@ private void solveTOI(final TimeStep step) {

if (stepComplete) {
for (Body b : bodies) {
b.m_flags &= ~Body.e_islandFlag;
b.setIslandFlag(false);
b.m_sweep.alpha0 = 0.0f;
}

Expand Down Expand Up @@ -613,8 +613,8 @@ private void solveTOI(final TimeStep step) {
island.add(bB);
island.add(minContact);

bA.m_flags |= Body.e_islandFlag;
bB.m_flags |= Body.e_islandFlag;
bA.setIslandFlag(true);
bB.setIslandFlag(true);
minContact.m_flags |= Contact.ISLAND_FLAG;

// Get contacts on bodyA and bodyB.
Expand Down Expand Up @@ -654,7 +654,7 @@ private void solveTOI(final TimeStep step) {

// Tentatively advance the body to the TOI.
backup1.set(other.m_sweep);
if ((other.m_flags & Body.e_islandFlag) == 0) {
if (other.isIslandFlagOff()) {
other.advance(minAlpha);
}

Expand All @@ -680,12 +680,12 @@ private void solveTOI(final TimeStep step) {
island.add(contact);

// Has the other body already been added to the island?
if ((other.m_flags & Body.e_islandFlag) != 0) {
if (other.isIslandFlagOn()) {
continue;
}

// Add the other body to the island.
other.m_flags |= Body.e_islandFlag;
other.setIslandFlag(true);

if (other.getType() != BodyType.STATIC) {
other.setAwake(true);
Expand Down

0 comments on commit 8197d51

Please sign in to comment.