physical_bone_3d.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. /**************************************************************************/
  2. /* physical_bone_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. #include "physical_bone_3d.h"
  31. #ifndef DISABLE_DEPRECATED
  32. #include "scene/3d/skeleton_3d.h"
  33. #endif //_DISABLE_DEPRECATED
  34. bool PhysicalBone3D::JointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  35. return false;
  36. }
  37. bool PhysicalBone3D::JointData::_get(const StringName &p_name, Variant &r_ret) const {
  38. return false;
  39. }
  40. void PhysicalBone3D::JointData::_get_property_list(List<PropertyInfo> *p_list) const {
  41. }
  42. void PhysicalBone3D::apply_central_impulse(const Vector3 &p_impulse) {
  43. PhysicsServer3D::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
  44. }
  45. void PhysicalBone3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) {
  46. PhysicsServer3D::get_singleton()->body_apply_impulse(get_rid(), p_impulse, p_position);
  47. }
  48. void PhysicalBone3D::set_linear_velocity(const Vector3 &p_velocity) {
  49. linear_velocity = p_velocity;
  50. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  51. }
  52. Vector3 PhysicalBone3D::get_linear_velocity() const {
  53. return linear_velocity;
  54. }
  55. void PhysicalBone3D::set_angular_velocity(const Vector3 &p_velocity) {
  56. angular_velocity = p_velocity;
  57. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
  58. }
  59. Vector3 PhysicalBone3D::get_angular_velocity() const {
  60. return angular_velocity;
  61. }
  62. void PhysicalBone3D::set_use_custom_integrator(bool p_enable) {
  63. if (custom_integrator == p_enable) {
  64. return;
  65. }
  66. custom_integrator = p_enable;
  67. PhysicsServer3D::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
  68. }
  69. bool PhysicalBone3D::is_using_custom_integrator() {
  70. return custom_integrator;
  71. }
  72. void PhysicalBone3D::reset_physics_simulation_state() {
  73. if (simulate_physics) {
  74. _start_physics_simulation();
  75. } else {
  76. _stop_physics_simulation();
  77. }
  78. }
  79. void PhysicalBone3D::reset_to_rest_position() {
  80. PhysicalBoneSimulator3D *simulator = get_simulator();
  81. Skeleton3D *skeleton = get_skeleton();
  82. if (simulator && skeleton) {
  83. if (bone_id == -1) {
  84. set_global_transform((skeleton->get_global_transform() * body_offset).orthonormalized());
  85. } else {
  86. set_global_transform((skeleton->get_global_transform() * simulator->get_bone_global_pose(bone_id) * body_offset).orthonormalized());
  87. }
  88. }
  89. }
  90. bool PhysicalBone3D::PinJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  91. if (JointData::_set(p_name, p_value, j)) {
  92. return true;
  93. }
  94. bool is_valid_pin = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_PIN;
  95. if ("joint_constraints/bias" == p_name) {
  96. bias = p_value;
  97. if (is_valid_pin) {
  98. PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_BIAS, bias);
  99. }
  100. } else if ("joint_constraints/damping" == p_name) {
  101. damping = p_value;
  102. if (is_valid_pin) {
  103. PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_DAMPING, damping);
  104. }
  105. } else if ("joint_constraints/impulse_clamp" == p_name) {
  106. impulse_clamp = p_value;
  107. if (is_valid_pin) {
  108. PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP, impulse_clamp);
  109. }
  110. } else {
  111. return false;
  112. }
  113. return true;
  114. }
  115. bool PhysicalBone3D::PinJointData::_get(const StringName &p_name, Variant &r_ret) const {
  116. if (JointData::_get(p_name, r_ret)) {
  117. return true;
  118. }
  119. if ("joint_constraints/bias" == p_name) {
  120. r_ret = bias;
  121. } else if ("joint_constraints/damping" == p_name) {
  122. r_ret = damping;
  123. } else if ("joint_constraints/impulse_clamp" == p_name) {
  124. r_ret = impulse_clamp;
  125. } else {
  126. return false;
  127. }
  128. return true;
  129. }
  130. void PhysicalBone3D::PinJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  131. JointData::_get_property_list(p_list);
  132. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/bias"), PROPERTY_HINT_RANGE, "0.01,0.99,0.01"));
  133. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/damping"), PROPERTY_HINT_RANGE, "0.01,8.0,0.01"));
  134. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/impulse_clamp"), PROPERTY_HINT_RANGE, "0.0,64.0,0.01"));
  135. }
  136. bool PhysicalBone3D::ConeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  137. if (JointData::_set(p_name, p_value, j)) {
  138. return true;
  139. }
  140. bool is_valid_cone = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_CONE_TWIST;
  141. if ("joint_constraints/swing_span" == p_name) {
  142. swing_span = Math::deg_to_rad(real_t(p_value));
  143. if (is_valid_cone) {
  144. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN, swing_span);
  145. }
  146. } else if ("joint_constraints/twist_span" == p_name) {
  147. twist_span = Math::deg_to_rad(real_t(p_value));
  148. if (is_valid_cone) {
  149. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN, twist_span);
  150. }
  151. } else if ("joint_constraints/bias" == p_name) {
  152. bias = p_value;
  153. if (is_valid_cone) {
  154. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_BIAS, bias);
  155. }
  156. } else if ("joint_constraints/softness" == p_name) {
  157. softness = p_value;
  158. if (is_valid_cone) {
  159. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS, softness);
  160. }
  161. } else if ("joint_constraints/relaxation" == p_name) {
  162. relaxation = p_value;
  163. if (is_valid_cone) {
  164. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION, relaxation);
  165. }
  166. } else {
  167. return false;
  168. }
  169. return true;
  170. }
  171. bool PhysicalBone3D::ConeJointData::_get(const StringName &p_name, Variant &r_ret) const {
  172. if (JointData::_get(p_name, r_ret)) {
  173. return true;
  174. }
  175. if ("joint_constraints/swing_span" == p_name) {
  176. r_ret = Math::rad_to_deg(swing_span);
  177. } else if ("joint_constraints/twist_span" == p_name) {
  178. r_ret = Math::rad_to_deg(twist_span);
  179. } else if ("joint_constraints/bias" == p_name) {
  180. r_ret = bias;
  181. } else if ("joint_constraints/softness" == p_name) {
  182. r_ret = softness;
  183. } else if ("joint_constraints/relaxation" == p_name) {
  184. r_ret = relaxation;
  185. } else {
  186. return false;
  187. }
  188. return true;
  189. }
  190. void PhysicalBone3D::ConeJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  191. JointData::_get_property_list(p_list);
  192. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/swing_span"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  193. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/twist_span"), PROPERTY_HINT_RANGE, "-40000,40000,0.1,or_less,or_greater"));
  194. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/bias"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  195. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/softness"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  196. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/relaxation"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  197. }
  198. bool PhysicalBone3D::HingeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  199. if (JointData::_set(p_name, p_value, j)) {
  200. return true;
  201. }
  202. bool is_valid_hinge = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_HINGE;
  203. if ("joint_constraints/angular_limit_enabled" == p_name) {
  204. angular_limit_enabled = p_value;
  205. if (is_valid_hinge) {
  206. PhysicsServer3D::get_singleton()->hinge_joint_set_flag(j, PhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT, angular_limit_enabled);
  207. }
  208. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  209. angular_limit_upper = Math::deg_to_rad(real_t(p_value));
  210. if (is_valid_hinge) {
  211. PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_UPPER, angular_limit_upper);
  212. }
  213. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  214. angular_limit_lower = Math::deg_to_rad(real_t(p_value));
  215. if (is_valid_hinge) {
  216. PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_LOWER, angular_limit_lower);
  217. }
  218. } else if ("joint_constraints/angular_limit_bias" == p_name) {
  219. angular_limit_bias = p_value;
  220. if (is_valid_hinge) {
  221. PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_BIAS, angular_limit_bias);
  222. }
  223. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  224. angular_limit_softness = p_value;
  225. if (is_valid_hinge) {
  226. PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_SOFTNESS, angular_limit_softness);
  227. }
  228. } else if ("joint_constraints/angular_limit_relaxation" == p_name) {
  229. angular_limit_relaxation = p_value;
  230. if (is_valid_hinge) {
  231. PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_RELAXATION, angular_limit_relaxation);
  232. }
  233. } else {
  234. return false;
  235. }
  236. return true;
  237. }
  238. bool PhysicalBone3D::HingeJointData::_get(const StringName &p_name, Variant &r_ret) const {
  239. if (JointData::_get(p_name, r_ret)) {
  240. return true;
  241. }
  242. if ("joint_constraints/angular_limit_enabled" == p_name) {
  243. r_ret = angular_limit_enabled;
  244. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  245. r_ret = Math::rad_to_deg(angular_limit_upper);
  246. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  247. r_ret = Math::rad_to_deg(angular_limit_lower);
  248. } else if ("joint_constraints/angular_limit_bias" == p_name) {
  249. r_ret = angular_limit_bias;
  250. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  251. r_ret = angular_limit_softness;
  252. } else if ("joint_constraints/angular_limit_relaxation" == p_name) {
  253. r_ret = angular_limit_relaxation;
  254. } else {
  255. return false;
  256. }
  257. return true;
  258. }
  259. void PhysicalBone3D::HingeJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  260. JointData::_get_property_list(p_list);
  261. p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("joint_constraints/angular_limit_enabled")));
  262. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_upper"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  263. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_lower"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  264. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_bias"), PROPERTY_HINT_RANGE, "0.01,0.99,0.01"));
  265. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  266. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_relaxation"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  267. }
  268. bool PhysicalBone3D::SliderJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  269. if (JointData::_set(p_name, p_value, j)) {
  270. return true;
  271. }
  272. bool is_valid_slider = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_SLIDER;
  273. if ("joint_constraints/linear_limit_upper" == p_name) {
  274. linear_limit_upper = p_value;
  275. if (is_valid_slider) {
  276. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER, linear_limit_upper);
  277. }
  278. } else if ("joint_constraints/linear_limit_lower" == p_name) {
  279. linear_limit_lower = p_value;
  280. if (is_valid_slider) {
  281. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER, linear_limit_lower);
  282. }
  283. } else if ("joint_constraints/linear_limit_softness" == p_name) {
  284. linear_limit_softness = p_value;
  285. if (is_valid_slider) {
  286. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, linear_limit_softness);
  287. }
  288. } else if ("joint_constraints/linear_limit_restitution" == p_name) {
  289. linear_limit_restitution = p_value;
  290. if (is_valid_slider) {
  291. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, linear_limit_restitution);
  292. }
  293. } else if ("joint_constraints/linear_limit_damping" == p_name) {
  294. linear_limit_damping = p_value;
  295. if (is_valid_slider) {
  296. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, linear_limit_restitution);
  297. }
  298. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  299. angular_limit_upper = Math::deg_to_rad(real_t(p_value));
  300. if (is_valid_slider) {
  301. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, angular_limit_upper);
  302. }
  303. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  304. angular_limit_lower = Math::deg_to_rad(real_t(p_value));
  305. if (is_valid_slider) {
  306. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, angular_limit_lower);
  307. }
  308. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  309. angular_limit_softness = p_value;
  310. if (is_valid_slider) {
  311. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness);
  312. }
  313. } else if ("joint_constraints/angular_limit_restitution" == p_name) {
  314. angular_limit_restitution = p_value;
  315. if (is_valid_slider) {
  316. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness);
  317. }
  318. } else if ("joint_constraints/angular_limit_damping" == p_name) {
  319. angular_limit_damping = p_value;
  320. if (is_valid_slider) {
  321. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, angular_limit_damping);
  322. }
  323. } else {
  324. return false;
  325. }
  326. return true;
  327. }
  328. bool PhysicalBone3D::SliderJointData::_get(const StringName &p_name, Variant &r_ret) const {
  329. if (JointData::_get(p_name, r_ret)) {
  330. return true;
  331. }
  332. if ("joint_constraints/linear_limit_upper" == p_name) {
  333. r_ret = linear_limit_upper;
  334. } else if ("joint_constraints/linear_limit_lower" == p_name) {
  335. r_ret = linear_limit_lower;
  336. } else if ("joint_constraints/linear_limit_softness" == p_name) {
  337. r_ret = linear_limit_softness;
  338. } else if ("joint_constraints/linear_limit_restitution" == p_name) {
  339. r_ret = linear_limit_restitution;
  340. } else if ("joint_constraints/linear_limit_damping" == p_name) {
  341. r_ret = linear_limit_damping;
  342. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  343. r_ret = Math::rad_to_deg(angular_limit_upper);
  344. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  345. r_ret = Math::rad_to_deg(angular_limit_lower);
  346. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  347. r_ret = angular_limit_softness;
  348. } else if ("joint_constraints/angular_limit_restitution" == p_name) {
  349. r_ret = angular_limit_restitution;
  350. } else if ("joint_constraints/angular_limit_damping" == p_name) {
  351. r_ret = angular_limit_damping;
  352. } else {
  353. return false;
  354. }
  355. return true;
  356. }
  357. void PhysicalBone3D::SliderJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  358. JointData::_get_property_list(p_list);
  359. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_upper")));
  360. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_lower")));
  361. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  362. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_restitution"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  363. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_damping"), PROPERTY_HINT_RANGE, "0,16.0,0.01"));
  364. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_upper"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  365. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_lower"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  366. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  367. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_restitution"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  368. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_damping"), PROPERTY_HINT_RANGE, "0,16.0,0.01"));
  369. }
  370. bool PhysicalBone3D::SixDOFJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  371. if (JointData::_set(p_name, p_value, j)) {
  372. return true;
  373. }
  374. String path = p_name;
  375. if (!path.begins_with("joint_constraints/")) {
  376. return false;
  377. }
  378. Vector3::Axis axis;
  379. {
  380. const String axis_s = path.get_slicec('/', 1);
  381. if ("x" == axis_s) {
  382. axis = Vector3::AXIS_X;
  383. } else if ("y" == axis_s) {
  384. axis = Vector3::AXIS_Y;
  385. } else if ("z" == axis_s) {
  386. axis = Vector3::AXIS_Z;
  387. } else {
  388. return false;
  389. }
  390. }
  391. String var_name = path.get_slicec('/', 2);
  392. bool is_valid_6dof = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_6DOF;
  393. if ("linear_limit_enabled" == var_name) {
  394. axis_data[axis].linear_limit_enabled = p_value;
  395. if (is_valid_6dof) {
  396. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, axis_data[axis].linear_limit_enabled);
  397. }
  398. } else if ("linear_limit_upper" == var_name) {
  399. axis_data[axis].linear_limit_upper = p_value;
  400. if (is_valid_6dof) {
  401. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_UPPER_LIMIT, axis_data[axis].linear_limit_upper);
  402. }
  403. } else if ("linear_limit_lower" == var_name) {
  404. axis_data[axis].linear_limit_lower = p_value;
  405. if (is_valid_6dof) {
  406. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_LOWER_LIMIT, axis_data[axis].linear_limit_lower);
  407. }
  408. } else if ("linear_limit_softness" == var_name) {
  409. axis_data[axis].linear_limit_softness = p_value;
  410. if (is_valid_6dof) {
  411. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, axis_data[axis].linear_limit_softness);
  412. }
  413. } else if ("linear_spring_enabled" == var_name) {
  414. axis_data[axis].linear_spring_enabled = p_value;
  415. if (is_valid_6dof) {
  416. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, axis_data[axis].linear_spring_enabled);
  417. }
  418. } else if ("linear_spring_stiffness" == var_name) {
  419. axis_data[axis].linear_spring_stiffness = p_value;
  420. if (is_valid_6dof) {
  421. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, axis_data[axis].linear_spring_stiffness);
  422. }
  423. } else if ("linear_spring_damping" == var_name) {
  424. axis_data[axis].linear_spring_damping = p_value;
  425. if (is_valid_6dof) {
  426. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_DAMPING, axis_data[axis].linear_spring_damping);
  427. }
  428. } else if ("linear_equilibrium_point" == var_name) {
  429. axis_data[axis].linear_equilibrium_point = p_value;
  430. if (is_valid_6dof) {
  431. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].linear_equilibrium_point);
  432. }
  433. } else if ("linear_restitution" == var_name) {
  434. axis_data[axis].linear_restitution = p_value;
  435. if (is_valid_6dof) {
  436. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_RESTITUTION, axis_data[axis].linear_restitution);
  437. }
  438. } else if ("linear_damping" == var_name) {
  439. axis_data[axis].linear_damping = p_value;
  440. if (is_valid_6dof) {
  441. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_DAMPING, axis_data[axis].linear_damping);
  442. }
  443. } else if ("angular_limit_enabled" == var_name) {
  444. axis_data[axis].angular_limit_enabled = p_value;
  445. if (is_valid_6dof) {
  446. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, axis_data[axis].angular_limit_enabled);
  447. }
  448. } else if ("angular_limit_upper" == var_name) {
  449. axis_data[axis].angular_limit_upper = Math::deg_to_rad(real_t(p_value));
  450. if (is_valid_6dof) {
  451. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, axis_data[axis].angular_limit_upper);
  452. }
  453. } else if ("angular_limit_lower" == var_name) {
  454. axis_data[axis].angular_limit_lower = Math::deg_to_rad(real_t(p_value));
  455. if (is_valid_6dof) {
  456. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, axis_data[axis].angular_limit_lower);
  457. }
  458. } else if ("angular_limit_softness" == var_name) {
  459. axis_data[axis].angular_limit_softness = p_value;
  460. if (is_valid_6dof) {
  461. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, axis_data[axis].angular_limit_softness);
  462. }
  463. } else if ("angular_restitution" == var_name) {
  464. axis_data[axis].angular_restitution = p_value;
  465. if (is_valid_6dof) {
  466. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_RESTITUTION, axis_data[axis].angular_restitution);
  467. }
  468. } else if ("angular_damping" == var_name) {
  469. axis_data[axis].angular_damping = p_value;
  470. if (is_valid_6dof) {
  471. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_DAMPING, axis_data[axis].angular_damping);
  472. }
  473. } else if ("erp" == var_name) {
  474. axis_data[axis].erp = p_value;
  475. if (is_valid_6dof) {
  476. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_ERP, axis_data[axis].erp);
  477. }
  478. } else if ("angular_spring_enabled" == var_name) {
  479. axis_data[axis].angular_spring_enabled = p_value;
  480. if (is_valid_6dof) {
  481. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, axis_data[axis].angular_spring_enabled);
  482. }
  483. } else if ("angular_spring_stiffness" == var_name) {
  484. axis_data[axis].angular_spring_stiffness = p_value;
  485. if (is_valid_6dof) {
  486. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, axis_data[axis].angular_spring_stiffness);
  487. }
  488. } else if ("angular_spring_damping" == var_name) {
  489. axis_data[axis].angular_spring_damping = p_value;
  490. if (is_valid_6dof) {
  491. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, axis_data[axis].angular_spring_damping);
  492. }
  493. } else if ("angular_equilibrium_point" == var_name) {
  494. axis_data[axis].angular_equilibrium_point = p_value;
  495. if (is_valid_6dof) {
  496. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].angular_equilibrium_point);
  497. }
  498. } else {
  499. return false;
  500. }
  501. return true;
  502. }
  503. bool PhysicalBone3D::SixDOFJointData::_get(const StringName &p_name, Variant &r_ret) const {
  504. if (JointData::_get(p_name, r_ret)) {
  505. return true;
  506. }
  507. String path = p_name;
  508. if (!path.begins_with("joint_constraints/")) {
  509. return false;
  510. }
  511. int axis;
  512. {
  513. const String axis_s = path.get_slicec('/', 1);
  514. if ("x" == axis_s) {
  515. axis = 0;
  516. } else if ("y" == axis_s) {
  517. axis = 1;
  518. } else if ("z" == axis_s) {
  519. axis = 2;
  520. } else {
  521. return false;
  522. }
  523. }
  524. String var_name = path.get_slicec('/', 2);
  525. if ("linear_limit_enabled" == var_name) {
  526. r_ret = axis_data[axis].linear_limit_enabled;
  527. } else if ("linear_limit_upper" == var_name) {
  528. r_ret = axis_data[axis].linear_limit_upper;
  529. } else if ("linear_limit_lower" == var_name) {
  530. r_ret = axis_data[axis].linear_limit_lower;
  531. } else if ("linear_limit_softness" == var_name) {
  532. r_ret = axis_data[axis].linear_limit_softness;
  533. } else if ("linear_spring_enabled" == var_name) {
  534. r_ret = axis_data[axis].linear_spring_enabled;
  535. } else if ("linear_spring_stiffness" == var_name) {
  536. r_ret = axis_data[axis].linear_spring_stiffness;
  537. } else if ("linear_spring_damping" == var_name) {
  538. r_ret = axis_data[axis].linear_spring_damping;
  539. } else if ("linear_equilibrium_point" == var_name) {
  540. r_ret = axis_data[axis].linear_equilibrium_point;
  541. } else if ("linear_restitution" == var_name) {
  542. r_ret = axis_data[axis].linear_restitution;
  543. } else if ("linear_damping" == var_name) {
  544. r_ret = axis_data[axis].linear_damping;
  545. } else if ("angular_limit_enabled" == var_name) {
  546. r_ret = axis_data[axis].angular_limit_enabled;
  547. } else if ("angular_limit_upper" == var_name) {
  548. r_ret = Math::rad_to_deg(axis_data[axis].angular_limit_upper);
  549. } else if ("angular_limit_lower" == var_name) {
  550. r_ret = Math::rad_to_deg(axis_data[axis].angular_limit_lower);
  551. } else if ("angular_limit_softness" == var_name) {
  552. r_ret = axis_data[axis].angular_limit_softness;
  553. } else if ("angular_restitution" == var_name) {
  554. r_ret = axis_data[axis].angular_restitution;
  555. } else if ("angular_damping" == var_name) {
  556. r_ret = axis_data[axis].angular_damping;
  557. } else if ("erp" == var_name) {
  558. r_ret = axis_data[axis].erp;
  559. } else if ("angular_spring_enabled" == var_name) {
  560. r_ret = axis_data[axis].angular_spring_enabled;
  561. } else if ("angular_spring_stiffness" == var_name) {
  562. r_ret = axis_data[axis].angular_spring_stiffness;
  563. } else if ("angular_spring_damping" == var_name) {
  564. r_ret = axis_data[axis].angular_spring_damping;
  565. } else if ("angular_equilibrium_point" == var_name) {
  566. r_ret = axis_data[axis].angular_equilibrium_point;
  567. } else {
  568. return false;
  569. }
  570. return true;
  571. }
  572. void PhysicalBone3D::SixDOFJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  573. const StringName axis_names[] = { PNAME("x"), PNAME("y"), PNAME("z") };
  574. for (int i = 0; i < 3; ++i) {
  575. const String prefix = vformat("%s/%s/", PNAME("joint_constraints"), axis_names[i]);
  576. p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("linear_limit_enabled")));
  577. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_upper")));
  578. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_lower")));
  579. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  580. p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("linear_spring_enabled")));
  581. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_spring_stiffness")));
  582. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_spring_damping")));
  583. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_equilibrium_point")));
  584. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_restitution"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  585. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_damping"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  586. p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("angular_limit_enabled")));
  587. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_upper"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  588. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_lower"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  589. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  590. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_restitution"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  591. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_damping"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  592. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("erp")));
  593. p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("angular_spring_enabled")));
  594. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_spring_stiffness")));
  595. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_spring_damping")));
  596. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_equilibrium_point")));
  597. }
  598. }
  599. bool PhysicalBone3D::_set(const StringName &p_name, const Variant &p_value) {
  600. if (p_name == "bone_name") {
  601. set_bone_name(p_value);
  602. return true;
  603. }
  604. if (joint_data) {
  605. if (joint_data->_set(p_name, p_value, joint)) {
  606. #ifdef TOOLS_ENABLED
  607. update_gizmos();
  608. #endif
  609. return true;
  610. }
  611. }
  612. return false;
  613. }
  614. bool PhysicalBone3D::_get(const StringName &p_name, Variant &r_ret) const {
  615. if (p_name == "bone_name") {
  616. r_ret = get_bone_name();
  617. return true;
  618. }
  619. if (joint_data) {
  620. return joint_data->_get(p_name, r_ret);
  621. }
  622. return false;
  623. }
  624. void PhysicalBone3D::_get_property_list(List<PropertyInfo> *p_list) const {
  625. Skeleton3D *skeleton = get_skeleton();
  626. if (skeleton) {
  627. p_list->push_back(PropertyInfo(Variant::STRING_NAME, PNAME("bone_name"), PROPERTY_HINT_ENUM, skeleton->get_concatenated_bone_names()));
  628. } else {
  629. p_list->push_back(PropertyInfo(Variant::STRING_NAME, PNAME("bone_name")));
  630. }
  631. if (joint_data) {
  632. joint_data->_get_property_list(p_list);
  633. }
  634. }
  635. void PhysicalBone3D::_notification(int p_what) {
  636. switch (p_what) {
  637. case NOTIFICATION_ENTER_TREE:
  638. case NOTIFICATION_PARENTED:
  639. _update_simulator_path();
  640. update_bone_id();
  641. reset_to_rest_position();
  642. reset_physics_simulation_state();
  643. if (joint_data) {
  644. _reload_joint();
  645. }
  646. break;
  647. case NOTIFICATION_EXIT_TREE: {
  648. PhysicalBoneSimulator3D *simulator = get_simulator();
  649. if (simulator) {
  650. if (bone_id != -1) {
  651. simulator->unbind_physical_bone_from_bone(bone_id);
  652. bone_id = -1;
  653. }
  654. }
  655. PhysicsServer3D::get_singleton()->joint_clear(joint);
  656. } break;
  657. case NOTIFICATION_TRANSFORM_CHANGED: {
  658. if (Engine::get_singleton()->is_editor_hint()) {
  659. update_offset();
  660. }
  661. } break;
  662. }
  663. }
  664. void PhysicalBone3D::_sync_body_state(PhysicsDirectBodyState3D *p_state) {
  665. set_ignore_transform_notification(true);
  666. set_global_transform(p_state->get_transform());
  667. set_ignore_transform_notification(false);
  668. linear_velocity = p_state->get_linear_velocity();
  669. angular_velocity = p_state->get_angular_velocity();
  670. }
  671. void PhysicalBone3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
  672. if (!simulate_physics || !_internal_simulate_physics) {
  673. return;
  674. }
  675. if (GDVIRTUAL_IS_OVERRIDDEN(_integrate_forces)) {
  676. _sync_body_state(p_state);
  677. Transform3D old_transform = get_global_transform();
  678. GDVIRTUAL_CALL(_integrate_forces, p_state);
  679. Transform3D new_transform = get_global_transform();
  680. if (new_transform != old_transform) {
  681. // Update the physics server with the new transform, to prevent it from being overwritten at the sync below.
  682. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_TRANSFORM, new_transform);
  683. }
  684. }
  685. _sync_body_state(p_state);
  686. _on_transform_changed();
  687. Transform3D global_transform(p_state->get_transform());
  688. // Update simulator
  689. PhysicalBoneSimulator3D *simulator = get_simulator();
  690. Skeleton3D *skeleton = get_skeleton();
  691. if (simulator && skeleton) {
  692. if (bone_id != -1) {
  693. simulator->set_bone_global_pose(bone_id, skeleton->get_global_transform().affine_inverse() * (global_transform * body_offset_inverse));
  694. }
  695. }
  696. }
  697. void PhysicalBone3D::_bind_methods() {
  698. ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &PhysicalBone3D::apply_central_impulse);
  699. ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &PhysicalBone3D::apply_impulse, Vector3());
  700. ClassDB::bind_method(D_METHOD("set_joint_type", "joint_type"), &PhysicalBone3D::set_joint_type);
  701. ClassDB::bind_method(D_METHOD("get_joint_type"), &PhysicalBone3D::get_joint_type);
  702. ClassDB::bind_method(D_METHOD("set_joint_offset", "offset"), &PhysicalBone3D::set_joint_offset);
  703. ClassDB::bind_method(D_METHOD("get_joint_offset"), &PhysicalBone3D::get_joint_offset);
  704. ClassDB::bind_method(D_METHOD("set_joint_rotation", "euler"), &PhysicalBone3D::set_joint_rotation);
  705. ClassDB::bind_method(D_METHOD("get_joint_rotation"), &PhysicalBone3D::get_joint_rotation);
  706. ClassDB::bind_method(D_METHOD("set_body_offset", "offset"), &PhysicalBone3D::set_body_offset);
  707. ClassDB::bind_method(D_METHOD("get_body_offset"), &PhysicalBone3D::get_body_offset);
  708. ClassDB::bind_method(D_METHOD("get_simulate_physics"), &PhysicalBone3D::get_simulate_physics);
  709. ClassDB::bind_method(D_METHOD("is_simulating_physics"), &PhysicalBone3D::is_simulating_physics);
  710. ClassDB::bind_method(D_METHOD("get_bone_id"), &PhysicalBone3D::get_bone_id);
  711. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &PhysicalBone3D::set_mass);
  712. ClassDB::bind_method(D_METHOD("get_mass"), &PhysicalBone3D::get_mass);
  713. ClassDB::bind_method(D_METHOD("set_friction", "friction"), &PhysicalBone3D::set_friction);
  714. ClassDB::bind_method(D_METHOD("get_friction"), &PhysicalBone3D::get_friction);
  715. ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &PhysicalBone3D::set_bounce);
  716. ClassDB::bind_method(D_METHOD("get_bounce"), &PhysicalBone3D::get_bounce);
  717. ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &PhysicalBone3D::set_gravity_scale);
  718. ClassDB::bind_method(D_METHOD("get_gravity_scale"), &PhysicalBone3D::get_gravity_scale);
  719. ClassDB::bind_method(D_METHOD("set_linear_damp_mode", "linear_damp_mode"), &PhysicalBone3D::set_linear_damp_mode);
  720. ClassDB::bind_method(D_METHOD("get_linear_damp_mode"), &PhysicalBone3D::get_linear_damp_mode);
  721. ClassDB::bind_method(D_METHOD("set_angular_damp_mode", "angular_damp_mode"), &PhysicalBone3D::set_angular_damp_mode);
  722. ClassDB::bind_method(D_METHOD("get_angular_damp_mode"), &PhysicalBone3D::get_angular_damp_mode);
  723. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &PhysicalBone3D::set_linear_damp);
  724. ClassDB::bind_method(D_METHOD("get_linear_damp"), &PhysicalBone3D::get_linear_damp);
  725. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &PhysicalBone3D::set_angular_damp);
  726. ClassDB::bind_method(D_METHOD("get_angular_damp"), &PhysicalBone3D::get_angular_damp);
  727. ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &PhysicalBone3D::set_linear_velocity);
  728. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &PhysicalBone3D::get_linear_velocity);
  729. ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &PhysicalBone3D::set_angular_velocity);
  730. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &PhysicalBone3D::get_angular_velocity);
  731. ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &PhysicalBone3D::set_use_custom_integrator);
  732. ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &PhysicalBone3D::is_using_custom_integrator);
  733. ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &PhysicalBone3D::set_can_sleep);
  734. ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &PhysicalBone3D::is_able_to_sleep);
  735. GDVIRTUAL_BIND(_integrate_forces, "state");
  736. ADD_GROUP("Joint", "joint_");
  737. ADD_PROPERTY(PropertyInfo(Variant::INT, "joint_type", PROPERTY_HINT_ENUM, "None,PinJoint,ConeJoint,HingeJoint,SliderJoint,6DOFJoint"), "set_joint_type", "get_joint_type");
  738. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "joint_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_joint_offset", "get_joint_offset");
  739. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "joint_rotation", PROPERTY_HINT_RANGE, "-360,360,0.01,or_less,or_greater,radians_as_degrees"), "set_joint_rotation", "get_joint_rotation");
  740. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "body_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_body_offset", "get_body_offset");
  741. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass", PROPERTY_HINT_RANGE, "0.01,1000,0.01,or_greater,exp,suffix:kg"), "set_mass", "get_mass");
  742. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_friction", "get_friction");
  743. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_bounce", "get_bounce");
  744. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_scale", PROPERTY_HINT_RANGE, "-8,8,0.001,or_less,or_greater"), "set_gravity_scale", "get_gravity_scale");
  745. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
  746. ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_linear_damp_mode", "get_linear_damp_mode");
  747. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
  748. ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_angular_damp_mode", "get_angular_damp_mode");
  749. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
  750. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity", PROPERTY_HINT_NONE, "suffix:m/s"), "set_linear_velocity", "get_linear_velocity");
  751. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity", PROPERTY_HINT_NONE, U"radians_as_degrees,suffix:\u00B0/s"), "set_angular_velocity", "get_angular_velocity");
  752. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
  753. BIND_ENUM_CONSTANT(DAMP_MODE_COMBINE);
  754. BIND_ENUM_CONSTANT(DAMP_MODE_REPLACE);
  755. BIND_ENUM_CONSTANT(JOINT_TYPE_NONE);
  756. BIND_ENUM_CONSTANT(JOINT_TYPE_PIN);
  757. BIND_ENUM_CONSTANT(JOINT_TYPE_CONE);
  758. BIND_ENUM_CONSTANT(JOINT_TYPE_HINGE);
  759. BIND_ENUM_CONSTANT(JOINT_TYPE_SLIDER);
  760. BIND_ENUM_CONSTANT(JOINT_TYPE_6DOF);
  761. }
  762. void PhysicalBone3D::_update_joint_offset() {
  763. _fix_joint_offset();
  764. set_ignore_transform_notification(true);
  765. reset_to_rest_position();
  766. set_ignore_transform_notification(false);
  767. #ifdef TOOLS_ENABLED
  768. update_gizmos();
  769. #endif
  770. }
  771. void PhysicalBone3D::_fix_joint_offset() {
  772. // Clamp joint origin to bone origin
  773. PhysicalBoneSimulator3D *simulator = get_simulator();
  774. if (simulator) {
  775. joint_offset.origin = body_offset.affine_inverse().origin;
  776. }
  777. }
  778. void PhysicalBone3D::_reload_joint() {
  779. PhysicalBoneSimulator3D *simulator = get_simulator();
  780. if (!simulator || !simulator->get_skeleton()) {
  781. PhysicsServer3D::get_singleton()->joint_clear(joint);
  782. return;
  783. }
  784. PhysicalBone3D *body_a = simulator->get_physical_bone_parent(bone_id);
  785. if (!body_a) {
  786. PhysicsServer3D::get_singleton()->joint_clear(joint);
  787. return;
  788. }
  789. Transform3D joint_transf = get_global_transform() * joint_offset;
  790. Transform3D local_a = body_a->get_global_transform().affine_inverse() * joint_transf;
  791. local_a.orthonormalize();
  792. switch (get_joint_type()) {
  793. case JOINT_TYPE_PIN: {
  794. PhysicsServer3D::get_singleton()->joint_make_pin(joint, body_a->get_rid(), local_a.origin, get_rid(), joint_offset.origin);
  795. const PinJointData *pjd(static_cast<const PinJointData *>(joint_data));
  796. PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_BIAS, pjd->bias);
  797. PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_DAMPING, pjd->damping);
  798. PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP, pjd->impulse_clamp);
  799. } break;
  800. case JOINT_TYPE_CONE: {
  801. PhysicsServer3D::get_singleton()->joint_make_cone_twist(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
  802. const ConeJointData *cjd(static_cast<const ConeJointData *>(joint_data));
  803. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN, cjd->swing_span);
  804. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN, cjd->twist_span);
  805. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_BIAS, cjd->bias);
  806. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS, cjd->softness);
  807. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION, cjd->relaxation);
  808. } break;
  809. case JOINT_TYPE_HINGE: {
  810. PhysicsServer3D::get_singleton()->joint_make_hinge(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
  811. const HingeJointData *hjd(static_cast<const HingeJointData *>(joint_data));
  812. PhysicsServer3D::get_singleton()->hinge_joint_set_flag(joint, PhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT, hjd->angular_limit_enabled);
  813. PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_UPPER, hjd->angular_limit_upper);
  814. PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_LOWER, hjd->angular_limit_lower);
  815. PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_BIAS, hjd->angular_limit_bias);
  816. PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_SOFTNESS, hjd->angular_limit_softness);
  817. PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_RELAXATION, hjd->angular_limit_relaxation);
  818. } break;
  819. case JOINT_TYPE_SLIDER: {
  820. PhysicsServer3D::get_singleton()->joint_make_slider(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
  821. const SliderJointData *sjd(static_cast<const SliderJointData *>(joint_data));
  822. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER, sjd->linear_limit_upper);
  823. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER, sjd->linear_limit_lower);
  824. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, sjd->linear_limit_softness);
  825. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, sjd->linear_limit_restitution);
  826. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, sjd->linear_limit_restitution);
  827. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, sjd->angular_limit_upper);
  828. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, sjd->angular_limit_lower);
  829. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness);
  830. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness);
  831. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, sjd->angular_limit_damping);
  832. } break;
  833. case JOINT_TYPE_6DOF: {
  834. PhysicsServer3D::get_singleton()->joint_make_generic_6dof(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
  835. const SixDOFJointData *g6dofjd(static_cast<const SixDOFJointData *>(joint_data));
  836. for (int axis = 0; axis < 3; ++axis) {
  837. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, g6dofjd->axis_data[axis].linear_limit_enabled);
  838. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_UPPER_LIMIT, g6dofjd->axis_data[axis].linear_limit_upper);
  839. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_LOWER_LIMIT, g6dofjd->axis_data[axis].linear_limit_lower);
  840. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].linear_limit_softness);
  841. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, g6dofjd->axis_data[axis].linear_spring_enabled);
  842. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].linear_spring_stiffness);
  843. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_DAMPING, g6dofjd->axis_data[axis].linear_spring_damping);
  844. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].linear_equilibrium_point);
  845. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_RESTITUTION, g6dofjd->axis_data[axis].linear_restitution);
  846. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_DAMPING, g6dofjd->axis_data[axis].linear_damping);
  847. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, g6dofjd->axis_data[axis].angular_limit_enabled);
  848. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, g6dofjd->axis_data[axis].angular_limit_upper);
  849. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, g6dofjd->axis_data[axis].angular_limit_lower);
  850. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].angular_limit_softness);
  851. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_RESTITUTION, g6dofjd->axis_data[axis].angular_restitution);
  852. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_DAMPING, g6dofjd->axis_data[axis].angular_damping);
  853. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_ERP, g6dofjd->axis_data[axis].erp);
  854. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, g6dofjd->axis_data[axis].angular_spring_enabled);
  855. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].angular_spring_stiffness);
  856. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, g6dofjd->axis_data[axis].angular_spring_damping);
  857. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].angular_equilibrium_point);
  858. }
  859. } break;
  860. case JOINT_TYPE_NONE: {
  861. } break;
  862. }
  863. }
  864. void PhysicalBone3D::_on_bone_parent_changed() {
  865. _reload_joint();
  866. }
  867. void PhysicalBone3D::_update_simulator_path() {
  868. simulator_id = ObjectID();
  869. PhysicalBoneSimulator3D *sim = cast_to<PhysicalBoneSimulator3D>(get_parent());
  870. if (sim) {
  871. simulator_id = sim->get_instance_id();
  872. return;
  873. }
  874. #ifndef DISABLE_DEPRECATED
  875. Skeleton3D *sk = cast_to<Skeleton3D>(get_parent());
  876. if (sk) {
  877. PhysicalBoneSimulator3D *ssim = cast_to<PhysicalBoneSimulator3D>(sk->get_simulator());
  878. if (ssim) {
  879. simulator_id = ssim->get_instance_id();
  880. }
  881. }
  882. #endif // _DISABLE_DEPRECATED
  883. }
  884. PhysicalBoneSimulator3D *PhysicalBone3D::get_simulator() const {
  885. return Object::cast_to<PhysicalBoneSimulator3D>(ObjectDB::get_instance(simulator_id));
  886. }
  887. Skeleton3D *PhysicalBone3D::get_skeleton() const {
  888. PhysicalBoneSimulator3D *simulator = get_simulator();
  889. if (simulator) {
  890. return simulator->get_skeleton();
  891. }
  892. return nullptr;
  893. }
  894. #ifdef TOOLS_ENABLED
  895. void PhysicalBone3D::_set_gizmo_move_joint(bool p_move_joint) {
  896. gizmo_move_joint = p_move_joint;
  897. }
  898. Transform3D PhysicalBone3D::get_global_gizmo_transform() const {
  899. return gizmo_move_joint ? get_global_transform() * joint_offset : get_global_transform();
  900. }
  901. Transform3D PhysicalBone3D::get_local_gizmo_transform() const {
  902. return gizmo_move_joint ? get_transform() * joint_offset : get_transform();
  903. }
  904. #endif
  905. const PhysicalBone3D::JointData *PhysicalBone3D::get_joint_data() const {
  906. return joint_data;
  907. }
  908. void PhysicalBone3D::set_joint_type(JointType p_joint_type) {
  909. if (p_joint_type == get_joint_type()) {
  910. return;
  911. }
  912. if (joint_data) {
  913. memdelete(joint_data);
  914. }
  915. joint_data = nullptr;
  916. switch (p_joint_type) {
  917. case JOINT_TYPE_PIN:
  918. joint_data = memnew(PinJointData);
  919. break;
  920. case JOINT_TYPE_CONE:
  921. joint_data = memnew(ConeJointData);
  922. break;
  923. case JOINT_TYPE_HINGE:
  924. joint_data = memnew(HingeJointData);
  925. break;
  926. case JOINT_TYPE_SLIDER:
  927. joint_data = memnew(SliderJointData);
  928. break;
  929. case JOINT_TYPE_6DOF:
  930. joint_data = memnew(SixDOFJointData);
  931. break;
  932. case JOINT_TYPE_NONE:
  933. break;
  934. }
  935. _reload_joint();
  936. #ifdef TOOLS_ENABLED
  937. notify_property_list_changed();
  938. update_gizmos();
  939. #endif
  940. }
  941. PhysicalBone3D::JointType PhysicalBone3D::get_joint_type() const {
  942. return joint_data ? joint_data->get_joint_type() : JOINT_TYPE_NONE;
  943. }
  944. void PhysicalBone3D::set_joint_offset(const Transform3D &p_offset) {
  945. joint_offset = p_offset;
  946. _update_joint_offset();
  947. }
  948. const Transform3D &PhysicalBone3D::get_joint_offset() const {
  949. return joint_offset;
  950. }
  951. void PhysicalBone3D::set_joint_rotation(const Vector3 &p_euler_rad) {
  952. joint_offset.basis.set_euler_scale(p_euler_rad, joint_offset.basis.get_scale());
  953. _update_joint_offset();
  954. }
  955. Vector3 PhysicalBone3D::get_joint_rotation() const {
  956. return joint_offset.basis.get_euler_normalized();
  957. }
  958. const Transform3D &PhysicalBone3D::get_body_offset() const {
  959. return body_offset;
  960. }
  961. void PhysicalBone3D::set_body_offset(const Transform3D &p_offset) {
  962. body_offset = p_offset;
  963. body_offset_inverse = body_offset.affine_inverse();
  964. _update_joint_offset();
  965. }
  966. void PhysicalBone3D::set_simulate_physics(bool p_simulate) {
  967. if (simulate_physics == p_simulate) {
  968. return;
  969. }
  970. simulate_physics = p_simulate;
  971. reset_physics_simulation_state();
  972. }
  973. bool PhysicalBone3D::get_simulate_physics() {
  974. return simulate_physics;
  975. }
  976. bool PhysicalBone3D::is_simulating_physics() {
  977. return _internal_simulate_physics;
  978. }
  979. void PhysicalBone3D::set_bone_name(const String &p_name) {
  980. bone_name = p_name;
  981. bone_id = -1;
  982. update_bone_id();
  983. reset_to_rest_position();
  984. }
  985. const String &PhysicalBone3D::get_bone_name() const {
  986. return bone_name;
  987. }
  988. void PhysicalBone3D::set_mass(real_t p_mass) {
  989. ERR_FAIL_COND(p_mass <= 0);
  990. mass = p_mass;
  991. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_MASS, mass);
  992. }
  993. real_t PhysicalBone3D::get_mass() const {
  994. return mass;
  995. }
  996. void PhysicalBone3D::set_friction(real_t p_friction) {
  997. ERR_FAIL_COND(p_friction < 0 || p_friction > 1);
  998. friction = p_friction;
  999. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, friction);
  1000. }
  1001. real_t PhysicalBone3D::get_friction() const {
  1002. return friction;
  1003. }
  1004. void PhysicalBone3D::set_bounce(real_t p_bounce) {
  1005. ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
  1006. bounce = p_bounce;
  1007. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, bounce);
  1008. }
  1009. real_t PhysicalBone3D::get_bounce() const {
  1010. return bounce;
  1011. }
  1012. void PhysicalBone3D::set_gravity_scale(real_t p_gravity_scale) {
  1013. gravity_scale = p_gravity_scale;
  1014. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
  1015. }
  1016. real_t PhysicalBone3D::get_gravity_scale() const {
  1017. return gravity_scale;
  1018. }
  1019. void PhysicalBone3D::set_linear_damp_mode(DampMode p_mode) {
  1020. linear_damp_mode = p_mode;
  1021. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE, linear_damp_mode);
  1022. }
  1023. PhysicalBone3D::DampMode PhysicalBone3D::get_linear_damp_mode() const {
  1024. return linear_damp_mode;
  1025. }
  1026. void PhysicalBone3D::set_angular_damp_mode(DampMode p_mode) {
  1027. angular_damp_mode = p_mode;
  1028. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE, angular_damp_mode);
  1029. }
  1030. PhysicalBone3D::DampMode PhysicalBone3D::get_angular_damp_mode() const {
  1031. return angular_damp_mode;
  1032. }
  1033. void PhysicalBone3D::set_linear_damp(real_t p_linear_damp) {
  1034. ERR_FAIL_COND(p_linear_damp < 0);
  1035. linear_damp = p_linear_damp;
  1036. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP, linear_damp);
  1037. }
  1038. real_t PhysicalBone3D::get_linear_damp() const {
  1039. return linear_damp;
  1040. }
  1041. void PhysicalBone3D::set_angular_damp(real_t p_angular_damp) {
  1042. ERR_FAIL_COND(p_angular_damp < 0);
  1043. angular_damp = p_angular_damp;
  1044. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP, angular_damp);
  1045. }
  1046. real_t PhysicalBone3D::get_angular_damp() const {
  1047. return angular_damp;
  1048. }
  1049. void PhysicalBone3D::set_can_sleep(bool p_active) {
  1050. can_sleep = p_active;
  1051. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_CAN_SLEEP, p_active);
  1052. }
  1053. bool PhysicalBone3D::is_able_to_sleep() const {
  1054. return can_sleep;
  1055. }
  1056. PhysicalBone3D::PhysicalBone3D() :
  1057. PhysicsBody3D(PhysicsServer3D::BODY_MODE_STATIC) {
  1058. joint = PhysicsServer3D::get_singleton()->joint_create();
  1059. reset_physics_simulation_state();
  1060. }
  1061. PhysicalBone3D::~PhysicalBone3D() {
  1062. if (joint_data) {
  1063. memdelete(joint_data);
  1064. }
  1065. ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
  1066. PhysicsServer3D::get_singleton()->free(joint);
  1067. }
  1068. void PhysicalBone3D::update_bone_id() {
  1069. PhysicalBoneSimulator3D *simulator = get_simulator();
  1070. if (!simulator) {
  1071. return;
  1072. }
  1073. const int new_bone_id = simulator->find_bone(bone_name);
  1074. if (new_bone_id != bone_id) {
  1075. if (bone_id != -1) {
  1076. // Assert the unbind from old node
  1077. simulator->unbind_physical_bone_from_bone(bone_id);
  1078. }
  1079. bone_id = new_bone_id;
  1080. simulator->bind_physical_bone_to_bone(bone_id, this);
  1081. _fix_joint_offset();
  1082. reset_physics_simulation_state();
  1083. }
  1084. }
  1085. void PhysicalBone3D::update_offset() {
  1086. #ifdef TOOLS_ENABLED
  1087. PhysicalBoneSimulator3D *simulator = get_simulator();
  1088. Skeleton3D *skeleton = get_skeleton();
  1089. if (simulator && skeleton) {
  1090. Transform3D bone_transform(skeleton->get_global_transform());
  1091. if (bone_id != -1) {
  1092. bone_transform *= simulator->get_bone_global_pose(bone_id);
  1093. }
  1094. if (gizmo_move_joint) {
  1095. bone_transform *= body_offset;
  1096. set_joint_offset(bone_transform.affine_inverse() * get_global_transform());
  1097. } else {
  1098. set_body_offset(bone_transform.affine_inverse() * get_global_transform());
  1099. }
  1100. }
  1101. #endif
  1102. }
  1103. void PhysicalBone3D::_start_physics_simulation() {
  1104. if (_internal_simulate_physics || !simulator_id.is_valid() || bone_id == -1) {
  1105. return;
  1106. }
  1107. reset_to_rest_position();
  1108. set_body_mode(PhysicsServer3D::BODY_MODE_RIGID);
  1109. PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer());
  1110. PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask());
  1111. PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), get_collision_priority());
  1112. PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &PhysicalBone3D::_body_state_changed));
  1113. set_as_top_level(true);
  1114. _internal_simulate_physics = true;
  1115. }
  1116. void PhysicalBone3D::_stop_physics_simulation() {
  1117. PhysicalBoneSimulator3D *simulator = get_simulator();
  1118. if (simulator) {
  1119. if (simulator->is_active() && bone_id != -1) {
  1120. set_body_mode(PhysicsServer3D::BODY_MODE_KINEMATIC);
  1121. PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer());
  1122. PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask());
  1123. PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), get_collision_priority());
  1124. } else {
  1125. set_body_mode(PhysicsServer3D::BODY_MODE_STATIC);
  1126. PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), 0);
  1127. PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), 0);
  1128. PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), 1.0);
  1129. }
  1130. }
  1131. if (_internal_simulate_physics) {
  1132. PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), Callable());
  1133. set_as_top_level(false);
  1134. _internal_simulate_physics = false;
  1135. }
  1136. }