diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2020-07-02 18:39:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-02 18:39:16 +0200 |
commit | 67e4082b1e73f3cbe518c499eb328b0f68f3419b (patch) | |
tree | ccb3953feff88ee0c6f7bf2013cb49e0404c2247 /servers/physics_2d | |
parent | d12124856292e80c9069593ed263d72d01f60d02 (diff) | |
parent | ba27deef06f0152a59c86e3b0ab3ebb0976344ad (diff) |
Merge pull request #37350 from aaronfranke/force-impulse
Refactor physics force and impulse code to use (force, position) order
Diffstat (limited to 'servers/physics_2d')
-rw-r--r-- | servers/physics_2d/body_2d_sw.h | 18 | ||||
-rw-r--r-- | servers/physics_2d/body_pair_2d_sw.cpp | 9 | ||||
-rw-r--r-- | servers/physics_2d/joints_2d_sw.cpp | 24 | ||||
-rw-r--r-- | servers/physics_2d/physics_server_2d_sw.cpp | 8 | ||||
-rw-r--r-- | servers/physics_2d/physics_server_2d_sw.h | 4 |
5 files changed, 31 insertions, 32 deletions
diff --git a/servers/physics_2d/body_2d_sw.h b/servers/physics_2d/body_2d_sw.h index 2300c9cdee..8c7876e5cd 100644 --- a/servers/physics_2d/body_2d_sw.h +++ b/servers/physics_2d/body_2d_sw.h @@ -203,18 +203,18 @@ public: linear_velocity += p_impulse * _inv_mass; } - _FORCE_INLINE_ void apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse) { + _FORCE_INLINE_ void apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) { linear_velocity += p_impulse * _inv_mass; - angular_velocity += _inv_inertia * p_offset.cross(p_impulse); + angular_velocity += _inv_inertia * p_position.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; - biased_angular_velocity += _inv_inertia * p_pos.cross(p_j); + _FORCE_INLINE_ void apply_bias_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) { + biased_linear_velocity += p_impulse * _inv_mass; + biased_angular_velocity += _inv_inertia * p_position.cross(p_impulse); } void set_active(bool p_active); @@ -246,9 +246,9 @@ public: applied_force += p_force; } - _FORCE_INLINE_ void add_force(const Vector2 &p_offset, const Vector2 &p_force) { + _FORCE_INLINE_ void add_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) { applied_force += p_force; - applied_torque += p_offset.cross(p_force); + applied_torque += p_position.cross(p_force); } _FORCE_INLINE_ void add_torque(real_t p_torque) { @@ -360,10 +360,10 @@ public: 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_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) { body->add_force(p_force, p_position); } 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_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) { body->apply_impulse(p_impulse, p_position); } 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); } diff --git a/servers/physics_2d/body_pair_2d_sw.cpp b/servers/physics_2d/body_pair_2d_sw.cpp index e483ddf1cc..258979ff22 100644 --- a/servers/physics_2d/body_pair_2d_sw.cpp +++ b/servers/physics_2d/body_pair_2d_sw.cpp @@ -427,10 +427,9 @@ bool BodyPair2DSW::setup(real_t p_step) { // Apply normal + friction impulse Vector2 P = c.acc_normal_impulse * c.normal + c.acc_tangent_impulse * tangent; - A->apply_impulse(c.rA, -P); - B->apply_impulse(c.rB, P); + A->apply_impulse(-P, c.rA); + B->apply_impulse(P, c.rB); } - #endif c.bounce = combine_bounce(A, B); @@ -497,8 +496,8 @@ void BodyPair2DSW::solve(real_t p_step) { Vector2 j = c.normal * (c.acc_normal_impulse - jnOld) + tangent * (c.acc_tangent_impulse - jtOld); - A->apply_impulse(c.rA, -j); - B->apply_impulse(c.rB, j); + A->apply_impulse(-j, c.rA); + B->apply_impulse(j, c.rB); } } diff --git a/servers/physics_2d/joints_2d_sw.cpp b/servers/physics_2d/joints_2d_sw.cpp index 81e961e90d..e7d26645e9 100644 --- a/servers/physics_2d/joints_2d_sw.cpp +++ b/servers/physics_2d/joints_2d_sw.cpp @@ -136,9 +136,9 @@ bool PinJoint2DSW::setup(real_t p_step) { bias = delta * -(get_bias() == 0 ? space->get_constraint_bias() : get_bias()) * (1.0 / p_step); // apply accumulated impulse - A->apply_impulse(rA, -P); + A->apply_impulse(-P, rA); if (B) { - B->apply_impulse(rB, P); + B->apply_impulse(P, rB); } return true; @@ -161,9 +161,9 @@ void PinJoint2DSW::solve(real_t p_step) { Vector2 impulse = M.basis_xform(bias - rel_vel - Vector2(softness, softness) * P); - A->apply_impulse(rA, -impulse); + A->apply_impulse(-impulse, rA); if (B) { - B->apply_impulse(rB, impulse); + B->apply_impulse(impulse, rB); } P += impulse; @@ -301,8 +301,8 @@ bool GrooveJoint2DSW::setup(real_t p_step) { gbias = (delta * -(_b == 0 ? space->get_constraint_bias() : _b) * (1.0 / p_step)).clamped(get_max_bias()); // apply accumulated impulse - A->apply_impulse(rA, -jn_acc); - B->apply_impulse(rB, jn_acc); + A->apply_impulse(-jn_acc, rA); + B->apply_impulse(jn_acc, rB); correct = true; return true; @@ -320,8 +320,8 @@ void GrooveJoint2DSW::solve(real_t p_step) { j = jn_acc - jOld; - A->apply_impulse(rA, -j); - B->apply_impulse(rB, j); + A->apply_impulse(-j, rA); + B->apply_impulse(j, rB); } GrooveJoint2DSW::GrooveJoint2DSW(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, Body2DSW *p_body_a, Body2DSW *p_body_b) : @@ -370,8 +370,8 @@ bool DampedSpringJoint2DSW::setup(real_t p_step) { real_t f_spring = (rest_length - dist) * stiffness; Vector2 j = n * f_spring * (p_step); - A->apply_impulse(rA, -j); - B->apply_impulse(rB, j); + A->apply_impulse(-j, rA); + B->apply_impulse(j, rB); return true; } @@ -386,8 +386,8 @@ void DampedSpringJoint2DSW::solve(real_t p_step) { target_vrn = vrn + v_damp; Vector2 j = n * v_damp * n_mass; - A->apply_impulse(rA, -j); - B->apply_impulse(rB, j); + A->apply_impulse(-j, rA); + B->apply_impulse(j, rB); } void DampedSpringJoint2DSW::set_param(PhysicsServer2D::DampedSpringParam p_param, real_t p_value) { diff --git a/servers/physics_2d/physics_server_2d_sw.cpp b/servers/physics_2d/physics_server_2d_sw.cpp index 6983225668..1ed1ba6958 100644 --- a/servers/physics_2d/physics_server_2d_sw.cpp +++ b/servers/physics_2d/physics_server_2d_sw.cpp @@ -823,13 +823,13 @@ void PhysicsServer2DSW::body_apply_torque_impulse(RID p_body, real_t p_torque) { body->apply_torque_impulse(p_torque); } -void PhysicsServer2DSW::body_apply_impulse(RID p_body, const Vector2 &p_pos, const Vector2 &p_impulse) { +void PhysicsServer2DSW::body_apply_impulse(RID p_body, const Vector2 &p_impulse, const Vector2 &p_position) { Body2DSW *body = body_owner.getornull(p_body); ERR_FAIL_COND(!body); _update_shapes(); - body->apply_impulse(p_pos, p_impulse); + body->apply_impulse(p_impulse, p_position); body->wakeup(); }; @@ -841,11 +841,11 @@ void PhysicsServer2DSW::body_add_central_force(RID p_body, const Vector2 &p_forc body->wakeup(); }; -void PhysicsServer2DSW::body_add_force(RID p_body, const Vector2 &p_offset, const Vector2 &p_force) { +void PhysicsServer2DSW::body_add_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position) { Body2DSW *body = body_owner.getornull(p_body); ERR_FAIL_COND(!body); - body->add_force(p_offset, p_force); + body->add_force(p_force, p_position); body->wakeup(); }; diff --git a/servers/physics_2d/physics_server_2d_sw.h b/servers/physics_2d/physics_server_2d_sw.h index 093c775cb5..9fcb7ff5f2 100644 --- a/servers/physics_2d/physics_server_2d_sw.h +++ b/servers/physics_2d/physics_server_2d_sw.h @@ -221,12 +221,12 @@ public: virtual real_t body_get_applied_torque(RID p_body) const; virtual void body_add_central_force(RID p_body, const Vector2 &p_force); - virtual void body_add_force(RID p_body, const Vector2 &p_offset, const Vector2 &p_force); + virtual void body_add_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position = Vector2()); virtual void body_add_torque(RID p_body, real_t p_torque); virtual void body_apply_central_impulse(RID p_body, const Vector2 &p_impulse); virtual void body_apply_torque_impulse(RID p_body, real_t p_torque); - virtual void body_apply_impulse(RID p_body, const Vector2 &p_pos, const Vector2 &p_impulse); + virtual void body_apply_impulse(RID p_body, const Vector2 &p_impulse, const Vector2 &p_position = Vector2()); virtual void body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity); virtual void body_add_collision_exception(RID p_body, RID p_body_b); |