Skip to content

Commit

Permalink
[KT] Box2d idiomatic - Use ?: for early return in some places where i…
Browse files Browse the repository at this point in the history
…t returns on null

PiperOrigin-RevId: 720655830
  • Loading branch information
Googler authored and copybara-github committed Jan 28, 2025
1 parent 7bd0d3a commit dc432e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,7 @@ class Island {
}

fun report(constraints: Array<ContactVelocityConstraint>) {
if (listener == null) {
return
}
val listener = this.listener ?: return
for (i in 0 until contactCount) {
val c = contacts[i]!!
val vc = constraints[i]
Expand All @@ -563,7 +561,7 @@ class Island {
impulse.normalImpulses[j] = vc.points[j].normalImpulse
impulse.tangentImpulses[j] = vc.points[j].tangentImpulse
}
listener!!.postSolve(c, impulse)
listener.postSolve(c, impulse)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class World(gravity: Vec2, val pool: IWorldPool, broadPhaseStrategy: BroadPhaseS
*/
constructor(
gravity: Vec2,
pool: IWorldPool = DefaultWorldPool(WORLD_POOL_SIZE, WORLD_POOL_CONTAINER_SIZE)
pool: IWorldPool = DefaultWorldPool(WORLD_POOL_SIZE, WORLD_POOL_CONTAINER_SIZE),
) : this(gravity, pool, DynamicTree()) {}

init {
Expand Down Expand Up @@ -576,10 +576,8 @@ class World(gravity: Vec2, val pool: IWorldPool, broadPhaseStrategy: BroadPhaseS

/** Call this to draw shapes and other debug draw data. */
fun drawDebugData() {
if (debugDraw == null) {
return
}
val flags: Int = debugDraw!!.flags
val debugDraw = this.debugDraw ?: return
val flags: Int = debugDraw.flags
if (flags and DebugDraw.E_SHAPE_BIT == DebugDraw.E_SHAPE_BIT) {
var b = bodyList
while (b != null) {
Expand Down Expand Up @@ -622,7 +620,7 @@ class World(gravity: Vec2, val pool: IWorldPool, broadPhaseStrategy: BroadPhaseS
val fixtureB = c.fixtureB
fixtureA.getAABB(c.indexA)!!.getCenterToOut(cA)
fixtureB.getAABB(c.indexB)!!.getCenterToOut(cB)
debugDraw!!.drawSegment(cA, cB, color)
debugDraw.drawSegment(cA, cB, color)
c = c.next
}
}
Expand All @@ -644,7 +642,7 @@ class World(gravity: Vec2, val pool: IWorldPool, broadPhaseStrategy: BroadPhaseS
vs[1].set(aabb.upperBound.x, aabb.lowerBound.y)
vs[2].set(aabb.upperBound.x, aabb.upperBound.y)
vs[3].set(aabb.lowerBound.x, aabb.upperBound.y)
debugDraw!!.drawPolygon(vs, 4, color)
debugDraw.drawPolygon(vs, 4, color)
}
f = f.next
}
Expand All @@ -656,12 +654,12 @@ class World(gravity: Vec2, val pool: IWorldPool, broadPhaseStrategy: BroadPhaseS
while (b != null) {
xf.set(b.xf)
xf.p.set(b.sweep.c)
debugDraw!!.drawTransform(xf)
debugDraw.drawTransform(xf)
b = b.next
}
}
if (flags and DebugDraw.E_DYNAMIC_TREE_BIT == DebugDraw.E_DYNAMIC_TREE_BIT) {
contactManager.broadPhase.drawTree(debugDraw!!)
contactManager.broadPhase.drawTree(debugDraw)
}
}

Expand Down Expand Up @@ -726,7 +724,7 @@ class World(gravity: Vec2, val pool: IWorldPool, broadPhaseStrategy: BroadPhaseS
bodyCount,
contactManager.contactCount,
jointCount,
contactManager.contactListener
contactManager.contactListener,
)

// Clear all the island flags.
Expand Down Expand Up @@ -905,7 +903,7 @@ class World(gravity: Vec2, val pool: IWorldPool, broadPhaseStrategy: BroadPhaseS
2 * Settings.MAX_TOI_CONTACTS,
Settings.MAX_TOI_CONTACTS,
0,
contactManager.contactListener
contactManager.contactListener,
)
if (stepComplete) {
var b = bodyList
Expand Down

0 comments on commit dc432e4

Please sign in to comment.