mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-10 13:49:04 +01:00
Improve collision
This commit is contained in:
parent
4f20580482
commit
d8b92d23ba
@ -136,9 +136,8 @@ public class CollisionUtils {
|
||||
* @param newPosition the future target position
|
||||
* @return the position with the world border collision applied (can be {@code newPosition} if not changed)
|
||||
*/
|
||||
@NotNull
|
||||
public static Point applyWorldBorder(@NotNull Instance instance,
|
||||
@NotNull Point currentPosition, @NotNull Point newPosition) {
|
||||
public static @NotNull Pos applyWorldBorder(@NotNull Instance instance,
|
||||
@NotNull Pos currentPosition, @NotNull Pos newPosition) {
|
||||
final WorldBorder worldBorder = instance.getWorldBorder();
|
||||
final WorldBorder.CollisionAxis collisionAxis = worldBorder.getCollisionAxis(newPosition);
|
||||
switch (collisionAxis) {
|
||||
@ -147,13 +146,13 @@ public class CollisionUtils {
|
||||
return newPosition;
|
||||
case BOTH:
|
||||
// Apply Y velocity/gravity
|
||||
return new Vec(currentPosition.x(), newPosition.y(), currentPosition.z());
|
||||
return new Pos(currentPosition.x(), newPosition.y(), currentPosition.z());
|
||||
case X:
|
||||
// Apply Y/Z velocity/gravity
|
||||
return new Vec(currentPosition.x(), newPosition.y(), newPosition.z());
|
||||
return new Pos(currentPosition.x(), newPosition.y(), newPosition.z());
|
||||
case Z:
|
||||
// Apply X/Y velocity/gravity
|
||||
return new Vec(newPosition.x(), newPosition.y(), currentPosition.z());
|
||||
return new Pos(newPosition.x(), newPosition.y(), currentPosition.z());
|
||||
}
|
||||
throw new IllegalStateException("Something weird happened...");
|
||||
}
|
||||
|
@ -507,7 +507,7 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
|
||||
);
|
||||
|
||||
if (this.hasPhysics) {
|
||||
final CollisionUtils.PhysicsResult physicsResult = CollisionUtils.handlePhysics(this, deltaPos);
|
||||
final var physicsResult = CollisionUtils.handlePhysics(this, deltaPos);
|
||||
this.onGround = physicsResult.isOnGround();
|
||||
newPosition = physicsResult.newPosition();
|
||||
newVelocity = physicsResult.newVelocity();
|
||||
@ -521,7 +521,7 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
|
||||
}
|
||||
|
||||
// World border collision
|
||||
final Point finalVelocityPosition = CollisionUtils.applyWorldBorder(instance, position, newPosition);
|
||||
final var finalVelocityPosition = CollisionUtils.applyWorldBorder(instance, position, newPosition);
|
||||
final Chunk finalChunk = ChunkUtils.retrieve(instance, currentChunk, finalVelocityPosition);
|
||||
if (!ChunkUtils.isLoaded(finalChunk)) {
|
||||
// Entity shouldn't be updated when moving in an unloaded chunk
|
||||
@ -530,7 +530,7 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
|
||||
|
||||
// Apply the position if changed
|
||||
if (!finalVelocityPosition.samePoint(position)) {
|
||||
refreshPosition((Pos) finalVelocityPosition, true);
|
||||
refreshPosition(finalVelocityPosition, true);
|
||||
}
|
||||
|
||||
// Update velocity
|
||||
|
@ -1,11 +1,11 @@
|
||||
package net.minestom.server.instance;
|
||||
|
||||
import net.minestom.server.coordinate.Point;
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.network.packet.server.ServerPacket;
|
||||
import net.minestom.server.network.packet.server.play.*;
|
||||
import net.minestom.server.utils.PacketUtils;
|
||||
import net.minestom.server.coordinate.Point;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
@ -166,13 +166,15 @@ public class WorldBorder {
|
||||
final double radius = getDiameter() / 2d;
|
||||
final boolean checkX = point.x() <= getCenterX() + radius && point.x() >= getCenterX() - radius;
|
||||
final boolean checkZ = point.z() <= getCenterZ() + radius && point.z() >= getCenterZ() - radius;
|
||||
if (!checkX || !checkZ) {
|
||||
if (!checkX && !checkZ) {
|
||||
return CollisionAxis.BOTH;
|
||||
} else if (!checkX) {
|
||||
return CollisionAxis.X;
|
||||
} else if (!checkZ) {
|
||||
} else {
|
||||
return CollisionAxis.Z;
|
||||
}
|
||||
}
|
||||
return CollisionAxis.NONE;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user