Remove dedicated Faces class

This commit is contained in:
themode 2022-03-13 21:20:11 +01:00
parent a7ca1a37bf
commit 11708a1c1a
2 changed files with 20 additions and 30 deletions

View File

@ -11,8 +11,6 @@ import net.minestom.server.utils.chunk.ChunkUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
final class BlockCollision {
// Minimum move amount, minimum final velocity
private static final double MIN_DELTA = 0.001;
@ -28,7 +26,7 @@ final class BlockCollision {
*/
static PhysicsResult handlePhysics(@NotNull Entity entity, @NotNull Vec entityVelocity,
@Nullable PhysicsResult lastPhysicsResult) {
final BoundingBox.Faces faces = entity.getBoundingBox().faces();
final var faces = entity.getBoundingBox().faces();
Vec remainingMove = entityVelocity;
// Allocate once and update values
@ -74,8 +72,7 @@ final class BlockCollision {
return new PhysicsResult(entity.getPosition(), Vec.ZERO, false, false, false, false, entityVelocity, null, Block.AIR);
// Query faces to get the points needed for collision
Vec queryVec = new Vec(Math.signum(remainingMove.x()), Math.signum(remainingMove.y()), Math.signum(remainingMove.z()));
List<Vec> allFaces = faces.query().get(queryVec);
Vec[] allFaces = faces.get(new Vec(Math.signum(remainingMove.x()), Math.signum(remainingMove.y()), Math.signum(remainingMove.z())));
PhysicsResult res = handlePhysics(entity, remainingMove, entity.getPosition(), allFaces, finalResult);
@ -108,8 +105,7 @@ final class BlockCollision {
// If the entity isn't moving, break
if (res.newVelocity().isZero()) break;
queryVec = new Vec(Math.signum(remainingMove.x()), Math.signum(remainingMove.y()), Math.signum(remainingMove.z()));
allFaces = faces.query().get(queryVec);
allFaces = faces.get(new Vec(Math.signum(remainingMove.x()), Math.signum(remainingMove.y()), Math.signum(remainingMove.z())));
res = handlePhysics(entity, res.newVelocity(), res.newPosition(), allFaces, finalResult);
}
@ -134,7 +130,7 @@ final class BlockCollision {
* @return result of physics calculation
*/
private static PhysicsResult handlePhysics(@NotNull Entity entity, @NotNull Vec deltaPosition, Pos entityPosition,
@NotNull List<Vec> allFaces, @NotNull SweepResult finalResult) {
@NotNull Vec[] allFaces, @NotNull SweepResult finalResult) {
final Instance instance = entity.getInstance();
final Chunk originChunk = entity.getChunk();
final BoundingBox boundingBox = entity.getBoundingBox();
@ -190,9 +186,9 @@ final class BlockCollision {
// Pass through (+1, +1, +1)
if (pointBefore.blockX() != pointAfter.blockX()
&& pointBefore.blockY() != pointAfter.blockY()
&& pointBefore.blockZ() != pointAfter.blockZ())
checkBoundingBox(pointAfter.blockX(), pointAfter.blockY(), pointAfter.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult);
&& pointBefore.blockY() != pointAfter.blockY()
&& pointBefore.blockZ() != pointAfter.blockZ())
checkBoundingBox(pointAfter.blockX(), pointAfter.blockY(), pointAfter.blockZ(), deltaPosition, entityPosition, boundingBox, instance, originChunk, finalResult);
}
} else {
// When large moves are done we need to ray-cast to find all blocks that could intersect with the movement

View File

@ -18,7 +18,7 @@ import java.util.stream.Stream;
public final class BoundingBox implements Shape {
private final double width, height, depth;
private final Point offset;
private Faces faces;
private Map<Vec, Vec[]> faces;
BoundingBox(double width, double height, double depth, Point offset) {
this.width = width;
@ -140,8 +140,8 @@ public final class BoundingBox implements Shape {
return depth;
}
@NotNull Faces faces() {
Faces faces = this.faces;
@NotNull Map<Vec, Vec[]> faces() {
Map<Vec, Vec[]> faces = this.faces;
if (faces == null) {
this.faces = faces = retrieveFaces();
}
@ -172,29 +172,23 @@ public final class BoundingBox implements Shape {
return relativeEnd().z();
}
record Faces(Map<Vec, List<Vec>> query) {
public Faces {
query = Map.copyOf(query);
}
private Vec[] buildSet(Collection<Vec> a) {
return a.toArray(Vec[]::new);
}
private List<Vec> buildSet(Set<Vec> a) {
return a.stream().toList();
}
private List<Vec> buildSet(Set<Vec> a, Set<Vec> b) {
private Vec[] buildSet(Collection<Vec> a, Collection<Vec> b) {
Set<Vec> allFaces = new HashSet<>();
Stream.of(a, b).forEach(allFaces::addAll);
return allFaces.stream().toList();
return allFaces.toArray(Vec[]::new);
}
private List<Vec> buildSet(Set<Vec> a, Set<Vec> b, Set<Vec> c) {
private Vec[] buildSet(Collection<Vec> a, Collection<Vec> b, Collection<Vec> c) {
Set<Vec> allFaces = new HashSet<>();
Stream.of(a, b, c).forEach(allFaces::addAll);
return allFaces.stream().toList();
return allFaces.toArray(Vec[]::new);
}
private Faces retrieveFaces() {
private Map<Vec, Vec[]> retrieveFaces() {
double minX = minX();
double maxX = maxX();
double minY = minY();
@ -245,8 +239,8 @@ public final class BoundingBox implements Shape {
// X -1 left | 1 right
// Y -1 bottom | 1 top
// Z -1 front | 1 back
var query = new HashMap<Vec, List<Vec>>();
query.put(new Vec(0, 0, 0), List.of());
var query = new HashMap<Vec, Vec[]>();
query.put(new Vec(0, 0, 0), new Vec[0]);
query.put(new Vec(-1, 0, 0), buildSet(left));
query.put(new Vec(1, 0, 0), buildSet(right));
@ -279,6 +273,6 @@ public final class BoundingBox implements Shape {
query.put(new Vec(-1, -1, 1), buildSet(left, bottom, back));
query.put(new Vec(-1, -1, -1), buildSet(left, bottom, front));
return new Faces(query);
return query;
}
}