Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
Collider2D Class Reference

A 2D collider base that defines the shape and physical properties of a GameObject for collision detection. More...

#include <collider_2d.h>

Inheritance diagram for Collider2D:
[legend]

Public Member Functions

 Collider2D (float friction=default_friction_2d, float bounciness=default_bounciness_2d, Point offset={0.0f, 0.0f})
 
 ~Collider2D () override=default
 
void update (float dt) override
 
void on_trigger_enter (Collider2D &other)
 Called when another collider enters this collider's trigger area.
 
size_t add_on_trigger_enter (const std::function< void(Collider2D &, Collider2D &)> &action)
 
void remove_on_trigger_enter (int index)
 
void on_trigger_exit (Collider2D &other)
 Called when another collider exits this collider's trigger area.
 
size_t add_on_trigger_exit (const std::function< void(Collider2D &, Collider2D &)> &action)
 
void remove_on_trigger_exit (int index)
 
void on_collision_enter (Collider2D &other)
 Called when another collider exits this collider's trigger area.
 
size_t add_on_collision_enter (const std::function< void(Collider2D &, Collider2D &)> &action)
 
void remove_on_collision_enter (int index)
 
void on_collision_exit (Collider2D &other)
 Called when another collider exits this collider's trigger area.
 
size_t add_on_collision_exit (const std::function< void(Collider2D &, Collider2D &)> &action)
 
void remove_on_collision_exit (int index)
 
BodyDistance2D distance (Collider2D &other, bool use_fixture=false)
 Calculate the distance to another collider.
 
ColliderRayResult raycast (const Point &direction, float max_distance)
 Perform a raycast from this collider in a specified direction.
 
std::reference_wrapper< Rigidbody2Dget_rigidbody ()
 
float friction () const noexcept
 
virtual Collider2Dfriction (float value) noexcept
 
float bounciness () const noexcept
 
virtual Collider2Dbounciness (float value) noexcept
 
PhysicsCreationFlagscreation_flags () noexcept
 
Collider2Dcreation_flags (PhysicsCreationFlags value) noexcept
 
virtual Point offset () const noexcept
 
virtual Collider2Doffset (Point value) noexcept
 
std::string type_name () const override
 Provides a consistent type name for the component.
 
- Public Member Functions inherited from Renderable
 Renderable ()
 
 Renderable (int layer)
 
 ~Renderable () override=default
 
Renderableorder_in_layer (int layer)
 
int order_in_layer () const
 
void set_render_strategy (Component &component)
 
virtual IRenderingStrategyrender_strategy () const
 
std::string type_name () const override
 Provides a consistent type name for the component.
 
Renderabledisable_draw () noexcept
 
Renderableenable_draw () noexcept
 
bool should_draw () const noexcept
 
- Public Member Functions inherited from Component
 Component ()
 
virtual ~Component ()=default
 
bool active () const noexcept
 Checks if the component is active.
 
Componentactive (bool value) noexcept
 
bool marked_for_deletion () const noexcept
 Activates the component.
 
Componentmark_for_deletion () noexcept
 
virtual void on_attach ()
 Called when the component is attached to a GameObject.
 
virtual void on_detach ()
 Called when the component is detached from a GameObject.
 
virtual void on_serialize (std::vector< uint8_t > &) const
 Serializes the component's state into a byte array.
 
virtual void on_deserialize (const std::vector< uint8_t > &, size_t &)
 Deserializes the component's state from a byte array.
 
const std::optional< std::reference_wrapper< GameObject > > & parent () const noexcept
 Gets the parent GameObject of this component.
 
std::optional< std::reference_wrapper< GameObject > > & parent () noexcept
 Gets the parent GameObject of this component.
 
Componentparent (GameObject &parent)
 
Componentparent (std::nullopt_t nullopt)
 
size_t add_on_attach (const std::function< void(Component &)> &action)
 Retrieves the type information of the component.
 
void remove_on_attach (size_t index)
 
size_t add_on_detach (const std::function< void(Component &)> &action)
 Adds an action to be performed when the component is attached.
 
void remove_on_detach (size_t index)
 

Protected Attributes

PhysicsCreationFlags creation_flags_ {}
 
float friction_
 
float bounciness_
 
Point offset_
 
- Protected Attributes inherited from Renderable
std::unique_ptr< IRenderingStrategyrender_strategy_
 
int ordering_layer_ = 0
 
bool draw {true}
 

Detailed Description

A 2D collider base that defines the shape and physical properties of a GameObject for collision detection.

Note
This is an abstract base class and should be inherited by specific collider shapes like CircleCollider2D, BoxCollider2D, etc.

This component allows a GameObject to participate in collision detection and response within the physics engine. It provides methods for handling collision events and querying collision information.

Note
Users should derive from this class to implement specific collider shapes.

Constructor & Destructor Documentation

◆ Collider2D()

Collider2D::Collider2D ( float  friction = default_friction_2d,
float  bounciness = default_bounciness_2d,
Point  offset = {0.0f, 0.0f} 
)

◆ ~Collider2D()

Collider2D::~Collider2D ( )
overridedefault

Member Function Documentation

◆ add_on_collision_enter()

size_t Collider2D::add_on_collision_enter ( const std::function< void(Collider2D &, Collider2D &)> &  action)

◆ add_on_collision_exit()

size_t Collider2D::add_on_collision_exit ( const std::function< void(Collider2D &, Collider2D &)> &  action)

◆ add_on_trigger_enter()

size_t Collider2D::add_on_trigger_enter ( const std::function< void(Collider2D &, Collider2D &)> &  action)

◆ add_on_trigger_exit()

size_t Collider2D::add_on_trigger_exit ( const std::function< void(Collider2D &, Collider2D &)> &  action)

◆ bounciness() [1/2]

float Collider2D::bounciness ( ) const
noexcept

◆ bounciness() [2/2]

virtual Collider2D & Collider2D::bounciness ( float  value)
virtualnoexcept

Reimplemented in BoxCollider2D, and CircleCollider2D.

◆ creation_flags() [1/2]

PhysicsCreationFlags & Collider2D::creation_flags ( )
noexcept

◆ creation_flags() [2/2]

Collider2D & Collider2D::creation_flags ( PhysicsCreationFlags  value)
noexcept

◆ distance()

BodyDistance2D Collider2D::distance ( Collider2D other,
bool  use_fixture = false 
)

Calculate the distance to another collider.

Parameters
otherThe other collider to measure distance to.
use_fixtureWhether to use fixture shapes for distance calculation.
Returns
BodyDistance2D The distance information between the two colliders.

◆ friction() [1/2]

float Collider2D::friction ( ) const
noexcept

◆ friction() [2/2]

virtual Collider2D & Collider2D::friction ( float  value)
virtualnoexcept

Reimplemented in BoxCollider2D, and CircleCollider2D.

◆ get_rigidbody()

std::reference_wrapper< Rigidbody2D > Collider2D::get_rigidbody ( )

◆ offset() [1/2]

virtual Point Collider2D::offset ( ) const
virtualnoexcept

Reimplemented in BoxCollider2D, and CircleCollider2D.

◆ offset() [2/2]

virtual Collider2D & Collider2D::offset ( Point  value)
virtualnoexcept

Reimplemented in BoxCollider2D, and CircleCollider2D.

◆ on_collision_enter()

void Collider2D::on_collision_enter ( Collider2D other)

Called when another collider exits this collider's trigger area.

Note
This method can be overridden to implement custom behavior on trigger exit events.
Parameters
otherThe other collider that exited the trigger.

◆ on_collision_exit()

void Collider2D::on_collision_exit ( Collider2D other)

Called when another collider exits this collider's trigger area.

Note
This method can be overridden to implement custom behavior on trigger exit events.
Parameters
otherThe other collider that exited the trigger.

◆ on_trigger_enter()

void Collider2D::on_trigger_enter ( Collider2D other)

Called when another collider enters this collider's trigger area.

Note
This method can be overridden to implement custom behavior on trigger enter events.
Parameters
otherThe other collider that entered the trigger.

◆ on_trigger_exit()

void Collider2D::on_trigger_exit ( Collider2D other)

Called when another collider exits this collider's trigger area.

Note
This method can be overridden to implement custom behavior on trigger exit events.
Parameters
otherThe other collider that exited the trigger.

◆ raycast()

ColliderRayResult Collider2D::raycast ( const Point direction,
float  max_distance 
)

Perform a raycast from this collider in a specified direction.

Parameters
directionThe direction of the raycast.
max_distanceThe maximum distance the ray should check for collisions.
Returns
ColliderRayResult The result of the raycast.

◆ remove_on_collision_enter()

void Collider2D::remove_on_collision_enter ( int  index)

◆ remove_on_collision_exit()

void Collider2D::remove_on_collision_exit ( int  index)

◆ remove_on_trigger_enter()

void Collider2D::remove_on_trigger_enter ( int  index)

◆ remove_on_trigger_exit()

void Collider2D::remove_on_trigger_exit ( int  index)

◆ type_name()

std::string Collider2D::type_name ( ) const
overridevirtual

Provides a consistent type name for the component.

Returns
A string representing the type name of the component. @CAUTION: All derived components must implement this method to ensure consistent serialization across different compilers and platforms.

Implements Component.

◆ update()

void Collider2D::update ( float  dt)
overridevirtual

Implements Component.

Member Data Documentation

◆ bounciness_

float Collider2D::bounciness_
protected

◆ creation_flags_

PhysicsCreationFlags Collider2D::creation_flags_ {}
protected

◆ friction_

float Collider2D::friction_
protected

◆ offset_

Point Collider2D::offset_
protected

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