physics_joint.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. /**************************************************************************/
  2. /* physics_joint.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 "physics_joint.h"
  31. #include "scene/scene_string_names.h"
  32. void Joint::_disconnect_signals() {
  33. Node *node_a = get_node_or_null(a);
  34. PhysicsBody *body_a = Object::cast_to<PhysicsBody>(node_a);
  35. if (body_a) {
  36. body_a->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
  37. }
  38. Node *node_b = get_node_or_null(b);
  39. PhysicsBody *body_b = Object::cast_to<PhysicsBody>(node_b);
  40. if (body_b) {
  41. body_b->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
  42. }
  43. }
  44. void Joint::_body_exit_tree() {
  45. _disconnect_signals();
  46. _update_joint(true);
  47. }
  48. void Joint::_update_joint(bool p_only_free) {
  49. if (joint.is_valid()) {
  50. if (ba.is_valid() && bb.is_valid()) {
  51. PhysicsServer::get_singleton()->body_remove_collision_exception(ba, bb);
  52. PhysicsServer::get_singleton()->body_remove_collision_exception(bb, ba);
  53. }
  54. PhysicsServer::get_singleton()->free(joint);
  55. joint = RID();
  56. ba = RID();
  57. bb = RID();
  58. }
  59. if (p_only_free || !is_inside_tree()) {
  60. warning = String();
  61. return;
  62. }
  63. Node *node_a = get_node_or_null(a);
  64. Node *node_b = get_node_or_null(b);
  65. PhysicsBody *body_a = Object::cast_to<PhysicsBody>(node_a);
  66. PhysicsBody *body_b = Object::cast_to<PhysicsBody>(node_b);
  67. if (node_a && !body_a && node_b && !body_b) {
  68. warning = TTR("Node A and Node B must be PhysicsBodies");
  69. update_configuration_warning();
  70. return;
  71. }
  72. if (node_a && !body_a) {
  73. warning = TTR("Node A must be a PhysicsBody");
  74. update_configuration_warning();
  75. return;
  76. }
  77. if (node_b && !body_b) {
  78. warning = TTR("Node B must be a PhysicsBody");
  79. update_configuration_warning();
  80. return;
  81. }
  82. if (!body_a && !body_b) {
  83. warning = TTR("Joint is not connected to any PhysicsBodies");
  84. update_configuration_warning();
  85. return;
  86. }
  87. if (body_a == body_b) {
  88. warning = TTR("Node A and Node B must be different PhysicsBodies");
  89. update_configuration_warning();
  90. return;
  91. }
  92. warning = String();
  93. update_configuration_warning();
  94. if (body_a) {
  95. joint = _configure_joint(body_a, body_b);
  96. } else if (body_b) {
  97. joint = _configure_joint(body_b, nullptr);
  98. }
  99. ERR_FAIL_COND_MSG(!joint.is_valid(), "Failed to configure the joint.");
  100. PhysicsServer::get_singleton()->joint_set_solver_priority(joint, solver_priority);
  101. if (body_a) {
  102. ba = body_a->get_rid();
  103. body_a->connect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
  104. }
  105. if (body_b) {
  106. bb = body_b->get_rid();
  107. body_b->connect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
  108. }
  109. PhysicsServer::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision);
  110. }
  111. void Joint::set_node_a(const NodePath &p_node_a) {
  112. if (a == p_node_a) {
  113. return;
  114. }
  115. if (joint.is_valid()) {
  116. _disconnect_signals();
  117. }
  118. a = p_node_a;
  119. _update_joint();
  120. }
  121. NodePath Joint::get_node_a() const {
  122. return a;
  123. }
  124. void Joint::set_node_b(const NodePath &p_node_b) {
  125. if (b == p_node_b) {
  126. return;
  127. }
  128. if (joint.is_valid()) {
  129. _disconnect_signals();
  130. }
  131. b = p_node_b;
  132. _update_joint();
  133. }
  134. NodePath Joint::get_node_b() const {
  135. return b;
  136. }
  137. void Joint::set_solver_priority(int p_priority) {
  138. solver_priority = p_priority;
  139. if (joint.is_valid()) {
  140. PhysicsServer::get_singleton()->joint_set_solver_priority(joint, solver_priority);
  141. }
  142. }
  143. int Joint::get_solver_priority() const {
  144. return solver_priority;
  145. }
  146. void Joint::_notification(int p_what) {
  147. switch (p_what) {
  148. case NOTIFICATION_POST_ENTER_TREE: {
  149. if (joint.is_valid()) {
  150. _disconnect_signals();
  151. }
  152. _update_joint();
  153. } break;
  154. case NOTIFICATION_EXIT_TREE: {
  155. if (joint.is_valid()) {
  156. _disconnect_signals();
  157. }
  158. _update_joint(true);
  159. } break;
  160. }
  161. }
  162. void Joint::set_exclude_nodes_from_collision(bool p_enable) {
  163. if (exclude_from_collision == p_enable) {
  164. return;
  165. }
  166. if (joint.is_valid()) {
  167. _disconnect_signals();
  168. }
  169. _update_joint(true);
  170. exclude_from_collision = p_enable;
  171. _update_joint();
  172. }
  173. bool Joint::get_exclude_nodes_from_collision() const {
  174. return exclude_from_collision;
  175. }
  176. String Joint::get_configuration_warning() const {
  177. String node_warning = Node::get_configuration_warning();
  178. if (!warning.empty()) {
  179. if (!node_warning.empty()) {
  180. node_warning += "\n\n";
  181. }
  182. node_warning += warning;
  183. }
  184. return node_warning;
  185. }
  186. void Joint::_bind_methods() {
  187. ClassDB::bind_method(D_METHOD("_body_exit_tree"), &Joint::_body_exit_tree);
  188. ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint::set_node_a);
  189. ClassDB::bind_method(D_METHOD("get_node_a"), &Joint::get_node_a);
  190. ClassDB::bind_method(D_METHOD("set_node_b", "node"), &Joint::set_node_b);
  191. ClassDB::bind_method(D_METHOD("get_node_b"), &Joint::get_node_b);
  192. ClassDB::bind_method(D_METHOD("set_solver_priority", "priority"), &Joint::set_solver_priority);
  193. ClassDB::bind_method(D_METHOD("get_solver_priority"), &Joint::get_solver_priority);
  194. ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint::set_exclude_nodes_from_collision);
  195. ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint::get_exclude_nodes_from_collision);
  196. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "nodes/node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody"), "set_node_a", "get_node_a");
  197. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "nodes/node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody"), "set_node_b", "get_node_b");
  198. ADD_PROPERTY(PropertyInfo(Variant::INT, "solver/priority", PROPERTY_HINT_RANGE, "1,8,1"), "set_solver_priority", "get_solver_priority");
  199. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision/exclude_nodes"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");
  200. }
  201. Joint::Joint() {
  202. exclude_from_collision = true;
  203. solver_priority = 1;
  204. set_notify_transform(true);
  205. }
  206. ///////////////////////////////////
  207. void PinJoint::_bind_methods() {
  208. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &PinJoint::set_param);
  209. ClassDB::bind_method(D_METHOD("get_param", "param"), &PinJoint::get_param);
  210. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/bias", PROPERTY_HINT_RANGE, "0.01,0.99,0.01"), "set_param", "get_param", PARAM_BIAS);
  211. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/damping", PROPERTY_HINT_RANGE, "0.01,8.0,0.01"), "set_param", "get_param", PARAM_DAMPING);
  212. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/impulse_clamp", PROPERTY_HINT_RANGE, "0.0,64.0,0.01"), "set_param", "get_param", PARAM_IMPULSE_CLAMP);
  213. BIND_ENUM_CONSTANT(PARAM_BIAS);
  214. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  215. BIND_ENUM_CONSTANT(PARAM_IMPULSE_CLAMP);
  216. }
  217. void PinJoint::set_param(Param p_param, float p_value) {
  218. ERR_FAIL_INDEX(p_param, 3);
  219. params[p_param] = p_value;
  220. if (get_joint().is_valid()) {
  221. PhysicsServer::get_singleton()->pin_joint_set_param(get_joint(), PhysicsServer::PinJointParam(p_param), p_value);
  222. }
  223. }
  224. float PinJoint::get_param(Param p_param) const {
  225. ERR_FAIL_INDEX_V(p_param, 3, 0);
  226. return params[p_param];
  227. }
  228. RID PinJoint::_configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) {
  229. Vector3 pinpos = get_global_transform().origin;
  230. Vector3 local_a = body_a->get_global_transform().affine_inverse().xform(pinpos);
  231. Vector3 local_b;
  232. if (body_b) {
  233. local_b = body_b->get_global_transform().affine_inverse().xform(pinpos);
  234. } else {
  235. local_b = pinpos;
  236. }
  237. RID j = PhysicsServer::get_singleton()->joint_create_pin(body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b);
  238. for (int i = 0; i < 3; i++) {
  239. PhysicsServer::get_singleton()->pin_joint_set_param(j, PhysicsServer::PinJointParam(i), params[i]);
  240. }
  241. return j;
  242. }
  243. PinJoint::PinJoint() {
  244. params[PARAM_BIAS] = 0.3;
  245. params[PARAM_DAMPING] = 1;
  246. params[PARAM_IMPULSE_CLAMP] = 0;
  247. }
  248. /////////////////////////////////////////////////
  249. ///////////////////////////////////
  250. void HingeJoint::_set_upper_limit(float p_limit) {
  251. set_param(PARAM_LIMIT_UPPER, Math::deg2rad(p_limit));
  252. }
  253. float HingeJoint::_get_upper_limit() const {
  254. return Math::rad2deg(get_param(PARAM_LIMIT_UPPER));
  255. }
  256. void HingeJoint::_set_lower_limit(float p_limit) {
  257. set_param(PARAM_LIMIT_LOWER, Math::deg2rad(p_limit));
  258. }
  259. float HingeJoint::_get_lower_limit() const {
  260. return Math::rad2deg(get_param(PARAM_LIMIT_LOWER));
  261. }
  262. void HingeJoint::_bind_methods() {
  263. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &HingeJoint::set_param);
  264. ClassDB::bind_method(D_METHOD("get_param", "param"), &HingeJoint::get_param);
  265. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enabled"), &HingeJoint::set_flag);
  266. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &HingeJoint::get_flag);
  267. ClassDB::bind_method(D_METHOD("_set_upper_limit", "upper_limit"), &HingeJoint::_set_upper_limit);
  268. ClassDB::bind_method(D_METHOD("_get_upper_limit"), &HingeJoint::_get_upper_limit);
  269. ClassDB::bind_method(D_METHOD("_set_lower_limit", "lower_limit"), &HingeJoint::_set_lower_limit);
  270. ClassDB::bind_method(D_METHOD("_get_lower_limit"), &HingeJoint::_get_lower_limit);
  271. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/bias", PROPERTY_HINT_RANGE, "0.00,0.99,0.01"), "set_param", "get_param", PARAM_BIAS);
  272. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_limit/enable"), "set_flag", "get_flag", FLAG_USE_LIMIT);
  273. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit/upper", PROPERTY_HINT_RANGE, "-180,180,0.1"), "_set_upper_limit", "_get_upper_limit");
  274. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit/lower", PROPERTY_HINT_RANGE, "-180,180,0.1"), "_set_lower_limit", "_get_lower_limit");
  275. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit/bias", PROPERTY_HINT_RANGE, "0.01,0.99,0.01"), "set_param", "get_param", PARAM_LIMIT_BIAS);
  276. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit/softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param", "get_param", PARAM_LIMIT_SOFTNESS);
  277. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit/relaxation", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param", "get_param", PARAM_LIMIT_RELAXATION);
  278. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "motor/enable"), "set_flag", "get_flag", FLAG_ENABLE_MOTOR);
  279. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "motor/target_velocity", PROPERTY_HINT_RANGE, "-200,200,0.01,or_greater,or_lesser"), "set_param", "get_param", PARAM_MOTOR_TARGET_VELOCITY);
  280. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "motor/max_impulse", PROPERTY_HINT_RANGE, "0.01,1024,0.01"), "set_param", "get_param", PARAM_MOTOR_MAX_IMPULSE);
  281. BIND_ENUM_CONSTANT(PARAM_BIAS);
  282. BIND_ENUM_CONSTANT(PARAM_LIMIT_UPPER);
  283. BIND_ENUM_CONSTANT(PARAM_LIMIT_LOWER);
  284. BIND_ENUM_CONSTANT(PARAM_LIMIT_BIAS);
  285. BIND_ENUM_CONSTANT(PARAM_LIMIT_SOFTNESS);
  286. BIND_ENUM_CONSTANT(PARAM_LIMIT_RELAXATION);
  287. BIND_ENUM_CONSTANT(PARAM_MOTOR_TARGET_VELOCITY);
  288. BIND_ENUM_CONSTANT(PARAM_MOTOR_MAX_IMPULSE);
  289. BIND_ENUM_CONSTANT(PARAM_MAX);
  290. BIND_ENUM_CONSTANT(FLAG_USE_LIMIT);
  291. BIND_ENUM_CONSTANT(FLAG_ENABLE_MOTOR);
  292. BIND_ENUM_CONSTANT(FLAG_MAX);
  293. }
  294. void HingeJoint::set_param(Param p_param, float p_value) {
  295. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  296. params[p_param] = p_value;
  297. if (get_joint().is_valid()) {
  298. PhysicsServer::get_singleton()->hinge_joint_set_param(get_joint(), PhysicsServer::HingeJointParam(p_param), p_value);
  299. }
  300. update_gizmo();
  301. }
  302. float HingeJoint::get_param(Param p_param) const {
  303. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  304. return params[p_param];
  305. }
  306. void HingeJoint::set_flag(Flag p_flag, bool p_value) {
  307. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  308. flags[p_flag] = p_value;
  309. if (get_joint().is_valid()) {
  310. PhysicsServer::get_singleton()->hinge_joint_set_flag(get_joint(), PhysicsServer::HingeJointFlag(p_flag), p_value);
  311. }
  312. update_gizmo();
  313. }
  314. bool HingeJoint::get_flag(Flag p_flag) const {
  315. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  316. return flags[p_flag];
  317. }
  318. RID HingeJoint::_configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) {
  319. Transform gt = get_global_transform();
  320. Transform ainv = body_a->get_global_transform().affine_inverse();
  321. Transform local_a = ainv * gt;
  322. local_a.orthonormalize();
  323. Transform local_b = gt;
  324. if (body_b) {
  325. Transform binv = body_b->get_global_transform().affine_inverse();
  326. local_b = binv * gt;
  327. }
  328. local_b.orthonormalize();
  329. RID j = RID_PRIME(PhysicsServer::get_singleton()->joint_create_hinge(body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b));
  330. for (int i = 0; i < PARAM_MAX; i++) {
  331. PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HingeJointParam(i), params[i]);
  332. }
  333. for (int i = 0; i < FLAG_MAX; i++) {
  334. set_flag(Flag(i), flags[i]);
  335. PhysicsServer::get_singleton()->hinge_joint_set_flag(j, PhysicsServer::HingeJointFlag(i), flags[i]);
  336. }
  337. return j;
  338. }
  339. HingeJoint::HingeJoint() {
  340. params[PARAM_BIAS] = 0.3;
  341. params[PARAM_LIMIT_UPPER] = Math_PI * 0.5;
  342. params[PARAM_LIMIT_LOWER] = -Math_PI * 0.5;
  343. params[PARAM_LIMIT_BIAS] = 0.3;
  344. params[PARAM_LIMIT_SOFTNESS] = 0.9;
  345. params[PARAM_LIMIT_RELAXATION] = 1.0;
  346. params[PARAM_MOTOR_TARGET_VELOCITY] = 1;
  347. params[PARAM_MOTOR_MAX_IMPULSE] = 1;
  348. flags[FLAG_USE_LIMIT] = false;
  349. flags[FLAG_ENABLE_MOTOR] = false;
  350. }
  351. /////////////////////////////////////////////////
  352. //////////////////////////////////
  353. void SliderJoint::_set_upper_limit_angular(float p_limit_angular) {
  354. set_param(PARAM_ANGULAR_LIMIT_UPPER, Math::deg2rad(p_limit_angular));
  355. }
  356. float SliderJoint::_get_upper_limit_angular() const {
  357. return Math::rad2deg(get_param(PARAM_ANGULAR_LIMIT_UPPER));
  358. }
  359. void SliderJoint::_set_lower_limit_angular(float p_limit_angular) {
  360. set_param(PARAM_ANGULAR_LIMIT_LOWER, Math::deg2rad(p_limit_angular));
  361. }
  362. float SliderJoint::_get_lower_limit_angular() const {
  363. return Math::rad2deg(get_param(PARAM_ANGULAR_LIMIT_LOWER));
  364. }
  365. void SliderJoint::_bind_methods() {
  366. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &SliderJoint::set_param);
  367. ClassDB::bind_method(D_METHOD("get_param", "param"), &SliderJoint::get_param);
  368. ClassDB::bind_method(D_METHOD("_set_upper_limit_angular", "upper_limit_angular"), &SliderJoint::_set_upper_limit_angular);
  369. ClassDB::bind_method(D_METHOD("_get_upper_limit_angular"), &SliderJoint::_get_upper_limit_angular);
  370. ClassDB::bind_method(D_METHOD("_set_lower_limit_angular", "lower_limit_angular"), &SliderJoint::_set_lower_limit_angular);
  371. ClassDB::bind_method(D_METHOD("_get_lower_limit_angular"), &SliderJoint::_get_lower_limit_angular);
  372. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit/upper_distance", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_param", "get_param", PARAM_LINEAR_LIMIT_UPPER);
  373. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit/lower_distance", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_param", "get_param", PARAM_LINEAR_LIMIT_LOWER);
  374. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit/softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_LIMIT_SOFTNESS);
  375. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit/restitution", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_LIMIT_RESTITUTION);
  376. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_LIMIT_DAMPING);
  377. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_motion/softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_MOTION_SOFTNESS);
  378. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_motion/restitution", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_MOTION_RESTITUTION);
  379. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_motion/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_MOTION_DAMPING);
  380. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_ortho/softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_ORTHOGONAL_SOFTNESS);
  381. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_ortho/restitution", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_ORTHOGONAL_RESTITUTION);
  382. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_ortho/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_ORTHOGONAL_DAMPING);
  383. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit/upper_angle", PROPERTY_HINT_RANGE, "-180,180,0.1"), "_set_upper_limit_angular", "_get_upper_limit_angular");
  384. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit/lower_angle", PROPERTY_HINT_RANGE, "-180,180,0.1"), "_set_lower_limit_angular", "_get_lower_limit_angular");
  385. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit/softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_LIMIT_SOFTNESS);
  386. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit/restitution", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_LIMIT_RESTITUTION);
  387. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_LIMIT_DAMPING);
  388. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motion/softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_MOTION_SOFTNESS);
  389. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motion/restitution", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_MOTION_RESTITUTION);
  390. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motion/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_MOTION_DAMPING);
  391. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_ortho/softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_ORTHOGONAL_SOFTNESS);
  392. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_ortho/restitution", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_ORTHOGONAL_RESTITUTION);
  393. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_ortho/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_ORTHOGONAL_DAMPING);
  394. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_UPPER);
  395. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_LOWER);
  396. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_SOFTNESS);
  397. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_RESTITUTION);
  398. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_DAMPING);
  399. BIND_ENUM_CONSTANT(PARAM_LINEAR_MOTION_SOFTNESS);
  400. BIND_ENUM_CONSTANT(PARAM_LINEAR_MOTION_RESTITUTION);
  401. BIND_ENUM_CONSTANT(PARAM_LINEAR_MOTION_DAMPING);
  402. BIND_ENUM_CONSTANT(PARAM_LINEAR_ORTHOGONAL_SOFTNESS);
  403. BIND_ENUM_CONSTANT(PARAM_LINEAR_ORTHOGONAL_RESTITUTION);
  404. BIND_ENUM_CONSTANT(PARAM_LINEAR_ORTHOGONAL_DAMPING);
  405. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_UPPER);
  406. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_LOWER);
  407. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_SOFTNESS);
  408. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_RESTITUTION);
  409. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_DAMPING);
  410. BIND_ENUM_CONSTANT(PARAM_ANGULAR_MOTION_SOFTNESS);
  411. BIND_ENUM_CONSTANT(PARAM_ANGULAR_MOTION_RESTITUTION);
  412. BIND_ENUM_CONSTANT(PARAM_ANGULAR_MOTION_DAMPING);
  413. BIND_ENUM_CONSTANT(PARAM_ANGULAR_ORTHOGONAL_SOFTNESS);
  414. BIND_ENUM_CONSTANT(PARAM_ANGULAR_ORTHOGONAL_RESTITUTION);
  415. BIND_ENUM_CONSTANT(PARAM_ANGULAR_ORTHOGONAL_DAMPING);
  416. BIND_ENUM_CONSTANT(PARAM_MAX);
  417. }
  418. void SliderJoint::set_param(Param p_param, float p_value) {
  419. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  420. params[p_param] = p_value;
  421. if (get_joint().is_valid()) {
  422. PhysicsServer::get_singleton()->slider_joint_set_param(get_joint(), PhysicsServer::SliderJointParam(p_param), p_value);
  423. }
  424. update_gizmo();
  425. }
  426. float SliderJoint::get_param(Param p_param) const {
  427. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  428. return params[p_param];
  429. }
  430. RID SliderJoint::_configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) {
  431. Transform gt = get_global_transform();
  432. Transform ainv = body_a->get_global_transform().affine_inverse();
  433. Transform local_a = ainv * gt;
  434. local_a.orthonormalize();
  435. Transform local_b = gt;
  436. if (body_b) {
  437. Transform binv = body_b->get_global_transform().affine_inverse();
  438. local_b = binv * gt;
  439. }
  440. local_b.orthonormalize();
  441. RID j = PhysicsServer::get_singleton()->joint_create_slider(body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b);
  442. for (int i = 0; i < PARAM_MAX; i++) {
  443. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SliderJointParam(i), params[i]);
  444. }
  445. return j;
  446. }
  447. SliderJoint::SliderJoint() {
  448. params[PARAM_LINEAR_LIMIT_UPPER] = 1.0;
  449. params[PARAM_LINEAR_LIMIT_LOWER] = -1.0;
  450. params[PARAM_LINEAR_LIMIT_SOFTNESS] = 1.0;
  451. params[PARAM_LINEAR_LIMIT_RESTITUTION] = 0.7;
  452. params[PARAM_LINEAR_LIMIT_DAMPING] = 1.0;
  453. params[PARAM_LINEAR_MOTION_SOFTNESS] = 1.0;
  454. params[PARAM_LINEAR_MOTION_RESTITUTION] = 0.7;
  455. params[PARAM_LINEAR_MOTION_DAMPING] = 0; //1.0;
  456. params[PARAM_LINEAR_ORTHOGONAL_SOFTNESS] = 1.0;
  457. params[PARAM_LINEAR_ORTHOGONAL_RESTITUTION] = 0.7;
  458. params[PARAM_LINEAR_ORTHOGONAL_DAMPING] = 1.0;
  459. params[PARAM_ANGULAR_LIMIT_UPPER] = 0;
  460. params[PARAM_ANGULAR_LIMIT_LOWER] = 0;
  461. params[PARAM_ANGULAR_LIMIT_SOFTNESS] = 1.0;
  462. params[PARAM_ANGULAR_LIMIT_RESTITUTION] = 0.7;
  463. params[PARAM_ANGULAR_LIMIT_DAMPING] = 0; //1.0;
  464. params[PARAM_ANGULAR_MOTION_SOFTNESS] = 1.0;
  465. params[PARAM_ANGULAR_MOTION_RESTITUTION] = 0.7;
  466. params[PARAM_ANGULAR_MOTION_DAMPING] = 1.0;
  467. params[PARAM_ANGULAR_ORTHOGONAL_SOFTNESS] = 1.0;
  468. params[PARAM_ANGULAR_ORTHOGONAL_RESTITUTION] = 0.7;
  469. params[PARAM_ANGULAR_ORTHOGONAL_DAMPING] = 1.0;
  470. }
  471. //////////////////////////////////
  472. void ConeTwistJoint::_set_swing_span(float p_limit_angular) {
  473. set_param(PARAM_SWING_SPAN, Math::deg2rad(p_limit_angular));
  474. }
  475. float ConeTwistJoint::_get_swing_span() const {
  476. return Math::rad2deg(get_param(PARAM_SWING_SPAN));
  477. }
  478. void ConeTwistJoint::_set_twist_span(float p_limit_angular) {
  479. set_param(PARAM_TWIST_SPAN, Math::deg2rad(p_limit_angular));
  480. }
  481. float ConeTwistJoint::_get_twist_span() const {
  482. return Math::rad2deg(get_param(PARAM_TWIST_SPAN));
  483. }
  484. void ConeTwistJoint::_bind_methods() {
  485. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &ConeTwistJoint::set_param);
  486. ClassDB::bind_method(D_METHOD("get_param", "param"), &ConeTwistJoint::get_param);
  487. ClassDB::bind_method(D_METHOD("_set_swing_span", "swing_span"), &ConeTwistJoint::_set_swing_span);
  488. ClassDB::bind_method(D_METHOD("_get_swing_span"), &ConeTwistJoint::_get_swing_span);
  489. ClassDB::bind_method(D_METHOD("_set_twist_span", "twist_span"), &ConeTwistJoint::_set_twist_span);
  490. ClassDB::bind_method(D_METHOD("_get_twist_span"), &ConeTwistJoint::_get_twist_span);
  491. ADD_PROPERTY(PropertyInfo(Variant::REAL, "swing_span", PROPERTY_HINT_RANGE, "-180,180,0.1"), "_set_swing_span", "_get_swing_span");
  492. ADD_PROPERTY(PropertyInfo(Variant::REAL, "twist_span", PROPERTY_HINT_RANGE, "-40000,40000,0.1"), "_set_twist_span", "_get_twist_span");
  493. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "bias", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_BIAS);
  494. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_SOFTNESS);
  495. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "relaxation", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_RELAXATION);
  496. BIND_ENUM_CONSTANT(PARAM_SWING_SPAN);
  497. BIND_ENUM_CONSTANT(PARAM_TWIST_SPAN);
  498. BIND_ENUM_CONSTANT(PARAM_BIAS);
  499. BIND_ENUM_CONSTANT(PARAM_SOFTNESS);
  500. BIND_ENUM_CONSTANT(PARAM_RELAXATION);
  501. BIND_ENUM_CONSTANT(PARAM_MAX);
  502. }
  503. void ConeTwistJoint::set_param(Param p_param, float p_value) {
  504. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  505. params[p_param] = p_value;
  506. if (get_joint().is_valid()) {
  507. PhysicsServer::get_singleton()->cone_twist_joint_set_param(get_joint(), PhysicsServer::ConeTwistJointParam(p_param), p_value);
  508. }
  509. update_gizmo();
  510. }
  511. float ConeTwistJoint::get_param(Param p_param) const {
  512. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  513. return params[p_param];
  514. }
  515. RID ConeTwistJoint::_configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) {
  516. Transform gt = get_global_transform();
  517. //Vector3 cone_twistpos = gt.origin;
  518. //Vector3 cone_twistdir = gt.basis.get_axis(2);
  519. Transform ainv = body_a->get_global_transform().affine_inverse();
  520. Transform local_a = ainv * gt;
  521. local_a.orthonormalize();
  522. Transform local_b = gt;
  523. if (body_b) {
  524. Transform binv = body_b->get_global_transform().affine_inverse();
  525. local_b = binv * gt;
  526. }
  527. local_b.orthonormalize();
  528. RID j = PhysicsServer::get_singleton()->joint_create_cone_twist(body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b);
  529. for (int i = 0; i < PARAM_MAX; i++) {
  530. PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::ConeTwistJointParam(i), params[i]);
  531. }
  532. return j;
  533. }
  534. ConeTwistJoint::ConeTwistJoint() {
  535. params[PARAM_SWING_SPAN] = Math_PI * 0.25;
  536. params[PARAM_TWIST_SPAN] = Math_PI;
  537. params[PARAM_BIAS] = 0.3;
  538. params[PARAM_SOFTNESS] = 0.8;
  539. params[PARAM_RELAXATION] = 1.0;
  540. }
  541. /////////////////////////////////////////////////////////////////////
  542. void Generic6DOFJoint::_set_angular_hi_limit_x(float p_limit_angular) {
  543. set_param_x(PARAM_ANGULAR_UPPER_LIMIT, Math::deg2rad(p_limit_angular));
  544. }
  545. float Generic6DOFJoint::_get_angular_hi_limit_x() const {
  546. return Math::rad2deg(get_param_x(PARAM_ANGULAR_UPPER_LIMIT));
  547. }
  548. void Generic6DOFJoint::_set_angular_lo_limit_x(float p_limit_angular) {
  549. set_param_x(PARAM_ANGULAR_LOWER_LIMIT, Math::deg2rad(p_limit_angular));
  550. }
  551. float Generic6DOFJoint::_get_angular_lo_limit_x() const {
  552. return Math::rad2deg(get_param_x(PARAM_ANGULAR_LOWER_LIMIT));
  553. }
  554. void Generic6DOFJoint::_set_angular_hi_limit_y(float p_limit_angular) {
  555. set_param_y(PARAM_ANGULAR_UPPER_LIMIT, Math::deg2rad(p_limit_angular));
  556. }
  557. float Generic6DOFJoint::_get_angular_hi_limit_y() const {
  558. return Math::rad2deg(get_param_y(PARAM_ANGULAR_UPPER_LIMIT));
  559. }
  560. void Generic6DOFJoint::_set_angular_lo_limit_y(float p_limit_angular) {
  561. set_param_y(PARAM_ANGULAR_LOWER_LIMIT, Math::deg2rad(p_limit_angular));
  562. }
  563. float Generic6DOFJoint::_get_angular_lo_limit_y() const {
  564. return Math::rad2deg(get_param_y(PARAM_ANGULAR_LOWER_LIMIT));
  565. }
  566. void Generic6DOFJoint::_set_angular_hi_limit_z(float p_limit_angular) {
  567. set_param_z(PARAM_ANGULAR_UPPER_LIMIT, Math::deg2rad(p_limit_angular));
  568. }
  569. float Generic6DOFJoint::_get_angular_hi_limit_z() const {
  570. return Math::rad2deg(get_param_z(PARAM_ANGULAR_UPPER_LIMIT));
  571. }
  572. void Generic6DOFJoint::_set_angular_lo_limit_z(float p_limit_angular) {
  573. set_param_z(PARAM_ANGULAR_LOWER_LIMIT, Math::deg2rad(p_limit_angular));
  574. }
  575. float Generic6DOFJoint::_get_angular_lo_limit_z() const {
  576. return Math::rad2deg(get_param_z(PARAM_ANGULAR_LOWER_LIMIT));
  577. }
  578. void Generic6DOFJoint::_bind_methods() {
  579. ClassDB::bind_method(D_METHOD("_set_angular_hi_limit_x", "angle"), &Generic6DOFJoint::_set_angular_hi_limit_x);
  580. ClassDB::bind_method(D_METHOD("_get_angular_hi_limit_x"), &Generic6DOFJoint::_get_angular_hi_limit_x);
  581. ClassDB::bind_method(D_METHOD("_set_angular_lo_limit_x", "angle"), &Generic6DOFJoint::_set_angular_lo_limit_x);
  582. ClassDB::bind_method(D_METHOD("_get_angular_lo_limit_x"), &Generic6DOFJoint::_get_angular_lo_limit_x);
  583. ClassDB::bind_method(D_METHOD("_set_angular_hi_limit_y", "angle"), &Generic6DOFJoint::_set_angular_hi_limit_y);
  584. ClassDB::bind_method(D_METHOD("_get_angular_hi_limit_y"), &Generic6DOFJoint::_get_angular_hi_limit_y);
  585. ClassDB::bind_method(D_METHOD("_set_angular_lo_limit_y", "angle"), &Generic6DOFJoint::_set_angular_lo_limit_y);
  586. ClassDB::bind_method(D_METHOD("_get_angular_lo_limit_y"), &Generic6DOFJoint::_get_angular_lo_limit_y);
  587. ClassDB::bind_method(D_METHOD("_set_angular_hi_limit_z", "angle"), &Generic6DOFJoint::_set_angular_hi_limit_z);
  588. ClassDB::bind_method(D_METHOD("_get_angular_hi_limit_z"), &Generic6DOFJoint::_get_angular_hi_limit_z);
  589. ClassDB::bind_method(D_METHOD("_set_angular_lo_limit_z", "angle"), &Generic6DOFJoint::_set_angular_lo_limit_z);
  590. ClassDB::bind_method(D_METHOD("_get_angular_lo_limit_z"), &Generic6DOFJoint::_get_angular_lo_limit_z);
  591. ClassDB::bind_method(D_METHOD("set_param_x", "param", "value"), &Generic6DOFJoint::set_param_x);
  592. ClassDB::bind_method(D_METHOD("get_param_x", "param"), &Generic6DOFJoint::get_param_x);
  593. ClassDB::bind_method(D_METHOD("set_param_y", "param", "value"), &Generic6DOFJoint::set_param_y);
  594. ClassDB::bind_method(D_METHOD("get_param_y", "param"), &Generic6DOFJoint::get_param_y);
  595. ClassDB::bind_method(D_METHOD("set_param_z", "param", "value"), &Generic6DOFJoint::set_param_z);
  596. ClassDB::bind_method(D_METHOD("get_param_z", "param"), &Generic6DOFJoint::get_param_z);
  597. ClassDB::bind_method(D_METHOD("set_flag_x", "flag", "value"), &Generic6DOFJoint::set_flag_x);
  598. ClassDB::bind_method(D_METHOD("get_flag_x", "flag"), &Generic6DOFJoint::get_flag_x);
  599. ClassDB::bind_method(D_METHOD("set_flag_y", "flag", "value"), &Generic6DOFJoint::set_flag_y);
  600. ClassDB::bind_method(D_METHOD("get_flag_y", "flag"), &Generic6DOFJoint::get_flag_y);
  601. ClassDB::bind_method(D_METHOD("set_flag_z", "flag", "value"), &Generic6DOFJoint::set_flag_z);
  602. ClassDB::bind_method(D_METHOD("get_flag_z", "flag"), &Generic6DOFJoint::get_flag_z);
  603. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_limit_x/enabled"), "set_flag_x", "get_flag_x", FLAG_ENABLE_LINEAR_LIMIT);
  604. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_x/upper_distance"), "set_param_x", "get_param_x", PARAM_LINEAR_UPPER_LIMIT);
  605. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_x/lower_distance"), "set_param_x", "get_param_x", PARAM_LINEAR_LOWER_LIMIT);
  606. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_x/softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_x", "get_param_x", PARAM_LINEAR_LIMIT_SOFTNESS);
  607. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_x/restitution", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_x", "get_param_x", PARAM_LINEAR_RESTITUTION);
  608. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_x/damping", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_x", "get_param_x", PARAM_LINEAR_DAMPING);
  609. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_motor_x/enabled"), "set_flag_x", "get_flag_x", FLAG_ENABLE_LINEAR_MOTOR);
  610. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_motor_x/target_velocity"), "set_param_x", "get_param_x", PARAM_LINEAR_MOTOR_TARGET_VELOCITY);
  611. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_motor_x/force_limit"), "set_param_x", "get_param_x", PARAM_LINEAR_MOTOR_FORCE_LIMIT);
  612. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_spring_x/enabled"), "set_flag_x", "get_flag_x", FLAG_ENABLE_LINEAR_SPRING);
  613. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_spring_x/stiffness"), "set_param_x", "get_param_x", PARAM_LINEAR_SPRING_STIFFNESS);
  614. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_spring_x/damping"), "set_param_x", "get_param_x", PARAM_LINEAR_SPRING_DAMPING);
  615. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_spring_x/equilibrium_point"), "set_param_x", "get_param_x", PARAM_LINEAR_SPRING_EQUILIBRIUM_POINT);
  616. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_limit_x/enabled"), "set_flag_x", "get_flag_x", FLAG_ENABLE_ANGULAR_LIMIT);
  617. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit_x/upper_angle", PROPERTY_HINT_RANGE, "-180,180,0.01"), "_set_angular_hi_limit_x", "_get_angular_hi_limit_x");
  618. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit_x/lower_angle", PROPERTY_HINT_RANGE, "-180,180,0.01"), "_set_angular_lo_limit_x", "_get_angular_lo_limit_x");
  619. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_x/softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_x", "get_param_x", PARAM_ANGULAR_LIMIT_SOFTNESS);
  620. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_x/restitution", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_x", "get_param_x", PARAM_ANGULAR_RESTITUTION);
  621. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_x/damping", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_x", "get_param_x", PARAM_ANGULAR_DAMPING);
  622. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_x/force_limit"), "set_param_x", "get_param_x", PARAM_ANGULAR_FORCE_LIMIT);
  623. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_x/erp"), "set_param_x", "get_param_x", PARAM_ANGULAR_ERP);
  624. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_motor_x/enabled"), "set_flag_x", "get_flag_x", FLAG_ENABLE_MOTOR);
  625. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_x/target_velocity"), "set_param_x", "get_param_x", PARAM_ANGULAR_MOTOR_TARGET_VELOCITY);
  626. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_x/force_limit"), "set_param_x", "get_param_x", PARAM_ANGULAR_MOTOR_FORCE_LIMIT);
  627. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_spring_x/enabled"), "set_flag_x", "get_flag_x", FLAG_ENABLE_ANGULAR_SPRING);
  628. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_spring_x/stiffness"), "set_param_x", "get_param_x", PARAM_ANGULAR_SPRING_STIFFNESS);
  629. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_spring_x/damping"), "set_param_x", "get_param_x", PARAM_ANGULAR_SPRING_DAMPING);
  630. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_spring_x/equilibrium_point"), "set_param_x", "get_param_x", PARAM_ANGULAR_SPRING_EQUILIBRIUM_POINT);
  631. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_limit_y/enabled"), "set_flag_y", "get_flag_y", FLAG_ENABLE_LINEAR_LIMIT);
  632. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_y/upper_distance"), "set_param_y", "get_param_y", PARAM_LINEAR_UPPER_LIMIT);
  633. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_y/lower_distance"), "set_param_y", "get_param_y", PARAM_LINEAR_LOWER_LIMIT);
  634. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_y/softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_y", "get_param_y", PARAM_LINEAR_LIMIT_SOFTNESS);
  635. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_y/restitution", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_y", "get_param_y", PARAM_LINEAR_RESTITUTION);
  636. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_y/damping", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_y", "get_param_y", PARAM_LINEAR_DAMPING);
  637. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_motor_y/enabled"), "set_flag_y", "get_flag_y", FLAG_ENABLE_LINEAR_MOTOR);
  638. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_motor_y/target_velocity"), "set_param_y", "get_param_y", PARAM_LINEAR_MOTOR_TARGET_VELOCITY);
  639. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_motor_y/force_limit"), "set_param_y", "get_param_y", PARAM_LINEAR_MOTOR_FORCE_LIMIT);
  640. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_spring_y/enabled"), "set_flag_y", "get_flag_y", FLAG_ENABLE_LINEAR_SPRING);
  641. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_spring_y/stiffness"), "set_param_y", "get_param_y", PARAM_LINEAR_SPRING_STIFFNESS);
  642. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_spring_y/damping"), "set_param_y", "get_param_y", PARAM_LINEAR_SPRING_DAMPING);
  643. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_spring_y/equilibrium_point"), "set_param_y", "get_param_y", PARAM_LINEAR_SPRING_EQUILIBRIUM_POINT);
  644. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_limit_y/enabled"), "set_flag_y", "get_flag_y", FLAG_ENABLE_ANGULAR_LIMIT);
  645. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit_y/upper_angle", PROPERTY_HINT_RANGE, "-180,180,0.01"), "_set_angular_hi_limit_y", "_get_angular_hi_limit_y");
  646. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit_y/lower_angle", PROPERTY_HINT_RANGE, "-180,180,0.01"), "_set_angular_lo_limit_y", "_get_angular_lo_limit_y");
  647. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_y/softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_y", "get_param_y", PARAM_ANGULAR_LIMIT_SOFTNESS);
  648. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_y/restitution", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_y", "get_param_y", PARAM_ANGULAR_RESTITUTION);
  649. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_y/damping", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_y", "get_param_y", PARAM_ANGULAR_DAMPING);
  650. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_y/force_limit"), "set_param_y", "get_param_y", PARAM_ANGULAR_FORCE_LIMIT);
  651. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_y/erp"), "set_param_y", "get_param_y", PARAM_ANGULAR_ERP);
  652. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_motor_y/enabled"), "set_flag_y", "get_flag_y", FLAG_ENABLE_MOTOR);
  653. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_y/target_velocity"), "set_param_y", "get_param_y", PARAM_ANGULAR_MOTOR_TARGET_VELOCITY);
  654. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_y/force_limit"), "set_param_y", "get_param_y", PARAM_ANGULAR_MOTOR_FORCE_LIMIT);
  655. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_spring_y/enabled"), "set_flag_y", "get_flag_y", FLAG_ENABLE_ANGULAR_SPRING);
  656. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_spring_y/stiffness"), "set_param_y", "get_param_y", PARAM_ANGULAR_SPRING_STIFFNESS);
  657. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_spring_y/damping"), "set_param_y", "get_param_y", PARAM_ANGULAR_SPRING_DAMPING);
  658. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_spring_y/equilibrium_point"), "set_param_y", "get_param_y", PARAM_ANGULAR_SPRING_EQUILIBRIUM_POINT);
  659. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_limit_z/enabled"), "set_flag_z", "get_flag_z", FLAG_ENABLE_LINEAR_LIMIT);
  660. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_z/upper_distance"), "set_param_z", "get_param_z", PARAM_LINEAR_UPPER_LIMIT);
  661. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_z/lower_distance"), "set_param_z", "get_param_z", PARAM_LINEAR_LOWER_LIMIT);
  662. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_z/softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_z", "get_param_z", PARAM_LINEAR_LIMIT_SOFTNESS);
  663. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_z/restitution", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_z", "get_param_z", PARAM_LINEAR_RESTITUTION);
  664. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_z/damping", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_z", "get_param_z", PARAM_LINEAR_DAMPING);
  665. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_motor_z/enabled"), "set_flag_z", "get_flag_z", FLAG_ENABLE_LINEAR_MOTOR);
  666. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_motor_z/target_velocity"), "set_param_z", "get_param_z", PARAM_LINEAR_MOTOR_TARGET_VELOCITY);
  667. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_motor_z/force_limit"), "set_param_z", "get_param_z", PARAM_LINEAR_MOTOR_FORCE_LIMIT);
  668. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_spring_z/enabled"), "set_flag_z", "get_flag_z", FLAG_ENABLE_LINEAR_SPRING);
  669. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_spring_z/stiffness"), "set_param_z", "get_param_z", PARAM_LINEAR_SPRING_STIFFNESS);
  670. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_spring_z/damping"), "set_param_z", "get_param_z", PARAM_LINEAR_SPRING_DAMPING);
  671. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_spring_z/equilibrium_point"), "set_param_z", "get_param_z", PARAM_LINEAR_SPRING_EQUILIBRIUM_POINT);
  672. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_limit_z/enabled"), "set_flag_z", "get_flag_z", FLAG_ENABLE_ANGULAR_LIMIT);
  673. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit_z/upper_angle", PROPERTY_HINT_RANGE, "-180,180,0.01"), "_set_angular_hi_limit_z", "_get_angular_hi_limit_z");
  674. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit_z/lower_angle", PROPERTY_HINT_RANGE, "-180,180,0.01"), "_set_angular_lo_limit_z", "_get_angular_lo_limit_z");
  675. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_z/softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_z", "get_param_z", PARAM_ANGULAR_LIMIT_SOFTNESS);
  676. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_z/restitution", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_z", "get_param_z", PARAM_ANGULAR_RESTITUTION);
  677. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_z/damping", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param_z", "get_param_z", PARAM_ANGULAR_DAMPING);
  678. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_z/force_limit"), "set_param_z", "get_param_z", PARAM_ANGULAR_FORCE_LIMIT);
  679. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_z/erp"), "set_param_z", "get_param_z", PARAM_ANGULAR_ERP);
  680. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_motor_z/enabled"), "set_flag_z", "get_flag_z", FLAG_ENABLE_MOTOR);
  681. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_z/target_velocity"), "set_param_z", "get_param_z", PARAM_ANGULAR_MOTOR_TARGET_VELOCITY);
  682. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_z/force_limit"), "set_param_z", "get_param_z", PARAM_ANGULAR_MOTOR_FORCE_LIMIT);
  683. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_spring_z/enabled"), "set_flag_z", "get_flag_z", FLAG_ENABLE_ANGULAR_SPRING);
  684. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_spring_z/stiffness"), "set_param_z", "get_param_z", PARAM_ANGULAR_SPRING_STIFFNESS);
  685. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_spring_z/damping"), "set_param_z", "get_param_z", PARAM_ANGULAR_SPRING_DAMPING);
  686. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_spring_z/equilibrium_point"), "set_param_z", "get_param_z", PARAM_ANGULAR_SPRING_EQUILIBRIUM_POINT);
  687. BIND_ENUM_CONSTANT(PARAM_LINEAR_LOWER_LIMIT);
  688. BIND_ENUM_CONSTANT(PARAM_LINEAR_UPPER_LIMIT);
  689. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_SOFTNESS);
  690. BIND_ENUM_CONSTANT(PARAM_LINEAR_RESTITUTION);
  691. BIND_ENUM_CONSTANT(PARAM_LINEAR_DAMPING);
  692. BIND_ENUM_CONSTANT(PARAM_LINEAR_MOTOR_TARGET_VELOCITY);
  693. BIND_ENUM_CONSTANT(PARAM_LINEAR_MOTOR_FORCE_LIMIT);
  694. BIND_ENUM_CONSTANT(PARAM_LINEAR_SPRING_STIFFNESS);
  695. BIND_ENUM_CONSTANT(PARAM_LINEAR_SPRING_DAMPING);
  696. BIND_ENUM_CONSTANT(PARAM_LINEAR_SPRING_EQUILIBRIUM_POINT);
  697. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LOWER_LIMIT);
  698. BIND_ENUM_CONSTANT(PARAM_ANGULAR_UPPER_LIMIT);
  699. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_SOFTNESS);
  700. BIND_ENUM_CONSTANT(PARAM_ANGULAR_DAMPING);
  701. BIND_ENUM_CONSTANT(PARAM_ANGULAR_RESTITUTION);
  702. BIND_ENUM_CONSTANT(PARAM_ANGULAR_FORCE_LIMIT);
  703. BIND_ENUM_CONSTANT(PARAM_ANGULAR_ERP);
  704. BIND_ENUM_CONSTANT(PARAM_ANGULAR_MOTOR_TARGET_VELOCITY);
  705. BIND_ENUM_CONSTANT(PARAM_ANGULAR_MOTOR_FORCE_LIMIT);
  706. BIND_ENUM_CONSTANT(PARAM_ANGULAR_SPRING_STIFFNESS);
  707. BIND_ENUM_CONSTANT(PARAM_ANGULAR_SPRING_DAMPING);
  708. BIND_ENUM_CONSTANT(PARAM_ANGULAR_SPRING_EQUILIBRIUM_POINT);
  709. BIND_ENUM_CONSTANT(PARAM_MAX);
  710. BIND_ENUM_CONSTANT(FLAG_ENABLE_LINEAR_LIMIT);
  711. BIND_ENUM_CONSTANT(FLAG_ENABLE_ANGULAR_LIMIT);
  712. BIND_ENUM_CONSTANT(FLAG_ENABLE_LINEAR_SPRING);
  713. BIND_ENUM_CONSTANT(FLAG_ENABLE_ANGULAR_SPRING);
  714. BIND_ENUM_CONSTANT(FLAG_ENABLE_MOTOR);
  715. BIND_ENUM_CONSTANT(FLAG_ENABLE_LINEAR_MOTOR);
  716. BIND_ENUM_CONSTANT(FLAG_MAX);
  717. }
  718. void Generic6DOFJoint::set_param_x(Param p_param, float p_value) {
  719. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  720. params_x[p_param] = p_value;
  721. if (get_joint().is_valid()) {
  722. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(get_joint(), Vector3::AXIS_X, PhysicsServer::G6DOFJointAxisParam(p_param), p_value);
  723. }
  724. update_gizmo();
  725. }
  726. float Generic6DOFJoint::get_param_x(Param p_param) const {
  727. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  728. return params_x[p_param];
  729. }
  730. void Generic6DOFJoint::set_param_y(Param p_param, float p_value) {
  731. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  732. params_y[p_param] = p_value;
  733. if (get_joint().is_valid()) {
  734. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(get_joint(), Vector3::AXIS_Y, PhysicsServer::G6DOFJointAxisParam(p_param), p_value);
  735. }
  736. update_gizmo();
  737. }
  738. float Generic6DOFJoint::get_param_y(Param p_param) const {
  739. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  740. return params_y[p_param];
  741. }
  742. void Generic6DOFJoint::set_param_z(Param p_param, float p_value) {
  743. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  744. params_z[p_param] = p_value;
  745. if (get_joint().is_valid()) {
  746. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(get_joint(), Vector3::AXIS_Z, PhysicsServer::G6DOFJointAxisParam(p_param), p_value);
  747. }
  748. update_gizmo();
  749. }
  750. float Generic6DOFJoint::get_param_z(Param p_param) const {
  751. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  752. return params_z[p_param];
  753. }
  754. void Generic6DOFJoint::set_flag_x(Flag p_flag, bool p_enabled) {
  755. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  756. flags_x[p_flag] = p_enabled;
  757. if (get_joint().is_valid()) {
  758. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(get_joint(), Vector3::AXIS_X, PhysicsServer::G6DOFJointAxisFlag(p_flag), p_enabled);
  759. }
  760. update_gizmo();
  761. }
  762. bool Generic6DOFJoint::get_flag_x(Flag p_flag) const {
  763. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  764. return flags_x[p_flag];
  765. }
  766. void Generic6DOFJoint::set_flag_y(Flag p_flag, bool p_enabled) {
  767. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  768. flags_y[p_flag] = p_enabled;
  769. if (get_joint().is_valid()) {
  770. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(get_joint(), Vector3::AXIS_Y, PhysicsServer::G6DOFJointAxisFlag(p_flag), p_enabled);
  771. }
  772. update_gizmo();
  773. }
  774. bool Generic6DOFJoint::get_flag_y(Flag p_flag) const {
  775. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  776. return flags_y[p_flag];
  777. }
  778. void Generic6DOFJoint::set_flag_z(Flag p_flag, bool p_enabled) {
  779. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  780. flags_z[p_flag] = p_enabled;
  781. if (get_joint().is_valid()) {
  782. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(get_joint(), Vector3::AXIS_Z, PhysicsServer::G6DOFJointAxisFlag(p_flag), p_enabled);
  783. }
  784. update_gizmo();
  785. }
  786. bool Generic6DOFJoint::get_flag_z(Flag p_flag) const {
  787. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  788. return flags_z[p_flag];
  789. }
  790. RID Generic6DOFJoint::_configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) {
  791. Transform gt = get_global_transform();
  792. //Vector3 cone_twistpos = gt.origin;
  793. //Vector3 cone_twistdir = gt.basis.get_axis(2);
  794. Transform ainv = body_a->get_global_transform().affine_inverse();
  795. Transform local_a = ainv * gt;
  796. local_a.orthonormalize();
  797. Transform local_b = gt;
  798. if (body_b) {
  799. Transform binv = body_b->get_global_transform().affine_inverse();
  800. local_b = binv * gt;
  801. }
  802. local_b.orthonormalize();
  803. RID j = PhysicsServer::get_singleton()->joint_create_generic_6dof(body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b);
  804. for (int i = 0; i < PARAM_MAX; i++) {
  805. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, Vector3::AXIS_X, PhysicsServer::G6DOFJointAxisParam(i), params_x[i]);
  806. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, Vector3::AXIS_Y, PhysicsServer::G6DOFJointAxisParam(i), params_y[i]);
  807. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, Vector3::AXIS_Z, PhysicsServer::G6DOFJointAxisParam(i), params_z[i]);
  808. }
  809. for (int i = 0; i < FLAG_MAX; i++) {
  810. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, Vector3::AXIS_X, PhysicsServer::G6DOFJointAxisFlag(i), flags_x[i]);
  811. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, Vector3::AXIS_Y, PhysicsServer::G6DOFJointAxisFlag(i), flags_y[i]);
  812. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, Vector3::AXIS_Z, PhysicsServer::G6DOFJointAxisFlag(i), flags_z[i]);
  813. }
  814. return j;
  815. }
  816. Generic6DOFJoint::Generic6DOFJoint() {
  817. set_param_x(PARAM_LINEAR_LOWER_LIMIT, 0);
  818. set_param_x(PARAM_LINEAR_UPPER_LIMIT, 0);
  819. set_param_x(PARAM_LINEAR_LIMIT_SOFTNESS, 0.7);
  820. set_param_x(PARAM_LINEAR_RESTITUTION, 0.5);
  821. set_param_x(PARAM_LINEAR_DAMPING, 1.0);
  822. set_param_x(PARAM_LINEAR_MOTOR_TARGET_VELOCITY, 0);
  823. set_param_x(PARAM_LINEAR_MOTOR_FORCE_LIMIT, 0);
  824. set_param_x(PARAM_LINEAR_SPRING_STIFFNESS, 0.01);
  825. set_param_x(PARAM_LINEAR_SPRING_DAMPING, 0.01);
  826. set_param_x(PARAM_LINEAR_SPRING_EQUILIBRIUM_POINT, 0.0);
  827. set_param_x(PARAM_ANGULAR_LOWER_LIMIT, 0);
  828. set_param_x(PARAM_ANGULAR_UPPER_LIMIT, 0);
  829. set_param_x(PARAM_ANGULAR_LIMIT_SOFTNESS, 0.5f);
  830. set_param_x(PARAM_ANGULAR_DAMPING, 1.0f);
  831. set_param_x(PARAM_ANGULAR_RESTITUTION, 0);
  832. set_param_x(PARAM_ANGULAR_FORCE_LIMIT, 0);
  833. set_param_x(PARAM_ANGULAR_ERP, 0.5);
  834. set_param_x(PARAM_ANGULAR_MOTOR_TARGET_VELOCITY, 0);
  835. set_param_x(PARAM_ANGULAR_MOTOR_FORCE_LIMIT, 300);
  836. set_param_x(PARAM_ANGULAR_SPRING_STIFFNESS, 0);
  837. set_param_x(PARAM_ANGULAR_SPRING_DAMPING, 0);
  838. set_param_x(PARAM_ANGULAR_SPRING_EQUILIBRIUM_POINT, 0);
  839. set_flag_x(FLAG_ENABLE_ANGULAR_LIMIT, true);
  840. set_flag_x(FLAG_ENABLE_LINEAR_LIMIT, true);
  841. set_flag_x(FLAG_ENABLE_ANGULAR_SPRING, false);
  842. set_flag_x(FLAG_ENABLE_LINEAR_SPRING, false);
  843. set_flag_x(FLAG_ENABLE_MOTOR, false);
  844. set_flag_x(FLAG_ENABLE_LINEAR_MOTOR, false);
  845. set_param_y(PARAM_LINEAR_LOWER_LIMIT, 0);
  846. set_param_y(PARAM_LINEAR_UPPER_LIMIT, 0);
  847. set_param_y(PARAM_LINEAR_LIMIT_SOFTNESS, 0.7);
  848. set_param_y(PARAM_LINEAR_RESTITUTION, 0.5);
  849. set_param_y(PARAM_LINEAR_DAMPING, 1.0);
  850. set_param_y(PARAM_LINEAR_MOTOR_TARGET_VELOCITY, 0);
  851. set_param_y(PARAM_LINEAR_MOTOR_FORCE_LIMIT, 0);
  852. set_param_y(PARAM_LINEAR_SPRING_STIFFNESS, 0.01);
  853. set_param_y(PARAM_LINEAR_SPRING_DAMPING, 0.01);
  854. set_param_y(PARAM_LINEAR_SPRING_EQUILIBRIUM_POINT, 0.0);
  855. set_param_y(PARAM_ANGULAR_LOWER_LIMIT, 0);
  856. set_param_y(PARAM_ANGULAR_UPPER_LIMIT, 0);
  857. set_param_y(PARAM_ANGULAR_LIMIT_SOFTNESS, 0.5f);
  858. set_param_y(PARAM_ANGULAR_DAMPING, 1.0f);
  859. set_param_y(PARAM_ANGULAR_RESTITUTION, 0);
  860. set_param_y(PARAM_ANGULAR_FORCE_LIMIT, 0);
  861. set_param_y(PARAM_ANGULAR_ERP, 0.5);
  862. set_param_y(PARAM_ANGULAR_MOTOR_TARGET_VELOCITY, 0);
  863. set_param_y(PARAM_ANGULAR_MOTOR_FORCE_LIMIT, 300);
  864. set_param_y(PARAM_ANGULAR_SPRING_STIFFNESS, 0);
  865. set_param_y(PARAM_ANGULAR_SPRING_DAMPING, 0);
  866. set_param_y(PARAM_ANGULAR_SPRING_EQUILIBRIUM_POINT, 0);
  867. set_flag_y(FLAG_ENABLE_ANGULAR_LIMIT, true);
  868. set_flag_y(FLAG_ENABLE_LINEAR_LIMIT, true);
  869. set_flag_y(FLAG_ENABLE_ANGULAR_SPRING, false);
  870. set_flag_y(FLAG_ENABLE_LINEAR_SPRING, false);
  871. set_flag_y(FLAG_ENABLE_MOTOR, false);
  872. set_flag_y(FLAG_ENABLE_LINEAR_MOTOR, false);
  873. set_param_z(PARAM_LINEAR_LOWER_LIMIT, 0);
  874. set_param_z(PARAM_LINEAR_UPPER_LIMIT, 0);
  875. set_param_z(PARAM_LINEAR_LIMIT_SOFTNESS, 0.7);
  876. set_param_z(PARAM_LINEAR_RESTITUTION, 0.5);
  877. set_param_z(PARAM_LINEAR_DAMPING, 1.0);
  878. set_param_z(PARAM_LINEAR_MOTOR_TARGET_VELOCITY, 0);
  879. set_param_z(PARAM_LINEAR_MOTOR_FORCE_LIMIT, 0);
  880. set_param_z(PARAM_LINEAR_SPRING_STIFFNESS, 0.01);
  881. set_param_z(PARAM_LINEAR_SPRING_DAMPING, 0.01);
  882. set_param_z(PARAM_LINEAR_SPRING_EQUILIBRIUM_POINT, 0.0);
  883. set_param_z(PARAM_ANGULAR_LOWER_LIMIT, 0);
  884. set_param_z(PARAM_ANGULAR_UPPER_LIMIT, 0);
  885. set_param_z(PARAM_ANGULAR_LIMIT_SOFTNESS, 0.5f);
  886. set_param_z(PARAM_ANGULAR_DAMPING, 1.0f);
  887. set_param_z(PARAM_ANGULAR_RESTITUTION, 0);
  888. set_param_z(PARAM_ANGULAR_FORCE_LIMIT, 0);
  889. set_param_z(PARAM_ANGULAR_ERP, 0.5);
  890. set_param_z(PARAM_ANGULAR_MOTOR_TARGET_VELOCITY, 0);
  891. set_param_z(PARAM_ANGULAR_MOTOR_FORCE_LIMIT, 300);
  892. set_param_z(PARAM_ANGULAR_SPRING_STIFFNESS, 0);
  893. set_param_z(PARAM_ANGULAR_SPRING_DAMPING, 0);
  894. set_param_z(PARAM_ANGULAR_SPRING_EQUILIBRIUM_POINT, 0);
  895. set_flag_z(FLAG_ENABLE_ANGULAR_LIMIT, true);
  896. set_flag_z(FLAG_ENABLE_LINEAR_LIMIT, true);
  897. set_flag_z(FLAG_ENABLE_ANGULAR_SPRING, false);
  898. set_flag_z(FLAG_ENABLE_LINEAR_SPRING, false);
  899. set_flag_z(FLAG_ENABLE_MOTOR, false);
  900. set_flag_z(FLAG_ENABLE_LINEAR_MOTOR, false);
  901. }