Debian Codegraph @

scene/3d/spring_bone_collision_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* spring_bone_collision_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 "spring_bone_collision_3d.h"
32
33 #include "scene/3d/spring_bone_simulator_3d.h"
34
35 PackedStringArray SpringBoneCollision3D::get_configuration_warnings() const {
36 PackedStringArray warnings = Node3D::get_configuration_warnings();
37
38 SpringBoneSimulator3D *parent = Object::cast_to<SpringBoneSimulator3D>(get_parent());
39 if (!parent) {
40 warnings.push_back(RTR("Parent node should be a SpringBoneSimulator3D node."));
41 }
42
43 return warnings;
44 }
45
46 void SpringBoneCollision3D::_validate_property(PropertyInfo &p_property) const {
47 if (Engine::get_singleton()->is_editor_hint() && p_property.name == "bone_name") {
48 Skeleton3D *sk = get_skeleton();
49 if (sk) {
50 p_property.hint = PROPERTY_HINT_ENUM_SUGGESTION;
51 p_property.hint_string = sk->get_concatenated_bone_names();
52 } else {
53 p_property.hint = PROPERTY_HINT_NONE;
54 p_property.hint_string = "";
55 }
56 } else if (bone < 0 && (p_property.name == "position_offset" || p_property.name == "rotation_offset")) {
57 p_property.usage = PROPERTY_USAGE_NONE;
58 }
59 }
60
61 void SpringBoneCollision3D::_validate_bone_name() {
62 // Prior bone name.
63 if (!bone_name.is_empty()) {
64 set_bone_name(bone_name);
65 } else if (bone != -1) {
66 set_bone(bone);
67 }
68 }
69
70 Skeleton3D *SpringBoneCollision3D::get_skeleton() const {
71 SpringBoneSimulator3D *parent = Object::cast_to<SpringBoneSimulator3D>(get_parent());
72 if (!parent) {
73 return nullptr;
74 }
75 return parent->get_skeleton();
76 }
77
78 void SpringBoneCollision3D::set_bone_name(const String &p_name) {
79 bone_name = p_name;
80 Skeleton3D *sk = get_skeleton();
81 if (sk) {
82 set_bone(sk->find_bone(bone_name));
83 }
84 }
85
86 String SpringBoneCollision3D::get_bone_name() const {
87 return bone_name;
88 }
89
90 void SpringBoneCollision3D::set_bone(int p_bone) {
91 bone = p_bone;
92
93 Skeleton3D *sk = get_skeleton();
94 if (sk) {
95 if (bone <= -1 || bone >= sk->get_bone_count()) {
96 WARN_PRINT("Bone index out of range! Cannot connect BoneAttachment to node!");
97 bone = -1;
98 } else {
99 bone_name = sk->get_bone_name(bone);
100 }
101 }
102
103 notify_property_list_changed();
104 }
105
106 int SpringBoneCollision3D::get_bone() const {
107 return bone;
108 }
109
110 void SpringBoneCollision3D::set_position_offset(const Vector3 &p_offset) {
111 if (position_offset == p_offset) {
112 return;
113 }
114 position_offset = p_offset;
115 sync_pose();
116 #ifdef TOOLS_ENABLED
117 update_gizmos();
118 #endif // TOOLS_ENABLED
119 }
120
121 Vector3 SpringBoneCollision3D::get_position_offset() const {
122 return position_offset;
123 }
124
125 void SpringBoneCollision3D::set_rotation_offset(const Quaternion &p_offset) {
126 if (rotation_offset == p_offset) {
127 return;
128 }
129 rotation_offset = p_offset;
130 sync_pose();
131 #ifdef TOOLS_ENABLED
132 update_gizmos();
133 #endif // TOOLS_ENABLED
134 }
135
136 Quaternion SpringBoneCollision3D::get_rotation_offset() const {
137 return rotation_offset;
138 }
139
140 void SpringBoneCollision3D::sync_pose() {
141 if (bone >= 0) {
142 Skeleton3D *sk = get_skeleton();
143 if (sk) {
144 Transform3D tr = sk->get_global_transform() * sk->get_bone_global_pose(bone);
145 tr.origin += tr.basis.get_rotation_quaternion().xform(position_offset);
146 tr.basis *= Basis(rotation_offset);
147 set_global_transform(tr);
148 }
149 }
150 }
151
152 Transform3D SpringBoneCollision3D::get_transform_from_skeleton(const Transform3D &p_center) const {
153 Transform3D gtr = get_global_transform();
154 Skeleton3D *sk = get_skeleton();
155 if (sk) {
156 Transform3D tr = sk->get_global_transform();
157 gtr = tr.affine_inverse() * p_center * gtr;
158 }
159 return gtr;
160 }
161
162 void SpringBoneCollision3D::_bind_methods() {
163 ClassDB::bind_method(D_METHOD("get_skeleton"), &SpringBoneCollision3D::get_skeleton);
164
165 ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &SpringBoneCollision3D::set_bone_name);
166 ClassDB::bind_method(D_METHOD("get_bone_name"), &SpringBoneCollision3D::get_bone_name);
167
168 ClassDB::bind_method(D_METHOD("set_bone", "bone"), &SpringBoneCollision3D::set_bone);
169 ClassDB::bind_method(D_METHOD("get_bone"), &SpringBoneCollision3D::get_bone);
170
171 ClassDB::bind_method(D_METHOD("set_position_offset", "offset"), &SpringBoneCollision3D::set_position_offset);
172 ClassDB::bind_method(D_METHOD("get_position_offset"), &SpringBoneCollision3D::get_position_offset);
173
174 ClassDB::bind_method(D_METHOD("set_rotation_offset", "offset"), &SpringBoneCollision3D::set_rotation_offset);
175 ClassDB::bind_method(D_METHOD("get_rotation_offset"), &SpringBoneCollision3D::get_rotation_offset);
176
177 ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name");
178 ADD_PROPERTY(PropertyInfo(Variant::INT, "bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_bone", "get_bone");
179
180 ADD_GROUP("Offset", "");
181 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position_offset"), "set_position_offset", "get_position_offset");
182 ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "rotation_offset"), "set_rotation_offset", "get_rotation_offset");
183 }
184
185 void SpringBoneCollision3D::_notification(int p_what) {
186 switch (p_what) {
187 case NOTIFICATION_ENTER_TREE:
188 case NOTIFICATION_PARENTED: {
189 _validate_bone_name();
190 } break;
191 }
192 }
193
194 Vector3 SpringBoneCollision3D::collide(const Transform3D &p_center, float p_bone_radius, float p_bone_length, const Vector3 &p_current) const {
195 return _collide(p_center, p_bone_radius, p_bone_length, p_current);
196 }
197
198 Vector3 SpringBoneCollision3D::_collide(const Transform3D &p_center, float p_bone_radius, float p_bone_length, const Vector3 &p_current) const {
199 return Vector3(0, 0, 0);
200 }