43 lines
1.7 KiB
Java
43 lines
1.7 KiB
Java
package net.pivipi.physics;
|
|
|
|
import net.minestom.server.collision.BoundingBox;
|
|
import net.minestom.server.collision.SweepResult;
|
|
import net.minestom.server.collision.VisibleSweepResult;
|
|
import net.minestom.server.coordinate.Vec;
|
|
import net.minestom.server.entity.Entity;
|
|
import net.minestom.server.entity.Player;
|
|
|
|
public class Collision {
|
|
/**
|
|
*
|
|
* @param entity
|
|
* @param moving the moving entity
|
|
* @param velocity the applied velocity
|
|
* @return null if no collision
|
|
*/
|
|
public static VisibleSweepResult willCollideWithEntity(Entity entity, Entity moving, Vec movement) {
|
|
SweepResult sweepResult = new SweepResult(1, 0, 0, 0, null, 0, 0, 0);
|
|
boolean collided = entity.getBoundingBox().intersectBoxSwept(moving.getPosition(), movement, entity.getPosition(), moving.getBoundingBox(), sweepResult);
|
|
return collided ? new VisibleSweepResult(sweepResult) : null;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param groundY ground Y
|
|
* @param moving the moving entity
|
|
* @return null if no collision
|
|
*/
|
|
public static VisibleSweepResult willCollideWithGround(double groundY, Entity moving, Vec movement) {
|
|
SweepResult sweepResult = new SweepResult(1, 0, 0, 0, null, 0, 0, 0);
|
|
boolean collided = new BoundingBox(10, 1, 10).intersectBoxSwept(moving.getPosition(), movement, moving.getPosition().withY(groundY - 1).sub(5, 0, 5), moving.getBoundingBox(), sweepResult);
|
|
return collided ? new VisibleSweepResult(sweepResult) : null;
|
|
}
|
|
|
|
public static boolean collidesWithEntity(Entity entity1, Entity entity2) {
|
|
return entity1.getBoundingBox().intersectEntity(entity1.getPosition(), entity2);
|
|
}
|
|
|
|
public static boolean collidesWithGround(double groundY, Entity entity) {
|
|
return new BoundingBox(10, 1, 10).intersectEntity(entity.getPosition().withY(groundY - 1).sub(5, 0, 5), entity);
|
|
}
|
|
}
|