Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This function generates a 3x3 rotation matrix from a 4x4
- // I'm trying to literally use the same rotation matrix as the one I'm using in my render yet the box and entity are out of sync.
- private def rotationMatrix: Matrix3f = {
- val m = new Matrix3f()
- val c = correctionMatrix.clone().asInstanceOf[Matrix4f] // Creates a copy of correctionMatrix because
- //the javax Matrix modifies itself
- c.mul(renderMatrix) // Multiply's correctionMatrix by renderMatrix (see below)
- c.getRotationScale(m) // Replaces all values in m with the 3x3 rotation/scale matrix within c (which is now multiplied)
- m // returns m from our function
- }
- lazy val correctionMatrix: Matrix4f = {
- // Get the ship block (0,0,0)
- def blockState = ShipWorld.getBlockState(new BlockPos(0, 0, 0))
- // Figure out which way it's facing
- val axis = blockState.getValue(ShipCreatorBlock.FACING).getDirectionVec
- def rotationOffset(direction: Vec3i): Float = {
- if (direction.getX == 1) 0f
- else if (direction.getX == -1) 180f
- else if (direction.getZ == 1) 90f
- else if (direction.getZ == -1) 270f
- else 0f
- }
- // # Correction Matrix #
- // Basically rotates the matrix so that our rotation is relative to the Ship Block's front face.
- // We also never have to rotate our axis upwards or downwards so we only need to rotate the yaw (pitch) value.
- val rotCorrectionMatrix = new Matrix4f()
- rotCorrectionMatrix.rotY(Math.toRadians(rotationOffset(axis)).toFloat)
- rotCorrectionMatrix
- }
- // Ignore the lazy
- // This function just basically makes every rotation relative to which way the ship block is facing and not the world.
- lazy val correctionMatrix: Matrix4f = {
- // Get the ship block (0,0,0)
- def blockState = ShipWorld.getBlockState(new BlockPos(0, 0, 0))
- // Figure out which way it's facing
- val axis = blockState.getValue(ShipCreatorBlock.FACING).getDirectionVec
- // Depending on the vector returned by axis, get a rotation amount that would rotate the axis to the way
- def rotationOffset(direction: Vec3i): Float = {
- if (direction.getX == 1) 0f
- else if (direction.getX == -1) 180f
- else if (direction.getZ == 1) 90f
- else if (direction.getZ == -1) 270f
- else 0f
- }
- // # Correction Matrix #
- // Basically rotates the matrix so that our rotation is relative to the Ship Block's front face.
- // We also never have to rotate our axis upwards or downwards so we only need to rotate the yaw (pitch) value.
- val rotCorrectionMatrix = new Matrix4f()
- rotCorrectionMatrix.rotY(Math.toRadians(rotationOffset(axis)).toFloat)
- rotCorrectionMatrix
- }
- def renderMatrix: Matrix4f = {
- val xMat: Matrix4f = new Matrix4f()
- xMat.rotX(Math.toRadians(rotationRoll).toFloat)
- // Switch Y and Z cause in Minecraft the Y axis is "up"
- val yMat: Matrix4f = new Matrix4f()
- yMat.rotZ(Math.toRadians(rotationPitch).toFloat)
- // See above ^
- val zMat: Matrix4f = new Matrix4f()
- zMat.rotY(Math.toRadians(rotationYaw).toFloat)
- // Multiply our rotation matrices into one unified rotation matrix
- xMat.mul(zMat)
- yMat.mul(xMat)
- // Odd multiplying syntax, but yMat has all the rotations in it
- yMat
- }
- // Rotation matrix is just passed to my RotatedBB class, which then multiplies the 3x3 matrix by each point in the rectangle.
- ... irrelevant code...
- // Map basically iterates through a collection, changes each element based on the specified operation,
- // then returns a collection containing the changed values
- a map (corner => {
- multiplyPointByMatrix(corner.subtract(RotationPoint), RotationMatrix).add(RotationPoint)
- // Rotation point is (0.5, 0.5). Sets center of rotation to center of ship block
- })
- // This function is supposed to multiply a 3x1 matrix (or rather a Vec3) by a 3x3 matrix
- // I very much dislike and don't understand matrix math, so this could be wrong
- private def multiplyPointByMatrix(P: Vec3, M: Matrix3f): Vec3 = {
- new Vec3(
- P.xCoord * M.m00 + P.yCoord * M.m01 + P.zCoord * M.m02,
- P.xCoord * M.m10 + P.yCoord * M.m11 + P.zCoord * M.m12,
- P.xCoord * M.m20 + P.yCoord * M.m21 + P.zCoord * M.m22
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment