Path: blob/master/scene/resources/2d/capsule_shape_2d.cpp
9903 views
/**************************************************************************/1/* capsule_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 "capsule_shape_2d.h"3132#include "core/math/geometry_2d.h"33#include "servers/physics_server_2d.h"34#include "servers/rendering_server.h"3536Vector<Vector2> CapsuleShape2D::_get_points() const {37Vector<Vector2> points;38const real_t turn_step = Math::TAU / 24.0;39for (int i = 0; i < 24; i++) {40Vector2 ofs = Vector2(0, (i > 6 && i <= 18) ? -height * 0.5f + radius : height * 0.5f - radius);4142points.push_back(Vector2(Math::sin(i * turn_step), Math::cos(i * turn_step)) * radius + ofs);43if (i == 6 || i == 18) {44points.push_back(Vector2(Math::sin(i * turn_step), Math::cos(i * turn_step)) * radius - ofs);45}46}4748return points;49}5051bool CapsuleShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {52return Geometry2D::is_point_in_polygon(p_point, _get_points());53}5455void CapsuleShape2D::_update_shape() {56PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), Vector2(radius, height));57emit_changed();58}5960void CapsuleShape2D::set_radius(real_t p_radius) {61ERR_FAIL_COND_MSG(p_radius < 0.0f, "CapsuleShape2D radius cannot be negative.");62if (radius == p_radius) {63return;64}65radius = p_radius;66if (height < radius * 2.0f) {67height = radius * 2.0f;68}69_update_shape();70}7172real_t CapsuleShape2D::get_radius() const {73return radius;74}7576void CapsuleShape2D::set_height(real_t p_height) {77ERR_FAIL_COND_MSG(p_height < 0.0f, "CapsuleShape2D height cannot be negative.");78if (height == p_height) {79return;80}81height = p_height;82if (radius > height * 0.5f) {83radius = height * 0.5f;84}85_update_shape();86}8788real_t CapsuleShape2D::get_height() const {89return height;90}9192void CapsuleShape2D::set_mid_height(real_t p_mid_height) {93ERR_FAIL_COND_MSG(p_mid_height < 0.0f, "CapsuleShape2D mid-height cannot be negative.");94height = p_mid_height + radius * 2.0f;95_update_shape();96}9798real_t CapsuleShape2D::get_mid_height() const {99return height - radius * 2.0f;100}101102void CapsuleShape2D::draw(const RID &p_to_rid, const Color &p_color) {103Vector<Vector2> points = _get_points();104Vector<Color> col = { p_color };105RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col);106107if (is_collision_outline_enabled()) {108points.push_back(points[0]);109col = { Color(p_color, 1.0f) };110RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, points, col);111}112}113114Rect2 CapsuleShape2D::get_rect() const {115const Vector2 half_size = Vector2(radius, height * 0.5f);116return Rect2(-half_size, half_size * 2.0f);117}118119real_t CapsuleShape2D::get_enclosing_radius() const {120return height * 0.5f;121}122123void CapsuleShape2D::_bind_methods() {124ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CapsuleShape2D::set_radius);125ClassDB::bind_method(D_METHOD("get_radius"), &CapsuleShape2D::get_radius);126127ClassDB::bind_method(D_METHOD("set_height", "height"), &CapsuleShape2D::set_height);128ClassDB::bind_method(D_METHOD("get_height"), &CapsuleShape2D::get_height);129130ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &CapsuleShape2D::set_mid_height);131ClassDB::bind_method(D_METHOD("get_mid_height"), &CapsuleShape2D::get_mid_height);132133ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px"), "set_radius", "get_radius");134ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px"), "set_height", "get_height");135ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mid_height", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px", PROPERTY_USAGE_NONE), "set_mid_height", "get_mid_height");136ADD_LINKED_PROPERTY("radius", "height");137ADD_LINKED_PROPERTY("height", "radius");138}139140CapsuleShape2D::CapsuleShape2D() :141Shape2D(PhysicsServer2D::get_singleton()->capsule_shape_create()) {142_update_shape();143}144145146