Skip to content

Instantly share code, notes, and snippets.

@Shazuli
Created February 22, 2020 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shazuli/c8a5d564f2abfa4dbd8e223a67a58ccb to your computer and use it in GitHub Desktop.
Save Shazuli/c8a5d564f2abfa4dbd8e223a67a58ccb to your computer and use it in GitHub Desktop.
Cuboid rotation problem.
private static final byte[] SIDE = {0,2,2,2,14,14}, SIDE_MOD = {0,0,2,2,16,16}; // The side shapes for connected and module hitbox.
public static final VoxelShape[] CONNECTION_SIDES = getSides(SIDE[0],SIDE[1],SIDE[2],SIDE[3],SIDE[4],SIDE[5]); // Get the sides.
public static final VoxelShape MIDDLE = Block.makeCuboidShape(2,2,2,14,14,14);
private static VoxelShape[] getSides(short x1, short y1, short z1, short x2, short y2, short z2)
{
VoxelShape[] t = new VoxelShape[6];
t[0] = Block.makeCuboidShape(x1,y1,z1,x2,y2,z2);
short[] k1 = {(short)(x1+8),(short)(y1+8),(short)(z1+8)}; // Transform it to the middle (16px/2 = 8).
short[] k2 = {(short)(x2+8),(short)(y2+8),(short)(z2+8)};
float[][] mY90 = Util.generate3DMatrix(90,'y'); // Generate 90 degrees matrix.
for (short i=1;i<6;i++) { // Loop through the sides.
if (i < 3) {
k1 = Util.rotateY(k1[0], k1[1], k1[2], mY90);
k2 = Util.rotateY(k2[0], k2[1], k2[2], mY90);
} else {
k1 = Util.rotate3D((short)(x1+8), (short)(y1+8), (short)(z1+8), Util.generate3DMatrix(90, 'x'));
k2 = Util.rotate3D((short)(x2+8), (short)(y2+8), (short)(z2+8), Util.generate3DMatrix(270, 'x'));
}
t[i] = Block.makeCuboidShape(k1[0]-8,k1[1]-8,k1[2]-8,k2[0]-8,k2[1]-8,k2[2]-8); // Make cuboid shape with correct space transform.
}
/*t[4] = Block.makeCuboidShape(2,14,2,14,16,14);
t[5] = Block.makeCuboidShape(2,0,2,14,2,14);*/
return t;
}
public static EnumProperty<CableCaseFaces> getFace(Direction direction)
{
switch (direction) {
case SOUTH:
return BlockProperties.CONNECTED_SOUTH;
case EAST:
return BlockProperties.CONNECTED_EAST;
case WEST:
return BlockProperties.CONNECTED_WEST;
case UP:
return BlockProperties.CONNECTED_UP;
case DOWN:
return BlockProperties.CONNECTED_DOWN;
default:
return BlockProperties.CONNECTED_NORTH;
}
}
public static enum Connections
{
NORTH((byte) 1),
SOUTH((byte) 3),
EAST((byte) 2),
WEST((byte) 0),
UP((byte) 4),
DOWN((byte) 5);
private final byte value;
Connections(byte value)
{
this.value = value;
}
public byte getValue()
{
return value;
}
public VoxelShape getShape()
{
return CONNECTION_SIDES[value];
}
}
/**
* Generate 3D rotation Matrix
*
* @param d Degrees to generate matrix from.
* @param axis What axis to generate matrix from (x,y,z).
* @return Double-Array matrix.
*/
public static float[][] generate3DMatrix(final float d, final char axis)
{
final double r = Math.toRadians(d);
final float c = (float) Math.cos(r), s = (float) Math.sin(r);
switch (axis) {
case 'x':
return new float[][] {
{1, 0, 0 },
{0, c, -s},
{0, s, c }
};
case 'z':
return new float[][] {
{c, -s, 0},
{s, c, 0},
{0, 0, 1}
};
default: // 'y'
return new float[][] {
{c, 0, s},
{0, 1, 0},
{-s, 0, c}
};
}
}
/**
* Rotate 3D coordinates from Matrix
*
* @param matrix Rotation matrix to be used.
* @return New transformed coordinates.
*/
public static float[] rotate3D(float x, float y, float z, float[][] matrix)
{
/*float t = x;
x = z;
z = t;*/
//return new float[] {matrix[0][0]*x+matrix[1][0]*y+matrix[2][0]*z,matrix[1][1]*x+matrix[1][1]*y+matrix[2][1]*z,matrix[0][2]*x+matrix[1][2]*y+matrix[2][2]*z};
// Nexustix's code example:
return new float[] {
(x * matrix[0][0]) + (y * matrix[1][0]) + (z * matrix[2][0]),
(x * matrix[0][1]) + (y * matrix[1][1]) + (z * matrix[2][1]),
(x * matrix[0][2]) + (y * matrix[1][2]) + (z * matrix[2][2])
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment