Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/3d/capsule_shape_3d.cpp
9898 views
1
/**************************************************************************/
2
/* capsule_shape_3d.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 "capsule_shape_3d.h"
32
33
#include "scene/resources/3d/primitive_meshes.h"
34
#include "servers/physics_server_3d.h"
35
36
Vector<Vector3> CapsuleShape3D::get_debug_mesh_lines() const {
37
float c_radius = get_radius();
38
float c_height = get_height();
39
40
Vector<Vector3> points;
41
42
Vector3 d(0, c_height * 0.5f - c_radius, 0);
43
for (int i = 0; i < 360; i++) {
44
float ra = Math::deg_to_rad((float)i);
45
float rb = Math::deg_to_rad((float)i + 1);
46
Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * c_radius;
47
Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * c_radius;
48
49
points.push_back(Vector3(a.x, 0, a.y) + d);
50
points.push_back(Vector3(b.x, 0, b.y) + d);
51
52
points.push_back(Vector3(a.x, 0, a.y) - d);
53
points.push_back(Vector3(b.x, 0, b.y) - d);
54
55
if (i % 90 == 0) {
56
points.push_back(Vector3(a.x, 0, a.y) + d);
57
points.push_back(Vector3(a.x, 0, a.y) - d);
58
}
59
60
Vector3 dud = i < 180 ? d : -d;
61
62
points.push_back(Vector3(0, a.x, a.y) + dud);
63
points.push_back(Vector3(0, b.x, b.y) + dud);
64
points.push_back(Vector3(a.y, a.x, 0) + dud);
65
points.push_back(Vector3(b.y, b.x, 0) + dud);
66
}
67
68
return points;
69
}
70
71
Ref<ArrayMesh> CapsuleShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
72
Array capsule_array;
73
capsule_array.resize(RS::ARRAY_MAX);
74
CapsuleMesh::create_mesh_array(capsule_array, radius, height, 32, 8);
75
76
Vector<Color> colors;
77
const PackedVector3Array &verts = capsule_array[RS::ARRAY_VERTEX];
78
const int32_t verts_size = verts.size();
79
for (int i = 0; i < verts_size; i++) {
80
colors.append(p_modulate);
81
}
82
83
Ref<ArrayMesh> capsule_mesh = memnew(ArrayMesh);
84
capsule_array[RS::ARRAY_COLOR] = colors;
85
capsule_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, capsule_array);
86
return capsule_mesh;
87
}
88
89
real_t CapsuleShape3D::get_enclosing_radius() const {
90
return height * 0.5f;
91
}
92
93
void CapsuleShape3D::_update_shape() {
94
Dictionary d;
95
d["radius"] = radius;
96
d["height"] = height;
97
PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), d);
98
Shape3D::_update_shape();
99
}
100
101
void CapsuleShape3D::set_radius(float p_radius) {
102
ERR_FAIL_COND_MSG(p_radius < 0.0f, "CapsuleShape3D radius cannot be negative.");
103
radius = p_radius;
104
if (height < radius * 2.0f) {
105
height = radius * 2.0f;
106
}
107
_update_shape();
108
emit_changed();
109
}
110
111
float CapsuleShape3D::get_radius() const {
112
return radius;
113
}
114
115
void CapsuleShape3D::set_height(float p_height) {
116
ERR_FAIL_COND_MSG(p_height < 0.0f, "CapsuleShape3D height cannot be negative.");
117
height = p_height;
118
if (radius > height * 0.5f) {
119
radius = height * 0.5f;
120
}
121
_update_shape();
122
emit_changed();
123
}
124
125
float CapsuleShape3D::get_height() const {
126
return height;
127
}
128
129
void CapsuleShape3D::set_mid_height(real_t p_mid_height) {
130
ERR_FAIL_COND_MSG(p_mid_height < 0.0f, "CapsuleShape3D mid-height cannot be negative.");
131
height = p_mid_height + radius * 2.0f;
132
_update_shape();
133
emit_changed();
134
}
135
136
real_t CapsuleShape3D::get_mid_height() const {
137
return height - radius * 2.0f;
138
}
139
140
void CapsuleShape3D::_bind_methods() {
141
ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CapsuleShape3D::set_radius);
142
ClassDB::bind_method(D_METHOD("get_radius"), &CapsuleShape3D::get_radius);
143
ClassDB::bind_method(D_METHOD("set_height", "height"), &CapsuleShape3D::set_height);
144
ClassDB::bind_method(D_METHOD("get_height"), &CapsuleShape3D::get_height);
145
ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &CapsuleShape3D::set_mid_height);
146
ClassDB::bind_method(D_METHOD("get_mid_height"), &CapsuleShape3D::get_mid_height);
147
148
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
149
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_height", "get_height");
150
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mid_height", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m", PROPERTY_USAGE_NONE), "set_mid_height", "get_mid_height");
151
ADD_LINKED_PROPERTY("radius", "height");
152
ADD_LINKED_PROPERTY("height", "radius");
153
}
154
155
CapsuleShape3D::CapsuleShape3D() :
156
Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CAPSULE)) {
157
_update_shape();
158
}
159
160