internal float CheckFloorCeilingCollisions(Vector3D moveVector, ref float halfSizeX, ref float halfSizeY, ref bool falling) { float x1 = X1 - halfSizeX; float x2 = X2 + halfSizeX; float z1 = Z1 - halfSizeX; float z2 = Z2 + halfSizeX; // check floor float y = Y2 + halfSizeY; Tuple <int, Point3D> result = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(x1, y, z1), new Point3D(x2, y, z1), new Point3D(x2, y, z2), new Point3D(x1, y, z2)), moveVector); if (result.Item1 == 1) { // interesects and less than half is inside the box float diff = result.Item2.Y - moveVector.Point2.Y; if (diff > ErrorTolerances.FloatEpsilon && diff < halfSizeY) { falling = false; return(diff); } } else if (result.Item1 == 0) { // check ceiling y = Y1 - halfSizeY; result = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(x1, y, z1), new Point3D(x2, y, z1), new Point3D(x2, y, z2), new Point3D(x1, y, z2)), moveVector); if (result.Item1 == 1) { float diff = moveVector.Point2.Y - result.Item2.Y; if (diff > ErrorTolerances.FloatEpsilon && diff < halfSizeY) { // intersects and less than half is inside the box return(-diff); } } } return(0); }
private void CheckBackZCollisions(Vector3D moveVector, ref float halfSizeX, ref float halfSizeY, ref float[] data) { float x1, x2, y1, y2, z1, z2; GetPoints(halfSizeX, halfSizeY, out x1, out x2, out y1, out y2, out z1, out z2); if (moveVector.Point1.Z < z2) { return; } if (_Box.CenterPoint.X - moveVector.Point1.X > WorldHelper.HalfSizeX) { x1 -= WorldHelper.SizeX; x2 -= WorldHelper.SizeX; } else if (moveVector.Point1.X - _Box.CenterPoint.X > WorldHelper.HalfSizeX) { x1 += WorldHelper.SizeX; x2 += WorldHelper.SizeX; } Tuple <int, Point3D> result = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(x1, y1, z2), new Point3D(x1, y2, z2), new Point3D(x2, y2, z2), new Point3D(x2, y1, z2)), moveVector); if (result.Item1 == 1) { float dist = result.Item2.GetDistanceSquare(moveVector.Point1); if (dist < data[0]) { float diff = result.Item2.Z - moveVector.Point2.Z; if (diff > ErrorTolerances.FloatEpsilon) { data[0] = dist; data[1] = diff; } } } }
/// <summary> /// Gets the intersection of a given line with this box (if any). /// </summary> /// <param name="line">Line</param> /// <param name="shiftX">Shift X</param> /// <param name="shiftZ">Shift Z</param> /// <returns>Intersection with given line</returns> public Intersection GetIntersection(Vector3D line, int shiftX, int shiftZ) { Intersection result = new Intersection(); // check front X Tuple <int, Point3D> intersect = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(X1 + shiftX, Y1, Z1 + shiftZ), new Point3D(X1 + shiftX, Y1, Z2 + shiftZ), new Point3D(X1 + shiftX, Y2, Z2 + shiftZ), new Point3D(X1 + shiftX, Y2, Z1 + shiftZ)), line); CheckIntersection(line, intersect, Sides.FrontX, result); // check back X intersect = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(X2 + shiftX, Y1, Z1 + shiftZ), new Point3D(X2 + shiftX, Y1, Z2 + shiftZ), new Point3D(X2 + shiftX, Y2, Z2 + shiftZ), new Point3D(X2 + shiftX, Y2, Z1 + shiftZ)), line); CheckIntersection(line, intersect, Sides.BackX, result); // check front Y intersect = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(X1 + shiftX, Y1, Z1 + shiftZ), new Point3D(X1 + shiftX, Y1, Z2 + shiftZ), new Point3D(X2 + shiftX, Y1, Z2 + shiftZ), new Point3D(X2 + shiftX, Y1, Z1 + shiftZ)), line); CheckIntersection(line, intersect, Sides.FrontY, result); // check back Y intersect = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(X1 + shiftX, Y2, Z1 + shiftZ), new Point3D(X1 + shiftX, Y2, Z2 + shiftZ), new Point3D(X2 + shiftX, Y2, Z2 + shiftZ), new Point3D(X2 + shiftX, Y2, Z1 + shiftZ)), line); CheckIntersection(line, intersect, Sides.BackY, result); // check front Z intersect = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(X1 + shiftX, Y1, Z1 + shiftZ), new Point3D(X1 + shiftX, Y2, Z1 + shiftZ), new Point3D(X2 + shiftX, Y2, Z1 + shiftZ), new Point3D(X2 + shiftX, Y1, Z1 + shiftZ)), line); CheckIntersection(line, intersect, Sides.FrontZ, result); // check back Z intersect = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(X1 + shiftX, Y1, Z2 + shiftZ), new Point3D(X1 + shiftX, Y2, Z2 + shiftZ), new Point3D(X2 + shiftX, Y2, Z2 + shiftZ), new Point3D(X2 + shiftX, Y1, Z2 + shiftZ)), line); CheckIntersection(line, intersect, Sides.BackZ, result); return(result); }
internal void CheckWallCollisions(Vector3D moveVector, ref float halfSizeX, ref float halfSizeY, ref float[] data) { float x1 = X1 - halfSizeX; float x2 = X2 + halfSizeX; float y1 = Y1 - halfSizeY; float y2 = Y2 + halfSizeY; float z1 = Z1 - halfSizeX; float z2 = Z2 + halfSizeX; // check front X wall Tuple <int, Point3D> result = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(x1, y1, z1), new Point3D(x1, y1, z2), new Point3D(x1, y2, z2), new Point3D(x1, y2, z1)), moveVector); if (result.Item1 == 1) { float dist = result.Item2.GetDistanceSquare(moveVector.Point1); if (dist < data[0]) { float diff = moveVector.Point2.X - result.Item2.X; if (diff > ErrorTolerances.FloatEpsilon) { data[0] = dist; data[1] = -diff; data[2] = 0; } } } // check back X wall result = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(x2, y1, z1), new Point3D(x2, y1, z2), new Point3D(x2, y2, z2), new Point3D(x2, y2, z1)), moveVector); if (result.Item1 == 1) { float dist = result.Item2.GetDistanceSquare(moveVector.Point1); if (dist < data[0]) { float diff = result.Item2.X - moveVector.Point2.X; if (diff > ErrorTolerances.FloatEpsilon) { data[0] = dist; data[1] = diff; data[2] = 0; } } } // check front Z wall result = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(x1, y1, z1), new Point3D(x1, y2, z1), new Point3D(x2, y2, z1), new Point3D(x2, y1, z1)), moveVector); if (result.Item1 == 1) { float dist = result.Item2.GetDistanceSquare(moveVector.Point1); if (dist < data[0]) { float diff = moveVector.Point2.Z - result.Item2.Z; if (diff > ErrorTolerances.FloatEpsilon) { data[0] = dist; data[1] = 0; data[2] = -diff; } } } // check back Z wall result = Point3D.GetRectangleIntersection(Tuple.Create( new Point3D(x1, y1, z2), new Point3D(x1, y2, z2), new Point3D(x2, y2, z2), new Point3D(x2, y1, z2)), moveVector); if (result.Item1 == 1) { float dist = result.Item2.GetDistanceSquare(moveVector.Point1); if (dist < data[0]) { float diff = result.Item2.Z - moveVector.Point2.Z; if (diff > ErrorTolerances.FloatEpsilon) { data[0] = dist; data[1] = 0; data[2] = diff; } } } }