Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/3d/cylinder_shape_3d.cpp
9898 views
1
/**************************************************************************/
2
/* cylinder_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 "cylinder_shape_3d.h"
32
33
#include "scene/resources/3d/primitive_meshes.h"
34
#include "servers/physics_server_3d.h"
35
36
Vector<Vector3> CylinderShape3D::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.5, 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
61
return points;
62
}
63
64
Ref<ArrayMesh> CylinderShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
65
Array cylinder_array;
66
cylinder_array.resize(RS::ARRAY_MAX);
67
CylinderMesh::create_mesh_array(cylinder_array, radius, radius, height, 32);
68
69
Vector<Color> colors;
70
const PackedVector3Array &verts = cylinder_array[RS::ARRAY_VERTEX];
71
const int32_t verts_size = verts.size();
72
for (int i = 0; i < verts_size; i++) {
73
colors.append(p_modulate);
74
}
75
76
Ref<ArrayMesh> cylinder_mesh = memnew(ArrayMesh);
77
cylinder_array[RS::ARRAY_COLOR] = colors;
78
cylinder_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, cylinder_array);
79
return cylinder_mesh;
80
}
81
82
real_t CylinderShape3D::get_enclosing_radius() const {
83
return Vector2(radius, height * 0.5).length();
84
}
85
86
void CylinderShape3D::_update_shape() {
87
Dictionary d;
88
d["radius"] = radius;
89
d["height"] = height;
90
PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), d);
91
Shape3D::_update_shape();
92
}
93
94
void CylinderShape3D::set_radius(float p_radius) {
95
ERR_FAIL_COND_MSG(p_radius < 0, "CylinderShape3D radius cannot be negative.");
96
radius = p_radius;
97
_update_shape();
98
emit_changed();
99
}
100
101
float CylinderShape3D::get_radius() const {
102
return radius;
103
}
104
105
void CylinderShape3D::set_height(float p_height) {
106
ERR_FAIL_COND_MSG(p_height < 0, "CylinderShape3D height cannot be negative.");
107
height = p_height;
108
_update_shape();
109
emit_changed();
110
}
111
112
float CylinderShape3D::get_height() const {
113
return height;
114
}
115
116
void CylinderShape3D::_bind_methods() {
117
ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CylinderShape3D::set_radius);
118
ClassDB::bind_method(D_METHOD("get_radius"), &CylinderShape3D::get_radius);
119
ClassDB::bind_method(D_METHOD("set_height", "height"), &CylinderShape3D::set_height);
120
ClassDB::bind_method(D_METHOD("get_height"), &CylinderShape3D::get_height);
121
122
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_height", "get_height");
123
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
124
}
125
126
CylinderShape3D::CylinderShape3D() :
127
Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CYLINDER)) {
128
_update_shape();
129
}
130
131