Skip to content

Commit

Permalink
Prevent setting too big or too small Collision Mask and Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
qarmin authored and Calinou committed Jun 27, 2021
1 parent 3a8bea3 commit cb5faca
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 16 deletions.
12 changes: 8 additions & 4 deletions modules/csg/csg_shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ uint32_t CSGShape::get_collision_mask() const {
}

void CSGShape::set_collision_mask_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
uint32_t mask = get_collision_mask();
if (p_value) {
mask |= 1 << p_bit;
Expand All @@ -97,20 +98,23 @@ void CSGShape::set_collision_mask_bit(int p_bit, bool p_value) {
}

bool CSGShape::get_collision_mask_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
return get_collision_mask() & (1 << p_bit);
}

void CSGShape::set_collision_layer_bit(int p_bit, bool p_value) {
uint32_t mask = get_collision_layer();
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision layer bit must be between 0 and 31 inclusive.");
uint32_t layer = get_collision_layer();
if (p_value) {
mask |= 1 << p_bit;
layer |= 1 << p_bit;
} else {
mask &= ~(1 << p_bit);
layer &= ~(1 << p_bit);
}
set_collision_layer(mask);
set_collision_layer(layer);
}

bool CSGShape::get_collision_layer_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision layer bit must be between 0 and 31 inclusive.");
return get_collision_layer() & (1 << p_bit);
}

Expand Down
12 changes: 8 additions & 4 deletions modules/gridmap/grid_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ uint32_t GridMap::get_collision_mask() const {
}

void GridMap::set_collision_mask_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
uint32_t mask = get_collision_mask();
if (p_value) {
mask |= 1 << p_bit;
Expand All @@ -161,20 +162,23 @@ void GridMap::set_collision_mask_bit(int p_bit, bool p_value) {
}

bool GridMap::get_collision_mask_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
return get_collision_mask() & (1 << p_bit);
}

void GridMap::set_collision_layer_bit(int p_bit, bool p_value) {
uint32_t mask = get_collision_layer();
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision layer bit must be between 0 and 31 inclusive.");
uint32_t layer = get_collision_layer();
if (p_value) {
mask |= 1 << p_bit;
layer |= 1 << p_bit;
} else {
mask &= ~(1 << p_bit);
layer &= ~(1 << p_bit);
}
set_collision_layer(mask);
set_collision_layer(layer);
}

bool GridMap::get_collision_layer_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision layer bit must be between 0 and 31 inclusive.");
return get_collision_layer() & (1 << p_bit);
}

Expand Down
4 changes: 4 additions & 0 deletions scene/2d/collision_object_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ uint32_t CollisionObject2D::get_collision_mask() const {
}

void CollisionObject2D::set_collision_layer_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision layer bit must be between 0 and 31 inclusive.");
uint32_t collision_layer = get_collision_layer();
if (p_value) {
collision_layer |= 1 << p_bit;
Expand All @@ -137,10 +138,12 @@ void CollisionObject2D::set_collision_layer_bit(int p_bit, bool p_value) {
}

bool CollisionObject2D::get_collision_layer_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision layer bit must be between 0 and 31 inclusive.");
return get_collision_layer() & (1 << p_bit);
}

void CollisionObject2D::set_collision_mask_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
uint32_t mask = get_collision_mask();
if (p_value) {
mask |= 1 << p_bit;
Expand All @@ -151,6 +154,7 @@ void CollisionObject2D::set_collision_mask_bit(int p_bit, bool p_value) {
}

bool CollisionObject2D::get_collision_mask_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
return get_collision_mask() & (1 << p_bit);
}

Expand Down
2 changes: 2 additions & 0 deletions scene/2d/ray_cast_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ uint32_t RayCast2D::get_collision_mask() const {
}

void RayCast2D::set_collision_mask_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
uint32_t mask = get_collision_mask();
if (p_value) {
mask |= 1 << p_bit;
Expand All @@ -65,6 +66,7 @@ void RayCast2D::set_collision_mask_bit(int p_bit, bool p_value) {
}

bool RayCast2D::get_collision_mask_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
return get_collision_mask() & (1 << p_bit);
}

Expand Down
4 changes: 4 additions & 0 deletions scene/2d/tile_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ void TileMap::set_collision_mask(uint32_t p_mask) {
}

void TileMap::set_collision_layer_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision layer bit must be between 0 and 31 inclusive.");
uint32_t layer = get_collision_layer();
if (p_value) {
layer |= 1 << p_bit;
Expand All @@ -1292,6 +1293,7 @@ void TileMap::set_collision_layer_bit(int p_bit, bool p_value) {
}

void TileMap::set_collision_mask_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
uint32_t mask = get_collision_mask();
if (p_value) {
mask |= 1 << p_bit;
Expand Down Expand Up @@ -1372,10 +1374,12 @@ uint32_t TileMap::get_collision_mask() const {
}

bool TileMap::get_collision_layer_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision layer bit must be between 0 and 31 inclusive.");
return get_collision_layer() & (1 << p_bit);
}

bool TileMap::get_collision_mask_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
return get_collision_mask() & (1 << p_bit);
}

Expand Down
2 changes: 2 additions & 0 deletions scene/3d/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ uint32_t ClippedCamera::get_collision_mask() const {
}

void ClippedCamera::set_collision_mask_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision layer bit must be between 0 and 31 inclusive.");
uint32_t mask = get_collision_mask();
if (p_value) {
mask |= 1 << p_bit;
Expand All @@ -781,6 +782,7 @@ void ClippedCamera::set_collision_mask_bit(int p_bit, bool p_value) {
}

bool ClippedCamera::get_collision_mask_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
return get_collision_mask() & (1 << p_bit);
}

Expand Down
4 changes: 4 additions & 0 deletions scene/3d/collision_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ uint32_t CollisionObject::get_collision_mask() const {
}

void CollisionObject::set_collision_layer_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision layer bit must be between 0 and 31 inclusive.");
uint32_t collision_layer = get_collision_layer();
if (p_value) {
collision_layer |= 1 << p_bit;
Expand All @@ -133,10 +134,12 @@ void CollisionObject::set_collision_layer_bit(int p_bit, bool p_value) {
}

bool CollisionObject::get_collision_layer_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision layer bit must be between 0 and 31 inclusive.");
return get_collision_layer() & (1 << p_bit);
}

void CollisionObject::set_collision_mask_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
uint32_t mask = get_collision_mask();
if (p_value) {
mask |= 1 << p_bit;
Expand All @@ -147,6 +150,7 @@ void CollisionObject::set_collision_mask_bit(int p_bit, bool p_value) {
}

bool CollisionObject::get_collision_mask_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
return get_collision_mask() & (1 << p_bit);
}

Expand Down
2 changes: 2 additions & 0 deletions scene/3d/navigation_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ uint32_t NavigationMesh::get_collision_mask() const {
}

void NavigationMesh::set_collision_mask_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
uint32_t mask = get_collision_mask();
if (p_value) {
mask |= 1 << p_bit;
Expand All @@ -104,6 +105,7 @@ void NavigationMesh::set_collision_mask_bit(int p_bit, bool p_value) {
}

bool NavigationMesh::get_collision_mask_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
return get_collision_mask() & (1 << p_bit);
}

Expand Down
2 changes: 2 additions & 0 deletions scene/3d/ray_cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ uint32_t RayCast::get_collision_mask() const {
}

void RayCast::set_collision_mask_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
uint32_t mask = get_collision_mask();
if (p_value) {
mask |= 1 << p_bit;
Expand All @@ -71,6 +72,7 @@ void RayCast::set_collision_mask_bit(int p_bit, bool p_value) {
}

bool RayCast::get_collision_mask_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
return get_collision_mask() & (1 << p_bit);
}

Expand Down
4 changes: 4 additions & 0 deletions scene/3d/soft_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ uint32_t SoftBody::get_collision_layer() const {
}

void SoftBody::set_collision_mask_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision mask bit must be between 0 and 31 inclusive.");
uint32_t mask = get_collision_mask();
if (p_value) {
mask |= 1 << p_bit;
Expand All @@ -522,10 +523,12 @@ void SoftBody::set_collision_mask_bit(int p_bit, bool p_value) {
}

bool SoftBody::get_collision_mask_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
return get_collision_mask() & (1 << p_bit);
}

void SoftBody::set_collision_layer_bit(int p_bit, bool p_value) {
ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision layer bit must be between 0 and 31 inclusive.");
uint32_t layer = get_collision_layer();
if (p_value) {
layer |= 1 << p_bit;
Expand All @@ -536,6 +539,7 @@ void SoftBody::set_collision_layer_bit(int p_bit, bool p_value) {
}

bool SoftBody::get_collision_layer_bit(int p_bit) const {
ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision layer bit must be between 0 and 31 inclusive.");
return get_collision_layer() & (1 << p_bit);
}

Expand Down
5 changes: 3 additions & 2 deletions servers/physics_2d_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,11 @@ float Physics2DShapeQueryParameters::get_margin() const {
return margin;
}

void Physics2DShapeQueryParameters::set_collision_mask(int p_collision_mask) {
void Physics2DShapeQueryParameters::set_collision_mask(uint32_t p_collision_mask) {
collision_mask = p_collision_mask;
}
int Physics2DShapeQueryParameters::get_collision_mask() const {

uint32_t Physics2DShapeQueryParameters::get_collision_mask() const {
return collision_mask;
}

Expand Down
4 changes: 2 additions & 2 deletions servers/physics_2d_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ class Physics2DShapeQueryParameters : public Reference {
void set_margin(float p_margin);
float get_margin() const;

void set_collision_mask(int p_collision_mask);
int get_collision_mask() const;
void set_collision_mask(uint32_t p_mask);
uint32_t get_collision_mask() const;

void set_collide_with_bodies(bool p_enable);
bool is_collide_with_bodies_enabled() const;
Expand Down
5 changes: 3 additions & 2 deletions servers/physics_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ float PhysicsShapeQueryParameters::get_margin() const {
return margin;
}

void PhysicsShapeQueryParameters::set_collision_mask(int p_collision_mask) {
void PhysicsShapeQueryParameters::set_collision_mask(uint32_t p_collision_mask) {
collision_mask = p_collision_mask;
}
int PhysicsShapeQueryParameters::get_collision_mask() const {

uint32_t PhysicsShapeQueryParameters::get_collision_mask() const {
return collision_mask;
}

Expand Down
4 changes: 2 additions & 2 deletions servers/physics_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ class PhysicsShapeQueryParameters : public Reference {
void set_margin(float p_margin);
float get_margin() const;

void set_collision_mask(int p_collision_mask);
int get_collision_mask() const;
void set_collision_mask(uint32_t p_collision_mask);
uint32_t get_collision_mask() const;

void set_exclude(const Vector<RID> &p_exclude);
Vector<RID> get_exclude() const;
Expand Down

0 comments on commit cb5faca

Please sign in to comment.