/// <summary> /// Calculates intersection between line and bounding box and if found, distance is returned. Otherwise null is returned. /// </summary> public static float?GetLineBoundingBoxIntersection(ref Line line, ref BoundingBox boundingBox) { // Create temporary ray and do intersection. But we can't rely only on it, because ray doesn't have end, yet our line does, so we // need to check if ray-bounding_box intersection lies in the range of our line VRageMath.Ray ray = new VRageMath.Ray(line.From, line.Direction); float? intersectionDistance = boundingBox.Intersects(ray); if (intersectionDistance.HasValue == false) { // No intersection between ray/line and bounding box return(null); } else { if (intersectionDistance.Value <= line.Length) { // Intersection between ray/line and bounding box IS withing the range of the line return(intersectionDistance.Value); } else { // Intersection between ray/line and bounding box IS NOT withing the range of the line return(null); } } }
private static Ray ComputeIntersectionLine(ref Plane p1, ref Plane p2) { Ray ray = new Ray(); ray.Direction = Vector3.Cross(p1.Normal, p2.Normal); float num = ray.Direction.LengthSquared(); ray.Position = Vector3.Cross(-p1.D * p2.Normal + p2.D * p1.Normal, ray.Direction) / num; return(ray); }
private static void TransformRay(ref Ray ray, ref Matrix matrix) { ray.Direction = Vector3.Transform(ray.Position + ray.Direction, ref matrix); ray.Position = Vector3.Transform(ray.Position, ref matrix); ray.Direction = ray.Direction - ray.Position; }
private static Ray ComputeIntersectionLine(ref Plane p1, ref Plane p2) { Ray ray = new Ray(); ray.Direction = Vector3.Cross(p1.Normal, p2.Normal); float num = ray.Direction.LengthSquared(); ray.Position = Vector3.Cross(-p1.D * p2.Normal + p2.D * p1.Normal, ray.Direction) / num; return ray; }
public float?Intersects(ref Ray ray) { Matrix matrix = Matrix.CreateFromQuaternion(this.Orientation); Vector3 vector = this.Center - ray.Position; float minValue = float.MinValue; float maxValue = float.MaxValue; float num3 = Vector3.Dot(matrix.Right, vector); float num4 = Vector3.Dot(matrix.Right, ray.Direction); if ((num4 >= -1E-20f) && (num4 <= 1E-20f)) { if (((-num3 - this.HalfExtent.X) > 0.0) || ((-num3 + this.HalfExtent.X) < 0f)) { return(null); } } else { float num5 = (num3 - this.HalfExtent.X) / num4; float num6 = (num3 + this.HalfExtent.X) / num4; if (num5 > num6) { float num7 = num5; num5 = num6; num6 = num7; } if (num5 > minValue) { minValue = num5; } if (num6 < maxValue) { maxValue = num6; } if ((maxValue < 0f) || (minValue > maxValue)) { return(null); } } num3 = Vector3.Dot(matrix.Up, vector); num4 = Vector3.Dot(matrix.Up, ray.Direction); if ((num4 >= -1E-20f) && (num4 <= 1E-20f)) { if (((-num3 - this.HalfExtent.Y) > 0.0) || ((-num3 + this.HalfExtent.Y) < 0f)) { return(null); } } else { float num8 = (num3 - this.HalfExtent.Y) / num4; float num9 = (num3 + this.HalfExtent.Y) / num4; if (num8 > num9) { float num10 = num8; num8 = num9; num9 = num10; } if (num8 > minValue) { minValue = num8; } if (num9 < maxValue) { maxValue = num9; } if ((maxValue < 0f) || (minValue > maxValue)) { return(null); } } num3 = Vector3.Dot(matrix.Forward, vector); num4 = Vector3.Dot(matrix.Forward, ray.Direction); if ((num4 >= -1E-20f) && (num4 <= 1E-20f)) { if (((-num3 - this.HalfExtent.Z) > 0.0) || ((-num3 + this.HalfExtent.Z) < 0f)) { return(null); } } else { float num11 = (num3 - this.HalfExtent.Z) / num4; float num12 = (num3 + this.HalfExtent.Z) / num4; if (num11 > num12) { float num13 = num11; num11 = num12; num12 = num13; } if (num11 > minValue) { minValue = num11; } if (num12 < maxValue) { maxValue = num12; } if ((maxValue < 0f) || (minValue > maxValue)) { return(null); } } return(new float?(minValue)); }