Guest User

Untitled

a guest
Apr 15th, 2016
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.26 KB | None | 0 0
  1.    // This function generates a 3x3 rotation matrix from a 4x4
  2.    // 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.
  3.    private def rotationMatrix: Matrix3f = {
  4.     val m = new Matrix3f()
  5.     val c = correctionMatrix.clone().asInstanceOf[Matrix4f] // Creates a copy of correctionMatrix because
  6.                                                             //the javax Matrix modifies itself
  7.     c.mul(renderMatrix) // Multiply's correctionMatrix by renderMatrix (see below)
  8.     c.getRotationScale(m) // Replaces all values in m with the 3x3 rotation/scale matrix within c (which is now multiplied)
  9.     m // returns m from our function
  10.   }
  11.  
  12.   lazy val correctionMatrix: Matrix4f = {
  13.     // Get the ship block (0,0,0)
  14.     def blockState = ShipWorld.getBlockState(new BlockPos(0, 0, 0))
  15.  
  16.     // Figure out which way it's facing
  17.     val axis = blockState.getValue(ShipCreatorBlock.FACING).getDirectionVec
  18.  
  19.     def rotationOffset(direction: Vec3i): Float = {
  20.       if (direction.getX == 1) 0f
  21.       else if (direction.getX == -1) 180f
  22.       else if (direction.getZ == 1) 90f
  23.       else if (direction.getZ == -1) 270f
  24.       else 0f
  25.     }
  26.  
  27.     // # Correction Matrix #
  28.     // Basically rotates the matrix so that our rotation is relative to the Ship Block's front face.
  29.     // We also never have to rotate our axis upwards or downwards so we only need to rotate the yaw (pitch) value.
  30.     val rotCorrectionMatrix = new Matrix4f()
  31.     rotCorrectionMatrix.rotY(Math.toRadians(rotationOffset(axis)).toFloat)
  32.  
  33.     rotCorrectionMatrix
  34.   }
  35.  
  36.   // Ignore the lazy
  37.   // This function just basically makes every rotation relative to which way the ship block is facing and not the world.
  38.   lazy val correctionMatrix: Matrix4f = {
  39.     // Get the ship block (0,0,0)
  40.     def blockState = ShipWorld.getBlockState(new BlockPos(0, 0, 0))
  41.  
  42.     // Figure out which way it's facing
  43.     val axis = blockState.getValue(ShipCreatorBlock.FACING).getDirectionVec
  44.    
  45.     // Depending on the vector returned by axis, get a rotation amount that would rotate the axis to the way
  46.     def rotationOffset(direction: Vec3i): Float = {
  47.       if (direction.getX == 1) 0f
  48.       else if (direction.getX == -1) 180f
  49.       else if (direction.getZ == 1) 90f
  50.       else if (direction.getZ == -1) 270f
  51.       else 0f
  52.     }
  53.  
  54.     // # Correction Matrix #
  55.     // Basically rotates the matrix so that our rotation is relative to the Ship Block's front face.
  56.     // We also never have to rotate our axis upwards or downwards so we only need to rotate the yaw (pitch) value.
  57.     val rotCorrectionMatrix = new Matrix4f()
  58.     rotCorrectionMatrix.rotY(Math.toRadians(rotationOffset(axis)).toFloat)
  59.  
  60.     rotCorrectionMatrix
  61.   }
  62.  
  63.   def renderMatrix: Matrix4f = {
  64.     val xMat: Matrix4f = new Matrix4f()
  65.     xMat.rotX(Math.toRadians(rotationRoll).toFloat)
  66.  
  67.     // Switch Y and Z cause in Minecraft the Y axis is "up"
  68.     val yMat: Matrix4f = new Matrix4f()
  69.     yMat.rotZ(Math.toRadians(rotationPitch).toFloat)
  70.  
  71.     // See above ^
  72.     val zMat: Matrix4f = new Matrix4f()
  73.     zMat.rotY(Math.toRadians(rotationYaw).toFloat)
  74.  
  75.     // Multiply our rotation matrices into one unified rotation matrix
  76.     xMat.mul(zMat)
  77.     yMat.mul(xMat)
  78.  
  79.     // Odd multiplying syntax, but yMat has all the rotations in it
  80.     yMat
  81.   }
  82.   // Rotation matrix is just passed to my RotatedBB class, which then multiplies the 3x3 matrix by each point in the rectangle.
  83.     ... irrelevant code...
  84.     // Map basically iterates through a collection, changes each element based on the specified operation,
  85.     // then returns a collection containing the changed values
  86.     a map (corner => {
  87.       multiplyPointByMatrix(corner.subtract(RotationPoint), RotationMatrix).add(RotationPoint)
  88.     // Rotation point is (0.5, 0.5). Sets center of rotation to center of ship block
  89.     })
  90.  
  91.   // This function is supposed to multiply a 3x1 matrix (or rather a Vec3) by a 3x3 matrix
  92.   // I very much dislike and don't understand matrix math, so this could be wrong
  93.   private def multiplyPointByMatrix(P: Vec3, M: Matrix3f): Vec3 = {
  94.     new Vec3(
  95.       P.xCoord * M.m00 + P.yCoord * M.m01 + P.zCoord * M.m02,
  96.       P.xCoord * M.m10 + P.yCoord * M.m11 + P.zCoord * M.m12,
  97.       P.xCoord * M.m20 + P.yCoord * M.m21 + P.zCoord * M.m22
  98.     )
  99.   }
Advertisement
Add Comment
Please, Sign In to add comment