From 5361601a3d41b7af9956ab8774993fdf92f3a62a Mon Sep 17 00:00:00 2001 From: Almas Baimagambetov Date: Wed, 22 Mar 2023 23:52:51 +0000 Subject: [PATCH] refactor: DistanceProxy --- .../physics/box2d/collision/Distance.java | 98 ---------------- .../box2d/collision/DistanceProxy.java | 109 ++++++++++++++++++ .../box2d/collision/SeparationFunction.java | 1 - .../physics/box2d/collision/TimeOfImpact.java | 1 - .../physics/box2d/collision/DistanceData.kt | 1 - 5 files changed, 109 insertions(+), 101 deletions(-) create mode 100644 fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/DistanceProxy.java diff --git a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/Distance.java b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/Distance.java index dd52af972..2df4509f1 100644 --- a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/Distance.java +++ b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/Distance.java @@ -430,104 +430,6 @@ public void solve3() { } } - /** - * A distance proxy is used by the GJK algorithm. It encapsulates any shape. jbox2dTODO: see if we can - * just do assignments with m_vertices, instead of copying stuff over - * - * @author daniel - */ - public static class DistanceProxy { - public final Vec2[] m_vertices = new Vec2[JBoxSettings.maxPolygonVertices]; - public int m_count = 0; - public float m_radius = 0f; - public final Vec2[] m_buffer = new Vec2[2]; - - public DistanceProxy() { - for (int i = 0; i < m_vertices.length; i++) { - m_vertices[i] = new Vec2(); - } - } - - /** - * Initialize the proxy using the given shape. The shape must remain in scope while the proxy is - * in use. - */ - public final void set(final Shape shape, int index) { - switch (shape.getType()) { - case CIRCLE: - final CircleShape circle = (CircleShape) shape; - m_vertices[0].set(circle.center); - m_count = 1; - m_radius = circle.getRadius(); - - break; - case POLYGON: - final PolygonShape poly = (PolygonShape) shape; - m_count = poly.getVertexCount(); - m_radius = poly.getRadius(); - for (int i = 0; i < m_count; i++) { - m_vertices[i].set(poly.m_vertices[i]); - } - break; - case CHAIN: - final ChainShape chain = (ChainShape) shape; - assert 0 <= index && index < chain.getCount(); - - m_buffer[0] = chain.getVertex(index); - if (index + 1 < chain.getCount()) { - m_buffer[1] = chain.getVertex(index + 1); - } else { - m_buffer[1] = chain.getVertex(0); - } - - m_vertices[0].set(m_buffer[0]); - m_vertices[1].set(m_buffer[1]); - m_count = 2; - m_radius = chain.getRadius(); - break; - case EDGE: - EdgeShape edge = (EdgeShape) shape; - m_vertices[0].set(edge.m_vertex1); - m_vertices[1].set(edge.m_vertex2); - m_count = 2; - m_radius = edge.getRadius(); - break; - default: - assert false; - } - } - - /** - * Get the supporting vertex index in the given direction. - * - * @param d - * @return - */ - public final int getSupport(final Vec2 d) { - int bestIndex = 0; - float bestValue = Vec2.dot(m_vertices[0], d); - for (int i = 1; i < m_count; i++) { - float value = Vec2.dot(m_vertices[i], d); - if (value > bestValue) { - bestIndex = i; - bestValue = value; - } - } - - return bestIndex; - } - - /** - * Used by Distance. - * - * @return a vertex by index - */ - public final Vec2 getVertex(int index) { - assert 0 <= index && index < m_count; - return m_vertices[index]; - } - } - private Simplex simplex = new Simplex(); private int[] saveA = new int[3]; private int[] saveB = new int[3]; diff --git a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/DistanceProxy.java b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/DistanceProxy.java new file mode 100644 index 000000000..d158e2d4e --- /dev/null +++ b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/DistanceProxy.java @@ -0,0 +1,109 @@ +/* + * FXGL - JavaFX Game Library. The MIT License (MIT). + * Copyright (c) AlmasB (almaslvl@gmail.com). + * See LICENSE for details. + */ + +package com.almasb.fxgl.physics.box2d.collision; + +import com.almasb.fxgl.core.math.Vec2; +import com.almasb.fxgl.physics.box2d.collision.shapes.*; +import com.almasb.fxgl.physics.box2d.common.JBoxSettings; + +/** + * A distance proxy is used by the GJK algorithm. It encapsulates any shape. jbox2dTODO: see if we can + * just do assignments with m_vertices, instead of copying stuff over + * + * @author daniel + */ +public class DistanceProxy { + public final Vec2[] m_vertices = new Vec2[JBoxSettings.maxPolygonVertices]; + public int m_count = 0; + public float m_radius = 0f; + public final Vec2[] m_buffer = new Vec2[2]; + + public DistanceProxy() { + for (int i = 0; i < m_vertices.length; i++) { + m_vertices[i] = new Vec2(); + } + } + + /** + * Initialize the proxy using the given shape. The shape must remain in scope while the proxy is + * in use. + */ + public final void set(final Shape shape, int index) { + switch (shape.getType()) { + case CIRCLE: + final CircleShape circle = (CircleShape) shape; + m_vertices[0].set(circle.center); + m_count = 1; + m_radius = circle.getRadius(); + + break; + case POLYGON: + final PolygonShape poly = (PolygonShape) shape; + m_count = poly.getVertexCount(); + m_radius = poly.getRadius(); + for (int i = 0; i < m_count; i++) { + m_vertices[i].set(poly.m_vertices[i]); + } + break; + case CHAIN: + final ChainShape chain = (ChainShape) shape; + assert 0 <= index && index < chain.getCount(); + + m_buffer[0] = chain.getVertex(index); + if (index + 1 < chain.getCount()) { + m_buffer[1] = chain.getVertex(index + 1); + } else { + m_buffer[1] = chain.getVertex(0); + } + + m_vertices[0].set(m_buffer[0]); + m_vertices[1].set(m_buffer[1]); + m_count = 2; + m_radius = chain.getRadius(); + break; + case EDGE: + EdgeShape edge = (EdgeShape) shape; + m_vertices[0].set(edge.m_vertex1); + m_vertices[1].set(edge.m_vertex2); + m_count = 2; + m_radius = edge.getRadius(); + break; + default: + assert false; + } + } + + /** + * Get the supporting vertex index in the given direction. + * + * @param d + * @return + */ + public final int getSupport(final Vec2 d) { + int bestIndex = 0; + float bestValue = Vec2.dot(m_vertices[0], d); + for (int i = 1; i < m_count; i++) { + float value = Vec2.dot(m_vertices[i], d); + if (value > bestValue) { + bestIndex = i; + bestValue = value; + } + } + + return bestIndex; + } + + /** + * Used by Distance. + * + * @return a vertex by index + */ + public final Vec2 getVertex(int index) { + assert 0 <= index && index < m_count; + return m_vertices[index]; + } +} diff --git a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/SeparationFunction.java b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/SeparationFunction.java index 50deb43c6..ca79fbc34 100644 --- a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/SeparationFunction.java +++ b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/SeparationFunction.java @@ -7,7 +7,6 @@ package com.almasb.fxgl.physics.box2d.collision; import com.almasb.fxgl.core.math.Vec2; -import com.almasb.fxgl.physics.box2d.collision.Distance.DistanceProxy; import com.almasb.fxgl.physics.box2d.collision.Distance.SimplexCache; import com.almasb.fxgl.physics.box2d.common.Rotation; import com.almasb.fxgl.physics.box2d.common.Sweep; diff --git a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/TimeOfImpact.java b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/TimeOfImpact.java index d6ba4ea99..2b33d2c89 100644 --- a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/TimeOfImpact.java +++ b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/TimeOfImpact.java @@ -6,7 +6,6 @@ package com.almasb.fxgl.physics.box2d.collision; import com.almasb.fxgl.core.math.FXGLMath; -import com.almasb.fxgl.physics.box2d.collision.Distance.DistanceProxy; import com.almasb.fxgl.physics.box2d.collision.Distance.SimplexCache; import com.almasb.fxgl.physics.box2d.common.JBoxSettings; import com.almasb.fxgl.physics.box2d.common.Sweep; diff --git a/fxgl-entity/src/main/kotlin/com/almasb/fxgl/physics/box2d/collision/DistanceData.kt b/fxgl-entity/src/main/kotlin/com/almasb/fxgl/physics/box2d/collision/DistanceData.kt index d5b0d85c2..206e0050d 100644 --- a/fxgl-entity/src/main/kotlin/com/almasb/fxgl/physics/box2d/collision/DistanceData.kt +++ b/fxgl-entity/src/main/kotlin/com/almasb/fxgl/physics/box2d/collision/DistanceData.kt @@ -7,7 +7,6 @@ package com.almasb.fxgl.physics.box2d.collision import com.almasb.fxgl.core.math.Vec2 -import com.almasb.fxgl.physics.box2d.collision.Distance.DistanceProxy import com.almasb.fxgl.physics.box2d.common.Transform /**