Skip to content

Commit

Permalink
Merge pull request #52668 from qarmin/cppcheck_servers_physics
Browse files Browse the repository at this point in the history
Initialize variables in servers/physics
  • Loading branch information
pouleyKetchoupp authored Sep 16, 2021
2 parents 104a619 + 91257c3 commit 062cff3
Show file tree
Hide file tree
Showing 53 changed files with 363 additions and 625 deletions.
11 changes: 0 additions & 11 deletions servers/physics_2d/area_2d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,6 @@ Area2DSW::Area2DSW() :
monitor_query_list(this),
moved_list(this) {
_set_static(true); //areas are not active by default
space_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
gravity = 9.80665;
gravity_vector = Vector2(0, -1);
gravity_is_point = false;
gravity_distance_scale = 0;
point_attenuation = 1;

angular_damp = 1.0;
linear_damp = 0.1;
priority = 0;
monitorable = false;
}

Area2DSW::~Area2DSW() {
Expand Down
27 changes: 13 additions & 14 deletions servers/physics_2d/area_2d_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ class Body2DSW;
class Constraint2DSW;

class Area2DSW : public CollisionObject2DSW {
PhysicsServer2D::AreaSpaceOverrideMode space_override_mode;
real_t gravity;
Vector2 gravity_vector;
bool gravity_is_point;
real_t gravity_distance_scale;
real_t point_attenuation;
real_t linear_damp;
real_t angular_damp;
int priority;
bool monitorable;
PhysicsServer2D::AreaSpaceOverrideMode space_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
real_t gravity = 9.80665;
Vector2 gravity_vector = Vector2(0, -1);
bool gravity_is_point = false;
real_t gravity_distance_scale = 0.0;
real_t point_attenuation = 1.0;
real_t linear_damp = 0.1;
real_t angular_damp = 1.0;
int priority = 0;
bool monitorable = false;

ObjectID monitor_callback_id;
StringName monitor_callback_method;
Expand All @@ -63,8 +63,8 @@ class Area2DSW : public CollisionObject2DSW {
struct BodyKey {
RID rid;
ObjectID instance_id;
uint32_t body_shape;
uint32_t area_shape;
uint32_t body_shape = 0;
uint32_t area_shape = 0;

_FORCE_INLINE_ bool operator<(const BodyKey &p_key) const {
if (rid == p_key.rid) {
Expand All @@ -84,10 +84,9 @@ class Area2DSW : public CollisionObject2DSW {
};

struct BodyState {
int state;
int state = 0;
_FORCE_INLINE_ void inc() { state++; }
_FORCE_INLINE_ void dec() { state--; }
_FORCE_INLINE_ BodyState() { state = 0; }
};

Map<BodyKey, BodyState> monitored_bodies;
Expand Down
10 changes: 5 additions & 5 deletions servers/physics_2d/body_2d_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class Body2DSW : public CollisionObject2DSW {
List<Pair<Constraint2DSW *, int>> constraint_list;

struct AreaCMP {
Area2DSW *area;
int refCount;
Area2DSW *area = nullptr;
int refCount = 0;
_FORCE_INLINE_ bool operator==(const AreaCMP &p_cmp) const { return area->get_self() == p_cmp.area->get_self(); }
_FORCE_INLINE_ bool operator<(const AreaCMP &p_cmp) const { return area->get_priority() < p_cmp.area->get_priority(); }
_FORCE_INLINE_ AreaCMP() {}
Expand All @@ -112,10 +112,10 @@ class Body2DSW : public CollisionObject2DSW {
struct Contact {
Vector2 local_pos;
Vector2 local_normal;
real_t depth;
int local_shape;
real_t depth = 0.0;
int local_shape = 0;
Vector2 collider_pos;
int collider_shape;
int collider_shape = 0;
ObjectID collider_instance_id;
RID collider;
Vector2 collider_velocity_at_pos;
Expand Down
18 changes: 9 additions & 9 deletions servers/physics_2d/body_pair_2d_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ class BodyPair2DSW : public Constraint2DSW {
Vector2 position;
Vector2 normal;
Vector2 local_A, local_B;
real_t acc_normal_impulse; // accumulated normal impulse (Pn)
real_t acc_tangent_impulse; // accumulated tangent impulse (Pt)
real_t acc_bias_impulse; // accumulated normal impulse for position bias (Pnb)
real_t mass_normal, mass_tangent;
real_t bias;
real_t acc_normal_impulse = 0.0; // accumulated normal impulse (Pn)
real_t acc_tangent_impulse = 0.0; // accumulated tangent impulse (Pt)
real_t acc_bias_impulse = 0.0; // accumulated normal impulse for position bias (Pnb)
real_t mass_normal, mass_tangent = 0.0;
real_t bias = 0.0;

real_t depth;
bool active;
real_t depth = 0.0;
bool active = false;
Vector2 rA, rB;
bool reused;
real_t bounce;
bool reused = false;
real_t bounce = 0.0;
};

Vector2 offset_B; //use local A coordinates to avoid numerical issues on collision detection
Expand Down
3 changes: 0 additions & 3 deletions servers/physics_2d/broad_phase_2d_bvh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,4 @@ BroadPhase2DSW *BroadPhase2DBVH::_create() {
BroadPhase2DBVH::BroadPhase2DBVH() {
bvh.set_pair_callback(_pair_callback, this);
bvh.set_unpair_callback(_unpair_callback, this);
pair_callback = nullptr;
pair_userdata = nullptr;
unpair_userdata = nullptr;
}
8 changes: 4 additions & 4 deletions servers/physics_2d/broad_phase_2d_bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class BroadPhase2DBVH : public BroadPhase2DSW {
static void *_pair_callback(void *, uint32_t, CollisionObject2DSW *, int, uint32_t, CollisionObject2DSW *, int);
static void _unpair_callback(void *, uint32_t, CollisionObject2DSW *, int, uint32_t, CollisionObject2DSW *, int, void *);

PairCallback pair_callback;
void *pair_userdata;
UnpairCallback unpair_callback;
void *unpair_userdata;
PairCallback pair_callback = nullptr;
void *pair_userdata = nullptr;
UnpairCallback unpair_callback = nullptr;
void *unpair_userdata = nullptr;

public:
// 0 is an invalid ID
Expand Down
5 changes: 0 additions & 5 deletions servers/physics_2d/collision_object_2d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,5 @@ void CollisionObject2DSW::_shape_changed() {

CollisionObject2DSW::CollisionObject2DSW(Type p_type) :
pending_shape_update_list(this) {
_static = true;
type = p_type;
space = nullptr;
collision_mask = 1;
collision_layer = 1;
pickable = true;
}
25 changes: 10 additions & 15 deletions servers/physics_2d/collision_object_2d_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,27 @@ class CollisionObject2DSW : public ShapeOwner2DSW {
RID self;
ObjectID instance_id;
ObjectID canvas_instance_id;
bool pickable;
bool pickable = true;

struct Shape {
Transform2D xform;
Transform2D xform_inv;
BroadPhase2DSW::ID bpid;
BroadPhase2DSW::ID bpid = 0;
Rect2 aabb_cache; //for rayqueries
Shape2DSW *shape;
Shape2DSW *shape = nullptr;
Variant metadata;
bool disabled;
bool one_way_collision;
real_t one_way_collision_margin;
Shape() {
disabled = false;
one_way_collision = false;
one_way_collision_margin = 0;
}
bool disabled = false;
bool one_way_collision = false;
real_t one_way_collision_margin = 0.0;
};

Vector<Shape> shapes;
Space2DSW *space;
Space2DSW *space = nullptr;
Transform2D transform;
Transform2D inv_transform;
uint32_t collision_mask;
uint32_t collision_layer;
bool _static;
uint32_t collision_mask = 1;
uint32_t collision_layer = 1;
bool _static = true;

SelfList<CollisionObject2DSW> pending_shape_update_list;

Expand Down
48 changes: 22 additions & 26 deletions servers/physics_2d/collision_solver_2d_sat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@

struct _CollectorCallback2D {
CollisionSolver2DSW::CallbackResult callback;
void *userdata;
bool swap;
bool collided;
void *userdata = nullptr;
bool swap = false;
bool collided = false;
Vector2 normal;
Vector2 *sep_axis;
Vector2 *sep_axis = nullptr;

_FORCE_INLINE_ void call(const Vector2 &p_point_A, const Vector2 &p_point_B) {
/*
Expand Down Expand Up @@ -75,9 +75,9 @@ _FORCE_INLINE_ static void _generate_contacts_point_edge(const Vector2 *p_points
}

struct _generate_contacts_Pair {
bool a;
int idx;
real_t d;
bool a = false;
int idx = 0;
real_t d = 0.0;
_FORCE_INLINE_ bool operator<(const _generate_contacts_Pair &l) const { return d < l.d; }
};

Expand Down Expand Up @@ -146,10 +146,10 @@ static void _generate_contacts_from_supports(const Vector2 *p_points_A, int p_po
}
};

int pointcount_B;
int pointcount_A;
const Vector2 *points_A;
const Vector2 *points_B;
int pointcount_B = 0;
int pointcount_A = 0;
const Vector2 *points_A = nullptr;
const Vector2 *points_B = nullptr;

if (p_point_count_A > p_point_count_B) {
//swap
Expand Down Expand Up @@ -177,18 +177,20 @@ static void _generate_contacts_from_supports(const Vector2 *p_points_A, int p_po

template <class ShapeA, class ShapeB, bool castA = false, bool castB = false, bool withMargin = false>
class SeparatorAxisTest2D {
const ShapeA *shape_A;
const ShapeB *shape_B;
const Transform2D *transform_A;
const Transform2D *transform_B;
real_t best_depth;
const ShapeA *shape_A = nullptr;
const ShapeB *shape_B = nullptr;
const Transform2D *transform_A = nullptr;
const Transform2D *transform_B = nullptr;
real_t best_depth = 1e15;
Vector2 best_axis;
int best_axis_count;
int best_axis_index;
#ifdef DEBUG_ENABLED
int best_axis_count = 0;
int best_axis_index = -1;
#endif
Vector2 motion_A;
Vector2 motion_B;
real_t margin_A;
real_t margin_B;
real_t margin_A = 0.0;
real_t margin_B = 0.0;
_CollectorCallback2D *callback;

public:
Expand Down Expand Up @@ -364,19 +366,13 @@ class SeparatorAxisTest2D {
_FORCE_INLINE_ SeparatorAxisTest2D(const ShapeA *p_shape_A, const Transform2D &p_transform_a, const ShapeB *p_shape_B, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_A = Vector2(), const Vector2 &p_motion_B = Vector2(), real_t p_margin_A = 0, real_t p_margin_B = 0) {
margin_A = p_margin_A;
margin_B = p_margin_B;
best_depth = 1e15;
shape_A = p_shape_A;
shape_B = p_shape_B;
transform_A = &p_transform_a;
transform_B = &p_transform_b;
motion_A = p_motion_A;
motion_B = p_motion_B;

callback = p_collector;
#ifdef DEBUG_ENABLED
best_axis_count = 0;
best_axis_index = -1;
#endif
}
};

Expand Down
22 changes: 11 additions & 11 deletions servers/physics_2d/collision_solver_2d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,20 @@ bool CollisionSolver2DSW::solve_separation_ray(const Shape2DSW *p_shape_A, const
}

struct _ConcaveCollisionInfo2D {
const Transform2D *transform_A;
const Shape2DSW *shape_A;
const Transform2D *transform_B;
const Transform2D *transform_A = nullptr;
const Shape2DSW *shape_A = nullptr;
const Transform2D *transform_B = nullptr;
Vector2 motion_A;
Vector2 motion_B;
real_t margin_A;
real_t margin_B;
real_t margin_A = 0.0;
real_t margin_B = 0.0;
CollisionSolver2DSW::CallbackResult result_callback;
void *userdata;
bool swap_result;
bool collided;
int aabb_tests;
int collisions;
Vector2 *sep_axis;
void *userdata = nullptr;
bool swap_result = false;
bool collided = false;
int aabb_tests = 0;
int collisions = 0;
Vector2 *sep_axis = nullptr;
};

bool CollisionSolver2DSW::concave_callback(void *p_userdata, Shape2DSW *p_convex) {
Expand Down
6 changes: 2 additions & 4 deletions servers/physics_2d/constraint_2d_sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,15 @@
class Constraint2DSW {
Body2DSW **_body_ptr;
int _body_count;
uint64_t island_step;
bool disabled_collisions_between_bodies;
uint64_t island_step = 0;
bool disabled_collisions_between_bodies = true;

RID self;

protected:
Constraint2DSW(Body2DSW **p_body_ptr = nullptr, int p_body_count = 0) {
_body_ptr = p_body_ptr;
_body_count = p_body_count;
island_step = 0;
disabled_collisions_between_bodies = true;
}

public:
Expand Down
6 changes: 1 addition & 5 deletions servers/physics_2d/joints_2d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void Joint2DSW::copy_settings_from(Joint2DSW *p_joint) {
}

static inline real_t k_scalar(Body2DSW *a, Body2DSW *b, const Vector2 &rA, const Vector2 &rB, const Vector2 &n) {
real_t value = 0;
real_t value = 0.0;

{
value += a->get_inv_mass();
Expand Down Expand Up @@ -213,8 +213,6 @@ PinJoint2DSW::PinJoint2DSW(const Vector2 &p_pos, Body2DSW *p_body_a, Body2DSW *p
anchor_A = p_body_a->get_inv_transform().xform(p_pos);
anchor_B = p_body_b ? p_body_b->get_inv_transform().xform(p_pos) : p_pos;

softness = 0;

p_body_a->add_constraint(this, 0);
if (p_body_b) {
p_body_b->add_constraint(this, 1);
Expand Down Expand Up @@ -482,8 +480,6 @@ DampedSpringJoint2DSW::DampedSpringJoint2DSW(const Vector2 &p_anchor_a, const Ve
anchor_B = B->get_inv_transform().xform(p_anchor_b);

rest_length = p_anchor_a.distance_to(p_anchor_b);
stiffness = 20;
damping = 1.5;

A->add_constraint(this, 0);
B->add_constraint(this, 1);
Expand Down
Loading

0 comments on commit 062cff3

Please sign in to comment.