Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/3d/box_shape_3d.cpp
9898 views
1
/**************************************************************************/
2
/* box_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 "box_shape_3d.h"
32
33
#include "scene/resources/3d/primitive_meshes.h"
34
#include "servers/physics_server_3d.h"
35
36
Vector<Vector3> BoxShape3D::get_debug_mesh_lines() const {
37
Vector<Vector3> lines;
38
AABB aabb;
39
aabb.position = -size / 2;
40
aabb.size = size;
41
42
for (int i = 0; i < 12; i++) {
43
Vector3 a, b;
44
aabb.get_edge(i, a, b);
45
lines.push_back(a);
46
lines.push_back(b);
47
}
48
49
return lines;
50
}
51
52
Ref<ArrayMesh> BoxShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
53
Array box_array;
54
box_array.resize(RS::ARRAY_MAX);
55
BoxMesh::create_mesh_array(box_array, size);
56
57
Vector<Color> colors;
58
const PackedVector3Array &verts = box_array[RS::ARRAY_VERTEX];
59
const int32_t verts_size = verts.size();
60
for (int i = 0; i < verts_size; i++) {
61
colors.append(p_modulate);
62
}
63
64
Ref<ArrayMesh> box_mesh = memnew(ArrayMesh);
65
box_array[RS::ARRAY_COLOR] = colors;
66
box_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, box_array);
67
return box_mesh;
68
}
69
70
real_t BoxShape3D::get_enclosing_radius() const {
71
return size.length() / 2;
72
}
73
74
void BoxShape3D::_update_shape() {
75
PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), size / 2);
76
Shape3D::_update_shape();
77
}
78
79
#ifndef DISABLE_DEPRECATED
80
bool BoxShape3D::_set(const StringName &p_name, const Variant &p_value) {
81
if (p_name == "extents") { // Compatibility with Godot 3.x.
82
// Convert to `size`, twice as big.
83
set_size((Vector3)p_value * 2);
84
return true;
85
}
86
return false;
87
}
88
89
bool BoxShape3D::_get(const StringName &p_name, Variant &r_property) const {
90
if (p_name == "extents") { // Compatibility with Godot 3.x.
91
// Convert to `extents`, half as big.
92
r_property = size / 2;
93
return true;
94
}
95
return false;
96
}
97
#endif // DISABLE_DEPRECATED
98
99
void BoxShape3D::set_size(const Vector3 &p_size) {
100
ERR_FAIL_COND_MSG(p_size.x < 0 || p_size.y < 0 || p_size.z < 0, "BoxShape3D size cannot be negative.");
101
size = p_size;
102
_update_shape();
103
emit_changed();
104
}
105
106
Vector3 BoxShape3D::get_size() const {
107
return size;
108
}
109
110
void BoxShape3D::_bind_methods() {
111
ClassDB::bind_method(D_METHOD("set_size", "size"), &BoxShape3D::set_size);
112
ClassDB::bind_method(D_METHOD("get_size"), &BoxShape3D::get_size);
113
114
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
115
}
116
117
BoxShape3D::BoxShape3D() :
118
Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_BOX)) {
119
set_size(Vector3(1, 1, 1));
120
}
121
122