Skip to content

Commit

Permalink
Attempt at implementing collision box rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Apr 2, 2024
1 parent 8840e78 commit a0aae36
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.geysermc.hydraulic.util.Constants;
import org.geysermc.hydraulic.util.PackUtil;
import org.geysermc.hydraulic.util.SingletonBlockGetter;
import org.geysermc.hydraulic.util.Vec3f;
import org.geysermc.pack.bedrock.resource.BedrockResourcePack;
import org.geysermc.pack.converter.converter.model.ModelStitcher;
import org.geysermc.pack.converter.data.ModelConversionData;
Expand Down Expand Up @@ -210,8 +211,8 @@ private void onDefineCustomBlocks(PackEventContext<GeyserDefineCustomBlocksEvent
VoxelShape shape = state.getShape(new SingletonBlockGetter(state), BlockPos.ZERO);
VoxelShape collisionShape = state.getCollisionShape(new SingletonBlockGetter(state), BlockPos.ZERO);

componentsBuilder.selectionBox(createBoxComponent(shape));
componentsBuilder.collisionBox(createBoxComponent(collisionShape));
componentsBuilder.selectionBox(createBoxComponent(shape, definition.variant().x(), definition.variant().y()));
componentsBuilder.collisionBox(createBoxComponent(collisionShape, definition.variant().x(), definition.variant().y()));
} else {
componentsBuilder.geometry(GeometryComponent.builder()
.identifier("minecraft:geometry.full_block")
Expand Down Expand Up @@ -519,6 +520,10 @@ private Map<String, String> getFaceMapping(Key parent) {
}

private static BoxComponent createBoxComponent(VoxelShape shape) {
return createBoxComponent(shape, 0, 0);
}

private static BoxComponent createBoxComponent(VoxelShape shape, float rotationX, float rotationY) {
if (shape.isEmpty()) {
return BoxComponent.emptyBox();
}
Expand Down Expand Up @@ -551,13 +556,19 @@ private static BoxComponent createBoxComponent(VoxelShape shape) {
maxY = MathUtils.clamp(maxY, 0, 1);
maxZ = MathUtils.clamp(maxZ, 0, 1);

Vec3f minTemp = new Vec3f(minX, minY, minZ).rotateX(rotationX).rotateY(rotationY);
Vec3f maxTemp = new Vec3f(maxX, maxY, maxZ).rotateX(rotationX).rotateY(rotationY);

Vec3f min = Vec3f.min(minTemp, maxTemp);
Vec3f max = Vec3f.max(minTemp, maxTemp);

return new BoxComponent(
16 * (1 - maxX) - 8, // For some odd reason X is mirrored on Bedrock
16 * minY,
16 * minZ - 8,
16 * (maxX - minX),
16 * (maxY - minY),
16 * (maxZ - minZ)
16 * (1 - max.x()) - 8, // For some odd reason X is mirrored on Bedrock
16 * min.y(),
16 * min.z() - 8,
16 * (max.x() - min.x()),
16 * (max.y() - min.y()),
16 * (max.z() - min.z())
);
}
}
37 changes: 37 additions & 0 deletions shared/src/main/java/org/geysermc/hydraulic/util/Vec3f.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.geysermc.hydraulic.util;

public record Vec3f(float x, float y, float z) {
private static final Vec3f CENTER = new Vec3f(0.5f, 0.5f, 0.5f);

public Vec3f rotateX(float angle) {
if (angle == 0) return this;

double radians = Math.toRadians(angle);
double cos = Math.cos(radians);
double sin = Math.sin(radians);
float newY = (float) ((y - CENTER.y) * cos - (z - CENTER.z) * sin + CENTER.y);
float newZ = (float) ((y - CENTER.y) * sin + (z - CENTER.z) * cos + CENTER.z);

return new Vec3f(x(), newY, newZ);
}

public Vec3f rotateY(float angle) {
if (angle == 0) return this;

double radians = Math.toRadians(angle);
double cos = Math.cos(radians);
double sin = Math.sin(radians);
float newX = (float) ((x - CENTER.x) * cos + (z - CENTER.z) * sin + CENTER.x);
float newZ = (float) (-(x - CENTER.x) * sin + (z - CENTER.z) * cos + CENTER.z);

return new Vec3f(newX, y(), newZ);
}

public static Vec3f min(Vec3f min, Vec3f max) {
return new Vec3f(Math.min(min.x(), max.x()), Math.min(min.y(), max.y()), Math.min(min.z(), max.z()));
}

public static Vec3f max(Vec3f min, Vec3f max) {
return new Vec3f(Math.max(min.x(), max.x()), Math.max(min.y(), max.y()), Math.max(min.z(), max.z()));
}
}

0 comments on commit a0aae36

Please sign in to comment.