Physics

namespace tnt
namespace doo

Enums

enum body_type

Values:

enumerator fixed
enumerator kinematic

< The body is fixed in a certain position.

enumerator dynamic

< The body can move freely and collide only with kinematic bodies.

< The body can move freely and collide with dynamic or fixed bodies.

enum collision_type

Values:

enumerator none
enumerator horizontal

< The objects don’t collide.

enumerator vertical

< The objects collide on their left/right side.

Variables

constexpr float beta = {0.05f}
constexpr Vector gravity = {0.f, 10.f}

< A constant used for baumgarte stabilization.

struct tnt::doo::physics_sys physics
struct physics_comp

Public Members

body_type type = {body_type::dynamic}
float mass
float damping
float restitution
Vector maxVel = {40.f, 40.f}
Vector maxAccel = {15.f, 15.f}
Rectangle bound_box
struct physics_sys
#include <Physics.hpp>

A struct that handles the physics data of all the objects.

Public Functions

void add_object(object const &id, physics_comp const &body)

Add a new object to the physics system.

Note

The position of the bounding box of the component is considered to be relative to the position of the body.

Parameters
  • id: The id of the object to add to the physics system.

  • body: The physics component that has the data of the object.

void addForce(object const &id, tnt::Vector const &force) noexcept

Apply the given force to the object with the given id.

Parameters
  • id: The id of the object.

  • force: The force to apply to the object.

void addGlobalForce(tnt::Vector const &force) noexcept

Add a force that would be applied to each object when Update is called.

Parameters
  • force: The force to be applied.

void Update(object const &id, float time_) noexcept

Update the physics data of the object with the given id.

Parameters
  • id: The id of the object to update.

  • time_: The time that passed since the last update call.

bool colliding(object const &id, object const &id2) noexcept

Check if the bounding boxes of the two objects are colliding.

Return

bool

Parameters
  • id: The id of the first object.

  • id2: The id of the second object.

void resolveVel(object const &id, object const &id2) noexcept

Resolve the velocity between two objects that have collided.

Note

This is called by resolve().

Parameters
  • id: The id of the first object.

  • id2: The id of the second object.

void resolveInterpenetration(object const &id, object const &id2) noexcept

Resolve the interpenetration between two objects that have collided.

Note

This is called by resolve().

Parameters
  • id: The id of the first object.

  • id2: The id of the second object.

void resolve(object const &id, object const &id2) noexcept

Resolve the collision between two objects.

Parameters
  • id: The id of the first object.

  • id2: The id of the second object.

void from_json(object const &id, nlohmann::json const &j)

Load objects physics data from a json chunk.

Parameters
  • id: The id of the object to load from json.

  • j: The json chunk that contains the objects data.

void to_json(object const &id, nlohmann::json &j)

Store physics data of a specific object to a json chunk.

Parameters
  • id: The id of the object to serialize to json.

  • j: The json chunk where the data will be saved.

void draw_imgui(object const &id, Window const &win) noexcept

Draw widgets on the given window to modify the datas of the system.

Parameters
  • id: The id of the active object.

  • win: The window where to draw the widgets.

void remove(object const &id) noexcept

Remove the desired object from the physics system.

Parameters
  • id: The id of the object you want to remove.

void clear() noexcept

Remove all the objects from the physics system.

Vector gVel(object const &id) const noexcept

Get the velocity of the object in global context.

Parameters
  • id: The id of the object.

Vector gMaxVel(object const &id) const noexcept

Get the maximal velocity of the object in global context.

Parameters
  • id: The id of the object.

Vector gAccel(object const &id) const noexcept

Get the acceleration of the object in global context.

Parameters
  • id: The id of the object.

Vector gMaxAccel(object const &id) const noexcept

Get the maximum acceleration of the object in global context.

Parameters
  • id: The id of the object.

Public Members

tnt::Vector totalForce = {0.f, 0.f}
std::vector<float> inv_mass
std::vector<float> damping

< The inverse of the mass of each objects.

std::vector<float> restitution

< The damping of the objects.

std::vector<Vector> vel

< The restitution of the objects.

std::vector<Vector> maxVel

< The velocities of the objects.

std::vector<Vector> accel

< The maximal velocities of the objects.

std::vector<Vector> maxAccel

< The accelerations of the objects.

std::vector<Rectangle> bound_box

< The maximal accelerations of the objects.

sparse_set<object> active

< The bounding boxes of the bodies.

namespace phys