Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/ik_modifier_3d.h
14709 views
1
/**************************************************************************/
2
/* ik_modifier_3d.h */
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
#pragma once
32
33
#include "scene/3d/skeleton_modifier_3d.h"
34
35
class IKModifier3D : public SkeletonModifier3D {
36
GDCLASS(IKModifier3D, SkeletonModifier3D);
37
38
protected:
39
#ifdef TOOLS_ENABLED
40
bool saving = false;
41
#endif // TOOLS_ENABLED
42
43
bool mutable_bone_axes = true;
44
Transform3D cached_space;
45
bool joints_dirty = false;
46
47
public:
48
struct BoneJoint {
49
StringName name;
50
int bone = -1;
51
};
52
53
struct IKModifier3DSolverInfo {
54
Quaternion current_lpose;
55
Quaternion current_lrest;
56
Quaternion current_gpose;
57
Quaternion current_grest;
58
Vector3 current_vector; // Global so needs xfrom_inv by gpose or grest in the process.
59
Vector3 forward_vector; // Local.
60
float length = 0.0;
61
};
62
63
struct IKModifier3DSetting {
64
bool simulation_dirty = true;
65
bool joints_dirty = false;
66
};
67
68
protected:
69
LocalVector<IKModifier3DSetting *> settings;
70
71
void _notification(int p_what);
72
static void _bind_methods();
73
74
virtual void _set_active(bool p_active) override;
75
virtual void _skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) override;
76
77
virtual void _validate_bone_names() override;
78
79
virtual void _make_all_joints_dirty();
80
virtual void _init_joints(Skeleton3D *p_skeleton, int p_index);
81
virtual void _update_joints(int p_index);
82
virtual void _make_simulation_dirty(int p_index);
83
virtual void _update_bone_axis(Skeleton3D *p_skeleton, int p_index);
84
85
#ifdef TOOLS_ENABLED
86
bool gizmo_dirty = false;
87
void _make_gizmo_dirty();
88
virtual void _update_mutable_info();
89
void _redraw_gizmo();
90
#endif // TOOLS_ENABLED
91
92
virtual void _process_modification(double p_delta) override;
93
virtual void _process_ik(Skeleton3D *p_skeleton, double p_delta);
94
95
template <typename T>
96
void _set_setting_count(int p_count) {
97
ERR_FAIL_COND(p_count < 0);
98
int delta = p_count - settings.size();
99
if (delta < 0) {
100
for (int i = delta; i < 0; i++) {
101
memdelete(static_cast<T *>(settings[settings.size() + i]));
102
settings[settings.size() + i] = nullptr;
103
}
104
}
105
settings.resize(p_count);
106
delta++;
107
if (delta > 1) {
108
for (int i = 1; i < delta; i++) {
109
settings[p_count - i] = memnew(T);
110
}
111
}
112
notify_property_list_changed();
113
}
114
template <typename T>
115
LocalVector<T *> _cast_settings() const {
116
LocalVector<T *> result;
117
for (uint32_t i = 0; i < settings.size(); i++) {
118
result.push_back(static_cast<T *>(settings[i]));
119
}
120
return result;
121
}
122
123
public:
124
int get_setting_count() const;
125
126
virtual void set_setting_count(int p_count) {
127
_set_setting_count<IKModifier3DSetting>(p_count);
128
}
129
virtual void clear_settings() {
130
_set_setting_count<IKModifier3DSetting>(0);
131
}
132
133
void set_mutable_bone_axes(bool p_enabled);
134
bool are_bone_axes_mutable() const;
135
136
// Helper.
137
static Quaternion get_local_pose_rotation(Skeleton3D *p_skeleton, int p_bone, const Quaternion &p_global_pose_rotation);
138
static Vector3 get_bone_axis(Skeleton3D *p_skeleton, int p_end_bone, BoneDirection p_direction, bool p_mutable_bone_axes);
139
140
// To process manually.
141
void reset();
142
143
~IKModifier3D();
144
};
145
146