Path: blob/master/scene/resources/2d/concave_polygon_shape_2d.cpp
9898 views
/**************************************************************************/1/* concave_polygon_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 "concave_polygon_shape_2d.h"3132#include "core/math/geometry_2d.h"33#include "servers/physics_server_2d.h"34#include "servers/rendering_server.h"3536bool ConcavePolygonShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {37Vector<Vector2> s = get_segments();38int len = s.size();39if (len == 0 || (len % 2) == 1) {40return false;41}4243const Vector2 *r = s.ptr();44for (int i = 0; i < len; i += 2) {45Vector2 closest = Geometry2D::get_closest_point_to_segment(p_point, r[i], r[i + 1]);46if (p_point.distance_to(closest) < p_tolerance) {47return true;48}49}5051return false;52}5354void ConcavePolygonShape2D::set_segments(const Vector<Vector2> &p_segments) {55PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), p_segments);56emit_changed();57}5859Vector<Vector2> ConcavePolygonShape2D::get_segments() const {60return PhysicsServer2D::get_singleton()->shape_get_data(get_rid());61}6263void ConcavePolygonShape2D::draw(const RID &p_to_rid, const Color &p_color) {64Vector<Vector2> s = get_segments();65int len = s.size();66if (len == 0 || (len % 2) == 1) {67return;68}6970const Vector2 *r = s.ptr();71for (int i = 0; i < len; i += 2) {72RenderingServer::get_singleton()->canvas_item_add_line(p_to_rid, r[i], r[i + 1], p_color, 2);73}74}7576Rect2 ConcavePolygonShape2D::get_rect() const {77Vector<Vector2> s = get_segments();78int len = s.size();79if (len == 0) {80return Rect2();81}8283Rect2 rect;8485const Vector2 *r = s.ptr();86for (int i = 0; i < len; i++) {87if (i == 0) {88rect.position = r[i];89} else {90rect.expand_to(r[i]);91}92}9394return rect;95}9697real_t ConcavePolygonShape2D::get_enclosing_radius() const {98Vector<Vector2> data = get_segments();99const Vector2 *read = data.ptr();100real_t r = 0.0;101for (int i(0); i < data.size(); i++) {102r = MAX(read[i].length_squared(), r);103}104return Math::sqrt(r);105}106107void ConcavePolygonShape2D::_bind_methods() {108ClassDB::bind_method(D_METHOD("set_segments", "segments"), &ConcavePolygonShape2D::set_segments);109ClassDB::bind_method(D_METHOD("get_segments"), &ConcavePolygonShape2D::get_segments);110111ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "segments"), "set_segments", "get_segments");112}113114ConcavePolygonShape2D::ConcavePolygonShape2D() :115Shape2D(PhysicsServer2D::get_singleton()->concave_polygon_shape_create()) {116Vector<Vector2> empty;117set_segments(empty);118}119120121