Skip to content

Commit 8197d51

Browse files
committed
refactor: encapsulate islang flag in Body
1 parent 35dc473 commit 8197d51

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/dynamics/Body.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @author Daniel Murphy
2929
*/
3030
public final class Body {
31-
static final int e_islandFlag = 0x0001;
31+
private static final int e_islandFlag = 0x0001;
3232

3333
private static final int e_awakeFlag = 0x0002;
3434
private static final int e_autoSleepFlag = 0x0004;
@@ -939,6 +939,26 @@ public void setBullet(boolean flag) {
939939
}
940940
}
941941

942+
boolean isIslandFlagOff() {
943+
return (m_flags & e_islandFlag) == 0;
944+
}
945+
946+
boolean isIslandFlagOn() {
947+
return (m_flags & e_islandFlag) != 0;
948+
}
949+
950+
boolean isIslandFlagOn2() {
951+
return (m_flags & e_islandFlag) == e_islandFlag;
952+
}
953+
954+
void setIslandFlag(boolean flag) {
955+
if (flag) {
956+
m_flags |= e_islandFlag;
957+
} else {
958+
m_flags &= ~e_islandFlag;
959+
}
960+
}
961+
942962
/**
943963
* You can disable sleeping on this body.
944964
* If you disable sleeping, the body will be woken.

fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/dynamics/Island.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ private void report(ContactVelocityConstraint[] constraints) {
530530
void resetFlagsAndSynchronizeBroadphaseProxies() {
531531
for (int i = 0; i < bodyCount; ++i) {
532532
Body body = bodies[i];
533-
body.m_flags &= ~Body.e_islandFlag;
533+
body.setIslandFlag(false);
534534

535535
if (body.getType() != BodyType.DYNAMIC) {
536536
continue;
@@ -550,7 +550,7 @@ void postSolveCleanup() {
550550
// Allow static bodies to participate in other islands.
551551
Body b = bodies[i];
552552
if (b.getType() == BodyType.STATIC) {
553-
b.m_flags &= ~Body.e_islandFlag;
553+
b.setIslandFlag(false);
554554
}
555555
}
556556
}

fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/dynamics/World.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private void solve(TimeStep step) {
308308

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

326326
for (Body seed : bodies) {
327-
if ((seed.m_flags & Body.e_islandFlag) == Body.e_islandFlag) {
327+
if (seed.isIslandFlagOn2()) {
328328
continue;
329329
}
330330

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

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

388388
// Was the other body already added to this island?
389-
if ((other.m_flags & Body.e_islandFlag) == Body.e_islandFlag) {
389+
if (other.isIslandFlagOn2()) {
390390
continue;
391391
}
392392

393393
assert stackCount < stackSize;
394394
stack[stackCount++] = other;
395-
other.m_flags |= Body.e_islandFlag;
395+
other.setIslandFlag(true);
396396
}
397397

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

414-
if ((other.m_flags & Body.e_islandFlag) == Body.e_islandFlag) {
414+
if (other.isIslandFlagOn2()) {
415415
continue;
416416
}
417417

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

@@ -459,7 +459,7 @@ private void solveTOI(final TimeStep step) {
459459

460460
if (stepComplete) {
461461
for (Body b : bodies) {
462-
b.m_flags &= ~Body.e_islandFlag;
462+
b.setIslandFlag(false);
463463
b.m_sweep.alpha0 = 0.0f;
464464
}
465465

@@ -613,8 +613,8 @@ private void solveTOI(final TimeStep step) {
613613
island.add(bB);
614614
island.add(minContact);
615615

616-
bA.m_flags |= Body.e_islandFlag;
617-
bB.m_flags |= Body.e_islandFlag;
616+
bA.setIslandFlag(true);
617+
bB.setIslandFlag(true);
618618
minContact.m_flags |= Contact.ISLAND_FLAG;
619619

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

655655
// Tentatively advance the body to the TOI.
656656
backup1.set(other.m_sweep);
657-
if ((other.m_flags & Body.e_islandFlag) == 0) {
657+
if (other.isIslandFlagOff()) {
658658
other.advance(minAlpha);
659659
}
660660

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

682682
// Has the other body already been added to the island?
683-
if ((other.m_flags & Body.e_islandFlag) != 0) {
683+
if (other.isIslandFlagOn()) {
684684
continue;
685685
}
686686

687687
// Add the other body to the island.
688-
other.m_flags |= Body.e_islandFlag;
688+
other.setIslandFlag(true);
689689

690690
if (other.getType() != BodyType.STATIC) {
691691
other.setAwake(true);

0 commit comments

Comments
 (0)