private BoundingRectangle CalculateScreenSpaceBounds(IGeometry item, View view) { //Create a bounding box around this geometry var box = new BoundingBox(item.BoundingSphere); box.GetCorners(_corners); //Multiply box corners by WVP matrix to move into screen space for (int i = 0; i < _corners.Length; i++) { //Why is world matrix Identity? //THe bounding sphere is already in world space, we don't need to apply it again! _corners[i] = view.Viewport.Project(_corners[i].ToXNA(), view.Camera.Projection.ToXNA(), view.Camera.View.ToXNA(), Microsoft.Xna.Framework.Matrix.Identity).FromXNA(); _corners2D[i] = _corners[i].XY(); } //Find a rectangle around this box var rect = BoundingRectangle.CreateFromPoints(_corners2D); return rect; }
private BoundingRectangle CalculateScreenSpaceBounds(IGeometry item, View view) { //Create a bounding box around this geometry var box = new BoundingBox(item.BoundingSphere); var corners = box.GetCorners(); //Multiply box corners by WVP matrix to move into screen space for (int i = 0; i < corners.Length; i++) { corners[i] = view.Viewport.Project(corners[i].ToXNA(), view.Camera.Projection.ToXNA(), view.Camera.View.ToXNA(), Matrix.Identity).FromXNA(); } //Find a rectangle around this box var rect = BoundingRectangle.CreateFromPoints(corners.Select(a => a.XY())); return rect; }
public static void AddBoundingBox(BoundingBox box, Color color, float life = 0f) { // Get a DebugShape we can use to draw the box DebugShape shape = GetShapeForLines(12, life); // Get the corners of the box box.GetCorners(_corners); // Fill in the vertices for the bottom of the box shape.Vertices[0] = new VertexPositionColor(_corners[0].ToXNA(), color); shape.Vertices[1] = new VertexPositionColor(_corners[1].ToXNA(), color); shape.Vertices[2] = new VertexPositionColor(_corners[1].ToXNA(), color); shape.Vertices[3] = new VertexPositionColor(_corners[2].ToXNA(), color); shape.Vertices[4] = new VertexPositionColor(_corners[2].ToXNA(), color); shape.Vertices[5] = new VertexPositionColor(_corners[3].ToXNA(), color); shape.Vertices[6] = new VertexPositionColor(_corners[3].ToXNA(), color); shape.Vertices[7] = new VertexPositionColor(_corners[0].ToXNA(), color); // Fill in the vertices for the top of the box shape.Vertices[8] = new VertexPositionColor(_corners[4].ToXNA(), color); shape.Vertices[9] = new VertexPositionColor(_corners[5].ToXNA(), color); shape.Vertices[10] = new VertexPositionColor(_corners[5].ToXNA(), color); shape.Vertices[11] = new VertexPositionColor(_corners[6].ToXNA(), color); shape.Vertices[12] = new VertexPositionColor(_corners[6].ToXNA(), color); shape.Vertices[13] = new VertexPositionColor(_corners[7].ToXNA(), color); shape.Vertices[14] = new VertexPositionColor(_corners[7].ToXNA(), color); shape.Vertices[15] = new VertexPositionColor(_corners[4].ToXNA(), color); // Fill in the vertices for the vertical sides of the box shape.Vertices[16] = new VertexPositionColor(_corners[0].ToXNA(), color); shape.Vertices[17] = new VertexPositionColor(_corners[4].ToXNA(), color); shape.Vertices[18] = new VertexPositionColor(_corners[1].ToXNA(), color); shape.Vertices[19] = new VertexPositionColor(_corners[5].ToXNA(), color); shape.Vertices[20] = new VertexPositionColor(_corners[2].ToXNA(), color); shape.Vertices[21] = new VertexPositionColor(_corners[6].ToXNA(), color); shape.Vertices[22] = new VertexPositionColor(_corners[3].ToXNA(), color); shape.Vertices[23] = new VertexPositionColor(_corners[7].ToXNA(), color); }
/// <summary> /// Checks whether the current BoundingSphere contains the specified BoundingBox. /// </summary> /// <param name="box">The BoundingBox to test for overlap.</param><param name="result">[OutAttribute] Enumeration indicating the extent of overlap.</param> public void Contains(ref BoundingBox box, out ContainmentType result) { //check if all corner is in sphere bool inside = true; foreach (Vector3 corner in box.GetCorners()) { if (Contains(corner) == ContainmentType.Disjoint) { inside = false; break; } } if (inside) { result = ContainmentType.Contains; return; } //check if the distance from sphere center to cube face < radius double dmin = 0; if (Center.X < box.Min.X) dmin += (Center.X - box.Min.X) * (Center.X - box.Min.X); else if (Center.X > box.Max.X) dmin += (Center.X - box.Max.X) * (Center.X - box.Max.X); if (Center.Y < box.Min.Y) dmin += (Center.Y - box.Min.Y) * (Center.Y - box.Min.Y); else if (Center.Y > box.Max.Y) dmin += (Center.Y - box.Max.Y) * (Center.Y - box.Max.Y); if (Center.Z < box.Min.Z) dmin += (Center.Z - box.Min.Z) * (Center.Z - box.Min.Z); else if (Center.Z > box.Max.Z) dmin += (Center.Z - box.Max.Z) * (Center.Z - box.Max.Z); if (dmin <= Radius * Radius) { result = ContainmentType.Intersects; return; } //else disjoint result = ContainmentType.Disjoint; }