From 9b149d5848985483d14a3d336d6b0d9b00c49282 Mon Sep 17 00:00:00 2001 From: kobewi Date: Fri, 17 Dec 2021 01:47:17 +0100 Subject: [PATCH] Add physics material to GridMap (cherry picked from commit 59a9b2192eb1287c4ec2fc99397903199b19df9a) --- modules/gridmap/doc_classes/GridMap.xml | 3 +++ modules/gridmap/grid_map.cpp | 18 ++++++++++++++++++ modules/gridmap/grid_map.h | 6 ++++++ 3 files changed, 27 insertions(+) diff --git a/modules/gridmap/doc_classes/GridMap.xml b/modules/gridmap/doc_classes/GridMap.xml index 8e67bc26056..8de0f434194 100644 --- a/modules/gridmap/doc_classes/GridMap.xml +++ b/modules/gridmap/doc_classes/GridMap.xml @@ -183,6 +183,9 @@ The assigned [MeshLibrary]. + + Overrides the default friction and bounce physics properties for the whole [GridMap]. + Controls whether this GridMap will be baked in a [BakedLightmap] or not. diff --git a/modules/gridmap/grid_map.cpp b/modules/gridmap/grid_map.cpp index ccac5db6d7f..6bae4607fd9 100644 --- a/modules/gridmap/grid_map.cpp +++ b/modules/gridmap/grid_map.cpp @@ -34,6 +34,7 @@ #include "core/message_queue.h" #include "scene/3d/light.h" #include "scene/resources/mesh_library.h" +#include "scene/resources/physics_material.h" #include "scene/resources/surface_tool.h" #include "scene/scene_string_names.h" #include "servers/navigation_server.h" @@ -183,6 +184,15 @@ bool GridMap::get_collision_layer_bit(int p_bit) const { return get_collision_layer() & (1 << p_bit); } +void GridMap::set_physics_material(Ref p_material) { + physics_material = p_material; + _recreate_octant_data(); +} + +Ref GridMap::get_physics_material() const { + return physics_material; +} + void GridMap::set_mesh_library(const Ref &p_mesh_library) { if (!mesh_library.is_null()) { mesh_library->unregister_owner(this); @@ -300,6 +310,10 @@ void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) { 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); + if (physics_material.is_valid()) { + PhysicsServer::get_singleton()->body_set_param(g->static_body, PhysicsServer::BODY_PARAM_FRICTION, physics_material->get_friction()); + PhysicsServer::get_singleton()->body_set_param(g->static_body, PhysicsServer::BODY_PARAM_BOUNCE, physics_material->get_bounce()); + } SceneTree *st = SceneTree::get_singleton(); if (st && st->is_debugging_collisions_hint()) { @@ -815,6 +829,9 @@ void GridMap::_bind_methods() { 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_physics_material", "material"), &GridMap::set_physics_material); + ClassDB::bind_method(D_METHOD("get_physics_material"), &GridMap::get_physics_material); + ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library); ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library); @@ -861,6 +878,7 @@ void GridMap::_bind_methods() { ClassDB::bind_method(D_METHOD("get_use_in_baked_light"), &GridMap::get_use_in_baked_light); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_in_baked_light"), "set_use_in_baked_light", "get_use_in_baked_light"); ADD_GROUP("Cell", "cell_"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size"), "set_cell_size", "get_cell_size"); diff --git a/modules/gridmap/grid_map.h b/modules/gridmap/grid_map.h index a5c2e3b2143..cfad7b302be 100644 --- a/modules/gridmap/grid_map.h +++ b/modules/gridmap/grid_map.h @@ -39,6 +39,8 @@ //heh heh, godotsphir!! this shares no code and the design is completely different with previous projects i've done.. //should scale better with hardware that supports instancing +class PhysicsMaterial; + class GridMap : public Spatial { GDCLASS(GridMap, Spatial); @@ -132,6 +134,7 @@ class GridMap : public Spatial { uint32_t collision_layer; uint32_t collision_mask; + Ref physics_material; Transform last_transform; @@ -219,6 +222,9 @@ public: void set_collision_mask_bit(int p_bit, bool p_value); bool get_collision_mask_bit(int p_bit) const; + void set_physics_material(Ref p_material); + Ref get_physics_material() const; + void set_mesh_library(const Ref &p_mesh_library); Ref get_mesh_library() const;