Skip to content

Instantly share code, notes, and snippets.

@Pokechu22
Last active June 26, 2022 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pokechu22/bf1d636e81584f0c01f4dc396e487d86 to your computer and use it in GitHub Desktop.
Save Pokechu22/bf1d636e81584f0c01f4dc396e487d86 to your computer and use it in GitHub Desktop.
Minecraft entity collision logic (simplified)
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