summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2018-01-03 11:19:09 +0100
committerGitHub <noreply@github.com>2018-01-03 11:19:09 +0100
commitfa9320cfc6a6662e6dd5692a5db4a2ad1c5947fc (patch)
tree1c0e047ad63b2277d4fd94615d5a2cc3c8c3a1e4
parent459ee51338ff9ffb8248a4351eee3b558438aa50 (diff)
parent5a15e6e75cad4f9051149b8330f3493870899ba5 (diff)
Merge pull request #15120 from AndreaCatania/gridm
Added layer and mask in the gridmap node
-rw-r--r--modules/gridmap/grid_map.cpp80
-rw-r--r--modules/gridmap/grid_map.h16
2 files changed, 96 insertions, 0 deletions
diff --git a/modules/gridmap/grid_map.cpp b/modules/gridmap/grid_map.cpp
index bd37118847..9a03bc410a 100644
--- a/modules/gridmap/grid_map.cpp
+++ b/modules/gridmap/grid_map.cpp
@@ -198,6 +198,58 @@ void GridMap::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
}
+void GridMap::set_collision_layer(uint32_t p_layer) {
+
+ collision_layer = p_layer;
+ _reset_physic_bodies_collision_filters();
+}
+
+uint32_t GridMap::get_collision_layer() const {
+
+ return collision_layer;
+}
+
+void GridMap::set_collision_mask(uint32_t p_mask) {
+
+ collision_mask = p_mask;
+ _reset_physic_bodies_collision_filters();
+}
+
+uint32_t GridMap::get_collision_mask() const {
+
+ return collision_mask;
+}
+
+void GridMap::set_collision_mask_bit(int p_bit, bool p_value) {
+
+ uint32_t mask = get_collision_mask();
+ if (p_value)
+ mask |= 1 << p_bit;
+ else
+ mask &= ~(1 << p_bit);
+ set_collision_mask(mask);
+}
+
+bool GridMap::get_collision_mask_bit(int p_bit) const {
+
+ 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();
+ if (p_value)
+ mask |= 1 << p_bit;
+ else
+ mask &= ~(1 << p_bit);
+ set_collision_layer(mask);
+}
+
+bool GridMap::get_collision_layer_bit(int p_bit) const {
+
+ return get_collision_layer() & (1 << p_bit);
+}
+
void GridMap::set_theme(const Ref<MeshLibrary> &p_theme) {
if (!theme.is_null())
@@ -311,6 +363,8 @@ void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
g->dirty = true;
g->static_body = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
PhysicsServer::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
+ PhysicsServer::get_singleton()->body_set_collision_layer(g->static_body, collision_layer);
+ PhysicsServer::get_singleton()->body_set_collision_mask(g->static_body, collision_mask);
SceneTree *st = SceneTree::get_singleton();
if (st && st->is_debugging_collisions_hint()) {
@@ -575,6 +629,13 @@ bool GridMap::_octant_update(const OctantKey &p_key) {
return false;
}
+void GridMap::_reset_physic_bodies_collision_filters() {
+ for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
+ PhysicsServer::get_singleton()->body_set_collision_layer(E->get()->static_body, collision_layer);
+ PhysicsServer::get_singleton()->body_set_collision_mask(E->get()->static_body, collision_mask);
+ }
+}
+
void GridMap::_octant_enter_world(const OctantKey &p_key) {
ERR_FAIL_COND(!octant_map.has(p_key));
@@ -815,6 +876,18 @@ void GridMap::_update_octants_callback() {
void GridMap::_bind_methods() {
+ ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
+ ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
+
+ ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
+ ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
+
+ ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &GridMap::set_collision_mask_bit);
+ ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &GridMap::get_collision_mask_bit);
+
+ ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &GridMap::set_collision_layer_bit);
+ ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &GridMap::get_collision_layer_bit);
+
ClassDB::bind_method(D_METHOD("set_theme", "theme"), &GridMap::set_theme);
ClassDB::bind_method(D_METHOD("get_theme"), &GridMap::get_theme);
@@ -855,6 +928,10 @@ void GridMap::_bind_methods() {
ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
+ ADD_GROUP("Collision", "collision_");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
+
BIND_CONSTANT(INVALID_CELL_ITEM);
}
@@ -1067,6 +1144,9 @@ RID GridMap::get_bake_mesh_instance(int p_idx) {
GridMap::GridMap() {
+ collision_layer = 1;
+ collision_mask = 1;
+
cell_size = Vector3(2, 2, 2);
octant_size = 8;
awaiting_update = false;
diff --git a/modules/gridmap/grid_map.h b/modules/gridmap/grid_map.h
index 18a9551edf..7b97fe3183 100644
--- a/modules/gridmap/grid_map.h
+++ b/modules/gridmap/grid_map.h
@@ -136,6 +136,9 @@ class GridMap : public Spatial {
OctantKey() { key = 0; }
};
+ uint32_t collision_layer;
+ uint32_t collision_mask;
+
Transform last_transform;
bool _in_tree;
@@ -173,6 +176,7 @@ class GridMap : public Spatial {
return Vector3(p_key.x, p_key.y, p_key.z) * cell_size * octant_size;
}
+ void _reset_physic_bodies_collision_filters();
void _octant_enter_world(const OctantKey &p_key);
void _octant_exit_world(const OctantKey &p_key);
bool _octant_update(const OctantKey &p_key);
@@ -210,6 +214,18 @@ public:
INVALID_CELL_ITEM = -1
};
+ void set_collision_layer(uint32_t p_layer);
+ uint32_t get_collision_layer() const;
+
+ void set_collision_mask(uint32_t p_mask);
+ uint32_t get_collision_mask() const;
+
+ void set_collision_layer_bit(int p_bit, bool p_value);
+ bool get_collision_layer_bit(int p_bit) const;
+
+ void set_collision_mask_bit(int p_bit, bool p_value);
+ bool get_collision_mask_bit(int p_bit) const;
+
void set_theme(const Ref<MeshLibrary> &p_theme);
Ref<MeshLibrary> get_theme() const;