PlayRho  2.0.0
An interactive physics engine & library.
playrho::d2::Shape Class Reference

Shape. More...

#include <playrho/d2/Shape.hpp>

Public Member Functions

 Shape () noexcept=default
 Default constructor. More...
 
 Shape (const Shape &other)
 Copy constructor.
 
 Shape (Shape &&other) noexcept=default
 Move constructor.
 
template<typename T , typename Tp = DecayedTypeIfNotSame<T, Shape>, typename = std::enable_if_t<std::is_constructible_v<Tp, T>>>
 Shape (T &&arg)
 Initializing constructor for alternative types. More...
 
bool has_value () const noexcept
 Checks whether this instance contains a value.
 
Shapeoperator= (const Shape &other)
 Copy assignment.
 
Shapeoperator= (Shape &&other)=default
 Move assignment operator.
 
template<typename T , typename Tp = DecayedTypeIfNotSame<T, Shape>, typename = std::enable_if_t<std::is_constructible_v<Tp, T>>>
Shapeoperator= (T &&arg)
 Move assignment operator for alternative types. More...
 
void swap (Shape &other) noexcept
 Swap support.
 

Static Public Attributes

static constexpr auto DefaultDensity = NonNegative<AreaDensity>{0_kgpm2}
 Default density of a default-constructed, or otherwise value-less, shape.
 

Friends

DistanceProxy GetChild (const Shape &shape, ChildCounter index)
 Gets the "child" for the given index. More...
 
ChildCounter GetChildCount (const Shape &shape) noexcept
 Gets the number of child primitives of the shape. More...
 
const void * GetData (const Shape &shape) noexcept
 Gets a pointer to the underlying data. More...
 
NonNegative< AreaDensityGetDensity (const Shape &shape) noexcept
 Gets the density of the given shape. More...
 
Filter GetFilter (const Shape &shape) noexcept
 Gets the filter value for the given shape. More...
 
NonNegativeFF< RealGetFriction (const Shape &shape) noexcept
 Gets the coefficient of friction. More...
 
MassData GetMassData (const Shape &shape)
 Gets the mass properties of this shape using its dimensions and density. More...
 
Real GetRestitution (const Shape &shape) noexcept
 Gets the coefficient of restitution value of the given shape. More...
 
TypeID GetType (const Shape &shape) noexcept
 Gets the type info of the use of the given shape. More...
 
NonNegative< LengthGetVertexRadius (const Shape &shape, ChildCounter idx)
 Gets the vertex radius of the indexed child of the given shape. More...
 
bool IsSensor (const Shape &shape) noexcept
 Gets whether or not the given shape is a sensor. More...
 
bool operator!= (const Shape &lhs, const Shape &rhs) noexcept
 Inequality operator for shape to shape comparisons.
 
bool operator== (const Shape &lhs, const Shape &rhs) noexcept
 Equality operator for shape to shape comparisons.
 
void Rotate (Shape &shape, const UnitVec &value)
 Rotates all of the given shape's vertices by the given amount. More...
 
void Scale (Shape &shape, const Vec2 &value)
 Scales all of the given shape's vertices by the given amount. More...
 
void SetDensity (Shape &shape, NonNegative< AreaDensity > value)
 Sets the density of the given shape. More...
 
void SetFilter (Shape &shape, Filter value)
 Sets the filter value for the given shape. More...
 
void SetFriction (Shape &shape, NonNegative< Real > value)
 Sets the coefficient of friction. More...
 
void SetRestitution (Shape &shape, Real value)
 Sets the coefficient of restitution value of the given shape. More...
 
void SetSensor (Shape &shape, bool value)
 Sets whether or not the given shape is a sensor. More...
 
void SetVertexRadius (Shape &shape, ChildCounter idx, NonNegative< Length > value)
 Sets the vertex radius of the indexed child of the given shape. More...
 
void Translate (Shape &shape, const Length2 &value)
 Translates all of the given shape's vertices by the given amount. More...
 
template<typename T >
std::add_pointer_t< std::add_const_t< T > > TypeCast (const Shape *value) noexcept
 Converts the given shape into its current configuration value. More...
 

Related Functions

(Note that these are not member functions.)

AABB ComputeAABB (const Shape &shape, const Transformation &xf)
 Computes the AABB for the given shape with the given transformation.
 
NonNegativeFF< RealGetDefaultFriction (const Shape &a, const Shape &b)
 Gets the default friction amount for the given shapes.
 
Real GetDefaultRestitution (const Shape &a, const Shape &b)
 Gets the default restitution amount for the given shapes.
 
VertexCounter GetVertexCount (const Shape &shape, ChildCounter index)
 Gets the vertex count for the specified child of the given shape.
 
auto IsDestroyed (const Shape &value) noexcept -> bool
 Gets whether the given entity is in the is-destroyed state.
 
RayCastOutput RayCast (const Shape &shape, ChildCounter childIndex, const RayCastInput &input, const Transformation &transform) noexcept
 Cast a ray against the child of the given shape. More...
 
bool ShouldCollide (const Shape &a, const Shape &b) noexcept
 Whether contact calculations should be performed between the two instances. More...
 
bool TestPoint (const Shape &shape, const Length2 &point) noexcept
 Test a point for containment in the given shape. More...
 
template<typename T >
TypeCast (const Shape &value)
 Casts the specified instance into the template specified type. More...
 

Detailed Description

Shape.

A shape is used for collision detection. You can create a shape from any supporting type. Shapes are conceptually made up of zero or more convex child shapes where each child shape is made up of zero or more vertices and an associated radius called its "vertex radius" or "skin".

Note
This class's design provides a "polymorphic value type" offering polymorphism without public inheritance. This is based on a technique that's described by Sean Parent in his January 2017 Norwegian Developers Conference London talk "Better Code: Runtime Polymorphism". With this implementation, different shapes types can be had by constructing instances of this class with the different types that provide the required support. Different shapes of a given type meanwhile are had by providing different values for the type.
A shape can be constructed from or have its value set to any value whose type T satisfies the requirement that IsValidShapeTypeV<T> == true.
See also
https://youtu.be/QGcVXgEVMJg
https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Polymorphic_Value_Types
Examples
Compositor.cpp.

Constructor & Destructor Documentation

◆ Shape() [1/2]

playrho::d2::Shape::Shape ( )
defaultnoexcept

Default constructor.

Postcondition
has_value() returns false.

Referenced by operator=().

◆ Shape() [2/2]

template<typename T , typename Tp = DecayedTypeIfNotSame<T, Shape>, typename = std::enable_if_t<std::is_constructible_v<Tp, T>>>
playrho::d2::Shape::Shape ( T &&  arg)
inlineexplicit

Initializing constructor for alternative types.

Parameters
argValue to construct a shape instance for.
Note
See the class notes section for an explanation of requirements on a type T for its values to be fully valid candidates for this function.
The IsValidShapeType trait is intentionally not used to eliminate this function from resolution so that the compiler may offer insight into exactly which requirements are not met by the given type.
Postcondition
has_value() returns true.
Exceptions
std::bad_allocif there's a failure allocating storage.

Member Function Documentation

◆ operator=()

template<typename T , typename Tp = DecayedTypeIfNotSame<T, Shape>, typename = std::enable_if_t<std::is_constructible_v<Tp, T>>>
Shape& playrho::d2::Shape::operator= ( T &&  arg)
inline

Move assignment operator for alternative types.

Note
See the class notes section for an explanation of requirements on a type T for its values to be fully valid candidates for this function.
The IsValidShapeType trait is intentionally not used to eliminate this function from resolution so that the compiler may offer insight into exactly which requirements are not met by the given type.
Postcondition
has_value() returns true.

Friends And Related Function Documentation

◆ GetChild

DistanceProxy GetChild ( const Shape shape,
ChildCounter  index 
)
friend

Gets the "child" for the given index.

Parameters
shapeShape to get "child" shape of.
indexIndex to a child element of the shape. Value must be less than the number of child primitives of the shape.
Warning
The shape must remain in scope while the proxy is in use!
Exceptions
InvalidArgumentif the given index is out of range.
See also
GetChildCount

◆ GetChildCount

ChildCounter GetChildCount ( const Shape shape)
friend

Gets the number of child primitives of the shape.

Returns
Non-negative count.

◆ GetData

const void* GetData ( const Shape shape)
friend

Gets a pointer to the underlying data.

Note
Provided for introspective purposes like visitation.

◆ GetDensity

NonNegative<AreaDensity> GetDensity ( const Shape shape)
friend

Gets the density of the given shape.

Returns
Non-negative density (in mass per area).
See also
SetDensity(Shape& shape, NonNegative<AreaDensity> value).

◆ GetFilter

Filter GetFilter ( const Shape shape)
friend

Gets the filter value for the given shape.

Returns
Filter for the given shape or the default filter is the shape has no value.
See also
SetFilter(Shape& shape, Filter value);.

◆ GetFriction

NonNegativeFF<Real> GetFriction ( const Shape shape)
friend

Gets the coefficient of friction.

Returns
Value of 0 or higher.
See also
SetFriction(Shape& shape, NonNegative<Real> value).

◆ GetMassData

MassData GetMassData ( const Shape shape)
friend

Gets the mass properties of this shape using its dimensions and density.

Returns
Mass data for this shape.

◆ GetRestitution

Real GetRestitution ( const Shape shape)
friend

Gets the coefficient of restitution value of the given shape.

See also
SetRestitution(Shape& shape, Real value).

◆ GetType

TypeID GetType ( const Shape shape)
friend

Gets the type info of the use of the given shape.

Note
This is not the same as calling GetTypeID<Shape>().
Returns
Type info of the underlying value's type.

◆ GetVertexRadius

NonNegative<Length> GetVertexRadius ( const Shape shape,
ChildCounter  idx 
)
friend

Gets the vertex radius of the indexed child of the given shape.

This gets the radius from the vertex that the shape's "skin" should extend outward by. While any edges - line segments between multiple vertices - are straight, corners between them (the vertices) are rounded and treated as rounded. Shapes with larger vertex radiuses compared to edge lengths therefore will be more prone to rolling or having other shapes more prone to roll off of them. Here's an image of a shape configured via a PolygonShapeConf with it's skin drawn:

Parameters
shapeShape to get child's vertex radius for.
idxChild index to get vertex radius for.
See also
UseVertexRadius
Exceptions
InvalidArgumentif the child index is not less than the child count.

◆ IsSensor

bool IsSensor ( const Shape shape)
friend

Gets whether or not the given shape is a sensor.

See also
SetSensor(Shape& shape, bool value).

◆ Rotate

void Rotate ( Shape shape,
const UnitVec value 
)
friend

Rotates all of the given shape's vertices by the given amount.

Note
This may throw std::bad_alloc or any exception that's thrown by the constructor for the model's underlying data type.
Exceptions
std::bad_allocif there's a failure allocating storage.

◆ Scale

void Scale ( Shape shape,
const Vec2 value 
)
friend

Scales all of the given shape's vertices by the given amount.

Note
This may throw std::bad_alloc or any exception that's thrown by the constructor for the model's underlying data type.
Exceptions
std::bad_allocif there's a failure allocating storage.

◆ SetDensity

void SetDensity ( Shape shape,
NonNegative< AreaDensity value 
)
friend

Sets the density of the given shape.

See also
GetDensity.

◆ SetFilter

void SetFilter ( Shape shape,
Filter  value 
)
friend

Sets the filter value for the given shape.

See also
GetFilter(const Shape& shape).

◆ SetFriction

void SetFriction ( Shape shape,
NonNegative< Real value 
)
friend

Sets the coefficient of friction.

See also
GetFriction(const Shape& shape).

◆ SetRestitution

void SetRestitution ( Shape shape,
Real  value 
)
friend

Sets the coefficient of restitution value of the given shape.

See also
GetRestitution(const Shape& shape).

◆ SetSensor

void SetSensor ( Shape shape,
bool  value 
)
friend

Sets whether or not the given shape is a sensor.

See also
IsSensor(const Shape& shape).

◆ SetVertexRadius

void SetVertexRadius ( Shape shape,
ChildCounter  idx,
NonNegative< Length value 
)
friend

Sets the vertex radius of the indexed child of the given shape.

Exceptions
InvalidArgumentif the vertex radius cannot be set to the specified value.
See also
GetVertexRadius(const Shape& shape, ChildCounter idx).

◆ ShouldCollide()

bool ShouldCollide ( const Shape a,
const Shape b 
)
related

Whether contact calculations should be performed between the two instances.

Returns
true if contact calculations should be performed between these two instances; false otherwise.

◆ Translate

void Translate ( Shape shape,
const Length2 value 
)
friend

Translates all of the given shape's vertices by the given amount.

Note
This may throw std::bad_alloc or any exception that's thrown by the constructor for the model's underlying data type.
Exceptions
std::bad_allocif there's a failure allocating storage.

◆ TypeCast() [1/2]

template<typename T >
T TypeCast ( const Shape value)
related

Casts the specified instance into the template specified type.

Exceptions
std::bad_castIf the template specified type is not the type of data underlying the given instance.
See also
GetType(const Shape&)

◆ TypeCast [2/2]

template<typename T >
std::add_pointer_t<std::add_const_t<T> > TypeCast ( const Shape value)
friend

Converts the given shape into its current configuration value.

Note
The design for this was based off the design of the C++17 std::any class and its associated std::any_cast function. The code for this is based off of the std::any code from the LLVM Project.
See also
https://llvm.org/
GetType(const Shape&)

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