summaryrefslogtreecommitdiff
path: root/servers/physics_2d/body_2d_sw.h
diff options
context:
space:
mode:
Diffstat (limited to 'servers/physics_2d/body_2d_sw.h')
-rw-r--r--servers/physics_2d/body_2d_sw.h105
1 files changed, 49 insertions, 56 deletions
diff --git a/servers/physics_2d/body_2d_sw.h b/servers/physics_2d/body_2d_sw.h
index 0514b263b4..4a3ef718ec 100644
--- a/servers/physics_2d/body_2d_sw.h
+++ b/servers/physics_2d/body_2d_sw.h
@@ -38,7 +38,6 @@
class Constraint2DSW;
class Body2DSW : public CollisionObject2DSW {
-
PhysicsServer2D::BodyMode mode;
Vector2 biased_linear_velocity;
@@ -87,7 +86,6 @@ class Body2DSW : public CollisionObject2DSW {
Map<Constraint2DSW *, int> constraint_map;
struct AreaCMP {
-
Area2DSW *area;
int refCount;
_FORCE_INLINE_ bool operator==(const AreaCMP &p_cmp) const { return area->get_self() == p_cmp.area->get_self(); }
@@ -102,7 +100,6 @@ class Body2DSW : public CollisionObject2DSW {
Vector<AreaCMP> areas;
struct Contact {
-
Vector2 local_pos;
Vector2 local_normal;
real_t depth;
@@ -118,7 +115,6 @@ class Body2DSW : public CollisionObject2DSW {
int contact_count;
struct ForceIntegrationCallback {
-
ObjectID id;
StringName method;
Variant callback_udata;
@@ -150,15 +146,18 @@ public:
int index = areas.find(AreaCMP(p_area));
if (index > -1) {
areas.write[index].refCount -= 1;
- if (areas[index].refCount < 1)
+ if (areas[index].refCount < 1) {
areas.remove(index);
+ }
}
}
_FORCE_INLINE_ void set_max_contacts_reported(int p_size) {
contacts.resize(p_size);
contact_count = 0;
- if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC && p_size) set_active(true);
+ if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC && p_size) {
+ set_active(true);
+ }
}
_FORCE_INLINE_ int get_max_contacts_reported() const { return contacts.size(); }
@@ -204,28 +203,27 @@ 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);
_FORCE_INLINE_ bool is_active() const { return active; }
_FORCE_INLINE_ void wakeup() {
- if ((!get_space()) || mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC)
+ if ((!get_space()) || mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
return;
+ }
set_active(true);
}
@@ -248,10 +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) {
@@ -277,7 +274,6 @@ public:
void integrate_velocities(real_t p_step);
_FORCE_INLINE_ Vector2 get_motion() const {
-
if (mode > PhysicsServer2D::BODY_MODE_KINEMATIC) {
return new_transform.get_origin() - get_transform().get_origin();
} else if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
@@ -298,11 +294,11 @@ public:
//add contact inline
void Body2DSW::add_contact(const Vector2 &p_local_pos, const Vector2 &p_local_normal, real_t p_depth, int p_local_shape, const Vector2 &p_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector2 &p_collider_velocity_at_pos) {
-
int c_max = contacts.size();
- if (c_max == 0)
+ if (c_max == 0) {
return;
+ }
Contact *c = contacts.ptrw();
@@ -311,11 +307,9 @@ void Body2DSW::add_contact(const Vector2 &p_local_pos, const Vector2 &p_local_no
if (contact_count < c_max) {
idx = contact_count++;
} else {
-
real_t least_depth = 1e20;
int least_deep = -1;
for (int i = 0; i < c_max; i++) {
-
if (i == 0 || c[i].depth < least_depth) {
least_deep = i;
least_depth = c[i].depth;
@@ -323,11 +317,11 @@ void Body2DSW::add_contact(const Vector2 &p_local_pos, const Vector2 &p_local_no
}
if (least_deep >= 0 && least_depth < p_depth) {
-
idx = least_deep;
}
- if (idx == -1)
+ if (idx == -1) {
return; //none least deepe than this
+ }
}
c[idx].local_pos = p_local_pos;
@@ -342,7 +336,6 @@ void Body2DSW::add_contact(const Vector2 &p_local_pos, const Vector2 &p_local_no
}
class PhysicsDirectBodyState2DSW : public PhysicsDirectBodyState2D {
-
GDCLASS(PhysicsDirectBodyState2DSW, PhysicsDirectBodyState2D);
public:
@@ -350,73 +343,73 @@ public:
Body2DSW *body;
real_t step;
- virtual Vector2 get_total_gravity() const { return body->gravity; } // get gravity vector working on this body space/area
- virtual real_t get_total_angular_damp() const { return body->area_angular_damp; } // get density of this body space/area
- virtual real_t get_total_linear_damp() const { return body->area_linear_damp; } // get density of this body space/area
+ virtual Vector2 get_total_gravity() const override { return body->gravity; } // get gravity vector working on this body space/area
+ virtual real_t get_total_angular_damp() const override { return body->area_angular_damp; } // get density of this body space/area
+ virtual real_t get_total_linear_damp() const override { return body->area_linear_damp; } // get density of this body space/area
- virtual real_t get_inverse_mass() const { return body->get_inv_mass(); } // get the mass
- virtual real_t get_inverse_inertia() const { return body->get_inv_inertia(); } // get density of this body space
+ virtual real_t get_inverse_mass() const override { return body->get_inv_mass(); } // get the mass
+ virtual real_t get_inverse_inertia() const override { return body->get_inv_inertia(); } // get density of this body space
- virtual void set_linear_velocity(const Vector2 &p_velocity) { body->set_linear_velocity(p_velocity); }
- virtual Vector2 get_linear_velocity() const { return body->get_linear_velocity(); }
+ virtual void set_linear_velocity(const Vector2 &p_velocity) override { body->set_linear_velocity(p_velocity); }
+ virtual Vector2 get_linear_velocity() const override { return body->get_linear_velocity(); }
- virtual void set_angular_velocity(real_t p_velocity) { body->set_angular_velocity(p_velocity); }
- virtual real_t get_angular_velocity() const { return body->get_angular_velocity(); }
+ virtual void set_angular_velocity(real_t p_velocity) override { body->set_angular_velocity(p_velocity); }
+ virtual real_t get_angular_velocity() const override { return body->get_angular_velocity(); }
- virtual void set_transform(const Transform2D &p_transform) { body->set_state(PhysicsServer2D::BODY_STATE_TRANSFORM, p_transform); }
- virtual Transform2D get_transform() const { return body->get_transform(); }
+ virtual void set_transform(const Transform2D &p_transform) override { body->set_state(PhysicsServer2D::BODY_STATE_TRANSFORM, p_transform); }
+ virtual Transform2D get_transform() const override { 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 add_central_force(const Vector2 &p_force) override { body->add_central_force(p_force); }
+ virtual void add_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) override { body->add_force(p_force, p_position); }
+ virtual void add_torque(real_t p_torque) override { body->add_torque(p_torque); }
+ virtual void apply_central_impulse(const Vector2 &p_impulse) override { body->apply_central_impulse(p_impulse); }
+ virtual void apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) override { body->apply_impulse(p_impulse, p_position); }
+ virtual void apply_torque_impulse(real_t p_torque) override { 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(); }
+ virtual void set_sleep_state(bool p_enable) override { body->set_active(!p_enable); }
+ virtual bool is_sleeping() const override { return !body->is_active(); }
- virtual int get_contact_count() const { return body->contact_count; }
+ virtual int get_contact_count() const override { return body->contact_count; }
- virtual Vector2 get_contact_local_position(int p_contact_idx) const {
+ virtual Vector2 get_contact_local_position(int p_contact_idx) const override {
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
return body->contacts[p_contact_idx].local_pos;
}
- virtual Vector2 get_contact_local_normal(int p_contact_idx) const {
+ virtual Vector2 get_contact_local_normal(int p_contact_idx) const override {
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
return body->contacts[p_contact_idx].local_normal;
}
- virtual int get_contact_local_shape(int p_contact_idx) const {
+ virtual int get_contact_local_shape(int p_contact_idx) const override {
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, -1);
return body->contacts[p_contact_idx].local_shape;
}
- virtual RID get_contact_collider(int p_contact_idx) const {
+ virtual RID get_contact_collider(int p_contact_idx) const override {
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, RID());
return body->contacts[p_contact_idx].collider;
}
- virtual Vector2 get_contact_collider_position(int p_contact_idx) const {
+ virtual Vector2 get_contact_collider_position(int p_contact_idx) const override {
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
return body->contacts[p_contact_idx].collider_pos;
}
- virtual ObjectID get_contact_collider_id(int p_contact_idx) const {
+ virtual ObjectID get_contact_collider_id(int p_contact_idx) const override {
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, ObjectID());
return body->contacts[p_contact_idx].collider_instance_id;
}
- virtual int get_contact_collider_shape(int p_contact_idx) const {
+ virtual int get_contact_collider_shape(int p_contact_idx) const override {
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0);
return body->contacts[p_contact_idx].collider_shape;
}
- virtual Variant get_contact_collider_shape_metadata(int p_contact_idx) const;
+ virtual Variant get_contact_collider_shape_metadata(int p_contact_idx) const override;
- virtual Vector2 get_contact_collider_velocity_at_position(int p_contact_idx) const {
+ virtual Vector2 get_contact_collider_velocity_at_position(int p_contact_idx) const override {
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector2());
return body->contacts[p_contact_idx].collider_velocity_at_pos;
}
- virtual PhysicsDirectSpaceState2D *get_space_state();
+ virtual PhysicsDirectSpaceState2D *get_space_state() override;
- virtual real_t get_step() const { return step; }
+ virtual real_t get_step() const override { return step; }
PhysicsDirectBodyState2DSW() {
singleton = this;
body = nullptr;