physics_joint.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*************************************************************************/
  2. /* physics_joint.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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. void Joint::_update_joint(bool p_only_free) {
  32. if (joint.is_valid()) {
  33. if (ba.is_valid() && bb.is_valid())
  34. PhysicsServer::get_singleton()->body_remove_collision_exception(ba, bb);
  35. PhysicsServer::get_singleton()->free(joint);
  36. joint = RID();
  37. ba = RID();
  38. bb = RID();
  39. }
  40. if (p_only_free || !is_inside_tree())
  41. return;
  42. Node *node_a = has_node(get_node_a()) ? get_node(get_node_a()) : (Node *)NULL;
  43. Node *node_b = has_node(get_node_b()) ? get_node(get_node_b()) : (Node *)NULL;
  44. if (!node_a || !node_b)
  45. return;
  46. PhysicsBody *body_a = Object::cast_to<PhysicsBody>(node_a);
  47. PhysicsBody *body_b = Object::cast_to<PhysicsBody>(node_b);
  48. if (!body_a || !body_b)
  49. return;
  50. if (!body_a) {
  51. SWAP(body_a, body_b);
  52. }
  53. joint = _configure_joint(body_a, body_b);
  54. if (!joint.is_valid())
  55. return;
  56. PhysicsServer::get_singleton()->joint_set_solver_priority(joint, solver_priority);
  57. ba = body_a->get_rid();
  58. bb = body_b->get_rid();
  59. if (exclude_from_collision)
  60. PhysicsServer::get_singleton()->body_add_collision_exception(body_a->get_rid(), body_b->get_rid());
  61. }
  62. void Joint::set_node_a(const NodePath &p_node_a) {
  63. if (a == p_node_a)
  64. return;
  65. a = p_node_a;
  66. _update_joint();
  67. }
  68. NodePath Joint::get_node_a() const {
  69. return a;
  70. }
  71. void Joint::set_node_b(const NodePath &p_node_b) {
  72. if (b == p_node_b)
  73. return;
  74. b = p_node_b;
  75. _update_joint();
  76. }
  77. NodePath Joint::get_node_b() const {
  78. return b;
  79. }
  80. void Joint::set_solver_priority(int p_priority) {
  81. solver_priority = p_priority;
  82. if (joint.is_valid())
  83. PhysicsServer::get_singleton()->joint_set_solver_priority(joint, solver_priority);
  84. }
  85. int Joint::get_solver_priority() const {
  86. return solver_priority;
  87. }
  88. void Joint::_notification(int p_what) {
  89. switch (p_what) {
  90. case NOTIFICATION_READY: {
  91. _update_joint();
  92. } break;
  93. case NOTIFICATION_EXIT_TREE: {
  94. if (joint.is_valid()) {
  95. _update_joint(true);
  96. }
  97. } break;
  98. }
  99. }
  100. void Joint::set_exclude_nodes_from_collision(bool p_enable) {
  101. if (exclude_from_collision == p_enable)
  102. return;
  103. exclude_from_collision = p_enable;
  104. _update_joint();
  105. }
  106. bool Joint::get_exclude_nodes_from_collision() const {
  107. return exclude_from_collision;
  108. }
  109. void Joint::_bind_methods() {
  110. ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint::set_node_a);
  111. ClassDB::bind_method(D_METHOD("get_node_a"), &Joint::get_node_a);
  112. ClassDB::bind_method(D_METHOD("set_node_b", "node"), &Joint::set_node_b);
  113. ClassDB::bind_method(D_METHOD("get_node_b"), &Joint::get_node_b);
  114. ClassDB::bind_method(D_METHOD("set_solver_priority", "priority"), &Joint::set_solver_priority);
  115. ClassDB::bind_method(D_METHOD("get_solver_priority"), &Joint::get_solver_priority);
  116. ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint::set_exclude_nodes_from_collision);
  117. ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint::get_exclude_nodes_from_collision);
  118. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "nodes/node_a"), "set_node_a", "get_node_a");
  119. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "nodes/node_b"), "set_node_b", "get_node_b");
  120. ADD_PROPERTY(PropertyInfo(Variant::INT, "solver/priority", PROPERTY_HINT_RANGE, "1,8,1"), "set_solver_priority", "get_solver_priority");
  121. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision/exclude_nodes"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");
  122. }
  123. Joint::Joint() {
  124. exclude_from_collision = true;
  125. solver_priority = 1;
  126. set_notify_transform(true);
  127. }
  128. ///////////////////////////////////
  129. void PinJoint::_bind_methods() {
  130. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &PinJoint::set_param);
  131. ClassDB::bind_method(D_METHOD("get_param", "param"), &PinJoint::get_param);
  132. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/bias", PROPERTY_HINT_RANGE, "0.01,0.99,0.01"), "set_param", "get_param", PARAM_BIAS);
  133. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/damping", PROPERTY_HINT_RANGE, "0.01,8.0,0.01"), "set_param", "get_param", PARAM_DAMPING);
  134. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/impulse_clamp", PROPERTY_HINT_RANGE, "0.0,64.0,0.01"), "set_param", "get_param", PARAM_IMPULSE_CLAMP);
  135. BIND_ENUM_CONSTANT(PARAM_BIAS);
  136. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  137. BIND_ENUM_CONSTANT(PARAM_IMPULSE_CLAMP);
  138. }
  139. void PinJoint::set_param(Param p_param, float p_value) {
  140. ERR_FAIL_INDEX(p_param, 3);
  141. params[p_param] = p_value;
  142. if (get_joint().is_valid())
  143. PhysicsServer::get_singleton()->pin_joint_set_param(get_joint(), PhysicsServer::PinJointParam(p_param), p_value);
  144. }
  145. float PinJoint::get_param(Param p_param) const {
  146. ERR_FAIL_INDEX_V(p_param, 3, 0);
  147. return params[p_param];
  148. }
  149. RID PinJoint::_configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) {
  150. Vector3 pinpos = get_global_transform().origin;
  151. Vector3 local_a = body_a->get_global_transform().affine_inverse().xform(pinpos);
  152. Vector3 local_b;
  153. if (body_b)
  154. local_b = body_b->get_global_transform().affine_inverse().xform(pinpos);
  155. else
  156. local_b = pinpos;
  157. RID j = PhysicsServer::get_singleton()->joint_create_pin(body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b);
  158. for (int i = 0; i < 3; i++) {
  159. PhysicsServer::get_singleton()->pin_joint_set_param(j, PhysicsServer::PinJointParam(i), params[i]);
  160. }
  161. return j;
  162. }
  163. PinJoint::PinJoint() {
  164. params[PARAM_BIAS] = 0.3;
  165. params[PARAM_DAMPING] = 1;
  166. params[PARAM_IMPULSE_CLAMP] = 0;
  167. }
  168. /////////////////////////////////////////////////
  169. ///////////////////////////////////
  170. void HingeJoint::_set_upper_limit(float p_limit) {
  171. set_param(PARAM_LIMIT_UPPER, Math::deg2rad(p_limit));
  172. }
  173. float HingeJoint::_get_upper_limit() const {
  174. return Math::rad2deg(get_param(PARAM_LIMIT_UPPER));
  175. }
  176. void HingeJoint::_set_lower_limit(float p_limit) {
  177. set_param(PARAM_LIMIT_LOWER, Math::deg2rad(p_limit));
  178. }
  179. float HingeJoint::_get_lower_limit() const {
  180. return Math::rad2deg(get_param(PARAM_LIMIT_LOWER));
  181. }
  182. void HingeJoint::_bind_methods() {
  183. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &HingeJoint::set_param);
  184. ClassDB::bind_method(D_METHOD("get_param", "param"), &HingeJoint::get_param);
  185. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enabled"), &HingeJoint::set_flag);
  186. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &HingeJoint::get_flag);
  187. ClassDB::bind_method(D_METHOD("_set_upper_limit", "upper_limit"), &HingeJoint::_set_upper_limit);
  188. ClassDB::bind_method(D_METHOD("_get_upper_limit"), &HingeJoint::_get_upper_limit);
  189. ClassDB::bind_method(D_METHOD("_set_lower_limit", "lower_limit"), &HingeJoint::_set_lower_limit);
  190. ClassDB::bind_method(D_METHOD("_get_lower_limit"), &HingeJoint::_get_lower_limit);
  191. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/bias", PROPERTY_HINT_RANGE, "0.01,0.99,0.01"), "set_param", "get_param", PARAM_BIAS);
  192. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_limit/enable"), "set_flag", "get_flag", FLAG_USE_LIMIT);
  193. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit/upper", PROPERTY_HINT_RANGE, "-180,180,0.1"), "_set_upper_limit", "_get_upper_limit");
  194. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit/lower", PROPERTY_HINT_RANGE, "-180,180,0.1"), "_set_lower_limit", "_get_lower_limit");
  195. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit/bias", PROPERTY_HINT_RANGE, "0.01,0.99,0.01"), "set_param", "get_param", PARAM_LIMIT_BIAS);
  196. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit/softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param", "get_param", PARAM_LIMIT_SOFTNESS);
  197. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit/relaxation", PROPERTY_HINT_RANGE, "0.01,16,0.01"), "set_param", "get_param", PARAM_LIMIT_RELAXATION);
  198. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "motor/enable"), "set_flag", "get_flag", FLAG_ENABLE_MOTOR);
  199. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "motor/target_velocity", PROPERTY_HINT_RANGE, "0.01,4096,0.01"), "set_param", "get_param", PARAM_MOTOR_TARGET_VELOCITY);
  200. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "motor/max_impulse", PROPERTY_HINT_RANGE, "0.01,1024,0.01"), "set_param", "get_param", PARAM_MOTOR_MAX_IMPULSE);
  201. BIND_ENUM_CONSTANT(PARAM_BIAS);
  202. BIND_ENUM_CONSTANT(PARAM_LIMIT_UPPER);
  203. BIND_ENUM_CONSTANT(PARAM_LIMIT_LOWER);
  204. BIND_ENUM_CONSTANT(PARAM_LIMIT_BIAS);
  205. BIND_ENUM_CONSTANT(PARAM_LIMIT_SOFTNESS);
  206. BIND_ENUM_CONSTANT(PARAM_LIMIT_RELAXATION);
  207. BIND_ENUM_CONSTANT(PARAM_MOTOR_TARGET_VELOCITY);
  208. BIND_ENUM_CONSTANT(PARAM_MOTOR_MAX_IMPULSE);
  209. BIND_ENUM_CONSTANT(PARAM_MAX);
  210. BIND_ENUM_CONSTANT(FLAG_USE_LIMIT);
  211. BIND_ENUM_CONSTANT(FLAG_ENABLE_MOTOR);
  212. BIND_ENUM_CONSTANT(FLAG_MAX);
  213. }
  214. void HingeJoint::set_param(Param p_param, float p_value) {
  215. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  216. params[p_param] = p_value;
  217. if (get_joint().is_valid())
  218. PhysicsServer::get_singleton()->hinge_joint_set_param(get_joint(), PhysicsServer::HingeJointParam(p_param), p_value);
  219. update_gizmo();
  220. }
  221. float HingeJoint::get_param(Param p_param) const {
  222. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  223. return params[p_param];
  224. }
  225. void HingeJoint::set_flag(Flag p_flag, bool p_value) {
  226. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  227. flags[p_flag] = p_value;
  228. if (get_joint().is_valid())
  229. PhysicsServer::get_singleton()->hinge_joint_set_flag(get_joint(), PhysicsServer::HingeJointFlag(p_flag), p_value);
  230. update_gizmo();
  231. }
  232. bool HingeJoint::get_flag(Flag p_flag) const {
  233. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  234. return flags[p_flag];
  235. }
  236. RID HingeJoint::_configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) {
  237. Transform gt = get_global_transform();
  238. Transform ainv = body_a->get_global_transform().affine_inverse();
  239. Transform local_a = ainv * gt;
  240. local_a.orthonormalize();
  241. Transform local_b = gt;
  242. if (body_b) {
  243. Transform binv = body_b->get_global_transform().affine_inverse();
  244. local_b = binv * gt;
  245. }
  246. local_b.orthonormalize();
  247. RID j = PhysicsServer::get_singleton()->joint_create_hinge(body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b);
  248. for (int i = 0; i < PARAM_MAX; i++) {
  249. PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HingeJointParam(i), params[i]);
  250. }
  251. for (int i = 0; i < FLAG_MAX; i++) {
  252. set_flag(Flag(i), flags[i]);
  253. PhysicsServer::get_singleton()->hinge_joint_set_flag(j, PhysicsServer::HingeJointFlag(i), flags[i]);
  254. }
  255. return j;
  256. }
  257. HingeJoint::HingeJoint() {
  258. params[PARAM_BIAS] = 0.3;
  259. params[PARAM_LIMIT_UPPER] = Math_PI * 0.5;
  260. params[PARAM_LIMIT_LOWER] = -Math_PI * 0.5;
  261. params[PARAM_LIMIT_BIAS] = 0.3;
  262. params[PARAM_LIMIT_SOFTNESS] = 0.9;
  263. params[PARAM_LIMIT_RELAXATION] = 1.0;
  264. params[PARAM_MOTOR_TARGET_VELOCITY] = 1;
  265. params[PARAM_MOTOR_MAX_IMPULSE] = 1;
  266. flags[FLAG_USE_LIMIT] = false;
  267. flags[FLAG_ENABLE_MOTOR] = false;
  268. }
  269. /////////////////////////////////////////////////
  270. //////////////////////////////////
  271. void SliderJoint::_set_upper_limit_angular(float p_limit_angular) {
  272. set_param(PARAM_ANGULAR_LIMIT_UPPER, Math::deg2rad(p_limit_angular));
  273. }
  274. float SliderJoint::_get_upper_limit_angular() const {
  275. return Math::rad2deg(get_param(PARAM_ANGULAR_LIMIT_UPPER));
  276. }
  277. void SliderJoint::_set_lower_limit_angular(float p_limit_angular) {
  278. set_param(PARAM_ANGULAR_LIMIT_LOWER, Math::deg2rad(p_limit_angular));
  279. }
  280. float SliderJoint::_get_lower_limit_angular() const {
  281. return Math::rad2deg(get_param(PARAM_ANGULAR_LIMIT_LOWER));
  282. }
  283. void SliderJoint::_bind_methods() {
  284. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &SliderJoint::set_param);
  285. ClassDB::bind_method(D_METHOD("get_param", "param"), &SliderJoint::get_param);
  286. ClassDB::bind_method(D_METHOD("_set_upper_limit_angular", "upper_limit_angular"), &SliderJoint::_set_upper_limit_angular);
  287. ClassDB::bind_method(D_METHOD("_get_upper_limit_angular"), &SliderJoint::_get_upper_limit_angular);
  288. ClassDB::bind_method(D_METHOD("_set_lower_limit_angular", "lower_limit_angular"), &SliderJoint::_set_lower_limit_angular);
  289. ClassDB::bind_method(D_METHOD("_get_lower_limit_angular"), &SliderJoint::_get_lower_limit_angular);
  290. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit/upper_distance", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_param", "get_param", PARAM_LINEAR_LIMIT_UPPER);
  291. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit/lower_distance", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_param", "get_param", PARAM_LINEAR_LIMIT_LOWER);
  292. 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);
  293. 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);
  294. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_LIMIT_DAMPING);
  295. 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);
  296. 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);
  297. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_motion/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_MOTION_DAMPING);
  298. 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);
  299. 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);
  300. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_ortho/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_LINEAR_ORTHOGONAL_DAMPING);
  301. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit/upper_angle", PROPERTY_HINT_RANGE, "-180,180,0.1"), "_set_upper_limit_angular", "_get_upper_limit_angular");
  302. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_limit/lower_angle", PROPERTY_HINT_RANGE, "-180,180,0.1"), "_set_lower_limit_angular", "_get_lower_limit_angular");
  303. 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);
  304. 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);
  305. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_LIMIT_DAMPING);
  306. 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);
  307. 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);
  308. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motion/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_MOTION_DAMPING);
  309. 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);
  310. 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);
  311. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_ortho/damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"), "set_param", "get_param", PARAM_ANGULAR_ORTHOGONAL_DAMPING);
  312. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_UPPER);
  313. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_LOWER);
  314. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_SOFTNESS);
  315. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_RESTITUTION);
  316. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_DAMPING);
  317. BIND_ENUM_CONSTANT(PARAM_LINEAR_MOTION_SOFTNESS);
  318. BIND_ENUM_CONSTANT(PARAM_LINEAR_MOTION_RESTITUTION);
  319. BIND_ENUM_CONSTANT(PARAM_LINEAR_MOTION_DAMPING);
  320. BIND_ENUM_CONSTANT(PARAM_LINEAR_ORTHOGONAL_SOFTNESS);
  321. BIND_ENUM_CONSTANT(PARAM_LINEAR_ORTHOGONAL_RESTITUTION);
  322. BIND_ENUM_CONSTANT(PARAM_LINEAR_ORTHOGONAL_DAMPING);
  323. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_UPPER);
  324. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_LOWER);
  325. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_SOFTNESS);
  326. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_RESTITUTION);
  327. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_DAMPING);
  328. BIND_ENUM_CONSTANT(PARAM_ANGULAR_MOTION_SOFTNESS);
  329. BIND_ENUM_CONSTANT(PARAM_ANGULAR_MOTION_RESTITUTION);
  330. BIND_ENUM_CONSTANT(PARAM_ANGULAR_MOTION_DAMPING);
  331. BIND_ENUM_CONSTANT(PARAM_ANGULAR_ORTHOGONAL_SOFTNESS);
  332. BIND_ENUM_CONSTANT(PARAM_ANGULAR_ORTHOGONAL_RESTITUTION);
  333. BIND_ENUM_CONSTANT(PARAM_ANGULAR_ORTHOGONAL_DAMPING);
  334. BIND_ENUM_CONSTANT(PARAM_MAX);
  335. }
  336. void SliderJoint::set_param(Param p_param, float p_value) {
  337. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  338. params[p_param] = p_value;
  339. if (get_joint().is_valid())
  340. PhysicsServer::get_singleton()->slider_joint_set_param(get_joint(), PhysicsServer::SliderJointParam(p_param), p_value);
  341. update_gizmo();
  342. }
  343. float SliderJoint::get_param(Param p_param) const {
  344. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  345. return params[p_param];
  346. }
  347. RID SliderJoint::_configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) {
  348. Transform gt = get_global_transform();
  349. Transform ainv = body_a->get_global_transform().affine_inverse();
  350. Transform local_a = ainv * gt;
  351. local_a.orthonormalize();
  352. Transform local_b = gt;
  353. if (body_b) {
  354. Transform binv = body_b->get_global_transform().affine_inverse();
  355. local_b = binv * gt;
  356. }
  357. local_b.orthonormalize();
  358. RID j = PhysicsServer::get_singleton()->joint_create_slider(body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b);
  359. for (int i = 0; i < PARAM_MAX; i++) {
  360. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SliderJointParam(i), params[i]);
  361. }
  362. return j;
  363. }
  364. SliderJoint::SliderJoint() {
  365. params[PARAM_LINEAR_LIMIT_UPPER] = 1.0;
  366. params[PARAM_LINEAR_LIMIT_LOWER] = -1.0;
  367. params[PARAM_LINEAR_LIMIT_SOFTNESS] = 1.0;
  368. params[PARAM_LINEAR_LIMIT_RESTITUTION] = 0.7;
  369. params[PARAM_LINEAR_LIMIT_DAMPING] = 1.0;
  370. params[PARAM_LINEAR_MOTION_SOFTNESS] = 1.0;
  371. params[PARAM_LINEAR_MOTION_RESTITUTION] = 0.7;
  372. params[PARAM_LINEAR_MOTION_DAMPING] = 0; //1.0;
  373. params[PARAM_LINEAR_ORTHOGONAL_SOFTNESS] = 1.0;
  374. params[PARAM_LINEAR_ORTHOGONAL_RESTITUTION] = 0.7;
  375. params[PARAM_LINEAR_ORTHOGONAL_DAMPING] = 1.0;
  376. params[PARAM_ANGULAR_LIMIT_UPPER] = 0;
  377. params[PARAM_ANGULAR_LIMIT_LOWER] = 0;
  378. params[PARAM_ANGULAR_LIMIT_SOFTNESS] = 1.0;
  379. params[PARAM_ANGULAR_LIMIT_RESTITUTION] = 0.7;
  380. params[PARAM_ANGULAR_LIMIT_DAMPING] = 0; //1.0;
  381. params[PARAM_ANGULAR_MOTION_SOFTNESS] = 1.0;
  382. params[PARAM_ANGULAR_MOTION_RESTITUTION] = 0.7;
  383. params[PARAM_ANGULAR_MOTION_DAMPING] = 1.0;
  384. params[PARAM_ANGULAR_ORTHOGONAL_SOFTNESS] = 1.0;
  385. params[PARAM_ANGULAR_ORTHOGONAL_RESTITUTION] = 0.7;
  386. params[PARAM_ANGULAR_ORTHOGONAL_DAMPING] = 1.0;
  387. }
  388. //////////////////////////////////
  389. void ConeTwistJoint::_set_swing_span(float p_limit_angular) {
  390. set_param(PARAM_SWING_SPAN, Math::deg2rad(p_limit_angular));
  391. }
  392. float ConeTwistJoint::_get_swing_span() const {
  393. return Math::rad2deg(get_param(PARAM_SWING_SPAN));
  394. }
  395. void ConeTwistJoint::_set_twist_span(float p_limit_angular) {
  396. set_param(PARAM_TWIST_SPAN, Math::deg2rad(p_limit_angular));
  397. }
  398. float ConeTwistJoint::_get_twist_span() const {
  399. return Math::rad2deg(get_param(PARAM_TWIST_SPAN));
  400. }
  401. void ConeTwistJoint::_bind_methods() {
  402. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &ConeTwistJoint::set_param);
  403. ClassDB::bind_method(D_METHOD("get_param", "param"), &ConeTwistJoint::get_param);
  404. ClassDB::bind_method(D_METHOD("_set_swing_span", "swing_span"), &ConeTwistJoint::_set_swing_span);
  405. ClassDB::bind_method(D_METHOD("_get_swing_span"), &ConeTwistJoint::_get_swing_span);
  406. ClassDB::bind_method(D_METHOD("_set_twist_span", "twist_span"), &ConeTwistJoint::_set_twist_span);
  407. ClassDB::bind_method(D_METHOD("_get_twist_span"), &ConeTwistJoint::_get_twist_span);
  408. ADD_PROPERTY(PropertyInfo(Variant::REAL, "swing_span", PROPERTY_HINT_RANGE, "-180,180,0.1"), "_set_swing_span", "_get_swing_span");
  409. ADD_PROPERTY(PropertyInfo(Variant::REAL, "twist_span", PROPERTY_HINT_RANGE, "-40000,40000,0.1"), "_set_twist_span", "_get_twist_span");
  410. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "bias", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_BIAS);
  411. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_SOFTNESS);
  412. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "relaxation", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"), "set_param", "get_param", PARAM_RELAXATION);
  413. BIND_ENUM_CONSTANT(PARAM_SWING_SPAN);
  414. BIND_ENUM_CONSTANT(PARAM_TWIST_SPAN);
  415. BIND_ENUM_CONSTANT(PARAM_BIAS);
  416. BIND_ENUM_CONSTANT(PARAM_SOFTNESS);
  417. BIND_ENUM_CONSTANT(PARAM_RELAXATION);
  418. BIND_ENUM_CONSTANT(PARAM_MAX);
  419. }
  420. void ConeTwistJoint::set_param(Param p_param, float p_value) {
  421. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  422. params[p_param] = p_value;
  423. if (get_joint().is_valid())
  424. PhysicsServer::get_singleton()->cone_twist_joint_set_param(get_joint(), PhysicsServer::ConeTwistJointParam(p_param), p_value);
  425. update_gizmo();
  426. }
  427. float ConeTwistJoint::get_param(Param p_param) const {
  428. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  429. return params[p_param];
  430. }
  431. RID ConeTwistJoint::_configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) {
  432. Transform gt = get_global_transform();
  433. //Vector3 cone_twistpos = gt.origin;
  434. //Vector3 cone_twistdir = gt.basis.get_axis(2);
  435. Transform ainv = body_a->get_global_transform().affine_inverse();
  436. Transform local_a = ainv * gt;
  437. local_a.orthonormalize();
  438. Transform local_b = gt;
  439. if (body_b) {
  440. Transform binv = body_b->get_global_transform().affine_inverse();
  441. local_b = binv * gt;
  442. }
  443. local_b.orthonormalize();
  444. RID j = PhysicsServer::get_singleton()->joint_create_cone_twist(body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b);
  445. for (int i = 0; i < PARAM_MAX; i++) {
  446. PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::ConeTwistJointParam(i), params[i]);
  447. }
  448. return j;
  449. }
  450. ConeTwistJoint::ConeTwistJoint() {
  451. params[PARAM_SWING_SPAN] = Math_PI * 0.25;
  452. params[PARAM_TWIST_SPAN] = Math_PI;
  453. params[PARAM_BIAS] = 0.3;
  454. params[PARAM_SOFTNESS] = 0.8;
  455. params[PARAM_RELAXATION] = 1.0;
  456. }
  457. /////////////////////////////////////////////////////////////////////
  458. void Generic6DOFJoint::_set_angular_hi_limit_x(float p_limit_angular) {
  459. set_param_x(PARAM_ANGULAR_UPPER_LIMIT, Math::deg2rad(p_limit_angular));
  460. }
  461. float Generic6DOFJoint::_get_angular_hi_limit_x() const {
  462. return Math::rad2deg(get_param_x(PARAM_ANGULAR_UPPER_LIMIT));
  463. }
  464. void Generic6DOFJoint::_set_angular_lo_limit_x(float p_limit_angular) {
  465. set_param_x(PARAM_ANGULAR_LOWER_LIMIT, Math::deg2rad(p_limit_angular));
  466. }
  467. float Generic6DOFJoint::_get_angular_lo_limit_x() const {
  468. return Math::rad2deg(get_param_x(PARAM_ANGULAR_LOWER_LIMIT));
  469. }
  470. void Generic6DOFJoint::_set_angular_hi_limit_y(float p_limit_angular) {
  471. set_param_y(PARAM_ANGULAR_UPPER_LIMIT, Math::deg2rad(p_limit_angular));
  472. }
  473. float Generic6DOFJoint::_get_angular_hi_limit_y() const {
  474. return Math::rad2deg(get_param_y(PARAM_ANGULAR_UPPER_LIMIT));
  475. }
  476. void Generic6DOFJoint::_set_angular_lo_limit_y(float p_limit_angular) {
  477. set_param_y(PARAM_ANGULAR_LOWER_LIMIT, Math::deg2rad(p_limit_angular));
  478. }
  479. float Generic6DOFJoint::_get_angular_lo_limit_y() const {
  480. return Math::rad2deg(get_param_y(PARAM_ANGULAR_LOWER_LIMIT));
  481. }
  482. void Generic6DOFJoint::_set_angular_hi_limit_z(float p_limit_angular) {
  483. set_param_z(PARAM_ANGULAR_UPPER_LIMIT, Math::deg2rad(p_limit_angular));
  484. }
  485. float Generic6DOFJoint::_get_angular_hi_limit_z() const {
  486. return Math::rad2deg(get_param_z(PARAM_ANGULAR_UPPER_LIMIT));
  487. }
  488. void Generic6DOFJoint::_set_angular_lo_limit_z(float p_limit_angular) {
  489. set_param_z(PARAM_ANGULAR_LOWER_LIMIT, Math::deg2rad(p_limit_angular));
  490. }
  491. float Generic6DOFJoint::_get_angular_lo_limit_z() const {
  492. return Math::rad2deg(get_param_z(PARAM_ANGULAR_LOWER_LIMIT));
  493. }
  494. void Generic6DOFJoint::_bind_methods() {
  495. ClassDB::bind_method(D_METHOD("_set_angular_hi_limit_x", "angle"), &Generic6DOFJoint::_set_angular_hi_limit_x);
  496. ClassDB::bind_method(D_METHOD("_get_angular_hi_limit_x"), &Generic6DOFJoint::_get_angular_hi_limit_x);
  497. ClassDB::bind_method(D_METHOD("_set_angular_lo_limit_x", "angle"), &Generic6DOFJoint::_set_angular_lo_limit_x);
  498. ClassDB::bind_method(D_METHOD("_get_angular_lo_limit_x"), &Generic6DOFJoint::_get_angular_lo_limit_x);
  499. ClassDB::bind_method(D_METHOD("_set_angular_hi_limit_y", "angle"), &Generic6DOFJoint::_set_angular_hi_limit_y);
  500. ClassDB::bind_method(D_METHOD("_get_angular_hi_limit_y"), &Generic6DOFJoint::_get_angular_hi_limit_y);
  501. ClassDB::bind_method(D_METHOD("_set_angular_lo_limit_y", "angle"), &Generic6DOFJoint::_set_angular_lo_limit_y);
  502. ClassDB::bind_method(D_METHOD("_get_angular_lo_limit_y"), &Generic6DOFJoint::_get_angular_lo_limit_y);
  503. ClassDB::bind_method(D_METHOD("_set_angular_hi_limit_z", "angle"), &Generic6DOFJoint::_set_angular_hi_limit_z);
  504. ClassDB::bind_method(D_METHOD("_get_angular_hi_limit_z"), &Generic6DOFJoint::_get_angular_hi_limit_z);
  505. ClassDB::bind_method(D_METHOD("_set_angular_lo_limit_z", "angle"), &Generic6DOFJoint::_set_angular_lo_limit_z);
  506. ClassDB::bind_method(D_METHOD("_get_angular_lo_limit_z"), &Generic6DOFJoint::_get_angular_lo_limit_z);
  507. ClassDB::bind_method(D_METHOD("set_param_x", "param", "value"), &Generic6DOFJoint::set_param_x);
  508. ClassDB::bind_method(D_METHOD("get_param_x", "param"), &Generic6DOFJoint::get_param_x);
  509. ClassDB::bind_method(D_METHOD("set_param_y", "param", "value"), &Generic6DOFJoint::set_param_y);
  510. ClassDB::bind_method(D_METHOD("get_param_y", "param"), &Generic6DOFJoint::get_param_y);
  511. ClassDB::bind_method(D_METHOD("set_param_z", "param", "value"), &Generic6DOFJoint::set_param_z);
  512. ClassDB::bind_method(D_METHOD("get_param_z", "param"), &Generic6DOFJoint::get_param_z);
  513. ClassDB::bind_method(D_METHOD("set_flag_x", "flag", "value"), &Generic6DOFJoint::set_flag_x);
  514. ClassDB::bind_method(D_METHOD("get_flag_x", "flag"), &Generic6DOFJoint::get_flag_x);
  515. ClassDB::bind_method(D_METHOD("set_flag_y", "flag", "value"), &Generic6DOFJoint::set_flag_y);
  516. ClassDB::bind_method(D_METHOD("get_flag_y", "flag"), &Generic6DOFJoint::get_flag_y);
  517. ClassDB::bind_method(D_METHOD("set_flag_z", "flag", "value"), &Generic6DOFJoint::set_flag_z);
  518. ClassDB::bind_method(D_METHOD("get_flag_z", "flag"), &Generic6DOFJoint::get_flag_z);
  519. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_limit_x/enabled"), "set_flag_x", "get_flag_x", FLAG_ENABLE_LINEAR_LIMIT);
  520. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_x/upper_distance"), "set_param_x", "get_param_x", PARAM_LINEAR_UPPER_LIMIT);
  521. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_x/lower_distance"), "set_param_x", "get_param_x", PARAM_LINEAR_LOWER_LIMIT);
  522. 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);
  523. 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);
  524. 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);
  525. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_limit_x/enabled"), "set_flag_x", "get_flag_x", FLAG_ENABLE_ANGULAR_LIMIT);
  526. 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");
  527. 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");
  528. 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);
  529. 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);
  530. 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);
  531. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_x/force_limit"), "set_param_x", "get_param_x", PARAM_ANGULAR_FORCE_LIMIT);
  532. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_x/erp"), "set_param_x", "get_param_x", PARAM_ANGULAR_ERP);
  533. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_motor_x/enabled"), "set_flag_x", "get_flag_x", FLAG_ENABLE_MOTOR);
  534. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_x/target_velocity"), "set_param_x", "get_param_x", PARAM_ANGULAR_MOTOR_TARGET_VELOCITY);
  535. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_x/force_limit"), "set_param_x", "get_param_x", PARAM_ANGULAR_MOTOR_FORCE_LIMIT);
  536. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_limit_y/enabled"), "set_flag_y", "get_flag_y", FLAG_ENABLE_LINEAR_LIMIT);
  537. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_y/upper_distance"), "set_param_y", "get_param_y", PARAM_LINEAR_UPPER_LIMIT);
  538. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_y/lower_distance"), "set_param_y", "get_param_y", PARAM_LINEAR_LOWER_LIMIT);
  539. 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);
  540. 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);
  541. 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);
  542. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_limit_y/enabled"), "set_flag_y", "get_flag_y", FLAG_ENABLE_ANGULAR_LIMIT);
  543. 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");
  544. 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");
  545. 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);
  546. 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);
  547. 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);
  548. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_y/force_limit"), "set_param_y", "get_param_y", PARAM_ANGULAR_FORCE_LIMIT);
  549. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_y/erp"), "set_param_y", "get_param_y", PARAM_ANGULAR_ERP);
  550. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_motor_y/enabled"), "set_flag_y", "get_flag_y", FLAG_ENABLE_MOTOR);
  551. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_y/target_velocity"), "set_param_y", "get_param_y", PARAM_ANGULAR_MOTOR_TARGET_VELOCITY);
  552. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_y/force_limit"), "set_param_y", "get_param_y", PARAM_ANGULAR_MOTOR_FORCE_LIMIT);
  553. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "linear_limit_z/enabled"), "set_flag_z", "get_flag_z", FLAG_ENABLE_LINEAR_LIMIT);
  554. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_z/upper_distance"), "set_param_z", "get_param_z", PARAM_LINEAR_UPPER_LIMIT);
  555. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_limit_z/lower_distance"), "set_param_z", "get_param_z", PARAM_LINEAR_LOWER_LIMIT);
  556. 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);
  557. 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);
  558. 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);
  559. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_limit_z/enabled"), "set_flag_z", "get_flag_z", FLAG_ENABLE_ANGULAR_LIMIT);
  560. 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");
  561. 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");
  562. 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);
  563. 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);
  564. 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);
  565. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_z/force_limit"), "set_param_z", "get_param_z", PARAM_ANGULAR_FORCE_LIMIT);
  566. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_limit_z/erp"), "set_param_z", "get_param_z", PARAM_ANGULAR_ERP);
  567. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "angular_motor_z/enabled"), "set_flag_z", "get_flag_z", FLAG_ENABLE_MOTOR);
  568. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_z/target_velocity"), "set_param_z", "get_param_z", PARAM_ANGULAR_MOTOR_TARGET_VELOCITY);
  569. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_motor_z/force_limit"), "set_param_z", "get_param_z", PARAM_ANGULAR_MOTOR_FORCE_LIMIT);
  570. BIND_ENUM_CONSTANT(PARAM_LINEAR_LOWER_LIMIT);
  571. BIND_ENUM_CONSTANT(PARAM_LINEAR_UPPER_LIMIT);
  572. BIND_ENUM_CONSTANT(PARAM_LINEAR_LIMIT_SOFTNESS);
  573. BIND_ENUM_CONSTANT(PARAM_LINEAR_RESTITUTION);
  574. BIND_ENUM_CONSTANT(PARAM_LINEAR_DAMPING);
  575. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LOWER_LIMIT);
  576. BIND_ENUM_CONSTANT(PARAM_ANGULAR_UPPER_LIMIT);
  577. BIND_ENUM_CONSTANT(PARAM_ANGULAR_LIMIT_SOFTNESS);
  578. BIND_ENUM_CONSTANT(PARAM_ANGULAR_DAMPING);
  579. BIND_ENUM_CONSTANT(PARAM_ANGULAR_RESTITUTION);
  580. BIND_ENUM_CONSTANT(PARAM_ANGULAR_FORCE_LIMIT);
  581. BIND_ENUM_CONSTANT(PARAM_ANGULAR_ERP);
  582. BIND_ENUM_CONSTANT(PARAM_ANGULAR_MOTOR_TARGET_VELOCITY);
  583. BIND_ENUM_CONSTANT(PARAM_ANGULAR_MOTOR_FORCE_LIMIT);
  584. BIND_ENUM_CONSTANT(PARAM_MAX);
  585. BIND_ENUM_CONSTANT(FLAG_ENABLE_LINEAR_LIMIT);
  586. BIND_ENUM_CONSTANT(FLAG_ENABLE_ANGULAR_LIMIT);
  587. BIND_ENUM_CONSTANT(FLAG_ENABLE_MOTOR);
  588. BIND_ENUM_CONSTANT(FLAG_MAX);
  589. }
  590. void Generic6DOFJoint::set_param_x(Param p_param, float p_value) {
  591. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  592. params_x[p_param] = p_value;
  593. if (get_joint().is_valid())
  594. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(get_joint(), Vector3::AXIS_X, PhysicsServer::G6DOFJointAxisParam(p_param), p_value);
  595. update_gizmo();
  596. }
  597. float Generic6DOFJoint::get_param_x(Param p_param) const {
  598. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  599. return params_x[p_param];
  600. }
  601. void Generic6DOFJoint::set_param_y(Param p_param, float p_value) {
  602. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  603. params_y[p_param] = p_value;
  604. if (get_joint().is_valid())
  605. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(get_joint(), Vector3::AXIS_Y, PhysicsServer::G6DOFJointAxisParam(p_param), p_value);
  606. update_gizmo();
  607. }
  608. float Generic6DOFJoint::get_param_y(Param p_param) const {
  609. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  610. return params_y[p_param];
  611. }
  612. void Generic6DOFJoint::set_param_z(Param p_param, float p_value) {
  613. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  614. params_z[p_param] = p_value;
  615. if (get_joint().is_valid())
  616. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(get_joint(), Vector3::AXIS_Z, PhysicsServer::G6DOFJointAxisParam(p_param), p_value);
  617. update_gizmo();
  618. }
  619. float Generic6DOFJoint::get_param_z(Param p_param) const {
  620. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  621. return params_z[p_param];
  622. }
  623. void Generic6DOFJoint::set_flag_x(Flag p_flag, bool p_enabled) {
  624. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  625. flags_x[p_flag] = p_enabled;
  626. if (get_joint().is_valid())
  627. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(get_joint(), Vector3::AXIS_X, PhysicsServer::G6DOFJointAxisFlag(p_flag), p_enabled);
  628. update_gizmo();
  629. }
  630. bool Generic6DOFJoint::get_flag_x(Flag p_flag) const {
  631. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  632. return flags_x[p_flag];
  633. }
  634. void Generic6DOFJoint::set_flag_y(Flag p_flag, bool p_enabled) {
  635. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  636. flags_y[p_flag] = p_enabled;
  637. if (get_joint().is_valid())
  638. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(get_joint(), Vector3::AXIS_Y, PhysicsServer::G6DOFJointAxisFlag(p_flag), p_enabled);
  639. update_gizmo();
  640. }
  641. bool Generic6DOFJoint::get_flag_y(Flag p_flag) const {
  642. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  643. return flags_y[p_flag];
  644. }
  645. void Generic6DOFJoint::set_flag_z(Flag p_flag, bool p_enabled) {
  646. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  647. flags_z[p_flag] = p_enabled;
  648. if (get_joint().is_valid())
  649. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(get_joint(), Vector3::AXIS_Z, PhysicsServer::G6DOFJointAxisFlag(p_flag), p_enabled);
  650. update_gizmo();
  651. }
  652. bool Generic6DOFJoint::get_flag_z(Flag p_flag) const {
  653. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  654. return flags_z[p_flag];
  655. }
  656. RID Generic6DOFJoint::_configure_joint(PhysicsBody *body_a, PhysicsBody *body_b) {
  657. Transform gt = get_global_transform();
  658. //Vector3 cone_twistpos = gt.origin;
  659. //Vector3 cone_twistdir = gt.basis.get_axis(2);
  660. Transform ainv = body_a->get_global_transform().affine_inverse();
  661. Transform local_a = ainv * gt;
  662. local_a.orthonormalize();
  663. Transform local_b = gt;
  664. if (body_b) {
  665. Transform binv = body_b->get_global_transform().affine_inverse();
  666. local_b = binv * gt;
  667. }
  668. local_b.orthonormalize();
  669. RID j = PhysicsServer::get_singleton()->joint_create_generic_6dof(body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b);
  670. for (int i = 0; i < PARAM_MAX; i++) {
  671. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, Vector3::AXIS_X, PhysicsServer::G6DOFJointAxisParam(i), params_x[i]);
  672. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, Vector3::AXIS_Y, PhysicsServer::G6DOFJointAxisParam(i), params_y[i]);
  673. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, Vector3::AXIS_Z, PhysicsServer::G6DOFJointAxisParam(i), params_z[i]);
  674. }
  675. for (int i = 0; i < FLAG_MAX; i++) {
  676. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, Vector3::AXIS_X, PhysicsServer::G6DOFJointAxisFlag(i), flags_x[i]);
  677. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, Vector3::AXIS_Y, PhysicsServer::G6DOFJointAxisFlag(i), flags_y[i]);
  678. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, Vector3::AXIS_Z, PhysicsServer::G6DOFJointAxisFlag(i), flags_z[i]);
  679. }
  680. return j;
  681. }
  682. Generic6DOFJoint::Generic6DOFJoint() {
  683. set_param_x(PARAM_LINEAR_LOWER_LIMIT, 0);
  684. set_param_x(PARAM_LINEAR_UPPER_LIMIT, 0);
  685. set_param_x(PARAM_LINEAR_LIMIT_SOFTNESS, 0.7);
  686. set_param_x(PARAM_LINEAR_RESTITUTION, 0.5);
  687. set_param_x(PARAM_LINEAR_DAMPING, 1.0);
  688. set_param_x(PARAM_ANGULAR_LOWER_LIMIT, 0);
  689. set_param_x(PARAM_ANGULAR_UPPER_LIMIT, 0);
  690. set_param_x(PARAM_ANGULAR_LIMIT_SOFTNESS, 0.5f);
  691. set_param_x(PARAM_ANGULAR_DAMPING, 1.0f);
  692. set_param_x(PARAM_ANGULAR_RESTITUTION, 0);
  693. set_param_x(PARAM_ANGULAR_FORCE_LIMIT, 0);
  694. set_param_x(PARAM_ANGULAR_ERP, 0.5);
  695. set_param_x(PARAM_ANGULAR_MOTOR_TARGET_VELOCITY, 0);
  696. set_param_x(PARAM_ANGULAR_MOTOR_FORCE_LIMIT, 300);
  697. set_flag_x(FLAG_ENABLE_ANGULAR_LIMIT, true);
  698. set_flag_x(FLAG_ENABLE_LINEAR_LIMIT, true);
  699. set_flag_x(FLAG_ENABLE_MOTOR, false);
  700. set_param_y(PARAM_LINEAR_LOWER_LIMIT, 0);
  701. set_param_y(PARAM_LINEAR_UPPER_LIMIT, 0);
  702. set_param_y(PARAM_LINEAR_LIMIT_SOFTNESS, 0.7);
  703. set_param_y(PARAM_LINEAR_RESTITUTION, 0.5);
  704. set_param_y(PARAM_LINEAR_DAMPING, 1.0);
  705. set_param_y(PARAM_ANGULAR_LOWER_LIMIT, 0);
  706. set_param_y(PARAM_ANGULAR_UPPER_LIMIT, 0);
  707. set_param_y(PARAM_ANGULAR_LIMIT_SOFTNESS, 0.5f);
  708. set_param_y(PARAM_ANGULAR_DAMPING, 1.0f);
  709. set_param_y(PARAM_ANGULAR_RESTITUTION, 0);
  710. set_param_y(PARAM_ANGULAR_FORCE_LIMIT, 0);
  711. set_param_y(PARAM_ANGULAR_ERP, 0.5);
  712. set_param_y(PARAM_ANGULAR_MOTOR_TARGET_VELOCITY, 0);
  713. set_param_y(PARAM_ANGULAR_MOTOR_FORCE_LIMIT, 300);
  714. set_flag_y(FLAG_ENABLE_ANGULAR_LIMIT, true);
  715. set_flag_y(FLAG_ENABLE_LINEAR_LIMIT, true);
  716. set_flag_y(FLAG_ENABLE_MOTOR, false);
  717. set_param_z(PARAM_LINEAR_LOWER_LIMIT, 0);
  718. set_param_z(PARAM_LINEAR_UPPER_LIMIT, 0);
  719. set_param_z(PARAM_LINEAR_LIMIT_SOFTNESS, 0.7);
  720. set_param_z(PARAM_LINEAR_RESTITUTION, 0.5);
  721. set_param_z(PARAM_LINEAR_DAMPING, 1.0);
  722. set_param_z(PARAM_ANGULAR_LOWER_LIMIT, 0);
  723. set_param_z(PARAM_ANGULAR_UPPER_LIMIT, 0);
  724. set_param_z(PARAM_ANGULAR_LIMIT_SOFTNESS, 0.5f);
  725. set_param_z(PARAM_ANGULAR_DAMPING, 1.0f);
  726. set_param_z(PARAM_ANGULAR_RESTITUTION, 0);
  727. set_param_z(PARAM_ANGULAR_FORCE_LIMIT, 0);
  728. set_param_z(PARAM_ANGULAR_ERP, 0.5);
  729. set_param_z(PARAM_ANGULAR_MOTOR_TARGET_VELOCITY, 0);
  730. set_param_z(PARAM_ANGULAR_MOTOR_FORCE_LIMIT, 300);
  731. set_flag_z(FLAG_ENABLE_ANGULAR_LIMIT, true);
  732. set_flag_z(FLAG_ENABLE_LINEAR_LIMIT, true);
  733. set_flag_z(FLAG_ENABLE_MOTOR, false);
  734. }