All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.parzivail.util.math.MathUtil Maven / Gradle / Ivy

package com.parzivail.util.math;

import D;
import F;
import net.minecraft.class_1297;
import net.minecraft.class_2350;
import net.minecraft.class_2379;
import net.minecraft.class_2382;
import net.minecraft.class_241;
import net.minecraft.class_243;
import net.minecraft.class_3532;
import net.minecraft.class_4587;
import net.minecraft.class_4587.class_4665;
import net.minecraft.util.math.*;
import org.jetbrains.annotations.NotNull;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3d;
import org.joml.Vector3f;

public class MathUtil
{
	public static final int TICKS_PER_SECOND = 20;

	public static final float fPI = (float)Math.PI;

	public static final double ONE_OVER_GOLDEN_RATIO = 0.61803398875;

	public static final float SPEED_OF_SOUND = 275f / TICKS_PER_SECOND; // m/tick

	public static final class_243 V3D_POS_X = new class_243(1, 0, 0);
	public static final class_243 V3D_NEG_X = new class_243(-1, 0, 0);
	public static final class_243 V3D_POS_Y = new class_243(0, 1, 0);
	public static final class_243 V3D_NEG_Y = new class_243(0, -1, 0);
	public static final class_243 V3D_POS_Z = new class_243(0, 0, 1);
	public static final class_243 V3D_NEG_Z = new class_243(0, 0, -1);

	public static final Vector3f V3F_POS_X = new Vector3f(1, 0, 0);
	public static final Vector3f V3F_NEG_X = new Vector3f(-1, 0, 0);
	public static final Vector3f V3F_POS_Y = new Vector3f(0, 1, 0);
	public static final Vector3f V3F_NEG_Y = new Vector3f(0, -1, 0);
	public static final Vector3f V3F_POS_Z = new Vector3f(0, 0, 1);
	public static final Vector3f V3F_NEG_Z = new Vector3f(0, 0, -1);

	public static final Matrix4f MAT4_IDENTITY = new Matrix4f();
	public static final Matrix4f MAT4_SCALE_10_16THS = new Matrix4f().scale(10 / 16f, 10 / 16f, 10 / 16f);

	public static float fract(double d)
	{
		return (float)(d - Math.floor(d));
	}

	public static class_241 fract(class_241 v)
	{
		return new class_241(fract(v.field_1343), fract(v.field_1342));
	}

	public static class_243 fract(class_243 v)
	{
		return new class_243(fract(v.field_1352), fract(v.field_1351), fract(v.field_1350));
	}

	public static double seed(double d, long seed)
	{
		return Double.longBitsToDouble(Double.doubleToLongBits(d) ^ seed);
	}

	public static class_241 floor(class_241 v)
	{
		return new class_241((float)Math.floor(v.field_1343), (float)Math.floor(v.field_1342));
	}

	public static class_243 floor(class_243 v)
	{
		return new class_243(Math.floor(v.field_1352), Math.floor(v.field_1351), Math.floor(v.field_1350));
	}

	public static class_2382 floorInt(class_243 v) {
		return new class_2382(class_3532.method_15357(v.field_1352), class_3532.method_15357(v.field_1351), class_3532.method_15357(v.field_1350));
	}

	public static class_243 lerp(float tickDelta, class_243 a, class_243 b)
	{
		return new class_243(class_3532.method_16436(tickDelta, a.field_1352, b.field_1352), class_3532.method_16436(tickDelta, a.field_1351, b.field_1351), class_3532.method_16436(tickDelta, a.field_1350, b.field_1350));
	}

	public static class_241 add(class_241 a, class_241 b)
	{
		return new class_241(a.field_1343 + b.field_1343, a.field_1342 + b.field_1342);
	}

	public static class_241 sub(class_241 a, class_241 b)
	{
		return new class_241(a.field_1343 - b.field_1343, a.field_1342 - b.field_1342);
	}

	public static double length(class_241 v)
	{
		return Math.sqrt(v.field_1343 * v.field_1343 + v.field_1342 * v.field_1342);
	}

	public static int clamp(int i, int min, int max)
	{
		return Math.max(min, Math.min(i, max));
	}

	public static float remap(float x, float iMin, float iMax, float oMin, float oMax)
	{
		return (x - iMin) / (iMax - iMin) * (oMax - oMin) + oMin;
	}

	public static float calculateDopplerShift(class_1297 a, class_1297 b)
	{
		// TODO: move doppler handling to OpenAL through SoundSystem's updateListenerPosition call?
		var velA = a.method_19538().method_1023(a.field_6014, a.field_6036, a.field_5969);
		var velB = b.method_19538().method_1023(b.field_6014, b.field_6036, b.field_5969);

		var posA = a.method_33571();
		var posB = b.method_33571();

		var relativeSpeed = posA.method_1022(posB) - posA.method_1019(velA).method_1022(posB.method_1019(velB));

		return class_3532.method_15363((float)(relativeSpeed / SPEED_OF_SOUND), -1, 1);
	}

	public static class_243 project(class_243 v, class_243 onto)
	{
		return onto.method_1021(v.method_1026(onto) / onto.method_1026(onto));
	}

	@NotNull
	public static class_243 anglesToLook(float pitch, float yaw)
	{
		var x = -class_3532.method_15374(yaw * class_3532.field_29847) * class_3532.method_15362(pitch * class_3532.field_29847);
		var y = -class_3532.method_15374(pitch * class_3532.field_29847);
		var z = class_3532.method_15362(yaw * class_3532.field_29847) * class_3532.method_15362(pitch * class_3532.field_29847);

		return new class_243(x, y, z).method_1029();
	}

	@NotNull
	public static class_2379 lookToAngles(class_243 forward)
	{
		forward = forward.method_1029();

		var yaw = -(float)Math.atan2(forward.field_1352, forward.field_1350);
		var pitch = -(float)Math.asin(forward.field_1351);

		return new class_2379(pitch * class_3532.field_29848, yaw * class_3532.field_29848, 0);
	}

	public static boolean rectContains(int l, int t, int w, int h, double x, double y)
	{
		return x >= l && x < l + w && y >= t && y < t + h;
	}

	public static float toRadians(float degrees)
	{
		return degrees * class_3532.field_29847;
	}

	public static class_243 transform(class_243 v, Matrix4f transform)
	{
		var vec3d = new Vector3d(v.field_1352, v.field_1351, v.field_1350).mulPosition(transform);
		return new class_243(vec3d.x, vec3d.y, vec3d.z);
	}

	public static void scalePos(class_4587 stack, float x, float y, float z)
	{
		var entry = stack.method_23760();
		entry.method_23761().scale(x, y, z);
	}

	public static Quaternionf getRotation(class_2350 direction)
	{
		return switch (direction)
		{
			case field_11033 -> new Quaternionf().rotationXYZ(0, 0, (float)(Math.PI / -2));
			case field_11036 -> new Quaternionf().rotationXYZ(0, 0, (float)(Math.PI / 2));
			case field_11043 -> new Quaternionf().rotationXYZ(0, (float)(Math.PI / 2), 0);
			case field_11035 -> new Quaternionf().rotationXYZ(0, (float)(Math.PI / -2), 0);
			case field_11039 -> new Quaternionf().rotationXYZ(0, (float)Math.PI, 0);
			case field_11034 -> new Quaternionf().rotationXYZ(0, 0, 0);
		};
	}

	public static class_243 reflect(class_243 incident, class_243 normal)
	{
		var reflection = normal.method_1021(2 * normal.method_1026(incident)).method_1020(incident);
		return reflection.method_1021(-1);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy