20 #ifndef ANTKEEPER_PHYSICS_RIGID_BODY_HPP
21 #define ANTKEEPER_PHYSICS_RIGID_BODY_HPP
44 m_current_transform = transform;
64 m_current_transform.
rotation = orientation;
90 m_previous_transform = transform;
110 m_previous_transform.
rotation = orientation;
136 m_center_of_mass = point;
144 inline constexpr
void set_mass(
float mass) noexcept
147 m_inverse_mass = (mass) ? 1.0f / mass : 0.0f;
158 m_inverse_inertia = (inertia) ? 1.0f / inertia : 0.0f;
178 m_linear_damping = damping;
188 m_angular_damping = damping;
198 m_linear_momentum = momentum;
199 m_linear_velocity = m_inverse_mass * m_linear_momentum;
209 m_angular_momentum = momentum;
210 m_angular_velocity = m_inverse_inertia * m_angular_momentum;
220 m_linear_velocity = velocity;
221 m_linear_momentum = m_mass * m_linear_velocity;
231 m_angular_velocity = velocity;
232 m_angular_momentum = m_inertia * m_angular_velocity;
238 return m_current_transform;
250 return m_current_transform.
rotation;
256 return m_current_transform.
scale;
262 return m_previous_transform;
274 return m_previous_transform.
rotation;
280 return m_previous_transform.
scale;
286 return m_center_of_mass;
290 [[nodiscard]]
inline constexpr
float get_mass() const noexcept
298 return m_inverse_mass;
310 return m_inverse_inertia;
316 return m_linear_damping;
322 return m_angular_damping;
326 [[nodiscard]]
inline constexpr
const std::shared_ptr<collider>&
get_collider() const noexcept
334 return m_linear_momentum;
340 return m_angular_momentum;
346 return m_linear_velocity;
352 return m_angular_velocity;
358 return m_applied_force;
364 return m_applied_torque;
376 return m_linear_velocity +
math::cross(m_angular_velocity, radius);
382 [[nodiscard]]
inline constexpr
bool is_static() const noexcept
384 return (m_mass == 0.0f);
395 m_applied_force += force;
406 m_applied_force += force;
416 m_applied_torque += torque;
427 m_linear_momentum += impulse;
428 m_angular_momentum +=
math::cross(radius, impulse);
431 m_linear_velocity = m_inverse_mass * m_linear_momentum;
432 m_angular_velocity = m_inverse_inertia * m_angular_momentum;
442 m_linear_momentum += impulse;
445 m_linear_velocity = m_inverse_mass * m_linear_momentum;
455 m_angular_momentum += torque;
458 m_angular_velocity = m_inverse_inertia * m_angular_momentum;
464 m_applied_force = {};
465 m_applied_torque = {};
516 float m_inverse_mass{1.0f};
519 float m_inertia{1.0f};
522 float m_inverse_inertia{1.0f};
525 float m_linear_damping{};
528 float m_angular_damping{};
531 std::shared_ptr<collider> m_collider;
Abstract base class for collision objects.
void integrate_velocities(float dt) noexcept
Integrates velocities, updating the center of mass and orientation of the rigid body.
constexpr void set_previous_scale(float scale) noexcept
Sets the previous scale of the rigid body.
constexpr void set_orientation(const math::fquat &orientation) noexcept
Sets the current orientation of the rigid body.
constexpr const math::fvec3 & get_linear_velocity() const noexcept
Returns the linear velocity of the rigid body, in m/s.
constexpr const math::fvec3 & get_applied_torque() const noexcept
Returns the total pre-integrated torque, in N⋅m.
constexpr float get_angular_damping() const noexcept
Returns the angular damping factor of the rigid body.
constexpr const std::shared_ptr< collider > & get_collider() const noexcept
Returns the collider of the rigid body.
constexpr const math::fquat & get_orientation() const noexcept
Returns the current orientation of the rigid body.
constexpr void set_angular_velocity(const math::fvec3 &velocity) noexcept
Sets the angular velocity of the rigid body.
constexpr void set_center_of_mass(const math::fvec3 &point) noexcept
Sets the center of mass of the rigid body.
constexpr void set_inertia(float inertia) noexcept
Sets the moment of inertia of the rigid body.
constexpr float get_linear_damping() const noexcept
Returns the linear damping factor of the rigid body.
constexpr const math::fvec3 & get_previous_scale() const noexcept
Returns the previous scale of the rigid body.
constexpr void clear_applied_forces() noexcept
Clears all pre-integrated forces.
constexpr const math::fvec3 & get_center_of_mass() const noexcept
Returns the center of mass of the rigid body.
constexpr void set_mass(float mass) noexcept
Sets mass of the rigid body.
constexpr void apply_torque(const math::fvec3 &torque) noexcept
Applies a torque to the rigid body.
constexpr void set_previous_orientation(const math::fquat &orientation) noexcept
Sets the previous orientation of the rigid body.
constexpr void apply_central_impulse(const math::fvec3 &impulse) noexcept
Applies an impulse at the center of mass of the rigid body.
constexpr const math::fvec3 & get_previous_position() const noexcept
Returns the previous position of the rigid body.
constexpr void apply_central_force(const math::fvec3 &force) noexcept
Applies a force at the center of mass of the rigid body.
constexpr const math::transform< float > & get_previous_transform() const noexcept
Returns the transformation representing the previous state of the rigid body.
math::transform< float > interpolate(float alpha) const
Returns a transformation representing a state of the rigid body between its current and previous stat...
constexpr const math::fvec3 & get_position() const noexcept
Returns the current position of the rigid body.
constexpr const math::fquat & get_previous_orientation() const noexcept
Returns the previous orientation of the rigid body.
constexpr const math::fvec3 & get_angular_velocity() const noexcept
Returns the angular velocity of the rigid body, in rad/s.
constexpr bool is_static() const noexcept
Returns true if the rigid body is static, false otherwise.
constexpr float get_inverse_inertia() const noexcept
Returns the inverse moment of inertia of the rigid body, in kg⋅m^2^-1.
constexpr void set_linear_damping(float damping) noexcept
Sets the linear damping factor of the rigid body.
constexpr void set_position(const math::fvec3 &position) noexcept
Sets the current position of the rigid body.
void integrate(float dt) noexcept
Integrates forces and velocities.
constexpr void apply_force(const math::fvec3 &force, const math::fvec3 &radius) noexcept
Applies a force at a point on the rigid body.
constexpr math::fvec3 get_point_velocity(const math::fvec3 &radius) const noexcept
Calculates the total velocity at a point on the rigid body.
constexpr void set_previous_scale(const math::fvec3 &scale) noexcept
Sets the previous scale of the rigid body.
constexpr const math::fvec3 & get_linear_momentum() const noexcept
Returns the linear momentum of the rigid body, in kg⋅m/s.
constexpr void set_transform(const math::transform< float > &transform) noexcept
Sets the transformation representing the current state of the rigid body.
void set_collider(std::shared_ptr< collider > collider) noexcept
Sets the collider of the rigid body.
void integrate_forces(float dt) noexcept
Integrates forces, updating the momentums and velocities of the rigid body.
constexpr const math::fvec3 & get_angular_momentum() const noexcept
Returns the angular momentum of the rigid body, in kg⋅m^2⋅s^-1.
constexpr void apply_impulse(const math::fvec3 &impulse, const math::fvec3 &radius) noexcept
Applies an impulse at a point on the rigid body.
constexpr float get_inverse_mass() const noexcept
Returns the inverse mass of the rigid body, in kg^-1.
constexpr float get_mass() const noexcept
Returns the mass of the rigid body, in kg.
constexpr void set_angular_damping(float damping) noexcept
Sets the angular damping factor of the rigid body.
constexpr void apply_torque_impulse(const math::fvec3 &torque) noexcept
Applies a torque impulse to the rigid body.
constexpr const math::fvec3 & get_applied_force() const noexcept
Returns the total pre-integrated force, in N.
constexpr void set_scale(float scale) noexcept
Sets the current scale of the rigid body.
constexpr void set_previous_position(const math::fvec3 &position) noexcept
Sets the previous position of the rigid body.
constexpr float get_inertia() const noexcept
Returns the moment of inertia of the rigid body, in kg⋅m^2.
constexpr void set_angular_momentum(const math::fvec3 &momentum) noexcept
Sets the angular momentum of the rigid body.
constexpr void set_previous_transform(const math::transform< float > &transform) noexcept
Sets the transformation representing the previous state of the rigid body.
constexpr void set_scale(const math::fvec3 &scale) noexcept
Sets the current scale of the rigid body.
constexpr const math::fvec3 & get_scale() const noexcept
Returns the current scale of the rigid body.
constexpr void set_linear_momentum(const math::fvec3 &momentum) noexcept
Sets the linear momentum of the rigid body.
constexpr void set_linear_velocity(const math::fvec3 &velocity) noexcept
Sets the linear velocity of the rigid body.
constexpr const math::transform< float > & get_transform() const noexcept
Returns the transformation representing the current state of the rigid body.
constexpr mat4< T > scale(const vec3< T > &v)
Constructs a scale matrix.
constexpr vector< T, 3 > cross(const vector< T, 3 > &x, const vector< T, 3 > &y) noexcept
Calculates the cross product of two vectors.
@ position
Vertex position (vec3)
Quaternion composed of a real scalar part and imaginary vector part.