From 3eba84e1d778cc15459ee44449050d668e85ca79 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Mon, 30 Nov 2015 21:04:24 +0100 Subject: [PATCH 1/2] Properly update node after clearing shapes in _update_shapes_from_children() (fix bug causing eg. CircleShape2D.set_radius to generate multiple shapes) --- scene/2d/collision_object_2d.cpp | 4 ++-- scene/3d/collision_object.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scene/2d/collision_object_2d.cpp b/scene/2d/collision_object_2d.cpp index 8b8caf13d33..809d2a95fac 100644 --- a/scene/2d/collision_object_2d.cpp +++ b/scene/2d/collision_object_2d.cpp @@ -32,14 +32,14 @@ void CollisionObject2D::_update_shapes_from_children() { - shapes.resize(0); + shapes.clear(); for(int i=0;icall("_add_to_collision_object",this); } -// _update_shapes(); + _update_shapes(); } void CollisionObject2D::_notification(int p_what) { diff --git a/scene/3d/collision_object.cpp b/scene/3d/collision_object.cpp index 5ecadb48b82..5a89423e5a3 100644 --- a/scene/3d/collision_object.cpp +++ b/scene/3d/collision_object.cpp @@ -31,14 +31,14 @@ #include "scene/scene_string_names.h" void CollisionObject::_update_shapes_from_children() { - shapes.resize(0); + shapes.clear(); for(int i=0;icall("_add_to_collision_object",this); } -// _update_shapes(); + _update_shapes(); } void CollisionObject::_notification(int p_what) { From 1231c795dedbaaefae8c99f23f7547d320a4ffc9 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 1 Dec 2015 14:30:39 +0100 Subject: [PATCH 2/2] Fix Body (and Body2D) add_area / remove_area when area have multiple shapes --- servers/physics/body_sw.h | 22 +++++++++++++++++++--- servers/physics_2d/body_2d_sw.h | 22 +++++++++++++++++++--- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/servers/physics/body_sw.h b/servers/physics/body_sw.h index 66d814bfd16..1a45a02744c 100644 --- a/servers/physics/body_sw.h +++ b/servers/physics/body_sw.h @@ -91,10 +91,11 @@ class BodySW : public CollisionObjectSW { struct AreaCMP { AreaSW *area; + int refCount; _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() {} - _FORCE_INLINE_ AreaCMP(AreaSW *p_area) { area=p_area;} + _FORCE_INLINE_ AreaCMP(AreaSW *p_area) { area=p_area; refCount=1;} }; Vector areas; @@ -141,8 +142,23 @@ public: void set_force_integration_callback(ObjectID p_id,const StringName& p_method,const Variant& p_udata=Variant()); - _FORCE_INLINE_ void add_area(AreaSW *p_area) { areas.ordered_insert(AreaCMP(p_area)); } - _FORCE_INLINE_ void remove_area(AreaSW *p_area) { areas.erase(AreaCMP(p_area)); } + _FORCE_INLINE_ void add_area(AreaSW *p_area) { + int index = areas.find(AreaCMP(p_area)); + if( index > -1 ) { + areas[index].refCount += 1; + } else { + areas.ordered_insert(AreaCMP(p_area)); + } + } + + _FORCE_INLINE_ void remove_area(AreaSW *p_area) { + int index = areas.find(AreaCMP(p_area)); + if( index > -1 ) { + 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==PhysicsServer::BODY_MODE_KINEMATIC && p_size) set_active(true);} _FORCE_INLINE_ int get_max_contacts_reported() const { return contacts.size(); } diff --git a/servers/physics_2d/body_2d_sw.h b/servers/physics_2d/body_2d_sw.h index 2fbfcaca600..9720b265dcf 100644 --- a/servers/physics_2d/body_2d_sw.h +++ b/servers/physics_2d/body_2d_sw.h @@ -92,10 +92,11 @@ class Body2DSW : public CollisionObject2DSW { struct AreaCMP { Area2DSW *area; + int refCount; _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() {} - _FORCE_INLINE_ AreaCMP(Area2DSW *p_area) { area=p_area;} + _FORCE_INLINE_ AreaCMP(Area2DSW *p_area) { area=p_area; refCount=1;} }; @@ -142,8 +143,23 @@ public: void set_force_integration_callback(ObjectID p_id, const StringName& p_method, const Variant &p_udata=Variant()); - _FORCE_INLINE_ void add_area(Area2DSW *p_area) { areas.ordered_insert(AreaCMP(p_area)); } - _FORCE_INLINE_ void remove_area(Area2DSW *p_area) { areas.erase(AreaCMP(p_area)); } + _FORCE_INLINE_ void add_area(Area2DSW *p_area) { + int index = areas.find(AreaCMP(p_area)); + if( index > -1 ) { + areas[index].refCount += 1; + } else { + areas.ordered_insert(AreaCMP(p_area)); + } + } + + _FORCE_INLINE_ void remove_area(Area2DSW *p_area) { + int index = areas.find(AreaCMP(p_area)); + if( index > -1 ) { + 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==Physics2DServer::BODY_MODE_KINEMATIC && p_size) set_active(true);}