Skip to content

Commit

Permalink
refactor: AllayEntityPhysicsService
Browse files Browse the repository at this point in the history
  • Loading branch information
IWareQ committed Jun 27, 2024
1 parent 8786aeb commit 818bb1f
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,28 @@ protected void computeBlockCollisionMotion(Entity entity, BlockState[][][] colli
protected void computeEntityCollisionMotion(Entity entity) {
var collidedEntities = getCachedEntityCollidingResult(entity);
collidedEntities.removeIf(e -> !e.computeEntityCollisionMotion());

var collisionMotion = new Vector3f(0, 0, 0);
var loc = entity.getLocation();
float r = entity.getPushSpeedReduction();

var location = entity.getLocation();
var pushSpeedReduction = entity.getPushSpeedReduction();
for (var other : collidedEntities) {
// https://github.com/lovexyn0827/Discovering-Minecraft/blob/master/Minecraft%E5%AE%9E%E4%BD%93%E8%BF%90%E5%8A%A8%E7%A0%94%E7%A9%B6%E4%B8%8E%E5%BA%94%E7%94%A8/5-Chapter-5.md
var ol = other.getLocation();
var direction = new Vector3f(entity.getLocation()).sub(other.getLocation(), new Vector3f()).normalize();
float distance = max(abs(ol.x() - loc.x()), abs(ol.z() - loc.z()));
float k = 0.05f * r;
var distance = max(abs(ol.x() - location.x()), abs(ol.z() - location.z()));
if (distance <= 0.01) continue;

var k = 0.05f * pushSpeedReduction;
if (distance <= 1) {
k *= MathUtils.fastFloatInverseSqrt(distance);
} else {
k /= distance;
}

collisionMotion.add(direction.mul(k));
}

collisionMotion.setComponent(1, 0);
entity.addMotion(collisionMotion);
}
Expand All @@ -209,7 +214,11 @@ protected void computeEntityCollisionMotion(Entity entity) {
*/
protected void updateMotion(Entity entity) {
var motion = entity.getMotion();
var blockUnder = dimension.getBlockState((int) entity.getLocation().x(), (int) (entity.getLocation().y() - 0.5), (int) entity.getLocation().z());
var blockUnder = dimension.getBlockState(
(int) entity.getLocation().x(),
(int) (entity.getLocation().y() - 0.5),
(int) entity.getLocation().z()
);

// 1. Multiplier factors
var movementFactor = entity.getMovementFactor();
Expand All @@ -220,7 +229,7 @@ protected void updateMotion(Entity entity) {
var effectFactor = (1f + 0.2f * speedLevel) * (1f - 0.15f * slownessLevel);

var slipperinessMultiplier = blockUnder != null ?
blockUnder.getBlockType().getBlockBehavior().getBlockAttributes(blockUnder).friction() :
blockUnder.getBlockAttributes().friction() :
DEFAULT_FRICTION;

var momentumMx = motion.x() * slipperinessMultiplier * MOMENTUM_FACTOR;
Expand Down

0 comments on commit 818bb1f

Please sign in to comment.