From 40c7716586db9182208b51d22e117d099bb4c97d Mon Sep 17 00:00:00 2001 From: Tiger Caldwell Date: Tue, 24 Jul 2018 03:49:12 -0400 Subject: Modified RigidBody, PhysicsDirectBodyState, PhysicsServer, and their respective 2D counterparts to be more consistent and to include more useful methods. RigidBody: - Added add_central_force - Added add_force - Added add_torque - Added apply_central_impulse RigidBody2D: - Added add_central_force - Added add_torque - Added apply_central_impulse - Added apply_torque_impulse PhysicsDirectBodyState: - Added apply_central_impulse Physics2DDirectBodyState: - Added add_central_force - Added add_force - Added add_torque - Added apply_central_impulse - Added apply_impulse - Added apply_torque_impulse PhysicsServer: - Added body_add_force - Added body_add_torque - Added body_add_central_force - Added body_apply_central_impulse Physics2DServer: - Added body_add_torque - Added body_add_central_force - Added body_apply_central_impulse - Added body_apply_torque_impulse Also fixed some small bugs along the way --- servers/physics_2d/body_2d_sw.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'servers/physics_2d/body_2d_sw.h') diff --git a/servers/physics_2d/body_2d_sw.h b/servers/physics_2d/body_2d_sw.h index 301bd6b299..7fe805b1f9 100644 --- a/servers/physics_2d/body_2d_sw.h +++ b/servers/physics_2d/body_2d_sw.h @@ -201,12 +201,20 @@ public: _FORCE_INLINE_ void set_biased_angular_velocity(real_t p_velocity) { biased_angular_velocity = p_velocity; } _FORCE_INLINE_ real_t get_biased_angular_velocity() const { return biased_angular_velocity; } + _FORCE_INLINE_ void apply_central_impulse(const Vector2 &p_impulse) { + linear_velocity += p_impulse * _inv_mass; + } + _FORCE_INLINE_ void apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse) { linear_velocity += p_impulse * _inv_mass; angular_velocity += _inv_inertia * p_offset.cross(p_impulse); } + _FORCE_INLINE_ void apply_torque_impulse(real_t p_torque) { + angular_velocity += _inv_inertia * p_torque; + } + _FORCE_INLINE_ void apply_bias_impulse(const Vector2 &p_pos, const Vector2 &p_j) { biased_linear_velocity += p_j * _inv_mass; @@ -237,12 +245,20 @@ public: void set_applied_torque(real_t p_torque) { applied_torque = p_torque; } real_t get_applied_torque() const { return applied_torque; } - _FORCE_INLINE_ void add_force(const Vector2 &p_force, const Vector2 &p_offset) { + _FORCE_INLINE_ void add_central_force(const Vector2 &p_force) { + applied_force += p_force; + } + + _FORCE_INLINE_ void add_force(const Vector2 &p_offset, const Vector2 &p_force) { applied_force += p_force; applied_torque += p_offset.cross(p_force); } + _FORCE_INLINE_ void add_torque(real_t p_torque) { + applied_torque += p_torque; + } + _FORCE_INLINE_ void set_continuous_collision_detection_mode(Physics2DServer::CCDMode p_mode) { continuous_cd_mode = p_mode; } _FORCE_INLINE_ Physics2DServer::CCDMode get_continuous_collision_detection_mode() const { return continuous_cd_mode; } @@ -357,6 +373,13 @@ public: virtual void set_transform(const Transform2D &p_transform) { body->set_state(Physics2DServer::BODY_STATE_TRANSFORM, p_transform); } virtual Transform2D get_transform() const { return body->get_transform(); } + virtual void add_central_force(const Vector2 &p_force) { body->add_central_force(p_force); } + virtual void add_force(const Vector2 &p_offset, const Vector2 &p_force) { body->add_force(p_offset, p_force); } + virtual void add_torque(real_t p_torque) { body->add_torque(p_torque); } + virtual void apply_central_impulse(const Vector2 &p_impulse) { body->apply_central_impulse(p_impulse); } + virtual void apply_impulse(const Vector2 &p_offset, const Vector2 &p_force) { body->apply_impulse(p_offset, p_force); } + virtual void apply_torque_impulse(real_t p_torque) { body->apply_torque_impulse(p_torque); } + virtual void set_sleep_state(bool p_enable) { body->set_active(!p_enable); } virtual bool is_sleeping() const { return !body->is_active(); } -- cgit v1.2.3