Path: blob/master/scene/2d/physics/collision_polygon_2d.cpp
9906 views
/**************************************************************************/1/* collision_polygon_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_polygon_2d.h"3132#include "core/math/geometry_2d.h"33#include "scene/2d/physics/area_2d.h"34#include "scene/2d/physics/collision_object_2d.h"35#include "scene/resources/2d/concave_polygon_shape_2d.h"36#include "scene/resources/2d/convex_polygon_shape_2d.h"3738void CollisionPolygon2D::_build_polygon() {39collision_object->shape_owner_clear_shapes(owner_id);4041bool solids = build_mode == BUILD_SOLIDS;4243if (solids) {44if (polygon.size() < 3) {45return;46}4748//here comes the sun, lalalala49//decompose concave into multiple convex polygons and add them50Vector<Vector<Vector2>> decomp = _decompose_in_convex();51for (int i = 0; i < decomp.size(); i++) {52Ref<ConvexPolygonShape2D> convex = memnew(ConvexPolygonShape2D);53convex->set_points(decomp[i]);54collision_object->shape_owner_add_shape(owner_id, convex);55}5657} else {58if (polygon.size() < 2) {59return;60}6162Ref<ConcavePolygonShape2D> concave = memnew(ConcavePolygonShape2D);6364Vector<Vector2> segments;65segments.resize(polygon.size() * 2);66Vector2 *w = segments.ptrw();6768for (int i = 0; i < polygon.size(); i++) {69w[(i << 1) + 0] = polygon[i];70w[(i << 1) + 1] = polygon[(i + 1) % polygon.size()];71}7273concave->set_segments(segments);7475collision_object->shape_owner_add_shape(owner_id, concave);76}77}7879Vector<Vector<Vector2>> CollisionPolygon2D::_decompose_in_convex() {80Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(polygon);81return decomp;82}8384void CollisionPolygon2D::_update_in_shape_owner(bool p_xform_only) {85collision_object->shape_owner_set_transform(owner_id, get_transform());86if (p_xform_only) {87return;88}89collision_object->shape_owner_set_disabled(owner_id, disabled);90collision_object->shape_owner_set_one_way_collision(owner_id, one_way_collision);91collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);92}9394void CollisionPolygon2D::_notification(int p_what) {95switch (p_what) {96case NOTIFICATION_PARENTED: {97collision_object = Object::cast_to<CollisionObject2D>(get_parent());98if (collision_object) {99owner_id = collision_object->create_shape_owner(this);100_build_polygon();101_update_in_shape_owner();102}103} break;104105case NOTIFICATION_ENTER_TREE: {106if (collision_object) {107_update_in_shape_owner();108}109} break;110111case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {112if (collision_object) {113_update_in_shape_owner(true);114}115} break;116117case NOTIFICATION_UNPARENTED: {118if (collision_object) {119collision_object->remove_shape_owner(owner_id);120}121owner_id = 0;122collision_object = nullptr;123} break;124125case NOTIFICATION_DRAW: {126ERR_FAIL_COND(!is_inside_tree());127if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {128break;129}130131if (polygon.size() > 2) {132#ifdef TOOLS_ENABLED133if (build_mode == BUILD_SOLIDS) {134Vector<Vector<Vector2>> decomp = _decompose_in_convex();135136Color c(0.4, 0.9, 0.1);137for (int i = 0; i < decomp.size(); i++) {138c.set_hsv(Math::fmod(c.get_h() + 0.738, 1), c.get_s(), c.get_v(), 0.5);139draw_colored_polygon(decomp[i], c);140}141}142#endif143144const Color stroke_color = get_tree()->get_debug_collisions_color();145draw_polyline(polygon, stroke_color);146// Draw the last segment.147draw_line(polygon[polygon.size() - 1], polygon[0], stroke_color);148}149150if (one_way_collision) {151Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4);152dcol.a = 1.0;153Vector2 line_to(0, 20);154draw_line(Vector2(), line_to, dcol, 3);155real_t tsize = 8;156157Vector<Vector2> pts = {158line_to + Vector2(0, tsize),159line_to + Vector2(Math::SQRT12 * tsize, 0),160line_to + Vector2(-Math::SQRT12 * tsize, 0)161};162163Vector<Color> cols{ dcol, dcol, dcol };164165draw_primitive(pts, cols, Vector<Vector2>()); //small arrow166}167} break;168}169}170171void CollisionPolygon2D::set_polygon(const Vector<Point2> &p_polygon) {172polygon = p_polygon;173174{175for (int i = 0; i < polygon.size(); i++) {176if (i == 0) {177aabb = Rect2(polygon[i], Size2());178} else {179aabb.expand_to(polygon[i]);180}181}182if (aabb == Rect2()) {183aabb = Rect2(-10, -10, 20, 20);184} else {185aabb.position -= aabb.size * 0.3;186aabb.size += aabb.size * 0.6;187}188}189190if (collision_object) {191_build_polygon();192_update_in_shape_owner();193}194queue_redraw();195update_configuration_warnings();196}197198Vector<Point2> CollisionPolygon2D::get_polygon() const {199return polygon;200}201202void CollisionPolygon2D::set_build_mode(BuildMode p_mode) {203ERR_FAIL_INDEX((int)p_mode, 2);204build_mode = p_mode;205if (collision_object) {206_build_polygon();207_update_in_shape_owner();208}209queue_redraw();210update_configuration_warnings();211}212213CollisionPolygon2D::BuildMode CollisionPolygon2D::get_build_mode() const {214return build_mode;215}216217#ifdef DEBUG_ENABLED218Rect2 CollisionPolygon2D::_edit_get_rect() const {219return aabb;220}221222bool CollisionPolygon2D::_edit_use_rect() const {223return true;224}225226bool CollisionPolygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {227return Geometry2D::is_point_in_polygon(p_point, Variant(polygon));228}229#endif230231PackedStringArray CollisionPolygon2D::get_configuration_warnings() const {232PackedStringArray warnings = Node2D::get_configuration_warnings();233234if (!Object::cast_to<CollisionObject2D>(get_parent())) {235warnings.push_back(RTR("CollisionPolygon2D only serves to provide a collision shape to a CollisionObject2D derived node. Please only use it as a child of Area2D, StaticBody2D, RigidBody2D, CharacterBody2D, etc. to give them a shape."));236}237238int polygon_count = polygon.size();239if (polygon_count == 0) {240warnings.push_back(RTR("An empty CollisionPolygon2D has no effect on collision."));241} else {242bool solids = build_mode == BUILD_SOLIDS;243if (solids) {244if (polygon_count < 3) {245warnings.push_back(RTR("Invalid polygon. At least 3 points are needed in 'Solids' build mode."));246}247} else if (polygon_count < 2) {248warnings.push_back(RTR("Invalid polygon. At least 2 points are needed in 'Segments' build mode."));249}250}251if (one_way_collision && Object::cast_to<Area2D>(get_parent())) {252warnings.push_back(RTR("The One Way Collision property will be ignored when the collision object is an Area2D."));253}254255return warnings;256}257258void CollisionPolygon2D::set_disabled(bool p_disabled) {259disabled = p_disabled;260queue_redraw();261if (collision_object) {262collision_object->shape_owner_set_disabled(owner_id, p_disabled);263}264}265266bool CollisionPolygon2D::is_disabled() const {267return disabled;268}269270void CollisionPolygon2D::set_one_way_collision(bool p_enable) {271one_way_collision = p_enable;272queue_redraw();273if (collision_object) {274collision_object->shape_owner_set_one_way_collision(owner_id, p_enable);275}276update_configuration_warnings();277}278279bool CollisionPolygon2D::is_one_way_collision_enabled() const {280return one_way_collision;281}282283void CollisionPolygon2D::set_one_way_collision_margin(real_t p_margin) {284one_way_collision_margin = p_margin;285if (collision_object) {286collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);287}288}289290real_t CollisionPolygon2D::get_one_way_collision_margin() const {291return one_way_collision_margin;292}293294void CollisionPolygon2D::_bind_methods() {295ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon2D::set_polygon);296ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon2D::get_polygon);297298ClassDB::bind_method(D_METHOD("set_build_mode", "build_mode"), &CollisionPolygon2D::set_build_mode);299ClassDB::bind_method(D_METHOD("get_build_mode"), &CollisionPolygon2D::get_build_mode);300ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon2D::set_disabled);301ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon2D::is_disabled);302ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionPolygon2D::set_one_way_collision);303ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionPolygon2D::is_one_way_collision_enabled);304ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionPolygon2D::set_one_way_collision_margin);305ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionPolygon2D::get_one_way_collision_margin);306307ADD_PROPERTY(PropertyInfo(Variant::INT, "build_mode", PROPERTY_HINT_ENUM, "Solids,Segments"), "set_build_mode", "get_build_mode");308ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");309ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");310311ADD_GROUP("One Way Collision", "one_way_collision");312ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision", PROPERTY_HINT_GROUP_ENABLE), "set_one_way_collision", "is_one_way_collision_enabled");313ADD_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");314315BIND_ENUM_CONSTANT(BUILD_SOLIDS);316BIND_ENUM_CONSTANT(BUILD_SEGMENTS);317}318319CollisionPolygon2D::CollisionPolygon2D() {320set_notify_local_transform(true);321set_hide_clip_children(true);322}323324325