Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void ProcessCameraCollisions(Vector3 previousCameraLocation)
- {
- /**
- * Process Camera Collisions
- */
- if (Camera.IsCollisionEnabled)
- {
- Vector3 currentCameraLocation = this.Camera.Location;
- float finalCameraLocationX = currentCameraLocation.X;
- float finalCameraLocationY = currentCameraLocation.Y;
- float finalCameraLocationZ = currentCameraLocation.Z;
- bool hasXCollision = false;
- bool hasYCollision = false;
- bool hasZCollision = false;
- BoundingSphere cameraBounds = Camera.HardBounds;
- Vector3 velocity = currentCameraLocation - previousCameraLocation;
- Ray cameraRay = new Ray(previousCameraLocation,currentCameraLocation);
- foreach (StaticGeometry geometry in this.StaticGeometry)
- {
- if (geometry.IsCollidable)
- {
- if (Vector3.Distance(geometry.Location, currentCameraLocation) <= Camera.CollisionDistance)
- {
- if (cameraBounds.Intersects(geometry.BoundingBox.ToBoundingBox()))
- {
- // Create the rotation matrix using the geometry's current rotation setting.
- Matrix rotationMatrix = VoidwalkerMath.CreateRotationMatrix(geometry.Rotation);
- 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;
- // 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 normal = Vector3.Cross(finalVertexLocation2 - finalVertexLocation1, finalVertexLocation3 - finalVertexLocation1);
- if (Vector3.Dot(velocity, normal) < 0)
- {
- 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, Camera.HardBoundsRadius);
- if (sphereX.Intersects(ref finalVertexLocation1, ref finalVertexLocation2, ref finalVertexLocation3))
- {
- finalCameraLocationX = previousCameraLocation.X;
- hasXCollision = true;
- this.Camera.Velocity = Vector3.Zero;
- }
- // Test Y
- BoundingSphere sphereY = new BoundingSphere(translationY, Camera.HardBoundsRadius);
- if (sphereY.Intersects(ref finalVertexLocation1, ref finalVertexLocation2, ref finalVertexLocation3))
- {
- finalCameraLocationY = previousCameraLocation.Y;
- hasYCollision = true;
- this.Camera.Velocity = Vector3.Zero;
- }
- // Test Z
- BoundingSphere sphereZ = new BoundingSphere(translationZ, Camera.HardBoundsRadius);
- if (sphereZ.Intersects(ref finalVertexLocation1, ref finalVertexLocation2, ref finalVertexLocation3))
- {
- finalCameraLocationZ = previousCameraLocation.Z;
- hasZCollision = true;
- this.Camera.Velocity = Vector3.Zero;
- }
- /**
- * Edge case early termination. If the camera is already colliding
- * on all axis, we don't need to test for any further collisions. This seems
- * to only trigger when the player is wedged in a corner.
- */
- if (hasXCollision && hasYCollision && hasZCollision)
- {
- this.Camera.Location = new Vector3(
- finalCameraLocationX,
- finalCameraLocationY,
- finalCameraLocationZ);
- return;
- }
- }
- }
- }
- }
- }
- this.Camera.Location = new Vector3(
- finalCameraLocationX,
- finalCameraLocationY,
- finalCameraLocationZ);
- OnCameraChanged();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement