Path: blob/master/scene/2d/physics/collision_shape_2d.cpp
22517 views
/**************************************************************************/1/* collision_shape_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "collision_shape_2d.h"3132#include "scene/2d/physics/area_2d.h"33#include "scene/2d/physics/collision_object_2d.h"34#include "scene/resources/2d/concave_polygon_shape_2d.h"35#include "scene/resources/2d/convex_polygon_shape_2d.h"3637void CollisionShape2D::_shape_changed() {38queue_redraw();39}4041void CollisionShape2D::_update_in_shape_owner(bool p_xform_only) {42collision_object->shape_owner_set_transform(owner_id, get_transform());43if (p_xform_only) {44return;45}46collision_object->shape_owner_set_disabled(owner_id, disabled);47collision_object->shape_owner_set_one_way_collision(owner_id, one_way_collision);48collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);49collision_object->shape_owner_set_one_way_collision_direction(owner_id, one_way_collision_direction);50}5152void CollisionShape2D::_notification(int p_what) {53switch (p_what) {54case NOTIFICATION_PARENTED: {55collision_object = Object::cast_to<CollisionObject2D>(get_parent());56if (collision_object) {57owner_id = collision_object->create_shape_owner(this);58if (shape.is_valid()) {59collision_object->shape_owner_add_shape(owner_id, shape);60}61_update_in_shape_owner();62}63} break;6465case NOTIFICATION_ENTER_TREE: {66if (collision_object) {67_update_in_shape_owner();68}69} break;7071case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {72if (collision_object) {73_update_in_shape_owner(true);74}75} break;7677case NOTIFICATION_UNPARENTED: {78if (collision_object) {79collision_object->remove_shape_owner(owner_id);80}81owner_id = 0;82collision_object = nullptr;83} break;8485case NOTIFICATION_DRAW: {86ERR_FAIL_COND(!is_inside_tree());8788if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {89break;90}9192if (shape.is_null()) {93break;94}9596rect = Rect2();9798Color draw_col = debug_color;99if (disabled) {100float g = draw_col.get_v();101draw_col.r = g;102draw_col.g = g;103draw_col.b = g;104draw_col.a *= 0.5;105}106shape->draw(get_canvas_item(), draw_col);107108rect = shape->get_rect();109rect = rect.grow(3);110111if (one_way_collision) {112// Draw an arrow indicating the one-way collision direction113draw_col = debug_color.inverted();114if (disabled) {115draw_col = draw_col.darkened(0.25);116}117118Vector2 line_to = 20.0 * one_way_collision_direction;119draw_line(Vector2(), line_to, draw_col, 2);120real_t tsize = 8;121122Vector<Vector2> pts{123line_to + tsize * one_way_collision_direction,124line_to + Math::SQRT12 * tsize * one_way_collision_direction.orthogonal(),125line_to - Math::SQRT12 * tsize * one_way_collision_direction.orthogonal(),126};127128Vector<Color> cols{ draw_col, draw_col, draw_col };129130draw_primitive(pts, cols, Vector<Vector2>());131}132} break;133}134}135136void CollisionShape2D::set_shape(const Ref<Shape2D> &p_shape) {137if (p_shape == shape) {138return;139}140if (shape.is_valid()) {141shape->disconnect_changed(callable_mp(this, &CollisionShape2D::_shape_changed));142}143shape = p_shape;144queue_redraw();145if (collision_object) {146collision_object->shape_owner_clear_shapes(owner_id);147if (shape.is_valid()) {148collision_object->shape_owner_add_shape(owner_id, shape);149}150_update_in_shape_owner();151}152153if (shape.is_valid()) {154shape->connect_changed(callable_mp(this, &CollisionShape2D::_shape_changed));155}156157update_configuration_warnings();158}159160Ref<Shape2D> CollisionShape2D::get_shape() const {161return shape;162}163164bool CollisionShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {165if (shape.is_null()) {166return false;167}168169return shape->_edit_is_selected_on_click(p_point, p_tolerance);170}171172PackedStringArray CollisionShape2D::get_configuration_warnings() const {173PackedStringArray warnings = Node2D::get_configuration_warnings();174175CollisionObject2D *col_object = Object::cast_to<CollisionObject2D>(get_parent());176if (col_object == nullptr) {177warnings.push_back(RTR("CollisionShape2D only serves to provide a collision shape to a CollisionObject2D derived node.\nPlease only use it as a child of Area2D, StaticBody2D, RigidBody2D, CharacterBody2D, etc. to give them a shape."));178}179if (shape.is_null()) {180warnings.push_back(RTR("A shape must be provided for CollisionShape2D to function. Please create a shape resource for it!"));181}182if (one_way_collision && Object::cast_to<Area2D>(col_object)) {183warnings.push_back(RTR("The One Way Collision property will be ignored when the collision object is an Area2D."));184}185186Ref<ConvexPolygonShape2D> convex = shape;187Ref<ConcavePolygonShape2D> concave = shape;188if (convex.is_valid() || concave.is_valid()) {189warnings.push_back(RTR("The CollisionShape2D node has limited editing options for polygon-based shapes. Consider using a CollisionPolygon2D node instead."));190}191192return warnings;193}194195void CollisionShape2D::set_disabled(bool p_disabled) {196disabled = p_disabled;197queue_redraw();198if (collision_object) {199collision_object->shape_owner_set_disabled(owner_id, p_disabled);200}201}202203bool CollisionShape2D::is_disabled() const {204return disabled;205}206207void CollisionShape2D::set_one_way_collision(bool p_enable) {208one_way_collision = p_enable;209queue_redraw();210if (collision_object) {211collision_object->shape_owner_set_one_way_collision(owner_id, p_enable);212}213update_configuration_warnings();214}215216bool CollisionShape2D::is_one_way_collision_enabled() const {217return one_way_collision;218}219220void CollisionShape2D::set_one_way_collision_margin(real_t p_margin) {221one_way_collision_margin = p_margin;222if (collision_object) {223collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);224}225}226227real_t CollisionShape2D::get_one_way_collision_margin() const {228return one_way_collision_margin;229}230231void CollisionShape2D::set_one_way_collision_direction(const Vector2 &p_direction) {232if (p_direction == one_way_collision_direction) {233return;234}235236one_way_collision_direction = p_direction.normalized();237if (collision_object) {238collision_object->shape_owner_set_one_way_collision_direction(owner_id, p_direction.normalized());239}240queue_redraw();241}242243Vector2 CollisionShape2D::get_one_way_collision_direction() const {244return one_way_collision_direction;245}246247Color CollisionShape2D::_get_default_debug_color() const {248const SceneTree *st = SceneTree::get_singleton();249return st ? st->get_debug_collisions_color() : Color(0.0, 0.0, 0.0, 0.0);250}251252void CollisionShape2D::set_debug_color(const Color &p_color) {253if (debug_color == p_color) {254return;255}256257debug_color = p_color;258queue_redraw();259}260261Color CollisionShape2D::get_debug_color() const {262return debug_color;263}264265#ifdef DEBUG_ENABLED266267bool CollisionShape2D::_property_can_revert(const StringName &p_name) const {268if (p_name == "debug_color") {269return true;270}271return false;272}273274bool CollisionShape2D::_property_get_revert(const StringName &p_name, Variant &r_property) const {275if (p_name == "debug_color") {276r_property = _get_default_debug_color();277return true;278}279return false;280}281282void CollisionShape2D::_validate_property(PropertyInfo &p_property) const {283if (p_property.name == "debug_color") {284if (debug_color == _get_default_debug_color()) {285p_property.usage = PROPERTY_USAGE_DEFAULT & ~PROPERTY_USAGE_STORAGE;286} else {287p_property.usage = PROPERTY_USAGE_DEFAULT;288}289}290}291292#endif // DEBUG_ENABLED293294void CollisionShape2D::_bind_methods() {295ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape);296ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape);297ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionShape2D::set_disabled);298ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape2D::is_disabled);299ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionShape2D::set_one_way_collision);300ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionShape2D::is_one_way_collision_enabled);301ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionShape2D::set_one_way_collision_margin);302ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionShape2D::get_one_way_collision_margin);303ClassDB::bind_method(D_METHOD("set_one_way_collision_direction", "p_direction"), &CollisionShape2D::set_one_way_collision_direction);304ClassDB::bind_method(D_METHOD("get_one_way_collision_direction"), &CollisionShape2D::get_one_way_collision_direction);305306ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, Shape2D::get_class_static()), "set_shape", "get_shape");307ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");308ADD_GROUP("One Way Collision", "one_way_collision");309ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision", PROPERTY_HINT_GROUP_ENABLE), "set_one_way_collision", "is_one_way_collision_enabled");310ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "one_way_collision_margin", PROPERTY_HINT_RANGE, "0,128,0.1,suffix:px"), "set_one_way_collision_margin", "get_one_way_collision_margin");311ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "one_way_collision_direction", PROPERTY_HINT_NONE, "suffix:px"), "set_one_way_collision_direction", "get_one_way_collision_direction");312313ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionShape2D::set_debug_color);314ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionShape2D::get_debug_color);315316ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");317// Default value depends on a project setting, override for doc generation purposes.318ADD_PROPERTY_DEFAULT("debug_color", Color(0.0, 0.0, 0.0, 0.0));319}320321CollisionShape2D::CollisionShape2D() {322set_notify_local_transform(true);323set_hide_clip_children(true);324debug_color = _get_default_debug_color();325}326327328