Get some bounding spheres at home

- Compute bounding spheres with the AABB method. Should be pretty well
  suited for funny block game.
- Remove Miniball from dependencies.
This commit is contained in:
Jozufozu 2024-01-26 16:40:15 -08:00
parent 80139cf673
commit db942965a5
2 changed files with 40 additions and 44 deletions

View file

@ -74,21 +74,6 @@ repositories {
mavenCentral()
}
// Fix for loading non-mod libraries in dev-env, used for Miniball.
// https://gist.github.com/SizableShrimp/66b22f1b24c255e1491c8d98d3f11f83
// v--------------------------------------------------------------------v
configurations {
library
implementation.extendsFrom library
}
minecraft.runs.configureEach {
lazyToken('minecraft_classpath') {
configurations.library.copyRecursive().resolve().collect { it.absolutePath }.join(File.pathSeparator)
}
}
// ^--------------------------------------------------------------------^
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"
@ -96,9 +81,6 @@ dependencies {
compileOnly fg.deobf("maven.modrinth:embeddium:0.2.10+mc1.20.1")
compileOnly fg.deobf("maven.modrinth:oculus:1.20.1-1.6.9")
jarJar(group: 'com.dreizak', name: 'miniball', version: '[1.0.3,2.0.0)')
library 'com.dreizak:miniball:1.0.3'
// https://discord.com/channels/313125603924639766/725850371834118214/910619168821354497
// Prevent Mixin annotation processor from getting into IntelliJ's annotation processor settings
// This allows 'Settings > Build, Execution, and Deployment > Build Tools > Gradle > Build and run using' set to IntelliJ to work correctly

View file

@ -4,12 +4,11 @@ import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector3f;
import org.joml.Vector4f;
import org.lwjgl.system.MemoryUtil;
import org.slf4j.Logger;
import com.dreizak.miniball.highdim.Miniball;
import com.dreizak.miniball.model.PointSet;
import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.api.model.Mesh;
import com.jozufozu.flywheel.api.vertex.VertexList;
@ -37,6 +36,7 @@ public final class ModelUtil {
* Meant to be used for virtual rendering.
*/
public static final BlockRenderDispatcher VANILLA_RENDERER = createVanillaRenderer();
private static final float BOUNDING_SPHERE_EPSILON = 1e-4f;
private ModelUtil() {
}
@ -128,33 +128,47 @@ public final class ModelUtil {
}
public static Vector4f computeBoundingSphere(VertexList vertexList) {
return computeBoundingSphere(new PointSet() {
@Override
public int size() {
return vertexList.vertexCount();
var center = computeCenterOfAABBContaining(vertexList);
var radius = computeMaxDistanceTo(vertexList, center) + BOUNDING_SPHERE_EPSILON;
return new Vector4f(center, radius);
}
@Override
public int dimension() {
return 3;
private static float computeMaxDistanceTo(VertexList vertexList, Vector3f pos) {
float farthestDistanceSquared = -1;
for (int i = 0; i < vertexList.vertexCount(); i++) {
var distanceSquared = pos.distanceSquared(vertexList.x(i), vertexList.y(i), vertexList.z(i));
if (distanceSquared > farthestDistanceSquared) {
farthestDistanceSquared = distanceSquared;
}
}
@Override
public double coord(int i, int dim) {
return switch (dim) {
case 0 -> vertexList.x(i);
case 1 -> vertexList.y(i);
case 2 -> vertexList.z(i);
default -> throw new IllegalArgumentException("Invalid dimension: " + dim);
};
}
});
return (float) Math.sqrt(farthestDistanceSquared);
}
public static Vector4f computeBoundingSphere(PointSet points) {
var miniball = new Miniball(points);
double[] center = miniball.center();
double radius = miniball.radius();
return new Vector4f((float) center[0], (float) center[1], (float) center[2], (float) radius);
private static Vector3f computeCenterOfAABBContaining(VertexList vertexList) {
var min = new Vector3f(Float.MAX_VALUE);
var max = new Vector3f(Float.MIN_VALUE);
for (int i = 0; i < vertexList.vertexCount(); i++) {
float x = vertexList.x(i);
float y = vertexList.y(i);
float z = vertexList.z(i);
// JOML's min/max methods don't accept floats :whywheel:
min.x = Math.min(min.x, x);
min.y = Math.min(min.y, y);
min.z = Math.min(min.z, z);
max.x = Math.max(max.x, x);
max.y = Math.max(max.y, y);
max.z = Math.max(max.z, z);
}
return min.add(max)
.mul(0.5f);
}
}