Advertisement
Guest User

Creating a block

a guest
Oct 18th, 2011
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.88 KB | None | 0 0
  1. List<Quad> quads = new List<Quad>();
  2. //pseudocode:
  3. foreach (Face Direction of block)
  4. {
  5.     quads.Add(CreateFace(position, Face Direction));
  6. }
  7. PlanetBlockGeometry newBlock = new PlanetBlockGeometry(position, quads, type, GetCenterOfBlock(position));
  8.  
  9. //CreateFace method
  10. private Quad CreateFace(Coordinate position, FACING_DIRECTION direction)
  11.         {
  12.             #region get offset from face direction
  13.  
  14.             Coordinate[] offsets = new Coordinate[4];
  15.             switch (direction)
  16.             {
  17.                 case FACING_DIRECTION.BOTTOM:
  18.                     {
  19.                         offsets[0] = new Coordinate(0, 0, 0, 1);
  20.                         offsets[1] = new Coordinate(0, 0, 0, 0);
  21.                         offsets[2] = new Coordinate(0, 1, 0, 0);
  22.                         offsets[3] = new Coordinate(0, 1, 0, 1);
  23.                         break;
  24.                     }
  25.                 case FACING_DIRECTION.LEFT:
  26.                     {
  27.                         offsets[0] = new Coordinate(0, 0, 1, 0);
  28.                         offsets[1] = new Coordinate(0, 0, 1, 1);
  29.                         offsets[2] = new Coordinate(0, 0, 0, 1);
  30.                         offsets[3] = new Coordinate(0, 0, 0, 0);
  31.                         break;
  32.                     }
  33.                 case FACING_DIRECTION.TOP:
  34.                     {
  35.                         offsets[0] = new Coordinate(0, 0, 1, 0);
  36.                         offsets[1] = new Coordinate(0, 1, 1, 0);
  37.                         offsets[2] = new Coordinate(0, 1, 1, 1);
  38.                         offsets[3] = new Coordinate(0, 0, 1, 1);
  39.                         break;
  40.                     }
  41.                 case FACING_DIRECTION.BACK:
  42.                     {
  43.                         offsets[0] = new Coordinate(0, 0, 1, 0);
  44.                         offsets[1] = new Coordinate(0, 1, 1, 0);
  45.                         offsets[2] = new Coordinate(0, 1, 0, 0);
  46.                         offsets[3] = new Coordinate(0, 0, 0, 0);
  47.                         break;
  48.                     }
  49.                 case FACING_DIRECTION.FRONT:
  50.                     {
  51.                         offsets[0] = new Coordinate(0, 0, 1, 1);
  52.                         offsets[1] = new Coordinate(0, 1, 1, 1);
  53.                         offsets[2] = new Coordinate(0, 1, 0, 1);
  54.                         offsets[3] = new Coordinate(0, 0, 0, 1);
  55.                         break;
  56.                     }
  57.                 case FACING_DIRECTION.RIGHT:
  58.                     {
  59.                         offsets[0] = new Coordinate(0, 1, 1, 1);
  60.                         offsets[1] = new Coordinate(0, 1, 1, 0);
  61.                         offsets[2] = new Coordinate(0, 1, 0, 0);
  62.                         offsets[3] = new Coordinate(0, 1, 0, 1);
  63.                         break;
  64.                     }
  65.             }
  66.             #endregion
  67.  
  68.             Vector3 correctedTopLeftPosition = GetCorrectedCubePosition(position + offsets[0]);
  69.             Vector3 topLeftPosition = MapToProjected(correctedTopLeftPosition, (position.Shell + offsets[0].Shell));
  70.  
  71.             Vector3 correctedTopRightPosition = GetCorrectedCubePosition(position + offsets[1]);
  72.             Vector3 topRightPosition = MapToProjected(correctedTopRightPosition, (position.Shell + offsets[1].Shell));
  73.  
  74.             Vector3 correctedBottomRightPosition = GetCorrectedCubePosition(position + offsets[2]);
  75.             Vector3 bottomRightPosition = MapToProjected(correctedBottomRightPosition, (position.Shell + offsets[2].Shell));
  76.  
  77.             Vector3 correctedBottomLeftPosition = GetCorrectedCubePosition(position + offsets[3]);
  78.             Vector3 bottomLeftPosition = MapToProjected(correctedBottomLeftPosition, (position.Shell + offsets[3].Shell));
  79.  
  80.             VertexPositionNormalTexture[] corners = new VertexPositionNormalTexture[4]
  81.             {
  82.                 new VertexPositionNormalTexture(topLeftPosition, Vector3.Zero, Vector2.Zero),
  83.                 new VertexPositionNormalTexture(topRightPosition, Vector3.Zero, Vector2.Zero),
  84.                 new VertexPositionNormalTexture(bottomRightPosition, Vector3.Zero, Vector2.Zero),
  85.                 new VertexPositionNormalTexture(bottomLeftPosition, Vector3.Zero, Vector2.Zero)
  86.             };
  87.  
  88.             Quad quad = new Quad(corners, direction);
  89.  
  90.             return quad;
  91.         }
  92.  
  93. //GetCorrectedCubePosition method
  94. private Vector3 GetCorrectedCubePosition(Coordinate position)
  95.         {
  96.             Vector3 positionOnCube = new Vector3(position.X, position.Y, 0);
  97.             positionOnCube -= new Vector3(planet.BlockMap.FaceWidth / 2, planet.BlockMap.FaceWidth / 2, planet.BlockMap.FaceWidth / 2);
  98.  
  99.             //adjust for positive and negative values
  100.             if (position.Face == PlanetBlockMap.SOUTH | position.Face == PlanetBlockMap.BACK | position.Face == PlanetBlockMap.WEST) //negative Y
  101.             {
  102.                 positionOnCube.Z *= -1;
  103.             }
  104.  
  105.             //rotate the whole vector to suit the direction this face is facing
  106.             Vector3 rotatedPosition = Vector3.Zero;
  107.             if (position.Face == 0 | position.Face == 1)
  108.             {
  109.                 rotatedPosition.X = positionOnCube.X;
  110.                 rotatedPosition.Y = positionOnCube.Z;
  111.                 rotatedPosition.Z = positionOnCube.Y;
  112.             }
  113.             else if (position.Face == 2 | position.Face == 3)
  114.             {
  115.                 rotatedPosition = positionOnCube;
  116.             }
  117.             else if (position.Face == 4 | position.Face == 5)
  118.             {
  119.                 rotatedPosition.X = positionOnCube.Z;
  120.                 rotatedPosition.Y = positionOnCube.Y;
  121.                 rotatedPosition.Z = positionOnCube.X;
  122.             }
  123.  
  124.             rotatedPosition *= (1f / planet.BlockMap.FaceWidth);
  125.  
  126.  
  127.  
  128.             return rotatedPosition;
  129.         }
  130.  
  131. //MapToProjected method
  132. private Vector3 MapToProjected(Vector3 positionOnCube, float radiusOfSphere)
  133.         {
  134.             Vector3 normal = positionOnCube;
  135.             normal.Normalize();
  136.             normal *= radiusOfSphere;
  137.             return normal;
  138.         }
  139.  
  140.  
  141. //GetCenterOfBlock Method
  142. private Vector3 GetCenterOfBlock(Coordinate position)
  143.         {
  144.             Vector3[] corners = new Vector3[8];
  145.             corners[0] = ProjectFromCoordinate(position);
  146.             corners[1] = ProjectFromCoordinate(position + new Coordinate(0, 0, 0, 1));
  147.             corners[2] = ProjectFromCoordinate(position + new Coordinate(0, 0, 1, 0));
  148.             corners[3] = ProjectFromCoordinate(position + new Coordinate(0, 1, 0, 0));
  149.             corners[4] = ProjectFromCoordinate(position + new Coordinate(0, 1, 1, 0));
  150.             corners[5] = ProjectFromCoordinate(position + new Coordinate(0, 1, 0, 1));
  151.             corners[6] = ProjectFromCoordinate(position + new Coordinate(0, 0, 1, 1));
  152.             corners[7] = ProjectFromCoordinate(position + new Coordinate(0, 1, 1, 1));
  153.             return (AverageVector3(corners));
  154.         }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement