-
Notifications
You must be signed in to change notification settings - Fork 0
Math Utilities
Moth edited this page Aug 9, 2026
·
1 revision
Butterfly API includes several small math helpers for common modding calculations.
The main utility classes are:
AnglesVecsBoxesMotionSamplingScalarsYawPitch
Use Angles for common yaw, pitch, and look-direction calculations.
double yawDelta =
Angles.deltaDegrees(
entity.getYaw(),
targetYaw
);Vec3d direction =
Angles.lookDirection(entity);YawPitch look =
Angles.lookAt(
entity.getEyePos(),
target.getEyePos()
);lookAt(...) returns a YawPitch value describing the rotation needed to look from one position toward another.
Use Vecs for reusable vector operations.
Vec3d from =
new Vec3d(
entity.getX(),
entity.getY(),
entity.getZ()
);
Vec3d to =
target.getEyePos();
Vec3d towardTarget =
Vecs.direction(
from,
to,
Vec3d.ZERO
);Vec3d capped =
Vecs.limitLength(
entity.getVelocity(),
0.5D
);Vec3d reflected =
Vecs.reflect(
incoming,
surfaceNormal
);Use Boxes for common Box calculations.
Box searchArea =
Boxes.around(
entity.getEyePos(),
8.0D
);Box beamArea =
Boxes.between(
start,
end,
0.25D
);Vec3d center =
Boxes.center(searchArea);Use Motion for common entity and projectile movement operations.
Motion.pushFrom(
target,
source,
0.8D,
0.2D
);Motion.pullToward(
projectile,
target,
0.4D,
0.0D
);Motion.setVelocityFromLook(
projectile,
1.5D
);Motion.addVelocityToward(
projectile,
target,
0.25D
);Sampling can generate collections of positions for effects such as particles.
Example circle:
for (Vec3d point : Sampling.circle(
center,
new Vec3d(0.0D, 1.0D, 0.0D),
2.0D,
16
)) {
world.addParticle(
particle,
point.x,
point.y,
point.z,
0.0D,
0.0D,
0.0D
);
}Use Scalars for common number operations.
double progress =
Scalars.clamp01(
Scalars.inverseLerp(
value,
min,
max
)
);double mapped =
Scalars.mapClamped(
distance,
0.0D,
32.0D,
1.0D,
0.0D
);double eased =
Scalars.approach(
current,
target,
step
);