PolygonCollider
namespace Griff.Scenes;
public sealed class PolygonCollider : Collider, IPointOutlineA shape for collisions with straight sides between its Points: a ramp, a wedge, a rock, a hill, a boat’s hull, a cup. Select it in the Scene view to drag its points, drag a plus on a side to add one, double-click one to take it off (Ctrl snaps to half units); Entity > Fit Polygon Collider to Sprite traces its sprite’s outline. A concave shape (an L, a cup) is split into convex pieces by itself. Points that don’t make a proper shape (fewer than three, all in a line, sides crossing) are fixed up and said so above its fields. Like a box, it stays upright whatever the entity’s rotation unless its BodyRotates: then it turns with the entity, round its middle, and tips, rolls over and stacks as it should.
This is a component: its saved properties are on its component page.
Properties
Section titled “Properties”GivenPoints
Section titled “GivenPoints”public IReadOnlyList<Vector2> GivenPoints { get; }The points as they’re written in Points, in its own units.
IgnoreLayers
Section titled “IgnoreLayers”public override string? IgnoreLayers { get; set; }IsTrigger
Section titled “IsTrigger”public override bool IsTrigger { get; set; }public override string? Layer { get; set; }Offset
Section titled “Offset”public override Vector2 Offset { get; set; }Outline
Section titled “Outline”public IReadOnlyList<Vector2> Outline { get; }The corners of the shape it is, in its own units, anticlockwise: its Points, without repeated points or points along a straight side, or as it’s fixed up when they don’t make a proper shape (see Problem).
PieceCount
Section titled “PieceCount”public int PieceCount { get; }How many convex pieces it’s made of: 1 for a convex shape, more for a concave one.
Points
Section titled “Points”public string? Points { get; set; }Its corners in order round it, as “x y” pairs separated by semicolons, in world units before the transform’s scale, round the entity (plus Offset): “-1 0; 1 0; 0 1” is a triangle 2 wide and 1 tall. They can go round either way.
Problem
Section titled “Problem”public string? Problem { get; }What’s wrong with its Points and how it’s been fixed up, e.g. “Its sides cross each other, so it’s taken as the shape round all its points.”; null when they make a proper shape.
WorldBounds
Section titled “WorldBounds”public override Box WorldBounds { get; }WorldCentroid
Section titled “WorldCentroid”public Vector2 WorldCentroid { get; }Where it balances in the world: what it turns round.
WorldPoints
Section titled “WorldPoints”public IReadOnlyList<Vector2> WorldPoints { get; }Its corners in the world, anticlockwise (as it’s turned, if it Turns).
Methods
Section titled “Methods”Contains
Section titled “Contains”public override bool Contains(Vector2 point)FormatPoints
Section titled “FormatPoints”public static string FormatPoints(IEnumerable<Vector2> points)Points written as Points takes them: “x y; x y; x y”, on hundredths.
FromWorld
Section titled “FromWorld”public Vector2 FromWorld(Vector2 point)Where a point in the world is in its own units (as in Points): the opposite of ToWorld.
PieceCorners
Section titled “PieceCorners”public IReadOnlyList<Vector2> PieceCorners(int piece)The corners of one of its convex pieces in the world, anticlockwise (from 0 to PieceCount less one): a concave shape is split into these.
SetupNote
Section titled “SetupNote”public override string? SetupNote()A line on its shape for the Inspector: what’s wrong with its points, if anything.
ToWorld
Section titled “ToWorld”public Vector2 ToWorld(Vector2 point)Where a point in its own units (as in Points) is in the world, as it’s placed and turned.
Fields
Section titled “Fields”DefaultPoints
Section titled “DefaultPoints”public const string DefaultPointsA new polygon’s points: a triangle 1 across.