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

Represents the physics world using Box2D. More...

#include <physics_world.h>

Public Member Functions

 PhysicsWorld ()
 Constructs a new physics world.
 
 PhysicsWorld (const PhysicsWorld &)=delete
 
PhysicsWorldoperator= (const PhysicsWorld &)=delete
 
 PhysicsWorld (PhysicsWorld &&)=delete
 
PhysicsWorldoperator= (PhysicsWorld &&)=delete
 
 ~PhysicsWorld ()
 
void step (float dt)
 Steps the physics simulation forward by the fixed time step.
 
void check_collision (const std::vector< std::reference_wrapper< GameObject > > &objects)
 Checks for collisions in the physics world.
 
std::optional< std::reference_wrapper< Collider2D > > find_collider_in_objects (const std::vector< std::reference_wrapper< GameObject > > &objects, Component *comp, b2ShapeId shape_id)
 Finds a collider in the given objects based on the component and shape ID.
 
void check_contact_touch_begin_events (const std::vector< std::reference_wrapper< GameObject > > &objects)
 Checks for the beginning of contact touch events.
 
void check_contact_touch_end_events (const std::vector< std::reference_wrapper< GameObject > > &objects)
 Checks for the ending of contact touch events.
 
void check_sensor_touch_begin_events (const std::vector< std::reference_wrapper< GameObject > > &objects)
 Checks for the beginning of sensor touch events.
 
void check_sensor_touch_end_events (const std::vector< std::reference_wrapper< GameObject > > &objects)
 Checks for the ending of sensor touch events.
 
void execute_valid_collider (const std::vector< std::reference_wrapper< GameObject > > &objects, b2ShapeId a, b2ShapeId b, const std::function< void(Collider2D &, Collider2D &)> &callback)
 Executes a callback if the given shape IDs are valid.
 
b2WorldId world_id () const noexcept
 
int32_t velocity_iterations () const noexcept
 
int32_t velocity_iterations (int32_t iterations) noexcept
 
float gravity_x () const noexcept
 
float gravity_x (float gx) noexcept
 
float gravity_y () const noexcept
 
float gravity_y (float gy) noexcept
 
bool world_exists () const noexcept
 

Static Public Member Functions

static BodyDistance2D distance (const Body2DTransform &a, const Body2DTransform &b)
 Calculate the distance between two bodies.
 
static BodyDistance2D fixture_distance (const Body2DTransform &a, const Body2DTransform &b)
 Calculate the distance between two bodies using their fixtures.
 

Static Public Attributes

static constexpr float pixels_per_meters = 16.0f
 
static constexpr float meters_per_pixels = 1.0f / pixels_per_meters
 

Detailed Description

Represents the physics world using Box2D.

This class encapsulates a Box2D world instance and provides settings such as gravity, time step, and conversion ratios.

Constructor & Destructor Documentation

◆ PhysicsWorld() [1/3]

PhysicsWorld::PhysicsWorld ( )

Constructs a new physics world.

◆ PhysicsWorld() [2/3]

PhysicsWorld::PhysicsWorld ( const PhysicsWorld )
delete

◆ PhysicsWorld() [3/3]

PhysicsWorld::PhysicsWorld ( PhysicsWorld &&  )
delete

◆ ~PhysicsWorld()

PhysicsWorld::~PhysicsWorld ( )

Member Function Documentation

◆ check_collision()

void PhysicsWorld::check_collision ( const std::vector< std::reference_wrapper< GameObject > > &  objects)

Checks for collisions in the physics world.

This method processes collision detection and resolution for the current state of the physics world. Its based on Box2D's internal collision handling and objects

◆ check_contact_touch_begin_events()

void PhysicsWorld::check_contact_touch_begin_events ( const std::vector< std::reference_wrapper< GameObject > > &  objects)

Checks for the beginning of contact touch events.

◆ check_contact_touch_end_events()

void PhysicsWorld::check_contact_touch_end_events ( const std::vector< std::reference_wrapper< GameObject > > &  objects)

Checks for the ending of contact touch events.

◆ check_sensor_touch_begin_events()

void PhysicsWorld::check_sensor_touch_begin_events ( const std::vector< std::reference_wrapper< GameObject > > &  objects)

Checks for the beginning of sensor touch events.

◆ check_sensor_touch_end_events()

void PhysicsWorld::check_sensor_touch_end_events ( const std::vector< std::reference_wrapper< GameObject > > &  objects)

Checks for the ending of sensor touch events.

◆ distance()

static BodyDistance2D PhysicsWorld::distance ( const Body2DTransform a,
const Body2DTransform b 
)
static

Calculate the distance between two bodies.

Parameters
aThe first body's transform.
bThe second body's transform.
Returns
BodyDistance2D The distance information between the two bodies.

◆ execute_valid_collider()

void PhysicsWorld::execute_valid_collider ( const std::vector< std::reference_wrapper< GameObject > > &  objects,
b2ShapeId  a,
b2ShapeId  b,
const std::function< void(Collider2D &, Collider2D &)> &  callback 
)

Executes a callback if the given shape IDs are valid.

◆ find_collider_in_objects()

std::optional< std::reference_wrapper< Collider2D > > PhysicsWorld::find_collider_in_objects ( const std::vector< std::reference_wrapper< GameObject > > &  objects,
Component comp,
b2ShapeId  shape_id 
)

Finds a collider in the given objects based on the component and shape ID.

◆ fixture_distance()

static BodyDistance2D PhysicsWorld::fixture_distance ( const Body2DTransform a,
const Body2DTransform b 
)
static

Calculate the distance between two bodies using their fixtures.

Fixture means the actual shapes attached to the bodies. So for instance a box body with multiple box fixtures will calculate the distance based on the closest fixture.

Parameters
aThe first body's transform.
bThe second body's transform.
Returns
BodyDistance2D The distance information between the two bodies' fixtures.

◆ gravity_x() [1/2]

float PhysicsWorld::gravity_x ( ) const
noexcept

◆ gravity_x() [2/2]

float PhysicsWorld::gravity_x ( float  gx)
noexcept

◆ gravity_y() [1/2]

float PhysicsWorld::gravity_y ( ) const
noexcept

◆ gravity_y() [2/2]

float PhysicsWorld::gravity_y ( float  gy)
noexcept

◆ operator=() [1/2]

PhysicsWorld & PhysicsWorld::operator= ( const PhysicsWorld )
delete

◆ operator=() [2/2]

PhysicsWorld & PhysicsWorld::operator= ( PhysicsWorld &&  )
delete

◆ step()

void PhysicsWorld::step ( float  dt)

Steps the physics simulation forward by the fixed time step.

This method advances the physics world by the configured time step, applying velocity and position updates to all bodies in the world.

Parameters
dtThe (deltatime) time step duration for the physics update.

◆ velocity_iterations() [1/2]

int32_t PhysicsWorld::velocity_iterations ( ) const
noexcept

◆ velocity_iterations() [2/2]

int32_t PhysicsWorld::velocity_iterations ( int32_t  iterations)
noexcept

◆ world_exists()

bool PhysicsWorld::world_exists ( ) const
noexcept

◆ world_id()

b2WorldId PhysicsWorld::world_id ( ) const
noexcept

Member Data Documentation

◆ meters_per_pixels

constexpr float PhysicsWorld::meters_per_pixels = 1.0f / pixels_per_meters
staticconstexpr

◆ pixels_per_meters

constexpr float PhysicsWorld::pixels_per_meters = 16.0f
staticconstexpr

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