Advertisement
Whiplash141

Whip's Inertia Tensor

Sep 27th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. /// <summary>
  2. /// This computes an inertial tensor relative to a reference block
  3. /// Help provided by Equinox :)
  4. /// </summary>
  5. /// <param name="reference">Reference block to base tensor off of</param>
  6. /// <returns>Inertia tensor of the grid</returns>
  7. MatrixD CalculateInertiaTensor(IMyShipController reference)
  8. {
  9.     MatrixD inertiaTensor = new MatrixD();
  10.     var shipMass = reference.CalculateShipMass().PhysicalMass;
  11.  
  12.     var localRotation = new Matrix();
  13.     double gridSize = reference.CubeGrid.GridSize; //get size of blocks on grid
  14.     reference.Orientation.GetMatrix(out localRotation);
  15.     localRotation.Translation = gridSize * reference.Position;
  16.     var localInverse = Matrix.Invert(localRotation);
  17.     var centerOfMass = Vector3D.Transform(reference.CenterOfMass, MatrixD.Invert(reference.WorldMatrix)); //convert COM to block relative
  18.     var gridMinimum = Vector3D.Transform(reference.CubeGrid.Min * gridSize, localInverse); //get minimum grid vector
  19.     var gridMaximum = Vector3D.Transform(reference.CubeGrid.Max * gridSize, localInverse); //get maximum grid vector
  20.    
  21.     var boundingBox = new BoundingBoxD(gridMinimum, gridMaximum); //Get bounding box of this ship
  22.     var corners = boundingBox.GetCorners(); //get corners of bounding box
  23.  
  24.     foreach (var thisCorner in corners)
  25.     {
  26.         var diagonal = centerOfMass - thisCorner;
  27.         var offset =  diagonal / 2;
  28.         var x = diagonal.X;
  29.         var y = diagonal.Y;
  30.         var z = diagonal.Z;
  31.         var localMass = shipMass / 8;
  32.         var I_xx = localMass / 12 * (y * y + z * z);
  33.         var I_yy = localMass / 12 * (x * x + z * z);
  34.         var I_zz = localMass / 12 * (y * y + x * x);
  35.         var localTensor = new MatrixD(I_xx, 0, 0,
  36.                                       0, I_yy, 0,
  37.                                       0, 0, I_zz);
  38.         inertiaTensor += localTensor + localMass * (offset.LengthSquared() * MatrixD.Identity - OuterProduct(offset, offset));
  39.     }
  40.     return inertiaTensor;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement