diff --git a/packages/core/src/commands/operator/summon.ts b/packages/core/src/commands/operator/summon.ts index 4816bf6f..2cfc16e1 100644 --- a/packages/core/src/commands/operator/summon.ts +++ b/packages/core/src/commands/operator/summon.ts @@ -156,18 +156,25 @@ export default register; /** * Calculates the rotation to face a target */ -function calculateRotationToFace(from: Vector3f, to: Vector3f): Rotation { - const dx = to.x - from.x; - const dy = to.y - from.y; - const dz = to.z - from.z; - - // Calculate the horizontal distance - const horizontalDist = Math.sqrt(dx * dx + dz * dz); - - // Calculate the yaw and pitch - const yaw = (Math.atan2(-dx, dz) * (180 / Math.PI)) % 360; - const pitch = Math.atan2(-dy, horizontalDist) * (180 / Math.PI); - - // Return the rotation - return new Rotation(yaw, pitch, yaw); -} \ No newline at end of file +const calculateRotationToFace = (() => { + // stop RAD_TO_DEG recalling it self every time it called. + const RAD_TO_DEG = 180 / Math.PI; + + return function (from: Vector3f, to: Vector3f): Rotation { + const dx = to.x - from.x; + const dy = to.y - from.y; + const dz = to.z - from.z; + // handle better. + const horizontal = Math.hypot(dx, dz); + // old code kinda goofy 270 and -90 give diffrent anwsers for same question in simple. + //old code did this 270 % 360 = 270; even tho mathmaticly i should just be -90. + let yaw = Math.atan2(-dx, dz) * RAD_TO_DEG; + if (yaw > 180) yaw -= 360; + if (yaw < -180) yaw += 360; + + let pitch = Math.atan2(-dy, horizontal) * RAD_TO_DEG; + pitch = Math.max(-90, Math.min(90, pitch)); + + return new Rotation(yaw, pitch, yaw); + }; +})();