private static bool HasCollisionHelper(ref VoxelRay centerRay, Pipliz.Vector3Int pos, RayCastType type, ref RayHit.VoxelHit hit) { ushort hitType = default(ushort); if (!World.TryGetTypeAt(pos, out hitType)) { return(true); } if (hitType == 0) { return(false); } if (hitType == BuiltinBlocks.Indices.water) { return(false); } if ((type & RayCastType.HitBoxesSelection) != 0) { ItemTypes.ItemType itemType = ItemTypes.GetType(hitType); if (itemType.BoxColliders != null && itemType.CollideSelection) { Vector3 vector = pos.Vector; List <BoundsPip> boxes = itemType.BoxColliders; for (int i = 0; i < boxes.Count; i++) { BoundsPip bounds2 = boxes[i]; bounds2.Shift(pos.Vector); if (centerRay.Intersects(bounds2, out float hitDist2, out VoxelSide hitSides2) && hitDist2 < hit.Distance) { hit.TypeHit = hitType; hit.Distance = hitDist2; hit.VoxelPositionHit = pos; hit.VoxelSideHit = hitSides2; hit.BoundsHit = new RotatedBounds(boxes[i], Quaternion.identity); hit.BoundsCenter = pos.Vector; } } return(false); } } if ((type & RayCastType.HitNonSolidAsSolid) == 0 && !ItemTypes.Solids[hitType]) { return(false); } BoundsPip bounds = default(BoundsPip); bounds.SetCenterSize(pos.Vector, Vector3.one); if (centerRay.Intersects(bounds, out float hitDist, out VoxelSide hitSides) && hitDist < hit.Distance) { hit.TypeHit = hitType; hit.Distance = hitDist; hit.VoxelPositionHit = pos; hit.VoxelSideHit = hitSides; hit.BoundsHit = new RotatedBounds(Vector3.zero, Vector3.one, Quaternion.identity); hit.BoundsCenter = pos.Vector; } return(false); }
public bool Intersects(BoundsPip bounds, out float dist, out VoxelSide hitSide) { dist = 0f; hitSide = VoxelSide.None; float txmin; float tmin = txmin = (bounds.Min.x - source.x) / dirNormalized.x; float txmax; float tmax = txmax = (bounds.Max.x - source.x) / dirNormalized.x; if (tmin > tmax) { Helper.Swap(ref tmin, ref tmax); } float tymin = (bounds.Min.y - source.y) / dirNormalized.y; float tymax = (bounds.Max.y - source.y) / dirNormalized.y; if (tymin > tymax) { Helper.Swap(ref tymin, ref tymax); } if (tmin > tymax || tymin > tmax) { return(false); } if (tymin > tmin) { tmin = tymin; } if (tymax < tmax) { tmax = tymax; } float tzmin = (bounds.Min.z - source.z) / dirNormalized.z; float tzmax = (bounds.Max.z - source.z) / dirNormalized.z; if (tzmin > tzmax) { Helper.Swap(ref tzmin, ref tzmax); } if (tmin > tzmax || tzmin > tmax) { return(false); } if (tzmin > tmin) { tmin = tzmin; } if (tzmax < tmax) { } if (tmin < 0f) { return(false); } dist = tmin; if (tmin == txmin || tmin == txmax) { hitSide = ((dirNormalized.x < 0f) ? VoxelSide.xPlus : VoxelSide.xMin); } else if (tmin == tymin || tmin == tymax) { hitSide = ((dirNormalized.y < 0f) ? VoxelSide.yPlus : VoxelSide.yMin); } else { hitSide = ((dirNormalized.z < 0f) ? VoxelSide.zPlus : VoxelSide.zMin); } return(true); }