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

com.parzivail.util.entity.collision.SweptTriangleVolume Maven / Gradle / Ivy

package com.parzivail.util.entity.collision;

import D;
import com.parzivail.util.math.CollisionUtil;
import com.parzivail.util.math.CollisionUtil.ClosestPoints;
import com.parzivail.util.math.MathUtil;
import com.parzivail.util.math.QuatUtil;
import net.minecraft.class_243;
import org.apache.commons.lang3.mutable.MutableObject;
import org.joml.Matrix4f;
import org.joml.Quaternionf;

public record SweptTriangleVolume(class_243 a, class_243 b, class_243 c, double radius) implements ICollisionVolume
{
	@Override
	public ICollisionVolume transform(Quaternionf q)
	{
		return new SweptTriangleVolume(QuatUtil.rotate(a, q), QuatUtil.rotate(b, q), QuatUtil.rotate(c, q), radius);
	}

	@Override
	public ICollisionVolume transform(Matrix4f m)
	{
		return new SweptTriangleVolume(MathUtil.transform(a, m), MathUtil.transform(b, m), MathUtil.transform(c, m), radius);
	}

	@Override
	public boolean resolveCapsuleCollision(CapsuleVolume sourceHitbox, MutableObject movementContainer)
	{
		var movement = movementContainer.getValue();
		var result = CollisionUtil.closestPointsTriangleSegment(sourceHitbox.start().method_1019(movement), sourceHitbox.end().method_1019(movement), a, b, c);

		var intersectionRay = result.b().method_1020(result.a());

		var minDistance = sourceHitbox.radius() + radius;

		// Check if the volume is intersecting
		if (result.squareDistance() > minDistance * minDistance)
			return false;

		var intersectionLength = intersectionRay.method_1033();
		var overlap = intersectionLength - minDistance;

		var impulse = intersectionRay.method_1021(overlap / intersectionLength);

		var slideMovement = movement.method_1019(impulse);

		movementContainer.setValue(slideMovement);

		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy