Skip to content

PolygonCollider

namespace Griff.Scenes;
public sealed class PolygonCollider : Collider, IPointOutline

A 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.

public IReadOnlyList<Vector2> GivenPoints { get; }

The points as they’re written in Points, in its own units.

public override string? IgnoreLayers { get; set; }
public override bool IsTrigger { get; set; }
public override string? Layer { get; set; }
public override Vector2 Offset { get; set; }
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).

public int PieceCount { get; }

How many convex pieces it’s made of: 1 for a convex shape, more for a concave one.

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.

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.

public override Box WorldBounds { get; }
public Vector2 WorldCentroid { get; }

Where it balances in the world: what it turns round.

public IReadOnlyList<Vector2> WorldPoints { get; }

Its corners in the world, anticlockwise (as it’s turned, if it Turns).

public override bool Contains(Vector2 point)
public static string FormatPoints(IEnumerable<Vector2> points)

Points written as Points takes them: “x y; x y; x y”, on hundredths.

public Vector2 FromWorld(Vector2 point)

Where a point in the world is in its own units (as in Points): the opposite of ToWorld.

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.

public override string? SetupNote()

A line on its shape for the Inspector: what’s wrong with its points, if anything.

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.

public const string DefaultPoints

A new polygon’s points: a triangle 1 across.