Skip to content

Commit 5361601

Browse files
committed
refactor: DistanceProxy
1 parent b650de0 commit 5361601

File tree

5 files changed

+109
-101
lines changed

5 files changed

+109
-101
lines changed

fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/Distance.java

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -430,104 +430,6 @@ public void solve3() {
430430
}
431431
}
432432

433-
/**
434-
* A distance proxy is used by the GJK algorithm. It encapsulates any shape. jbox2dTODO: see if we can
435-
* just do assignments with m_vertices, instead of copying stuff over
436-
*
437-
* @author daniel
438-
*/
439-
public static class DistanceProxy {
440-
public final Vec2[] m_vertices = new Vec2[JBoxSettings.maxPolygonVertices];
441-
public int m_count = 0;
442-
public float m_radius = 0f;
443-
public final Vec2[] m_buffer = new Vec2[2];
444-
445-
public DistanceProxy() {
446-
for (int i = 0; i < m_vertices.length; i++) {
447-
m_vertices[i] = new Vec2();
448-
}
449-
}
450-
451-
/**
452-
* Initialize the proxy using the given shape. The shape must remain in scope while the proxy is
453-
* in use.
454-
*/
455-
public final void set(final Shape shape, int index) {
456-
switch (shape.getType()) {
457-
case CIRCLE:
458-
final CircleShape circle = (CircleShape) shape;
459-
m_vertices[0].set(circle.center);
460-
m_count = 1;
461-
m_radius = circle.getRadius();
462-
463-
break;
464-
case POLYGON:
465-
final PolygonShape poly = (PolygonShape) shape;
466-
m_count = poly.getVertexCount();
467-
m_radius = poly.getRadius();
468-
for (int i = 0; i < m_count; i++) {
469-
m_vertices[i].set(poly.m_vertices[i]);
470-
}
471-
break;
472-
case CHAIN:
473-
final ChainShape chain = (ChainShape) shape;
474-
assert 0 <= index && index < chain.getCount();
475-
476-
m_buffer[0] = chain.getVertex(index);
477-
if (index + 1 < chain.getCount()) {
478-
m_buffer[1] = chain.getVertex(index + 1);
479-
} else {
480-
m_buffer[1] = chain.getVertex(0);
481-
}
482-
483-
m_vertices[0].set(m_buffer[0]);
484-
m_vertices[1].set(m_buffer[1]);
485-
m_count = 2;
486-
m_radius = chain.getRadius();
487-
break;
488-
case EDGE:
489-
EdgeShape edge = (EdgeShape) shape;
490-
m_vertices[0].set(edge.m_vertex1);
491-
m_vertices[1].set(edge.m_vertex2);
492-
m_count = 2;
493-
m_radius = edge.getRadius();
494-
break;
495-
default:
496-
assert false;
497-
}
498-
}
499-
500-
/**
501-
* Get the supporting vertex index in the given direction.
502-
*
503-
* @param d
504-
* @return
505-
*/
506-
public final int getSupport(final Vec2 d) {
507-
int bestIndex = 0;
508-
float bestValue = Vec2.dot(m_vertices[0], d);
509-
for (int i = 1; i < m_count; i++) {
510-
float value = Vec2.dot(m_vertices[i], d);
511-
if (value > bestValue) {
512-
bestIndex = i;
513-
bestValue = value;
514-
}
515-
}
516-
517-
return bestIndex;
518-
}
519-
520-
/**
521-
* Used by Distance.
522-
*
523-
* @return a vertex by index
524-
*/
525-
public final Vec2 getVertex(int index) {
526-
assert 0 <= index && index < m_count;
527-
return m_vertices[index];
528-
}
529-
}
530-
531433
private Simplex simplex = new Simplex();
532434
private int[] saveA = new int[3];
533435
private int[] saveB = new int[3];
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4+
* See LICENSE for details.
5+
*/
6+
7+
package com.almasb.fxgl.physics.box2d.collision;
8+
9+
import com.almasb.fxgl.core.math.Vec2;
10+
import com.almasb.fxgl.physics.box2d.collision.shapes.*;
11+
import com.almasb.fxgl.physics.box2d.common.JBoxSettings;
12+
13+
/**
14+
* A distance proxy is used by the GJK algorithm. It encapsulates any shape. jbox2dTODO: see if we can
15+
* just do assignments with m_vertices, instead of copying stuff over
16+
*
17+
* @author daniel
18+
*/
19+
public class DistanceProxy {
20+
public final Vec2[] m_vertices = new Vec2[JBoxSettings.maxPolygonVertices];
21+
public int m_count = 0;
22+
public float m_radius = 0f;
23+
public final Vec2[] m_buffer = new Vec2[2];
24+
25+
public DistanceProxy() {
26+
for (int i = 0; i < m_vertices.length; i++) {
27+
m_vertices[i] = new Vec2();
28+
}
29+
}
30+
31+
/**
32+
* Initialize the proxy using the given shape. The shape must remain in scope while the proxy is
33+
* in use.
34+
*/
35+
public final void set(final Shape shape, int index) {
36+
switch (shape.getType()) {
37+
case CIRCLE:
38+
final CircleShape circle = (CircleShape) shape;
39+
m_vertices[0].set(circle.center);
40+
m_count = 1;
41+
m_radius = circle.getRadius();
42+
43+
break;
44+
case POLYGON:
45+
final PolygonShape poly = (PolygonShape) shape;
46+
m_count = poly.getVertexCount();
47+
m_radius = poly.getRadius();
48+
for (int i = 0; i < m_count; i++) {
49+
m_vertices[i].set(poly.m_vertices[i]);
50+
}
51+
break;
52+
case CHAIN:
53+
final ChainShape chain = (ChainShape) shape;
54+
assert 0 <= index && index < chain.getCount();
55+
56+
m_buffer[0] = chain.getVertex(index);
57+
if (index + 1 < chain.getCount()) {
58+
m_buffer[1] = chain.getVertex(index + 1);
59+
} else {
60+
m_buffer[1] = chain.getVertex(0);
61+
}
62+
63+
m_vertices[0].set(m_buffer[0]);
64+
m_vertices[1].set(m_buffer[1]);
65+
m_count = 2;
66+
m_radius = chain.getRadius();
67+
break;
68+
case EDGE:
69+
EdgeShape edge = (EdgeShape) shape;
70+
m_vertices[0].set(edge.m_vertex1);
71+
m_vertices[1].set(edge.m_vertex2);
72+
m_count = 2;
73+
m_radius = edge.getRadius();
74+
break;
75+
default:
76+
assert false;
77+
}
78+
}
79+
80+
/**
81+
* Get the supporting vertex index in the given direction.
82+
*
83+
* @param d
84+
* @return
85+
*/
86+
public final int getSupport(final Vec2 d) {
87+
int bestIndex = 0;
88+
float bestValue = Vec2.dot(m_vertices[0], d);
89+
for (int i = 1; i < m_count; i++) {
90+
float value = Vec2.dot(m_vertices[i], d);
91+
if (value > bestValue) {
92+
bestIndex = i;
93+
bestValue = value;
94+
}
95+
}
96+
97+
return bestIndex;
98+
}
99+
100+
/**
101+
* Used by Distance.
102+
*
103+
* @return a vertex by index
104+
*/
105+
public final Vec2 getVertex(int index) {
106+
assert 0 <= index && index < m_count;
107+
return m_vertices[index];
108+
}
109+
}

fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/SeparationFunction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package com.almasb.fxgl.physics.box2d.collision;
88

99
import com.almasb.fxgl.core.math.Vec2;
10-
import com.almasb.fxgl.physics.box2d.collision.Distance.DistanceProxy;
1110
import com.almasb.fxgl.physics.box2d.collision.Distance.SimplexCache;
1211
import com.almasb.fxgl.physics.box2d.common.Rotation;
1312
import com.almasb.fxgl.physics.box2d.common.Sweep;

fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/collision/TimeOfImpact.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package com.almasb.fxgl.physics.box2d.collision;
77

88
import com.almasb.fxgl.core.math.FXGLMath;
9-
import com.almasb.fxgl.physics.box2d.collision.Distance.DistanceProxy;
109
import com.almasb.fxgl.physics.box2d.collision.Distance.SimplexCache;
1110
import com.almasb.fxgl.physics.box2d.common.JBoxSettings;
1211
import com.almasb.fxgl.physics.box2d.common.Sweep;

fxgl-entity/src/main/kotlin/com/almasb/fxgl/physics/box2d/collision/DistanceData.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package com.almasb.fxgl.physics.box2d.collision
88

99
import com.almasb.fxgl.core.math.Vec2
10-
import com.almasb.fxgl.physics.box2d.collision.Distance.DistanceProxy
1110
import com.almasb.fxgl.physics.box2d.common.Transform
1211

1312
/**

0 commit comments

Comments
 (0)