class

CP::Shape::Poly

Inherits Indexable / Enumerable / Iterable / Enumerable / CP::Shape / Reference / Object

A convex polygon shape

Slowest, but most flexible collision shape.

Constructors

new(body : Body | Nil, verts : Array(Vect) | Slice(Vect), transform : Transform = Transform::IDENTITY, radius : Number = 0)

Initialize a polygon shape with rounded corners. A convex hull will be created from the vertices.

The parameters are: the body to attach the poly to; the verts (vertices) of the polygon; the transform to apply to every vertex; the radius of the corners.

Adding a small radius will bevel the corners and can significantly reduce problems where the poly gets stuck on seams in your geometry.

Source
new(body : Body | Nil, verts : Array(Vect) | Slice(Vect), radius : Number)

Initialize a polygon shape with rounded corners. The vertices must be convex with a counter-clockwise winding.

Source

Class methods

area(verts : Array(Vect) | Slice(Vect), radius : Number = 0) : Float64

Calculate the signed area of a polygon.

A clockwise winding gives positive area. This is probably backwards from what you expect, but matches Chipmunk's winding for poly shapes.

Source
centroid(verts : Array(Vect) | Slice(Vect)) : Vect

Calculate the natural centroid of a polygon.

Source
convex_hull(verts : Array(Vect) | Slice(Vect), tol : Number = 0) : Tuple(Slice(Vect), Int32)

Calculate the convex hull of a given set of points.

tol is the allowed amount to shrink the hull when simplifying it. A tolerance of 0.0 creates an exact hull.

Returns the convex hull and the index where the first vertex in the hull came from (i.e. verts[first] == result[0])

Source
moment(m : Number, verts : Array(Vect) | Slice(Vect), offset : Vect = CP::Vect.new(0, 0), radius : Number = 0) : Float64

Calculate the moment of inertia for a solid polygon shape.

Assumes its center of gravity is at its centroid. The offset is added to each vertex.

Source

Instance methods

radius

Get the radius of a polygon shape.

Source
radius=(radius : Number)

Unsafe. Set the radius of a poly shape.

This change is only picked up as a change to the position of the shape's surface, but not its velocity. Changing it will not result in realistic physical behavior. Only use if you know what you are doing!

Source
set_verts(verts : Array(Vect) | Slice(Vect), transform : Transform = Transform::IDENTITY)

Unsafe. Set the vertices of a poly shape.

This change is only picked up as a change to the position of the shape's surface, but not its velocity. Changing it will not result in realistic physical behavior. Only use if you know what you are doing!

Source
size

Get the number of verts in a polygon shape.

Source
unsafe_fetch(index : Int) : Vect

Get the i-th vertex of a polygon shape.

Source