|
| 1 | +/**************************************************************************/ |
| 2 | +/* csg_capsule_2d.cpp */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +#include "csg_capsule_2d.h" |
| 32 | + |
| 33 | +#include "core/math/geometry_2d.h" |
| 34 | +#include "scene/resources/world_2d.h" |
| 35 | +#include "servers/physics_server_2d.h" |
| 36 | + |
| 37 | +#include "thirdparty/misc/polypartition.h" |
| 38 | + |
| 39 | +#ifdef TOOLS_ENABLED |
| 40 | +bool CSGCapsule2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { |
| 41 | + return Geometry2D::is_point_in_polygon(p_point, _get_vertices()); |
| 42 | +} |
| 43 | +#endif // TOOLS_ENABLED |
| 44 | + |
| 45 | +CSGBrush2D *CSGCapsule2D::_build_brush() { |
| 46 | + CSGBrush2D *new_brush = memnew(CSGBrush2D); |
| 47 | + |
| 48 | + LocalVector<CSGBrush2D::Outline> &outlines = new_brush->outlines; |
| 49 | + outlines.resize(1); |
| 50 | + CSGBrush2D::Outline &outline = outlines[0]; |
| 51 | + |
| 52 | + const Vector<Vector2> &cached_vertices = _get_vertices(); |
| 53 | + |
| 54 | + LocalVector<Vector2> &vertices = outline.vertices; |
| 55 | + |
| 56 | + vertices.resize(cached_vertices.size() + 1); |
| 57 | + |
| 58 | + for (int i = 0; i < cached_vertices.size(); i++) { |
| 59 | + vertices[i] = cached_vertices[i]; |
| 60 | + } |
| 61 | + vertices[cached_vertices.size()] = cached_vertices[0]; |
| 62 | + |
| 63 | + brush_outlines.resize(1); |
| 64 | + brush_outlines[0] = vertices; |
| 65 | + |
| 66 | + new_brush->build_from_outlines(outlines); |
| 67 | + |
| 68 | + return new_brush; |
| 69 | +} |
| 70 | + |
| 71 | +Vector<Vector2> CSGCapsule2D::_get_vertices() const { |
| 72 | + if (vertex_cache_dirty) { |
| 73 | + vertex_cache_dirty = false; |
| 74 | + |
| 75 | + vertex_cache.resize(radial_segments * 4 + 2); |
| 76 | + Vector2 *vertex_cache_ptrw = vertex_cache.ptrw(); |
| 77 | + int vertices_index = 0; |
| 78 | + |
| 79 | + const real_t turn_step = Math::TAU / float(radial_segments * 4); |
| 80 | + for (int i = 0; i < radial_segments * 4; i++) { |
| 81 | + Vector2 ofs = Vector2(0, (i > radial_segments && i <= radial_segments * 3) ? -height * 0.5 + radius : height * 0.5 - radius); |
| 82 | + |
| 83 | + vertex_cache_ptrw[vertices_index++] = Vector2(Math::sin(i * turn_step), Math::cos(i * turn_step)) * radius + ofs; |
| 84 | + if (i == radial_segments || i == radial_segments * 3) { |
| 85 | + vertex_cache_ptrw[vertices_index++] = Vector2(Math::sin(i * turn_step), Math::cos(i * turn_step)) * radius - ofs; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return vertex_cache; |
| 91 | +} |
| 92 | + |
| 93 | +void CSGCapsule2D::_bind_methods() { |
| 94 | + ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGCapsule2D::set_radius); |
| 95 | + ClassDB::bind_method(D_METHOD("get_radius"), &CSGCapsule2D::get_radius); |
| 96 | + |
| 97 | + ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGCapsule2D::set_height); |
| 98 | + ClassDB::bind_method(D_METHOD("get_height"), &CSGCapsule2D::get_height); |
| 99 | + |
| 100 | + ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &CSGCapsule2D::set_radial_segments); |
| 101 | + ClassDB::bind_method(D_METHOD("get_radial_segments"), &CSGCapsule2D::get_radial_segments); |
| 102 | + |
| 103 | + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_radius", "get_radius"); |
| 104 | + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_height", "get_height"); |
| 105 | + ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1"), "set_radial_segments", "get_radial_segments"); |
| 106 | +} |
| 107 | + |
| 108 | +void CSGCapsule2D::set_radius(float p_radius) { |
| 109 | + radius = p_radius; |
| 110 | + vertex_cache_dirty = true; |
| 111 | + _make_dirty(); |
| 112 | +} |
| 113 | + |
| 114 | +float CSGCapsule2D::get_radius() const { |
| 115 | + return radius; |
| 116 | +} |
| 117 | + |
| 118 | +void CSGCapsule2D::set_height(float p_height) { |
| 119 | + height = p_height; |
| 120 | + vertex_cache_dirty = true; |
| 121 | + _make_dirty(); |
| 122 | +} |
| 123 | + |
| 124 | +float CSGCapsule2D::get_height() const { |
| 125 | + return height; |
| 126 | +} |
| 127 | + |
| 128 | +void CSGCapsule2D::set_radial_segments(int p_segments) { |
| 129 | + radial_segments = p_segments > 0 ? p_segments : 1; |
| 130 | + vertex_cache_dirty = true; |
| 131 | + _make_dirty(); |
| 132 | +} |
| 133 | + |
| 134 | +int CSGCapsule2D::get_radial_segments() const { |
| 135 | + return radial_segments; |
| 136 | +} |
0 commit comments