Box2D  3.0.0
A Real-Time-Oriented 2-D Physics Engine
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
box2d::PolygonShape Class Reference

Polygon shape. More...

#include <PolygonShape.hpp>

Inheritance diagram for box2d::PolygonShape:
box2d::Shape

Classes

struct  Conf
 

Public Types

using vertex_count_t = std::uint8_t
 Vertex count type. More...
 

Public Member Functions

 PolygonShape (const Conf &conf=GetDefaultConf()) noexcept
 Default constructor. More...
 
 PolygonShape (const PolygonShape &)=default
 
 PolygonShape (Length hx, Length hy, const Conf &conf=GetDefaultConf()) noexcept
 Initializing constructor for rectangles. More...
 
 PolygonShape (Span< const Length2D > points, const Conf &conf=GetDefaultConf()) noexcept
 Creates a convex hull from the given array of local points. The size of the span must be in the range [1, MaxShapeVertices]. More...
 
child_count_t GetChildCount () const noexcept override
 Gets the number of child primitives. More...
 
DistanceProxy GetChild (child_count_t index) const noexcept override
 Gets the child for the given index. More...
 
bool TestPoint (const Transformation &xf, const Length2D p) const noexcept override
 Tests a point for containment in this shape. More...
 
MassData GetMassData () const noexcept override
 Computes the mass properties of this shape using its dimensions and density. The inertia tensor is computed about the local origin. More...
 
RayCastOutput RayCast (const RayCastInput &input, const Transformation &xf, child_count_t childIndex) const noexcept override
 Cast a ray against a child shape. More...
 
void Accept (Visitor &visitor) const override
 Accepts a visitor. More...
 
void Set (Span< const Length2D > points) noexcept
 Creates a convex hull from the given array of local points. The size of the span must be in the range [1, MaxShapeVertices]. More...
 
void Set (const VertexSet &points) noexcept
 Creates a convex hull from the given set of local points. The size of the set must be in the range [1, MaxShapeVertices]. More...
 
void SetAsBox (Length hx, Length hy) noexcept
 Build vertices to represent an axis-aligned box centered on the local origin. More...
 
void Transform (Transformation xfm) noexcept
 
vertex_count_t GetVertexCount () const noexcept
 Gets the vertex count. More...
 
Length2D GetVertex (vertex_count_t index) const
 Gets a vertex by index. More...
 
UnitVec2 GetNormal (vertex_count_t index) const
 Gets a normal by index. More...
 
Span< const Length2DGetVertices () const noexcept
 Gets the span of vertices. More...
 
Span< const UnitVec2GetNormals () const noexcept
 
Length2D GetCentroid () const noexcept
 
- Public Member Functions inherited from box2d::Shape
 Shape ()=delete
 Default constructor is deleted. More...
 
 Shape (const Conf &conf) noexcept
 Initializing constructor. More...
 
 Shape (const Shape &)=default
 
virtual ~Shape ()=default
 
Length GetVertexRadius () const noexcept
 Gets the vertex radius. More...
 
void SetVertexRadius (Length vertexRadius)
 
Density GetDensity () const noexcept
 Gets the density of this fixture. More...
 
void SetDensity (Density density) noexcept
 Sets the density of this fixture. More...
 
RealNum GetFriction () const noexcept
 Gets the coefficient of friction. More...
 
void SetFriction (RealNum friction) noexcept
 Sets the coefficient of friction. This will not change the friction of existing contacts. More...
 
RealNum GetRestitution () const noexcept
 Gets the coefficient of restitution. More...
 
void SetRestitution (RealNum restitution) noexcept
 Sets the coefficient of restitution. This will not change the restitution of existing contacts. More...
 

Static Public Member Functions

static constexpr Length GetDefaultVertexRadius () noexcept
 
static constexpr Conf GetDefaultConf () noexcept
 

Static Public Attributes

static constexpr auto InvalidVertex = static_cast<vertex_count_t>(-1)
 

Detailed Description

Polygon shape.

A convex polygon. The interior of the polygon is to the left of each edge. Polygons have a maximum number of vertices equal to MaxShapeVertices. In most cases you should not need many vertices for a convex polygon.

Note
This data structure is 64-bytes large (with 4-byte RealNum).

Member Typedef Documentation

◆ vertex_count_t

Vertex count type.

Note
This type must not support more than 255 vertices as that would conflict with the ContactFeature::index_t type.

Constructor & Destructor Documentation

◆ PolygonShape() [1/4]

box2d::PolygonShape::PolygonShape ( const Conf conf = GetDefaultConf())
inlineexplicitnoexcept

Default constructor.

Constructs a polygon shape with a 0,0 centroid and vertex count of 0.

Note
Polygons with a vertex count less than 1 are "degenerate" and should be treated as invalid.

◆ PolygonShape() [2/4]

box2d::PolygonShape::PolygonShape ( const PolygonShape )
default

◆ PolygonShape() [3/4]

PolygonShape::PolygonShape ( Length  hx,
Length  hy,
const Conf conf = GetDefaultConf() 
)
explicitnoexcept

Initializing constructor for rectangles.

Parameters
hxthe half-width.
hythe half-height.
confConfiguration data for the shape.

◆ PolygonShape() [4/4]

PolygonShape::PolygonShape ( Span< const Length2D points,
const Conf conf = GetDefaultConf() 
)
explicitnoexcept

Creates a convex hull from the given array of local points. The size of the span must be in the range [1, MaxShapeVertices].

Warning
the points may be re-ordered, even if they form a convex polygon
collinear points are handled but not removed. Collinear points may lead to poor stacking behavior.

Member Function Documentation

◆ Accept()

void box2d::PolygonShape::Accept ( Visitor visitor) const
inlineoverridevirtual

Accepts a visitor.

Implements box2d::Shape.

◆ GetCentroid()

Length2D box2d::PolygonShape::GetCentroid ( ) const
inlinenoexcept

◆ GetChild()

DistanceProxy box2d::PolygonShape::GetChild ( child_count_t  index) const
inlineoverridevirtualnoexcept

Gets the child for the given index.

Note
The shape must remain in scope while the proxy is in use.

Implements box2d::Shape.

◆ GetChildCount()

child_count_t box2d::PolygonShape::GetChildCount ( ) const
inlineoverridevirtualnoexcept

Gets the number of child primitives.

Returns
Positive non-zero count.

Implements box2d::Shape.

◆ GetDefaultConf()

static constexpr Conf box2d::PolygonShape::GetDefaultConf ( )
inlinestaticnoexcept

◆ GetDefaultVertexRadius()

static constexpr Length box2d::PolygonShape::GetDefaultVertexRadius ( )
inlinestaticnoexcept

◆ GetMassData()

MassData PolygonShape::GetMassData ( ) const
overridevirtualnoexcept

Computes the mass properties of this shape using its dimensions and density. The inertia tensor is computed about the local origin.

Note
Behavior is undefined if the density is negative.
Returns
Mass data for this shape.

Implements box2d::Shape.

◆ GetNormal()

UnitVec2 box2d::PolygonShape::GetNormal ( vertex_count_t  index) const
inline

Gets a normal by index.

These are 90-degree clockwise-rotated (outward-facing) unit-vectors of the edges defined by consecutive pairs of vertices starting with vertex 0.

Parameters
indexIndex of the normal to get.
Returns
Normal for the given index.

◆ GetNormals()

Span<const UnitVec2> box2d::PolygonShape::GetNormals ( ) const
inlinenoexcept

◆ GetVertex()

Length2D box2d::PolygonShape::GetVertex ( vertex_count_t  index) const
inline

Gets a vertex by index.

Vertices go counter-clockwise.

◆ GetVertexCount()

PolygonShape::vertex_count_t box2d::PolygonShape::GetVertexCount ( ) const
inlinenoexcept

Gets the vertex count.

Returns
value between 0 and MaxShapeVertices inclusive.
See also
MaxShapeVertices.

◆ GetVertices()

Span<const Length2D> box2d::PolygonShape::GetVertices ( ) const
inlinenoexcept

Gets the span of vertices.

Vertices go counter-clockwise.

◆ RayCast()

RayCastOutput PolygonShape::RayCast ( const RayCastInput input,
const Transformation xf,
child_count_t  childIndex 
) const
overridevirtualnoexcept

Cast a ray against a child shape.

Parameters
inputthe ray-cast input parameters.
xfthe transform to be applied to the shape.
childIndexthe child shape index

Implements box2d::Shape.

◆ Set() [1/2]

void PolygonShape::Set ( Span< const Length2D points)
noexcept

Creates a convex hull from the given array of local points. The size of the span must be in the range [1, MaxShapeVertices].

Warning
the points may be re-ordered, even if they form a convex polygon
collinear points are handled but not removed. Collinear points may lead to poor stacking behavior.

◆ Set() [2/2]

void PolygonShape::Set ( const VertexSet points)
noexcept

Creates a convex hull from the given set of local points. The size of the set must be in the range [1, MaxShapeVertices].

Warning
the points may be re-ordered, even if they form a convex polygon
collinear points are handled but not removed. Collinear points may lead to poor stacking behavior.

◆ SetAsBox()

void PolygonShape::SetAsBox ( Length  hx,
Length  hy 
)
noexcept

Build vertices to represent an axis-aligned box centered on the local origin.

Parameters
hxthe half-width.
hythe half-height.

◆ TestPoint()

bool PolygonShape::TestPoint ( const Transformation xf,
const Length2D  p 
) const
overridevirtualnoexcept

Tests a point for containment in this shape.

Parameters
xfthe shape world transform.
pa point in world coordinates.
Returns
true if point is contained in this shape, false otherwise.

Implements box2d::Shape.

◆ Transform()

void PolygonShape::Transform ( box2d::Transformation  xf)
noexcept

Member Data Documentation

◆ InvalidVertex

constexpr auto box2d::PolygonShape::InvalidVertex = static_cast<vertex_count_t>(-1)
static

The documentation for this class was generated from the following files: