Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void CheckKeyboardInput(float deltaTime)
- {
- Vector3 previousCameraLocation = Camera.Location;
- KeyboardState state = Keyboard.GetCurrentState();
- Camera.IsAccelerating = state.IsPressed(Key.LeftShift);
- bool isCameraMoving = false;
- if (state.IsPressed(Key.W))
- {
- Camera.TranslateForward(deltaTime);
- isCameraMoving = true;
- }
- if (state.IsPressed(Key.S))
- {
- Camera.TranslateBackward(deltaTime);
- isCameraMoving = true;
- }
- if (state.IsPressed(Key.A))
- {
- Camera.TranslateLeft(deltaTime);
- isCameraMoving = true;
- }
- if (state.IsPressed(Key.D))
- {
- Camera.TranslateRight(deltaTime);
- isCameraMoving = true;
- }
- if (state.IsPressed(Key.E))
- {
- Camera.TranslateUp(deltaTime);
- isCameraMoving = true;
- }
- if (state.IsPressed(Key.Q))
- {
- Camera.TranslateDown(deltaTime);
- isCameraMoving = true;
- }
- if (isCameraMoving)
- {
- float finalCameraLocationX = Camera.Location.X;
- float finalCameraLocationY = Camera.Location.Y;
- float finalCameraLocationZ = Camera.Location.Z;
- foreach (StaticGeometry geometry in this.StaticGeometry)
- {
- BoundingSphere cameraSphere = Camera.Bounds;
- for (int i = 0; i < geometry.Mesh.Vertices.Length; i += 3)
- {
- // Get the Vertex Positions for the current Triangle
- Vector3 position1 = geometry.Mesh.Vertices[i].Location;
- Vector3 position2 = geometry.Mesh.Vertices[i + 1].Location;
- Vector3 position3 = geometry.Mesh.Vertices[i + 2].Location;
- // Create the rotation matrix using the geometry's current rotation setting.
- Matrix rotationMatrix = VoidwalkerMath.CreateRotationMatrix(geometry.Rotation);
- // Transform the Coordinate with the Rotation Matrix, then add the geometry's location
- Vector3 finalVertexLocation1 = Vector3.TransformCoordinate(position1, rotationMatrix) + geometry.Location;
- Vector3 finalVertexLocation2 = Vector3.TransformCoordinate(position2, rotationMatrix) + geometry.Location;
- Vector3 finalVertexLocation3 = Vector3.TransformCoordinate(position3, rotationMatrix) + geometry.Location;
- Vector3 currentCameraLocation = this.Camera.Location;
- Vector3 translationX = new Vector3(currentCameraLocation.X, previousCameraLocation.Y, previousCameraLocation.Z);
- Vector3 translationY = new Vector3(previousCameraLocation.X, currentCameraLocation.Y, previousCameraLocation.Z);
- Vector3 translationZ = new Vector3(previousCameraLocation.X, previousCameraLocation.Y, currentCameraLocation.Z);
- // Test X
- BoundingSphere sphereX = new BoundingSphere(translationX, 2);
- if (sphereX.Intersects(ref finalVertexLocation1, ref finalVertexLocation2, ref finalVertexLocation3))
- {
- finalCameraLocationX = previousCameraLocation.X;
- }
- // Test Y
- BoundingSphere sphereY = new BoundingSphere(translationY, 2);
- if (sphereY.Intersects(ref finalVertexLocation1, ref finalVertexLocation2, ref finalVertexLocation3))
- {
- finalCameraLocationY = previousCameraLocation.Y;
- }
- // Test Z
- BoundingSphere sphereZ = new BoundingSphere(translationZ, 2);
- if (sphereZ.Intersects(ref finalVertexLocation1, ref finalVertexLocation2, ref finalVertexLocation3))
- {
- finalCameraLocationZ = previousCameraLocation.Z;
- }
- }
- }
- this.Camera.Location = new Vector3(finalCameraLocationX, finalCameraLocationY, finalCameraLocationZ);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement