Advertisement
Krythic

Camera Collision

Jul 22nd, 2020
2,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1. private void CheckKeyboardInput(float deltaTime)
  2.         {
  3.             Vector3 previousCameraLocation = Camera.Location;
  4.             KeyboardState state = Keyboard.GetCurrentState();
  5.             Camera.IsAccelerating = state.IsPressed(Key.LeftShift);
  6.             bool isCameraMoving = false;
  7.             if (state.IsPressed(Key.W))
  8.             {
  9.                 Camera.TranslateForward(deltaTime);
  10.                 isCameraMoving = true;
  11.             }
  12.             if (state.IsPressed(Key.S))
  13.             {
  14.                 Camera.TranslateBackward(deltaTime);
  15.                 isCameraMoving = true;
  16.             }
  17.             if (state.IsPressed(Key.A))
  18.             {
  19.                 Camera.TranslateLeft(deltaTime);
  20.                 isCameraMoving = true;
  21.             }
  22.             if (state.IsPressed(Key.D))
  23.             {
  24.                 Camera.TranslateRight(deltaTime);
  25.                 isCameraMoving = true;
  26.             }
  27.             if (state.IsPressed(Key.E))
  28.             {
  29.                 Camera.TranslateUp(deltaTime);
  30.                 isCameraMoving = true;
  31.             }
  32.             if (state.IsPressed(Key.Q))
  33.             {
  34.                 Camera.TranslateDown(deltaTime);
  35.                 isCameraMoving = true;
  36.             }
  37.             if (isCameraMoving)
  38.             {
  39.                 float finalCameraLocationX = Camera.Location.X;
  40.                 float finalCameraLocationY = Camera.Location.Y;
  41.                 float finalCameraLocationZ = Camera.Location.Z;
  42.                 foreach (StaticGeometry geometry in this.StaticGeometry)
  43.                 {
  44.                     BoundingSphere cameraSphere = Camera.Bounds;
  45.                     for (int i = 0; i < geometry.Mesh.Vertices.Length; i += 3)
  46.                     {
  47.                         // Get the Vertex Positions for the current Triangle
  48.                         Vector3 position1 = geometry.Mesh.Vertices[i].Location;
  49.                         Vector3 position2 = geometry.Mesh.Vertices[i + 1].Location;
  50.                         Vector3 position3 = geometry.Mesh.Vertices[i + 2].Location;
  51.  
  52.                         // Create the rotation matrix using the geometry's current rotation setting.
  53.                         Matrix rotationMatrix = VoidwalkerMath.CreateRotationMatrix(geometry.Rotation);
  54.  
  55.                         // Transform the Coordinate with the Rotation Matrix, then add the geometry's location
  56.                         Vector3 finalVertexLocation1 = Vector3.TransformCoordinate(position1, rotationMatrix) + geometry.Location;
  57.                         Vector3 finalVertexLocation2 = Vector3.TransformCoordinate(position2, rotationMatrix) + geometry.Location;
  58.                         Vector3 finalVertexLocation3 = Vector3.TransformCoordinate(position3, rotationMatrix) + geometry.Location;
  59.  
  60.                         Vector3 currentCameraLocation = this.Camera.Location;
  61.                         Vector3 translationX = new Vector3(currentCameraLocation.X, previousCameraLocation.Y, previousCameraLocation.Z);
  62.                         Vector3 translationY = new Vector3(previousCameraLocation.X, currentCameraLocation.Y, previousCameraLocation.Z);
  63.                         Vector3 translationZ = new Vector3(previousCameraLocation.X, previousCameraLocation.Y, currentCameraLocation.Z);
  64.                         // Test X
  65.                         BoundingSphere sphereX = new BoundingSphere(translationX, 2);
  66.                         if (sphereX.Intersects(ref finalVertexLocation1, ref finalVertexLocation2, ref finalVertexLocation3))
  67.                         {
  68.                             finalCameraLocationX = previousCameraLocation.X;
  69.                         }
  70.                         // Test Y
  71.                         BoundingSphere sphereY = new BoundingSphere(translationY, 2);
  72.                         if (sphereY.Intersects(ref finalVertexLocation1, ref finalVertexLocation2, ref finalVertexLocation3))
  73.                         {
  74.                             finalCameraLocationY = previousCameraLocation.Y;
  75.                         }
  76.                         // Test Z
  77.                         BoundingSphere sphereZ = new BoundingSphere(translationZ, 2);
  78.                         if (sphereZ.Intersects(ref finalVertexLocation1, ref finalVertexLocation2, ref finalVertexLocation3))
  79.                         {
  80.                             finalCameraLocationZ = previousCameraLocation.Z;
  81.                         }
  82.                     }
  83.                 }
  84.                 this.Camera.Location = new Vector3(finalCameraLocationX, finalCameraLocationY, finalCameraLocationZ);
  85.             }
  86.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement