private static Vector3?GetNearestGridIntersectionPoint(Ray ray, Vector3 gridMinCoord, float gridWidth, float gridHeight, float gridBreadth) { List <CollisionFace> faces = new List <CollisionFace>(); //Bottom faces.Add(new CollisionFace(gridMinCoord, gridMinCoord + new Vector3(gridWidth, 0, 0), gridMinCoord + new Vector3(gridWidth, 0, gridBreadth), gridMinCoord + new Vector3(0, 0, gridBreadth), new Vector3(0, 1, 0))); //Top faces.Add(new CollisionFace(gridMinCoord + new Vector3(0, gridHeight, 0), gridMinCoord + new Vector3(gridWidth, gridHeight, 0), gridMinCoord + new Vector3(gridWidth, gridHeight, gridBreadth), gridMinCoord + new Vector3(0, gridHeight, gridBreadth), new Vector3(0, 1, 0))); faces.Add(new CollisionFace(gridMinCoord, gridMinCoord + new Vector3(gridWidth, 0, 0), gridMinCoord + new Vector3(gridWidth, gridHeight, 0), gridMinCoord + new Vector3(0, gridHeight, 0), new Vector3(0, 0, 0))); faces.Add(new CollisionFace(gridMinCoord, gridMinCoord + new Vector3(0, 0, gridBreadth), gridMinCoord + new Vector3(0, gridHeight, gridBreadth), gridMinCoord + new Vector3(0, gridHeight, 0), new Vector3(0, 0, 0))); faces.Add(new CollisionFace(gridMinCoord + new Vector3(gridWidth, 0, 0), gridMinCoord + new Vector3(gridWidth, 0, gridBreadth), gridMinCoord + new Vector3(gridWidth, gridHeight, gridBreadth), gridMinCoord + new Vector3(gridWidth, gridHeight, 0), new Vector3(0, 1, 0))); faces.Add(new CollisionFace(gridMinCoord + new Vector3(0, 0, gridBreadth), gridMinCoord + new Vector3(gridWidth, 0, gridBreadth), gridMinCoord + new Vector3(gridWidth, gridHeight, gridBreadth), gridMinCoord + new Vector3(0, gridHeight, gridBreadth), new Vector3(0, 1, 0))); CollisionFace nearestFace = GetNearestCollisionFace(faces, ray); Vector3?nearestPoint = null; if (nearestFace != null) { nearestPoint = nearestFace.GetRayFaceIntersectionPoint(ray); } return(nearestPoint); }
public static Vector3?GetNearestWallAnchor(CollisionFace face, Ray ray) { Vector3?result = null; Vector3?pointOnFace = face.GetRayFaceIntersectionPoint((Ray)ray); if (pointOnFace != null) { Vector3 point = (Vector3)pointOnFace; //Get nearest wall anchor (i.e val.5) float xDecimal; float yDecimal; float zDecimal; if (point.X >= 0) { xDecimal = (int)point.X + 0.5f; } else { xDecimal = (int)point.X - 0.5f; } if (point.Y >= 0) { yDecimal = (int)point.Y + 0.5f; } else { yDecimal = (int)point.Y - 0.5f; } if (point.Z >= 0) { zDecimal = (int)point.Z + 0.5f; } else { zDecimal = (int)point.Z - 0.5f; } result = new Vector3(xDecimal, yDecimal, zDecimal); } return(result); }