diff options
Diffstat (limited to 'scene/3d/body_shape.cpp')
-rw-r--r-- | scene/3d/body_shape.cpp | 166 |
1 files changed, 76 insertions, 90 deletions
diff --git a/scene/3d/body_shape.cpp b/scene/3d/body_shape.cpp index af3fe3c0a4..8403806570 100644 --- a/scene/3d/body_shape.cpp +++ b/scene/3d/body_shape.cpp @@ -27,14 +27,14 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "body_shape.h" -#include "servers/visual_server.h" -#include "scene/resources/sphere_shape.h" -#include "scene/resources/ray_shape.h" #include "scene/resources/box_shape.h" #include "scene/resources/capsule_shape.h" -#include "scene/resources/convex_polygon_shape.h" #include "scene/resources/concave_polygon_shape.h" +#include "scene/resources/convex_polygon_shape.h" #include "scene/resources/plane_shape.h" +#include "scene/resources/ray_shape.h" +#include "scene/resources/sphere_shape.h" +#include "servers/visual_server.h" //TODO: Implement CylinderShape and HeightMapShape? #include "mesh_instance.h" #include "physics_body.h" @@ -48,7 +48,6 @@ void CollisionShape::_update_body() { return; if (get_parent() && get_parent()->cast_to<CollisionObject>()) get_parent()->cast_to<CollisionObject>()->_update_shapes_from_children(); - } void CollisionShape::make_convex_from_brothers() { @@ -57,12 +56,12 @@ void CollisionShape::make_convex_from_brothers() { if (!p) return; - for(int i=0;i<p->get_child_count();i++) { + for (int i = 0; i < p->get_child_count(); i++) { Node *n = p->get_child(i); if (n->cast_to<MeshInstance>()) { - MeshInstance *mi=n->cast_to<MeshInstance>(); + MeshInstance *mi = n->cast_to<MeshInstance>(); Ref<Mesh> m = mi->get_mesh(); if (m.is_valid()) { @@ -71,7 +70,6 @@ void CollisionShape::make_convex_from_brothers() { } } } - } /* @@ -302,32 +300,32 @@ void CollisionShape::_update_indicator() { } */ -void CollisionShape::_add_to_collision_object(Object* p_cshape) { +void CollisionShape::_add_to_collision_object(Object *p_cshape) { if (unparenting) return; - CollisionObject *co=p_cshape->cast_to<CollisionObject>(); + CollisionObject *co = p_cshape->cast_to<CollisionObject>(); ERR_FAIL_COND(!co); if (shape.is_valid()) { - update_shape_index=co->get_shape_count(); - co->add_shape(shape,get_transform()); + update_shape_index = co->get_shape_count(); + co->add_shape(shape, get_transform()); if (trigger) - co->set_shape_as_trigger( co->get_shape_count() -1, true ); + co->set_shape_as_trigger(co->get_shape_count() - 1, true); } else { - update_shape_index=-1; + update_shape_index = -1; } } void CollisionShape::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_ENTER_TREE: { - unparenting=false; - can_update_body=get_tree()->is_editor_hint(); + unparenting = false; + can_update_body = get_tree()->is_editor_hint(); set_notify_local_transform(!can_update_body); if (get_tree()->is_debugging_collisions_hint()) { @@ -343,19 +341,19 @@ void CollisionShape::_notification(int p_what) { } } break; case NOTIFICATION_EXIT_TREE: { - /* if (indicator_instance.is_valid()) { + /* if (indicator_instance.is_valid()) { VisualServer::get_singleton()->free(indicator_instance); indicator_instance=RID(); }*/ - can_update_body=false; + can_update_body = false; set_notify_local_transform(false); if (debug_shape) { debug_shape->queue_delete(); - debug_shape=NULL; + debug_shape = NULL; } } break; case NOTIFICATION_UNPARENTED: { - unparenting=true; + unparenting = true; if (can_update_body && updating_body) _update_body(); } break; @@ -365,34 +363,29 @@ void CollisionShape::_notification(int p_what) { } break; case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: { - if (!can_update_body && update_shape_index>=0) { + if (!can_update_body && update_shape_index >= 0) { CollisionObject *co = get_parent()->cast_to<CollisionObject>(); if (co) { - co->set_shape_transform(update_shape_index,get_transform()); + co->set_shape_transform(update_shape_index, get_transform()); } } } break; - } } - void CollisionShape::resource_changed(RES res) { update_gizmo(); - - } void CollisionShape::_set_update_shape_index(int p_index) { - - update_shape_index=p_index; + update_shape_index = p_index; } -int CollisionShape::_get_update_shape_index() const{ +int CollisionShape::_get_update_shape_index() const { return update_shape_index; } @@ -410,45 +403,42 @@ String CollisionShape::get_configuration_warning() const { return String(); } - void CollisionShape::_bind_methods() { //not sure if this should do anything - ClassDB::bind_method(D_METHOD("resource_changed","resource"),&CollisionShape::resource_changed); - ClassDB::bind_method(D_METHOD("set_shape","shape"),&CollisionShape::set_shape); - ClassDB::bind_method(D_METHOD("get_shape"),&CollisionShape::get_shape); - ClassDB::bind_method(D_METHOD("_add_to_collision_object"),&CollisionShape::_add_to_collision_object); - ClassDB::bind_method(D_METHOD("set_trigger","enable"),&CollisionShape::set_trigger); - ClassDB::bind_method(D_METHOD("is_trigger"),&CollisionShape::is_trigger); - ClassDB::bind_method(D_METHOD("make_convex_from_brothers"),&CollisionShape::make_convex_from_brothers); - ClassDB::set_method_flags("CollisionShape","make_convex_from_brothers",METHOD_FLAGS_DEFAULT|METHOD_FLAG_EDITOR); - ClassDB::bind_method(D_METHOD("_set_update_shape_index","index"),&CollisionShape::_set_update_shape_index); - ClassDB::bind_method(D_METHOD("_get_update_shape_index"),&CollisionShape::_get_update_shape_index); + ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &CollisionShape::resource_changed); + ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape::set_shape); + ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape::get_shape); + ClassDB::bind_method(D_METHOD("_add_to_collision_object"), &CollisionShape::_add_to_collision_object); + ClassDB::bind_method(D_METHOD("set_trigger", "enable"), &CollisionShape::set_trigger); + ClassDB::bind_method(D_METHOD("is_trigger"), &CollisionShape::is_trigger); + ClassDB::bind_method(D_METHOD("make_convex_from_brothers"), &CollisionShape::make_convex_from_brothers); + ClassDB::set_method_flags("CollisionShape", "make_convex_from_brothers", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR); + ClassDB::bind_method(D_METHOD("_set_update_shape_index", "index"), &CollisionShape::_set_update_shape_index); + ClassDB::bind_method(D_METHOD("_get_update_shape_index"), &CollisionShape::_get_update_shape_index); - ClassDB::bind_method(D_METHOD("get_collision_object_shape_index"),&CollisionShape::get_collision_object_shape_index); + ClassDB::bind_method(D_METHOD("get_collision_object_shape_index"), &CollisionShape::get_collision_object_shape_index); - ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape"), "set_shape", "get_shape"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL,"trigger"),"set_trigger","is_trigger"); - ADD_PROPERTY( PropertyInfo( Variant::INT, "_update_shape_index", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_NOEDITOR), "_set_update_shape_index", "_get_update_shape_index"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape"), "set_shape", "get_shape"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trigger"), "set_trigger", "is_trigger"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "_update_shape_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_update_shape_index", "_get_update_shape_index"); } - void CollisionShape::set_shape(const Ref<Shape> &p_shape) { if (!shape.is_null()) shape->unregister_owner(this); - shape=p_shape; + shape = p_shape; if (!shape.is_null()) shape->register_owner(this); update_gizmo(); if (updating_body) { _update_body(); - } else if (can_update_body && update_shape_index>=0 && is_inside_tree()){ + } else if (can_update_body && update_shape_index >= 0 && is_inside_tree()) { CollisionObject *co = get_parent()->cast_to<CollisionObject>(); if (co) { - co->set_shape(update_shape_index,p_shape); + co->set_shape(update_shape_index, p_shape); } - } } @@ -457,9 +447,8 @@ Ref<Shape> CollisionShape::get_shape() const { return shape; } - void CollisionShape::set_updating_body(bool p_update) { - updating_body=p_update; + updating_body = p_update; } bool CollisionShape::is_updating_body() const { @@ -469,31 +458,31 @@ bool CollisionShape::is_updating_body() const { void CollisionShape::set_trigger(bool p_trigger) { - trigger=p_trigger; - if (updating_body) { - _update_body(); - } else if (can_update_body && update_shape_index>=0 && is_inside_tree()){ - CollisionObject *co = get_parent()->cast_to<CollisionObject>(); - if (co) { - co->set_shape_as_trigger(update_shape_index,p_trigger); - } - } + trigger = p_trigger; + if (updating_body) { + _update_body(); + } else if (can_update_body && update_shape_index >= 0 && is_inside_tree()) { + CollisionObject *co = get_parent()->cast_to<CollisionObject>(); + if (co) { + co->set_shape_as_trigger(update_shape_index, p_trigger); + } + } } -bool CollisionShape::is_trigger() const{ +bool CollisionShape::is_trigger() const { - return trigger; + return trigger; } CollisionShape::CollisionShape() { //indicator = VisualServer::get_singleton()->mesh_create(); - updating_body=true; - unparenting=false; - update_shape_index=-1; - trigger=false; - can_update_body=false; - debug_shape=NULL; + updating_body = true; + unparenting = false; + update_shape_index = -1; + trigger = false; + can_update_body = false; + debug_shape = NULL; } CollisionShape::~CollisionShape() { @@ -504,10 +493,9 @@ CollisionShape::~CollisionShape() { void CollisionShape::_create_debug_shape() { - if (debug_shape) { debug_shape->queue_delete(); - debug_shape=NULL; + debug_shape = NULL; } Ref<Shape> s = get_shape(); @@ -515,37 +503,35 @@ void CollisionShape::_create_debug_shape() { if (s.is_null()) return; - Ref<Mesh> mesh = s->get_debug_mesh(); - MeshInstance *mi = memnew( MeshInstance ); + MeshInstance *mi = memnew(MeshInstance); mi->set_mesh(mesh); add_child(mi); - debug_shape=mi; - + debug_shape = mi; } #if 0 #include "body_volume.h" -#include "scene/3d/physics_body.h" #include "geometry.h" +#include "scene/3d/physics_body.h" -#define ADD_TRIANGLE( m_a, m_b, m_c, m_color)\ -{\ - Vector<Vector3> points;\ - points.resize(3);\ - points[0]=m_a;\ - points[1]=m_b;\ - points[2]=m_c;\ - Vector<Color> colors;\ - colors.resize(3);\ - colors[0]=m_color;\ - colors[1]=m_color;\ - colors[2]=m_color;\ - vs->poly_add_primitive(p_indicator,points,Vector<Vector3>(),colors,Vector<Vector3>());\ -} +#define ADD_TRIANGLE(m_a, m_b, m_c, m_color) \ + { \ + Vector<Vector3> points; \ + points.resize(3); \ + points[0] = m_a; \ + points[1] = m_b; \ + points[2] = m_c; \ + Vector<Color> colors; \ + colors.resize(3); \ + colors[0] = m_color; \ + colors[1] = m_color; \ + colors[2] = m_color; \ + vs->poly_add_primitive(p_indicator, points, Vector<Vector3>(), colors, Vector<Vector3>()); \ + } void CollisionShape::_notification(int p_what) { |