Path: blob/master/scene/2d/physics/collision_shape_2d.cpp
9906 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);49}5051void CollisionShape2D::_notification(int p_what) {52switch (p_what) {53case NOTIFICATION_PARENTED: {54collision_object = Object::cast_to<CollisionObject2D>(get_parent());55if (collision_object) {56owner_id = collision_object->create_shape_owner(this);57if (shape.is_valid()) {58collision_object->shape_owner_add_shape(owner_id, shape);59}60_update_in_shape_owner();61}62} break;6364case NOTIFICATION_ENTER_TREE: {65if (collision_object) {66_update_in_shape_owner();67}68} break;6970case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {71if (collision_object) {72_update_in_shape_owner(true);73}74} break;7576case NOTIFICATION_UNPARENTED: {77if (collision_object) {78collision_object->remove_shape_owner(owner_id);79}80owner_id = 0;81collision_object = nullptr;82} break;8384case NOTIFICATION_DRAW: {85ERR_FAIL_COND(!is_inside_tree());8687if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {88break;89}9091if (shape.is_null()) {92break;93}9495rect = Rect2();9697Color draw_col = debug_color;98if (disabled) {99float g = draw_col.get_v();100draw_col.r = g;101draw_col.g = g;102draw_col.b = g;103draw_col.a *= 0.5;104}105shape->draw(get_canvas_item(), draw_col);106107rect = shape->get_rect();108rect = rect.grow(3);109110if (one_way_collision) {111// Draw an arrow indicating the one-way collision direction112draw_col = debug_color.inverted();113if (disabled) {114draw_col = draw_col.darkened(0.25);115}116Vector2 line_to(0, 20);117draw_line(Vector2(), line_to, draw_col, 2);118real_t tsize = 8;119120Vector<Vector2> pts{121line_to + Vector2(0, tsize),122line_to + Vector2(Math::SQRT12 * tsize, 0),123line_to + Vector2(-Math::SQRT12 * tsize, 0)124};125126Vector<Color> cols{ draw_col, draw_col, draw_col };127128draw_primitive(pts, cols, Vector<Vector2>());129}130} break;131}132}133134void CollisionShape2D::set_shape(const Ref<Shape2D> &p_shape) {135if (p_shape == shape) {136return;137}138if (shape.is_valid()) {139shape->disconnect_changed(callable_mp(this, &CollisionShape2D::_shape_changed));140}141shape = p_shape;142queue_redraw();143if (collision_object) {144collision_object->shape_owner_clear_shapes(owner_id);145if (shape.is_valid()) {146collision_object->shape_owner_add_shape(owner_id, shape);147}148_update_in_shape_owner();149}150151if (shape.is_valid()) {152shape->connect_changed(callable_mp(this, &CollisionShape2D::_shape_changed));153}154155update_configuration_warnings();156}157158Ref<Shape2D> CollisionShape2D::get_shape() const {159return shape;160}161162bool CollisionShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {163if (shape.is_null()) {164return false;165}166167return shape->_edit_is_selected_on_click(p_point, p_tolerance);168}169170PackedStringArray CollisionShape2D::get_configuration_warnings() const {171PackedStringArray warnings = Node2D::get_configuration_warnings();172173CollisionObject2D *col_object = Object::cast_to<CollisionObject2D>(get_parent());174if (col_object == nullptr) {175warnings.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."));176}177if (shape.is_null()) {178warnings.push_back(RTR("A shape must be provided for CollisionShape2D to function. Please create a shape resource for it!"));179}180if (one_way_collision && Object::cast_to<Area2D>(col_object)) {181warnings.push_back(RTR("The One Way Collision property will be ignored when the collision object is an Area2D."));182}183184Ref<ConvexPolygonShape2D> convex = shape;185Ref<ConcavePolygonShape2D> concave = shape;186if (convex.is_valid() || concave.is_valid()) {187warnings.push_back(RTR("The CollisionShape2D node has limited editing options for polygon-based shapes. Consider using a CollisionPolygon2D node instead."));188}189190return warnings;191}192193void CollisionShape2D::set_disabled(bool p_disabled) {194disabled = p_disabled;195queue_redraw();196if (collision_object) {197collision_object->shape_owner_set_disabled(owner_id, p_disabled);198}199}200201bool CollisionShape2D::is_disabled() const {202return disabled;203}204205void CollisionShape2D::set_one_way_collision(bool p_enable) {206one_way_collision = p_enable;207queue_redraw();208if (collision_object) {209collision_object->shape_owner_set_one_way_collision(owner_id, p_enable);210}211update_configuration_warnings();212}213214bool CollisionShape2D::is_one_way_collision_enabled() const {215return one_way_collision;216}217218void CollisionShape2D::set_one_way_collision_margin(real_t p_margin) {219one_way_collision_margin = p_margin;220if (collision_object) {221collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);222}223}224225real_t CollisionShape2D::get_one_way_collision_margin() const {226return one_way_collision_margin;227}228229Color CollisionShape2D::_get_default_debug_color() const {230const SceneTree *st = SceneTree::get_singleton();231return st ? st->get_debug_collisions_color() : Color(0.0, 0.0, 0.0, 0.0);232}233234void CollisionShape2D::set_debug_color(const Color &p_color) {235if (debug_color == p_color) {236return;237}238239debug_color = p_color;240queue_redraw();241}242243Color CollisionShape2D::get_debug_color() const {244return debug_color;245}246247#ifdef DEBUG_ENABLED248249bool CollisionShape2D::_property_can_revert(const StringName &p_name) const {250if (p_name == "debug_color") {251return true;252}253return false;254}255256bool CollisionShape2D::_property_get_revert(const StringName &p_name, Variant &r_property) const {257if (p_name == "debug_color") {258r_property = _get_default_debug_color();259return true;260}261return false;262}263264void CollisionShape2D::_validate_property(PropertyInfo &p_property) const {265if (p_property.name == "debug_color") {266if (debug_color == _get_default_debug_color()) {267p_property.usage = PROPERTY_USAGE_DEFAULT & ~PROPERTY_USAGE_STORAGE;268} else {269p_property.usage = PROPERTY_USAGE_DEFAULT;270}271}272}273274#endif // DEBUG_ENABLED275276void CollisionShape2D::_bind_methods() {277ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape);278ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape);279ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionShape2D::set_disabled);280ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape2D::is_disabled);281ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionShape2D::set_one_way_collision);282ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionShape2D::is_one_way_collision_enabled);283ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionShape2D::set_one_way_collision_margin);284ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionShape2D::get_one_way_collision_margin);285286ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");287ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");288ADD_GROUP("One Way Collision", "one_way_collision");289ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision", PROPERTY_HINT_GROUP_ENABLE), "set_one_way_collision", "is_one_way_collision_enabled");290ADD_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");291292ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionShape2D::set_debug_color);293ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionShape2D::get_debug_color);294295ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");296// Default value depends on a project setting, override for doc generation purposes.297ADD_PROPERTY_DEFAULT("debug_color", Color(0.0, 0.0, 0.0, 0.0));298}299300CollisionShape2D::CollisionShape2D() {301set_notify_local_transform(true);302set_hide_clip_children(true);303debug_color = _get_default_debug_color();304}305306307