Skip to content

Commit

Permalink
Merge pull request #20404 from TigerCaldwell/master
Browse files Browse the repository at this point in the history
Ensured consistency between RigidBody, PhysicsDirectBodyState, PhysicsServers and their 2D counterparts
  • Loading branch information
akien-mga authored Jul 25, 2018
2 parents f8e8ac2 + 40c7716 commit f778bd8
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 2 deletions.
28 changes: 28 additions & 0 deletions modules/bullet/bullet_physics_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,34 @@ Vector3 BulletPhysicsServer::body_get_applied_torque(RID p_body) const {
return body->get_applied_torque();
}

void BulletPhysicsServer::body_add_central_force(RID p_body, const Vector3 &p_force) {
RigidBodyBullet *body = rigid_body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->apply_central_force(p_force);
}

void BulletPhysicsServer::body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_pos) {
RigidBodyBullet *body = rigid_body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->apply_force(p_force, p_pos);
}

void BulletPhysicsServer::body_add_torque(RID p_body, const Vector3 &p_torque) {
RigidBodyBullet *body = rigid_body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->apply_torque(p_torque);
}

void BulletPhysicsServer::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
RigidBodyBullet *body = rigid_body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->apply_central_impulse(p_impulse);
}

void BulletPhysicsServer::body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse) {
RigidBodyBullet *body = rigid_body_owner.get(p_body);
ERR_FAIL_COND(!body);
Expand Down
5 changes: 5 additions & 0 deletions modules/bullet/bullet_physics_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ class BulletPhysicsServer : public PhysicsServer {
virtual void body_set_applied_torque(RID p_body, const Vector3 &p_torque);
virtual Vector3 body_get_applied_torque(RID p_body) const;

virtual void body_add_central_force(RID p_body, const Vector3 &p_force);
virtual void body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_pos);
virtual void body_add_torque(RID p_body, const Vector3 &p_torque);

virtual void body_apply_central_impulse(RID p_body, const Vector3 &p_impulse);
virtual void body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse);
virtual void body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse);
virtual void body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity);
Expand Down
4 changes: 4 additions & 0 deletions modules/bullet/rigid_body_bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ void BulletPhysicsDirectBodyState::add_torque(const Vector3 &p_torque) {
body->apply_torque(p_torque);
}

void BulletPhysicsDirectBodyState::apply_central_impulse(const Vector3 &p_j) {
body->apply_central_impulse(p_j);
}

void BulletPhysicsDirectBodyState::apply_impulse(const Vector3 &p_pos, const Vector3 &p_j) {
body->apply_impulse(p_pos, p_j);
}
Expand Down
1 change: 1 addition & 0 deletions modules/bullet/rigid_body_bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class BulletPhysicsDirectBodyState : public PhysicsDirectBodyState {
virtual void add_central_force(const Vector3 &p_force);
virtual void add_force(const Vector3 &p_force, const Vector3 &p_pos);
virtual void add_torque(const Vector3 &p_torque);
virtual void apply_central_impulse(const Vector3 &p_impulse);
virtual void apply_impulse(const Vector3 &p_pos, const Vector3 &p_j);
virtual void apply_torque_impulse(const Vector3 &p_j);

Expand Down
20 changes: 20 additions & 0 deletions scene/2d/physics_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,11 +800,19 @@ int RigidBody2D::get_max_contacts_reported() const {
return max_contacts_reported;
}

void RigidBody2D::apply_central_impulse(const Vector2 &p_impulse) {
Physics2DServer::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
}

void RigidBody2D::apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse) {

Physics2DServer::get_singleton()->body_apply_impulse(get_rid(), p_offset, p_impulse);
}

void RigidBody2D::apply_torque_impulse(float p_torque) {
Physics2DServer::get_singleton()->body_apply_torque_impulse(get_rid(), p_torque);
}

void RigidBody2D::set_applied_force(const Vector2 &p_force) {

Physics2DServer::get_singleton()->body_set_applied_force(get_rid(), p_force);
Expand All @@ -825,11 +833,19 @@ float RigidBody2D::get_applied_torque() const {
return Physics2DServer::get_singleton()->body_get_applied_torque(get_rid());
};

void RigidBody2D::add_central_force(const Vector2 &p_force) {
Physics2DServer::get_singleton()->body_add_central_force(get_rid(), p_force);
}

void RigidBody2D::add_force(const Vector2 &p_offset, const Vector2 &p_force) {

Physics2DServer::get_singleton()->body_add_force(get_rid(), p_offset, p_force);
}

void RigidBody2D::add_torque(const float p_torque) {
Physics2DServer::get_singleton()->body_add_torque(get_rid(), p_torque);
}

void RigidBody2D::set_continuous_collision_detection_mode(CCDMode p_mode) {

ccd_mode = p_mode;
Expand Down Expand Up @@ -986,15 +1002,19 @@ void RigidBody2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_continuous_collision_detection_mode"), &RigidBody2D::get_continuous_collision_detection_mode);

ClassDB::bind_method(D_METHOD("set_axis_velocity", "axis_velocity"), &RigidBody2D::set_axis_velocity);
ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &RigidBody2D::apply_central_impulse);
ClassDB::bind_method(D_METHOD("apply_impulse", "offset", "impulse"), &RigidBody2D::apply_impulse);
ClassDB::bind_method(D_METHOD("apply_torque_impulse", "torque"), &RigidBody2D::apply_torque_impulse);

ClassDB::bind_method(D_METHOD("set_applied_force", "force"), &RigidBody2D::set_applied_force);
ClassDB::bind_method(D_METHOD("get_applied_force"), &RigidBody2D::get_applied_force);

ClassDB::bind_method(D_METHOD("set_applied_torque", "torque"), &RigidBody2D::set_applied_torque);
ClassDB::bind_method(D_METHOD("get_applied_torque"), &RigidBody2D::get_applied_torque);

ClassDB::bind_method(D_METHOD("add_central_force", "force"), &RigidBody2D::add_central_force);
ClassDB::bind_method(D_METHOD("add_force", "offset", "force"), &RigidBody2D::add_force);
ClassDB::bind_method(D_METHOD("add_torque", "torque"), &RigidBody2D::add_torque);

ClassDB::bind_method(D_METHOD("set_sleeping", "sleeping"), &RigidBody2D::set_sleeping);
ClassDB::bind_method(D_METHOD("is_sleeping"), &RigidBody2D::is_sleeping);
Expand Down
4 changes: 4 additions & 0 deletions scene/2d/physics_body_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,19 @@ class RigidBody2D : public PhysicsBody2D {
void set_continuous_collision_detection_mode(CCDMode p_mode);
CCDMode get_continuous_collision_detection_mode() const;

void apply_central_impulse(const Vector2 &p_impulse);
void apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse);
void apply_torque_impulse(float p_torque);

void set_applied_force(const Vector2 &p_force);
Vector2 get_applied_force() const;

void set_applied_torque(const float p_torque);
float get_applied_torque() const;

void add_central_force(const Vector2 &p_force);
void add_force(const Vector2 &p_offset, const Vector2 &p_force);
void add_torque(float p_torque);

Array get_colliding_bodies() const; //function for script

Expand Down
22 changes: 22 additions & 0 deletions scene/3d/physics_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,22 @@ int RigidBody::get_max_contacts_reported() const {
return max_contacts_reported;
}

void RigidBody::add_central_force(const Vector3 &p_force) {
PhysicsServer::get_singleton()->body_add_central_force(get_rid(), p_force);
}

void RigidBody::add_force(const Vector3 &p_force, const Vector3 &p_pos) {
PhysicsServer::get_singleton()->body_add_force(get_rid(), p_force, p_pos);
}

void RigidBody::add_torque(const Vector3 &p_torque) {
PhysicsServer::get_singleton()->body_add_torque(get_rid(), p_torque);
}

void RigidBody::apply_central_impulse(const Vector3 &p_impulse) {
PhysicsServer::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
}

void RigidBody::apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse) {

PhysicsServer::get_singleton()->body_apply_impulse(get_rid(), p_pos, p_impulse);
Expand Down Expand Up @@ -947,6 +963,12 @@ void RigidBody::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_using_continuous_collision_detection"), &RigidBody::is_using_continuous_collision_detection);

ClassDB::bind_method(D_METHOD("set_axis_velocity", "axis_velocity"), &RigidBody::set_axis_velocity);

ClassDB::bind_method(D_METHOD("add_central_force", "force"), &RigidBody::add_central_force);
ClassDB::bind_method(D_METHOD("add_force", "force", "position"), &RigidBody::add_force);
ClassDB::bind_method(D_METHOD("add_torque", "torque"), &RigidBody::add_torque);

ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &RigidBody::apply_central_impulse);
ClassDB::bind_method(D_METHOD("apply_impulse", "position", "impulse"), &RigidBody::apply_impulse);
ClassDB::bind_method(D_METHOD("apply_torque_impulse", "impulse"), &RigidBody::apply_torque_impulse);

Expand Down
5 changes: 5 additions & 0 deletions scene/3d/physics_body.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ class RigidBody : public PhysicsBody {

Array get_colliding_bodies() const;

void add_central_force(const Vector3 &p_force);
void add_force(const Vector3 &p_force, const Vector3 &p_pos);
void add_torque(const Vector3 &p_torque);

void apply_central_impulse(const Vector3 &p_impulse);
void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse);
void apply_torque_impulse(const Vector3 &p_impulse);

Expand Down
5 changes: 5 additions & 0 deletions servers/physics/body_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ class BodySW : public CollisionObjectSW {
_FORCE_INLINE_ const Vector3 &get_biased_linear_velocity() const { return biased_linear_velocity; }
_FORCE_INLINE_ const Vector3 &get_biased_angular_velocity() const { return biased_angular_velocity; }

_FORCE_INLINE_ void apply_central_impulse(const Vector3 &p_j) {
linear_velocity += p_j * _inv_mass;
}

_FORCE_INLINE_ void apply_impulse(const Vector3 &p_pos, const Vector3 &p_j) {

linear_velocity += p_j * _inv_mass;
Expand Down Expand Up @@ -421,6 +425,7 @@ class PhysicsDirectBodyStateSW : public PhysicsDirectBodyState {
virtual void add_central_force(const Vector3 &p_force) { body->add_central_force(p_force); }
virtual void add_force(const Vector3 &p_force, const Vector3 &p_pos) { body->add_force(p_force, p_pos); }
virtual void add_torque(const Vector3 &p_torque) { body->add_torque(p_torque); }
virtual void apply_central_impulse(const Vector3 &p_j) { body->apply_central_impulse(p_j); }
virtual void apply_impulse(const Vector3 &p_pos, const Vector3 &p_j) { body->apply_impulse(p_pos, p_j); }
virtual void apply_torque_impulse(const Vector3 &p_j) { body->apply_torque_impulse(p_j); }

Expand Down
34 changes: 34 additions & 0 deletions servers/physics/physics_server_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,40 @@ Vector3 PhysicsServerSW::body_get_applied_torque(RID p_body) const {
return body->get_applied_torque();
};

void PhysicsServerSW::body_add_central_force(RID p_body, const Vector3 &p_force) {
BodySW *body = body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->add_central_force(p_force);
body->wakeup();
}

void PhysicsServerSW::body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_pos) {
BodySW *body = body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->add_force(p_force, p_pos);
body->wakeup();
};

void PhysicsServerSW::body_add_torque(RID p_body, const Vector3 &p_torque) {
BodySW *body = body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->add_torque(p_torque);
body->wakeup();
};

void PhysicsServerSW::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
BodySW *body = body_owner.get(p_body);
ERR_FAIL_COND(!body);

_update_shapes();

body->apply_central_impulse(p_impulse);
body->wakeup();
}

void PhysicsServerSW::body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse) {

BodySW *body = body_owner.get(p_body);
Expand Down
5 changes: 5 additions & 0 deletions servers/physics/physics_server_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ class PhysicsServerSW : public PhysicsServer {
virtual void body_set_applied_torque(RID p_body, const Vector3 &p_torque);
virtual Vector3 body_get_applied_torque(RID p_body) const;

virtual void body_add_central_force(RID p_body, const Vector3 &p_force);
virtual void body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_pos);
virtual void body_add_torque(RID p_body, const Vector3 &p_torque);

virtual void body_apply_central_impulse(RID p_body, const Vector3 &p_impulse);
virtual void body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse);
virtual void body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse);
virtual void body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity);
Expand Down
25 changes: 24 additions & 1 deletion servers/physics_2d/body_2d_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,20 @@ class Body2DSW : public CollisionObject2DSW {
_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;
Expand Down Expand Up @@ -237,12 +245,20 @@ class Body2DSW : public CollisionObject2DSW {
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; }

Expand Down Expand Up @@ -357,6 +373,13 @@ class Physics2DDirectBodyStateSW : public Physics2DDirectBodyState {
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(); }

Expand Down
33 changes: 32 additions & 1 deletion servers/physics_2d/physics_2d_server_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,21 @@ real_t Physics2DServerSW::body_get_applied_torque(RID p_body) const {
return body->get_applied_torque();
};

void Physics2DServerSW::body_apply_central_impulse(RID p_body, const Vector2 &p_impulse) {
Body2DSW *body = body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->apply_central_impulse(p_impulse);
body->wakeup();
}

void Physics2DServerSW::body_apply_torque_impulse(RID p_body, real_t p_torque) {
Body2DSW *body = body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->apply_torque_impulse(p_torque);
}

void Physics2DServerSW::body_apply_impulse(RID p_body, const Vector2 &p_pos, const Vector2 &p_impulse) {

Body2DSW *body = body_owner.get(p_body);
Expand All @@ -863,12 +878,28 @@ void Physics2DServerSW::body_apply_impulse(RID p_body, const Vector2 &p_pos, con
body->wakeup();
};

void Physics2DServerSW::body_add_central_force(RID p_body, const Vector2 &p_force) {
Body2DSW *body = body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->add_central_force(p_force);
body->wakeup();
};

void Physics2DServerSW::body_add_force(RID p_body, const Vector2 &p_offset, const Vector2 &p_force) {

Body2DSW *body = body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->add_force(p_force, p_offset);
body->add_force(p_offset, p_force);
body->wakeup();
};

void Physics2DServerSW::body_add_torque(RID p_body, real_t p_torque) {
Body2DSW *body = body_owner.get(p_body);
ERR_FAIL_COND(!body);

body->add_torque(p_torque);
body->wakeup();
};

Expand Down
4 changes: 4 additions & 0 deletions servers/physics_2d/physics_2d_server_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,12 @@ class Physics2DServerSW : public Physics2DServer {
virtual void body_set_applied_torque(RID p_body, real_t p_torque);
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_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_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity);

Expand Down
4 changes: 4 additions & 0 deletions servers/physics_2d/physics_2d_server_wrap_mt.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ class Physics2DServerWrapMT : public Physics2DServer {
FUNC2(body_set_applied_torque, RID, real_t);
FUNC1RC(real_t, body_get_applied_torque, RID);

FUNC2(body_add_central_force, RID, const Vector2 &);
FUNC3(body_add_force, RID, const Vector2 &, const Vector2 &);
FUNC2(body_add_torque, RID, real_t);
FUNC2(body_apply_central_impulse, RID, const Vector2 &);
FUNC2(body_apply_torque_impulse, RID, real_t);
FUNC3(body_apply_impulse, RID, const Vector2 &, const Vector2 &);
FUNC2(body_set_axis_velocity, RID, const Vector2 &);

Expand Down
Loading

0 comments on commit f778bd8

Please sign in to comment.