Debian Codegraph @

scene/resources/3d/sphere_shape_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* sphere_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 "sphere_shape_3d.h"
32
33 #include "scene/resources/3d/primitive_meshes.h"
34 #include "servers/physics_3d/physics_server_3d.h"
35
36 Vector<Vector3> SphereShape3D::get_debug_mesh_lines() const {
37 float r = get_radius();
38
39 Vector<Vector3> points;
40
41 for (int i = 0; i <= 360; i++) {
42 float ra = Math::deg_to_rad((float)i);
43 float rb = Math::deg_to_rad((float)i + 1);
44 Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
45 Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
46
47 points.push_back(Vector3(a.x, 0, a.y));
48 points.push_back(Vector3(b.x, 0, b.y));
49 points.push_back(Vector3(0, a.x, a.y));
50 points.push_back(Vector3(0, b.x, b.y));
51 points.push_back(Vector3(a.x, a.y, 0));
52 points.push_back(Vector3(b.x, b.y, 0));
53 }
54
55 return points;
56 }
57
58 Ref<ArrayMesh> SphereShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
59 Array sphere_array;
60 sphere_array.resize(RS::ARRAY_MAX);
61 SphereMesh::create_mesh_array(sphere_array, radius, radius * 2, 32);
62
63 Vector<Color> colors;
64 const PackedVector3Array &verts = sphere_array[RS::ARRAY_VERTEX];
65 const int32_t verts_size = verts.size();
66 for (int i = 0; i < verts_size; i++) {
67 colors.append(p_modulate);
68 }
69
70 Ref<ArrayMesh> sphere_mesh = memnew(ArrayMesh);
71 sphere_array[RS::ARRAY_COLOR] = colors;
72 sphere_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, sphere_array);
73 return sphere_mesh;
74 }
75
76 real_t SphereShape3D::get_enclosing_radius() const {
77 return radius;
78 }
79
80 void SphereShape3D::_update_shape() {
81 PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), radius);
82 Shape3D::_update_shape();
83 }
84
85 void SphereShape3D::set_radius(float p_radius) {
86 ERR_FAIL_COND_MSG(p_radius < 0, "SphereShape3D radius cannot be negative.");
87 radius = p_radius;
88 _update_shape();
89 emit_changed();
90 }
91
92 float SphereShape3D::get_radius() const {
93 return radius;
94 }
95
96 void SphereShape3D::_bind_methods() {
97 ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SphereShape3D::set_radius);
98 ClassDB::bind_method(D_METHOD("get_radius"), &SphereShape3D::get_radius);
99
100 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
101 }
102
103 SphereShape3D::SphereShape3D() :
104 Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_SPHERE)) {
105 set_radius(0.5);
106 }