Last active
June 26, 2022 18:29
-
-
Save Pokechu22/bf1d636e81584f0c01f4dc396e487d86 to your computer and use it in GitHub Desktop.
Minecraft entity collision logic (simplified)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Entity { | |
public void applyEntityCollision(Entity other) { | |
double dx = other.posX - this.posX; | |
double dz = other.posZ - this.posZ; | |
double largestDistance = Math.max(Math.abs(dx), Math.abs(dz)); | |
if (largestDistance >= 0.01) { | |
double vx = dx / 20; | |
double vz = dz / 20; | |
if (largestDistance < 1) { | |
vx = vx / Math.sqrt(largestDistance); | |
vz = vz / Math.sqrt(largestDistance); | |
} else { | |
vx = vx / largestDistance; | |
vz = vz / largestDistance; | |
} | |
this.xVelocity = this.xVelocity - vx; | |
this.zVelocity = this.zVelocity - vz; | |
other.xVelocity = other.xVelocity + vx; | |
other.zVelocity = other.zVelocity + vz; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Entities can also have custom hitbox sizes: https://docs.microsoft.com/en-us/minecraft/creator/reference/content/entityreference/examples/entityproperties/minecraftproperty_collision_box