-
Notifications
You must be signed in to change notification settings - Fork 0
Core.Data.RectF
Dennis Steffen edited this page Jan 4, 2026
·
1 revision
The RectF structure, located in the Meatcorps.Engine.Core.Data namespace, is a lightweight, floating-point representation of a 2D rectangle. It is a fundamental data type used throughout the engine for spatial calculations, UI layout, and collision detection.
RectF defines a rectangular area using four primary components:
- X: The horizontal position of the top-left corner.
- Y: The vertical position of the top-left corner.
- Width: The horizontal span of the rectangle.
- Height: The vertical span of the rectangle.
The structure is marked with [DataContract], making it suitable for serialization, and implements IEquatable<RectF> for efficient comparisons.
-
Left / Top: Returns the
XandYcoordinates respectively. -
Right / Bottom: Calculates the far edges (
X + WidthandY + Height).
-
Position: Gets or sets the top-left corner as a
Vector2. -
Size: Gets or sets the dimensions as a
SizeF. -
Center: Returns the
Vector2representing the exact middle of the rectangle. -
Corners: Provides quick access to
TopLeft,TopRight,BottomLeft, andBottomRightasVector2points.
- Collision Detection: Determining if two objects overlap or if a point (like a mouse cursor) is within a specific boundary.
- UI Layout: Defining the bounds of buttons, panels, or text areas.
- Camera/Frustum Culling: Calculating the bounding box of a group of objects to determine if they are visible on screen.
- Physics & Spatial Queries: Finding the closest point on a boundary or measuring distances to objects.
Checking if a point (e.g., a mouse click) is inside a UI element.
using Meatcorps.Engine.Core.Data;
using System.Numerics;
// Create a rectangle at (10, 10) with a size of 100x50
RectF buttonBounds = new RectF(10f, 10f, 100f, 50f);
Vector2 clickPosition = new Vector2(50f, 25f);
if (buttonBounds.Contains(clickPosition))
{
Console.WriteLine("Button clicked!");
}Checking if two game objects are overlapping.
RectF playerBounds = new RectF(0f, 0f, 32f, 32f);
RectF enemyBounds = new RectF(20f, 20f, 32f, 32f);
if (playerBounds.Intersects(enemyBounds))
{
Console.WriteLine("Collision detected!");
}
// Get the actual overlapping area
RectF overlap = RectF.Intersection(playerBounds, enemyBounds);Using Inflate to grow a rectangle outward in all directions.
RectF zone = new RectF(100f, 100f, 50f, 50f);
// Expand by 10 units on both sides (Total width becomes 70, height 70)
// The X and Y will shift to (90, 90) to keep it centered
zone.Inflate(10f, 10f);Finding a single rectangle that encompasses multiple other rectangles.
RectF rect1 = new RectF(0, 0, 10, 10);
RectF rect2 = new RectF(50, 50, 10, 10);
RectF combined = RectF.RectBounds(rect1, rect2);
// combined will start at (0,0) and have a width/height of 60-
Precision: The structure uses
float. For comparisons, it utilizes an extension methodEqualsSafe(likely handling small floating-point epsilon differences) to ensure stability. -
Memory: As a
struct, it is passed by value. For performance-critical code, many methods offerrefandoutoverloads (e.g.,Intersects(ref RectF first, ref RectF second)) to avoid unnecessary copying. -
Conversion: It provides implicit conversion from
Rect(integer-based) and explicit conversion back toRect.