physical_bone_3d.cpp 59 KB

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