Skip to content

Commit

Permalink
[KT] Box2D idiomatic - To add coverage for property delegation, make …
Browse files Browse the repository at this point in the history
…a few properties `by lazy`

These also seem to be initialized conditionally. I did a quick test and found that in many (most) cases, the class is instantiated but the property is never initialized, so they seem like good candidates for `by lazy`.

PiperOrigin-RevId: 728827871
  • Loading branch information
Googler authored and copybara-github committed Feb 19, 2025
1 parent de88d90 commit 796775e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,8 @@ class Collision(private val pool: IWorldPool) {

/** This holds polygon B expressed in frame A. */
internal class TempPolygon {
val vertices = Array<Vec2>(Settings.MAX_POLYGON_VERTICES) { Vec2() }
val normals = Array<Vec2>(Settings.MAX_POLYGON_VERTICES) { Vec2() }
val vertices: Array<Vec2> by lazy { Array<Vec2>(Settings.MAX_POLYGON_VERTICES) { Vec2() } }
val normals: Array<Vec2> by lazy { Array<Vec2>(Settings.MAX_POLYGON_VERTICES) { Vec2() } }
var count = 0
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ class Distance {
}
}

/**
* this returns pooled objects. don't keep or modify them
*
* @return
*/
/** this returns pooled objects. don't keep or modify them */
fun getClosestPoint(out: Vec2) {
when (count) {
0 -> {
Expand Down Expand Up @@ -448,7 +444,7 @@ class Distance {
* @author daniel
*/
class DistanceProxy {
val vertices: Array<Vec2> = Array<Vec2>(Settings.MAX_POLYGON_VERTICES) { Vec2() }
val vertices: Array<Vec2> by lazy { Array<Vec2>(Settings.MAX_POLYGON_VERTICES) { Vec2() } }

var count: Int = 0
var radius: Float = 0f
Expand Down Expand Up @@ -498,12 +494,7 @@ class Distance {
}
}

/**
* Get the supporting vertex index in the given direction.
*
* @param d
* @return
*/
/** Get the supporting vertex index in the given direction. */
fun getSupport(d: Vec2): Int {
var bestIndex = 0
var bestValue = vertices[0] dot d
Expand All @@ -517,12 +508,7 @@ class Distance {
return bestIndex
}

/**
* Get the supporting vertex in the given direction.
*
* @param d
* @return
*/
/** Get the supporting vertex in the given direction. */
fun getSupportVertex(d: Vec2): Vec2 {
var bestIndex = 0
var bestValue = vertices[0] dot d
Expand All @@ -541,10 +527,6 @@ class Distance {
* Compute the closest points between two shapes. Supports any combination of: CircleShape and
* PolygonShape. The simplex cache is input/output. On the first call set SimplexCache.count to
* zero.
*
* @param output
* @param cache
* @param input
*/
fun distance(output: DistanceOutput, cache: SimplexCache, input: DistanceInput) {
GJK_CALLS++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class WorldManifold {
val normal: Vec2 = Vec2()

/** World contact point (point of intersection) */
val points: Array<Vec2> = Array<Vec2>(Settings.MAX_MANIFOLD_POINTS) { Vec2() }
val points: Array<Vec2> by lazy { Array<Vec2>(Settings.MAX_MANIFOLD_POINTS) { Vec2() } }
private val pool3 = Vec2()
private val pool4 = Vec2()

Expand All @@ -48,7 +48,7 @@ class WorldManifold {
xfA: Transform,
radiusA: Float,
xfB: Transform,
radiusB: Float
radiusB: Float,
) {
if (manifold.pointCount == 0) {
return
Expand Down

0 comments on commit 796775e

Please sign in to comment.