Skip to content

Commit c52d0ef

Browse files
committed
refactor: use joints array size to track joint count
1 parent 12e2cb4 commit c52d0ef

File tree

2 files changed

+23
-6
lines changed
  • fxgl-entity/src
    • main/java/com/almasb/fxgl/physics/box2d/dynamics
    • test/kotlin/com/almasb/fxgl/physics/box2d/dynamics

2 files changed

+23
-6
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public final class World {
6565
private Array<Body> bodies = new Array<>(WORLD_POOL_SIZE);
6666

6767
private Array<Joint> joints = new Array<>();
68-
private int jointCount = 0;
6968

7069
private final Vec2 gravity = new Vec2();
7170

@@ -128,7 +127,6 @@ public <T extends Joint> T createJoint(JointDef<T> def) {
128127
T j = Joint.create(this, def);
129128

130129
joints.add(j);
131-
++jointCount;
132130

133131
// Connect to the bodies' doubly linked lists.
134132
j.m_edgeA.joint = j;
@@ -215,8 +213,6 @@ public void destroyJoint(Joint j) {
215213

216214
Joint.destroy(j);
217215

218-
--jointCount;
219-
220216
// If the joint prevents collisions, then flag any contacts for filtering.
221217
if (!collideConnected) {
222218
flagContactsForFiltering(bodyA, bodyB);
@@ -308,7 +304,7 @@ private void solve(TimeStep step) {
308304
}
309305

310306
// Size the island for the worst case.
311-
island.init(getBodyCount(), contactManager.contactCount, jointCount, contactManager.getContactListener());
307+
island.init(getBodyCount(), contactManager.contactCount, getJointCount(), contactManager.getContactListener());
312308

313309
// Clear all the island flags.
314310
for (Body b : bodies) {
@@ -1189,7 +1185,7 @@ public int getBodyCount() {
11891185
* @return the number of joints
11901186
*/
11911187
public int getJointCount() {
1192-
return jointCount;
1188+
return joints.size();
11931189
}
11941190

11951191
/**

fxgl-entity/src/test/kotlin/com/almasb/fxgl/physics/box2d/dynamics/WorldTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
package com.almasb.fxgl.physics.box2d.dynamics
88

99
import com.almasb.fxgl.core.math.Vec2
10+
import com.almasb.fxgl.physics.box2d.dynamics.joints.RevoluteJointDef
11+
import org.hamcrest.CoreMatchers
12+
import org.hamcrest.CoreMatchers.*
13+
import org.hamcrest.MatcherAssert
14+
import org.hamcrest.MatcherAssert.*
1015
import org.junit.jupiter.api.Assertions.*
1116
import org.junit.jupiter.api.Test
1217

@@ -22,4 +27,20 @@ class WorldTest {
2227

2328
assertNotNull(world.contactManager.broadPhase)
2429
}
30+
31+
@Test
32+
fun `joint count`() {
33+
val world = World(Vec2())
34+
35+
val jointDef = RevoluteJointDef()
36+
jointDef.initialize(Body(BodyDef(), world), Body(BodyDef(), world), Vec2())
37+
38+
val joint = world.createJoint(jointDef)
39+
40+
assertThat(world.jointCount, `is`(1))
41+
42+
world.destroyJoint(joint)
43+
44+
assertThat(world.jointCount, `is`(0))
45+
}
2546
}

0 commit comments

Comments
 (0)