physics_body.cpp 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734
  1. /*************************************************************************/
  2. /* physics_body.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_body.h"
  31. #include "core/core_string_names.h"
  32. #include "core/engine.h"
  33. #include "core/list.h"
  34. #include "core/method_bind_ext.gen.inc"
  35. #include "core/object.h"
  36. #include "core/rid.h"
  37. #include "scene/scene_string_names.h"
  38. #ifdef TOOLS_ENABLED
  39. #include "editor/plugins/spatial_editor_plugin.h"
  40. #endif
  41. void PhysicsBody::_notification(int p_what) {
  42. }
  43. Vector3 PhysicsBody::get_linear_velocity() const {
  44. return Vector3();
  45. }
  46. Vector3 PhysicsBody::get_angular_velocity() const {
  47. return Vector3();
  48. }
  49. float PhysicsBody::get_inverse_mass() const {
  50. return 0;
  51. }
  52. void PhysicsBody::set_collision_layer(uint32_t p_layer) {
  53. collision_layer = p_layer;
  54. PhysicsServer::get_singleton()->body_set_collision_layer(get_rid(), p_layer);
  55. }
  56. uint32_t PhysicsBody::get_collision_layer() const {
  57. return collision_layer;
  58. }
  59. void PhysicsBody::set_collision_mask(uint32_t p_mask) {
  60. collision_mask = p_mask;
  61. PhysicsServer::get_singleton()->body_set_collision_mask(get_rid(), p_mask);
  62. }
  63. uint32_t PhysicsBody::get_collision_mask() const {
  64. return collision_mask;
  65. }
  66. void PhysicsBody::set_collision_mask_bit(int p_bit, bool p_value) {
  67. uint32_t mask = get_collision_mask();
  68. if (p_value)
  69. mask |= 1 << p_bit;
  70. else
  71. mask &= ~(1 << p_bit);
  72. set_collision_mask(mask);
  73. }
  74. bool PhysicsBody::get_collision_mask_bit(int p_bit) const {
  75. return get_collision_mask() & (1 << p_bit);
  76. }
  77. void PhysicsBody::set_collision_layer_bit(int p_bit, bool p_value) {
  78. uint32_t mask = get_collision_layer();
  79. if (p_value)
  80. mask |= 1 << p_bit;
  81. else
  82. mask &= ~(1 << p_bit);
  83. set_collision_layer(mask);
  84. }
  85. bool PhysicsBody::get_collision_layer_bit(int p_bit) const {
  86. return get_collision_layer() & (1 << p_bit);
  87. }
  88. Array PhysicsBody::get_collision_exceptions() {
  89. List<RID> exceptions;
  90. PhysicsServer::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);
  91. Array ret;
  92. for (List<RID>::Element *E = exceptions.front(); E; E = E->next()) {
  93. RID body = E->get();
  94. ObjectID instance_id = PhysicsServer::get_singleton()->body_get_object_instance_id(body);
  95. Object *obj = ObjectDB::get_instance(instance_id);
  96. PhysicsBody *physics_body = Object::cast_to<PhysicsBody>(obj);
  97. ret.append(physics_body);
  98. }
  99. return ret;
  100. }
  101. void PhysicsBody::add_collision_exception_with(Node *p_node) {
  102. ERR_FAIL_NULL(p_node);
  103. CollisionObject *collision_object = Object::cast_to<CollisionObject>(p_node);
  104. if (!collision_object) {
  105. ERR_EXPLAIN("Collision exception only works between two CollisionObject");
  106. }
  107. ERR_FAIL_COND(!collision_object);
  108. PhysicsServer::get_singleton()->body_add_collision_exception(get_rid(), collision_object->get_rid());
  109. }
  110. void PhysicsBody::remove_collision_exception_with(Node *p_node) {
  111. ERR_FAIL_NULL(p_node);
  112. CollisionObject *collision_object = Object::cast_to<CollisionObject>(p_node);
  113. if (!collision_object) {
  114. ERR_EXPLAIN("Collision exception only works between two CollisionObject");
  115. }
  116. ERR_FAIL_COND(!collision_object);
  117. PhysicsServer::get_singleton()->body_remove_collision_exception(get_rid(), collision_object->get_rid());
  118. }
  119. void PhysicsBody::_set_layers(uint32_t p_mask) {
  120. set_collision_layer(p_mask);
  121. set_collision_mask(p_mask);
  122. }
  123. uint32_t PhysicsBody::_get_layers() const {
  124. return get_collision_layer();
  125. }
  126. void PhysicsBody::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &PhysicsBody::set_collision_layer);
  128. ClassDB::bind_method(D_METHOD("get_collision_layer"), &PhysicsBody::get_collision_layer);
  129. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &PhysicsBody::set_collision_mask);
  130. ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsBody::get_collision_mask);
  131. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &PhysicsBody::set_collision_mask_bit);
  132. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &PhysicsBody::get_collision_mask_bit);
  133. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &PhysicsBody::set_collision_layer_bit);
  134. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &PhysicsBody::get_collision_layer_bit);
  135. ClassDB::bind_method(D_METHOD("_set_layers", "mask"), &PhysicsBody::_set_layers);
  136. ClassDB::bind_method(D_METHOD("_get_layers"), &PhysicsBody::_get_layers);
  137. ADD_GROUP("Collision", "collision_");
  138. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  139. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  140. }
  141. PhysicsBody::PhysicsBody(PhysicsServer::BodyMode p_mode) :
  142. CollisionObject(PhysicsServer::get_singleton()->body_create(p_mode), false) {
  143. collision_layer = 1;
  144. collision_mask = 1;
  145. }
  146. #ifndef DISABLE_DEPRECATED
  147. void StaticBody::set_friction(real_t p_friction) {
  148. if (p_friction == 1.0) { // default value, don't create an override for that
  149. return;
  150. }
  151. ERR_EXPLAIN("The method set_friction has been deprecated and will be removed in the future, use physics material instead.")
  152. WARN_DEPRECATED
  153. ERR_FAIL_COND(p_friction < 0 || p_friction > 1);
  154. if (physics_material_override.is_null()) {
  155. physics_material_override.instance();
  156. set_physics_material_override(physics_material_override);
  157. }
  158. physics_material_override->set_friction(p_friction);
  159. }
  160. real_t StaticBody::get_friction() const {
  161. ERR_EXPLAIN("The method get_friction has been deprecated and will be removed in the future, use physics material instead.")
  162. WARN_DEPRECATED
  163. if (physics_material_override.is_null()) {
  164. return 1;
  165. }
  166. return physics_material_override->get_friction();
  167. }
  168. void StaticBody::set_bounce(real_t p_bounce) {
  169. if (p_bounce == 0.0) { // default value, don't create an override for that
  170. return;
  171. }
  172. ERR_EXPLAIN("The method set_bounce has been deprecated and will be removed in the future, use physics material instead.")
  173. WARN_DEPRECATED
  174. ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
  175. if (physics_material_override.is_null()) {
  176. physics_material_override.instance();
  177. set_physics_material_override(physics_material_override);
  178. }
  179. physics_material_override->set_bounce(p_bounce);
  180. }
  181. real_t StaticBody::get_bounce() const {
  182. ERR_EXPLAIN("The method get_bounce has been deprecated and will be removed in the future, use physics material instead.")
  183. WARN_DEPRECATED
  184. if (physics_material_override.is_null()) {
  185. return 0;
  186. }
  187. return physics_material_override->get_bounce();
  188. }
  189. #endif
  190. void StaticBody::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  191. if (physics_material_override.is_valid()) {
  192. if (physics_material_override->is_connected(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics"))
  193. physics_material_override->disconnect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
  194. }
  195. physics_material_override = p_physics_material_override;
  196. if (physics_material_override.is_valid()) {
  197. physics_material_override->connect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
  198. }
  199. _reload_physics_characteristics();
  200. }
  201. Ref<PhysicsMaterial> StaticBody::get_physics_material_override() const {
  202. return physics_material_override;
  203. }
  204. void StaticBody::set_constant_linear_velocity(const Vector3 &p_vel) {
  205. constant_linear_velocity = p_vel;
  206. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_LINEAR_VELOCITY, constant_linear_velocity);
  207. }
  208. void StaticBody::set_constant_angular_velocity(const Vector3 &p_vel) {
  209. constant_angular_velocity = p_vel;
  210. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_ANGULAR_VELOCITY, constant_angular_velocity);
  211. }
  212. Vector3 StaticBody::get_constant_linear_velocity() const {
  213. return constant_linear_velocity;
  214. }
  215. Vector3 StaticBody::get_constant_angular_velocity() const {
  216. return constant_angular_velocity;
  217. }
  218. void StaticBody::_bind_methods() {
  219. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "vel"), &StaticBody::set_constant_linear_velocity);
  220. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "vel"), &StaticBody::set_constant_angular_velocity);
  221. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity"), &StaticBody::get_constant_linear_velocity);
  222. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity"), &StaticBody::get_constant_angular_velocity);
  223. #ifndef DISABLE_DEPRECATED
  224. ClassDB::bind_method(D_METHOD("set_friction", "friction"), &StaticBody::set_friction);
  225. ClassDB::bind_method(D_METHOD("get_friction"), &StaticBody::get_friction);
  226. ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &StaticBody::set_bounce);
  227. ClassDB::bind_method(D_METHOD("get_bounce"), &StaticBody::get_bounce);
  228. #endif // DISABLE_DEPRECATED
  229. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &StaticBody::set_physics_material_override);
  230. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &StaticBody::get_physics_material_override);
  231. ClassDB::bind_method(D_METHOD("_reload_physics_characteristics"), &StaticBody::_reload_physics_characteristics);
  232. ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody::get_collision_exceptions);
  233. ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody::add_collision_exception_with);
  234. ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody::remove_collision_exception_with);
  235. #ifndef DISABLE_DEPRECATED
  236. ADD_PROPERTY(PropertyInfo(Variant::REAL, "friction", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_friction", "get_friction");
  237. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_bounce", "get_bounce");
  238. #endif // DISABLE_DEPRECATED
  239. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  240. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_linear_velocity"), "set_constant_linear_velocity", "get_constant_linear_velocity");
  241. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_angular_velocity"), "set_constant_angular_velocity", "get_constant_angular_velocity");
  242. }
  243. StaticBody::StaticBody() :
  244. PhysicsBody(PhysicsServer::BODY_MODE_STATIC) {
  245. }
  246. StaticBody::~StaticBody() {}
  247. void StaticBody::_reload_physics_characteristics() {
  248. if (physics_material_override.is_null()) {
  249. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_BOUNCE, 0);
  250. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_FRICTION, 1);
  251. } else {
  252. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  253. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  254. }
  255. }
  256. void RigidBody::_body_enter_tree(ObjectID p_id) {
  257. Object *obj = ObjectDB::get_instance(p_id);
  258. Node *node = Object::cast_to<Node>(obj);
  259. ERR_FAIL_COND(!node);
  260. ERR_FAIL_COND(!contact_monitor);
  261. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(p_id);
  262. ERR_FAIL_COND(!E);
  263. ERR_FAIL_COND(E->get().in_tree);
  264. E->get().in_tree = true;
  265. contact_monitor->locked = true;
  266. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  267. for (int i = 0; i < E->get().shapes.size(); i++) {
  268. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].local_shape);
  269. }
  270. contact_monitor->locked = false;
  271. }
  272. void RigidBody::_body_exit_tree(ObjectID p_id) {
  273. Object *obj = ObjectDB::get_instance(p_id);
  274. Node *node = Object::cast_to<Node>(obj);
  275. ERR_FAIL_COND(!node);
  276. ERR_FAIL_COND(!contact_monitor);
  277. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(p_id);
  278. ERR_FAIL_COND(!E);
  279. ERR_FAIL_COND(!E->get().in_tree);
  280. E->get().in_tree = false;
  281. contact_monitor->locked = true;
  282. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  283. for (int i = 0; i < E->get().shapes.size(); i++) {
  284. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].local_shape);
  285. }
  286. contact_monitor->locked = false;
  287. }
  288. void RigidBody::_body_inout(int p_status, ObjectID p_instance, int p_body_shape, int p_local_shape) {
  289. bool body_in = p_status == 1;
  290. ObjectID objid = p_instance;
  291. Object *obj = ObjectDB::get_instance(objid);
  292. Node *node = Object::cast_to<Node>(obj);
  293. ERR_FAIL_COND(!contact_monitor);
  294. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(objid);
  295. ERR_FAIL_COND(!body_in && !E);
  296. if (body_in) {
  297. if (!E) {
  298. E = contact_monitor->body_map.insert(objid, BodyState());
  299. //E->get().rc=0;
  300. E->get().in_tree = node && node->is_inside_tree();
  301. if (node) {
  302. node->connect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree, make_binds(objid));
  303. node->connect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree, make_binds(objid));
  304. if (E->get().in_tree) {
  305. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  306. }
  307. }
  308. }
  309. //E->get().rc++;
  310. if (node)
  311. E->get().shapes.insert(ShapePair(p_body_shape, p_local_shape));
  312. if (E->get().in_tree) {
  313. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, objid, node, p_body_shape, p_local_shape);
  314. }
  315. } else {
  316. //E->get().rc--;
  317. if (node)
  318. E->get().shapes.erase(ShapePair(p_body_shape, p_local_shape));
  319. bool in_tree = E->get().in_tree;
  320. if (E->get().shapes.empty()) {
  321. if (node) {
  322. node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree);
  323. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
  324. if (in_tree)
  325. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  326. }
  327. contact_monitor->body_map.erase(E);
  328. }
  329. if (node && in_tree) {
  330. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, objid, obj, p_body_shape, p_local_shape);
  331. }
  332. }
  333. }
  334. struct _RigidBodyInOut {
  335. ObjectID id;
  336. int shape;
  337. int local_shape;
  338. };
  339. void RigidBody::_direct_state_changed(Object *p_state) {
  340. #ifdef DEBUG_ENABLED
  341. state = Object::cast_to<PhysicsDirectBodyState>(p_state);
  342. #else
  343. state = (PhysicsDirectBodyState *)p_state; //trust it
  344. #endif
  345. set_ignore_transform_notification(true);
  346. set_global_transform(state->get_transform());
  347. linear_velocity = state->get_linear_velocity();
  348. angular_velocity = state->get_angular_velocity();
  349. if (sleeping != state->is_sleeping()) {
  350. sleeping = state->is_sleeping();
  351. emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed);
  352. }
  353. if (get_script_instance())
  354. get_script_instance()->call("_integrate_forces", state);
  355. set_ignore_transform_notification(false);
  356. if (contact_monitor) {
  357. contact_monitor->locked = true;
  358. //untag all
  359. int rc = 0;
  360. for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  361. for (int i = 0; i < E->get().shapes.size(); i++) {
  362. E->get().shapes[i].tagged = false;
  363. rc++;
  364. }
  365. }
  366. _RigidBodyInOut *toadd = (_RigidBodyInOut *)alloca(state->get_contact_count() * sizeof(_RigidBodyInOut));
  367. int toadd_count = 0; //state->get_contact_count();
  368. RigidBody_RemoveAction *toremove = (RigidBody_RemoveAction *)alloca(rc * sizeof(RigidBody_RemoveAction));
  369. int toremove_count = 0;
  370. //put the ones to add
  371. for (int i = 0; i < state->get_contact_count(); i++) {
  372. ObjectID obj = state->get_contact_collider_id(i);
  373. int local_shape = state->get_contact_local_shape(i);
  374. int shape = state->get_contact_collider_shape(i);
  375. //bool found=false;
  376. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(obj);
  377. if (!E) {
  378. toadd[toadd_count].local_shape = local_shape;
  379. toadd[toadd_count].id = obj;
  380. toadd[toadd_count].shape = shape;
  381. toadd_count++;
  382. continue;
  383. }
  384. ShapePair sp(shape, local_shape);
  385. int idx = E->get().shapes.find(sp);
  386. if (idx == -1) {
  387. toadd[toadd_count].local_shape = local_shape;
  388. toadd[toadd_count].id = obj;
  389. toadd[toadd_count].shape = shape;
  390. toadd_count++;
  391. continue;
  392. }
  393. E->get().shapes[idx].tagged = true;
  394. }
  395. //put the ones to remove
  396. for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  397. for (int i = 0; i < E->get().shapes.size(); i++) {
  398. if (!E->get().shapes[i].tagged) {
  399. toremove[toremove_count].body_id = E->key();
  400. toremove[toremove_count].pair = E->get().shapes[i];
  401. toremove_count++;
  402. }
  403. }
  404. }
  405. //process remotions
  406. for (int i = 0; i < toremove_count; i++) {
  407. _body_inout(0, toremove[i].body_id, toremove[i].pair.body_shape, toremove[i].pair.local_shape);
  408. }
  409. //process aditions
  410. for (int i = 0; i < toadd_count; i++) {
  411. _body_inout(1, toadd[i].id, toadd[i].shape, toadd[i].local_shape);
  412. }
  413. contact_monitor->locked = false;
  414. }
  415. state = NULL;
  416. }
  417. void RigidBody::_notification(int p_what) {
  418. #ifdef TOOLS_ENABLED
  419. if (p_what == NOTIFICATION_ENTER_TREE) {
  420. if (Engine::get_singleton()->is_editor_hint()) {
  421. set_notify_local_transform(true); //used for warnings and only in editor
  422. }
  423. }
  424. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  425. if (Engine::get_singleton()->is_editor_hint()) {
  426. update_configuration_warning();
  427. }
  428. }
  429. #endif
  430. }
  431. void RigidBody::set_mode(Mode p_mode) {
  432. mode = p_mode;
  433. switch (p_mode) {
  434. case MODE_RIGID: {
  435. PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_RIGID);
  436. } break;
  437. case MODE_STATIC: {
  438. PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_STATIC);
  439. } break;
  440. case MODE_CHARACTER: {
  441. PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_CHARACTER);
  442. } break;
  443. case MODE_KINEMATIC: {
  444. PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_KINEMATIC);
  445. } break;
  446. }
  447. }
  448. RigidBody::Mode RigidBody::get_mode() const {
  449. return mode;
  450. }
  451. void RigidBody::set_mass(real_t p_mass) {
  452. ERR_FAIL_COND(p_mass <= 0);
  453. mass = p_mass;
  454. _change_notify("mass");
  455. _change_notify("weight");
  456. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_MASS, mass);
  457. }
  458. real_t RigidBody::get_mass() const {
  459. return mass;
  460. }
  461. void RigidBody::set_weight(real_t p_weight) {
  462. set_mass(p_weight / real_t(GLOBAL_DEF("physics/3d/default_gravity", 9.8)));
  463. }
  464. real_t RigidBody::get_weight() const {
  465. return mass * real_t(GLOBAL_DEF("physics/3d/default_gravity", 9.8));
  466. }
  467. #ifndef DISABLE_DEPRECATED
  468. void RigidBody::set_friction(real_t p_friction) {
  469. if (p_friction == 1.0) { // default value, don't create an override for that
  470. return;
  471. }
  472. ERR_EXPLAIN("The method set_friction has been deprecated and will be removed in the future, use physics material instead.")
  473. WARN_DEPRECATED
  474. ERR_FAIL_COND(p_friction < 0 || p_friction > 1);
  475. if (physics_material_override.is_null()) {
  476. physics_material_override.instance();
  477. set_physics_material_override(physics_material_override);
  478. }
  479. physics_material_override->set_friction(p_friction);
  480. }
  481. real_t RigidBody::get_friction() const {
  482. ERR_EXPLAIN("The method get_friction has been deprecated and will be removed in the future, use physics material instead.")
  483. WARN_DEPRECATED
  484. if (physics_material_override.is_null()) {
  485. return 1;
  486. }
  487. return physics_material_override->get_friction();
  488. }
  489. void RigidBody::set_bounce(real_t p_bounce) {
  490. if (p_bounce == 0.0) { // default value, don't create an override for that
  491. return;
  492. }
  493. ERR_EXPLAIN("The method set_bounce has been deprecated and will be removed in the future, use physics material instead.")
  494. WARN_DEPRECATED
  495. ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
  496. if (physics_material_override.is_null()) {
  497. physics_material_override.instance();
  498. set_physics_material_override(physics_material_override);
  499. }
  500. physics_material_override->set_bounce(p_bounce);
  501. }
  502. real_t RigidBody::get_bounce() const {
  503. ERR_EXPLAIN("The method get_bounce has been deprecated and will be removed in the future, use physics material instead.")
  504. WARN_DEPRECATED
  505. if (physics_material_override.is_null()) {
  506. return 0;
  507. }
  508. return physics_material_override->get_bounce();
  509. }
  510. #endif // DISABLE_DEPRECATED
  511. void RigidBody::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  512. if (physics_material_override.is_valid()) {
  513. if (physics_material_override->is_connected(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics"))
  514. physics_material_override->disconnect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
  515. }
  516. physics_material_override = p_physics_material_override;
  517. if (physics_material_override.is_valid()) {
  518. physics_material_override->connect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
  519. }
  520. _reload_physics_characteristics();
  521. }
  522. Ref<PhysicsMaterial> RigidBody::get_physics_material_override() const {
  523. return physics_material_override;
  524. }
  525. void RigidBody::set_gravity_scale(real_t p_gravity_scale) {
  526. gravity_scale = p_gravity_scale;
  527. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
  528. }
  529. real_t RigidBody::get_gravity_scale() const {
  530. return gravity_scale;
  531. }
  532. void RigidBody::set_linear_damp(real_t p_linear_damp) {
  533. ERR_FAIL_COND(p_linear_damp < -1);
  534. linear_damp = p_linear_damp;
  535. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_LINEAR_DAMP, linear_damp);
  536. }
  537. real_t RigidBody::get_linear_damp() const {
  538. return linear_damp;
  539. }
  540. void RigidBody::set_angular_damp(real_t p_angular_damp) {
  541. ERR_FAIL_COND(p_angular_damp < -1);
  542. angular_damp = p_angular_damp;
  543. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_ANGULAR_DAMP, angular_damp);
  544. }
  545. real_t RigidBody::get_angular_damp() const {
  546. return angular_damp;
  547. }
  548. void RigidBody::set_axis_velocity(const Vector3 &p_axis) {
  549. Vector3 v = state ? state->get_linear_velocity() : linear_velocity;
  550. Vector3 axis = p_axis.normalized();
  551. v -= axis * axis.dot(v);
  552. v += p_axis;
  553. if (state) {
  554. set_linear_velocity(v);
  555. } else {
  556. PhysicsServer::get_singleton()->body_set_axis_velocity(get_rid(), p_axis);
  557. linear_velocity = v;
  558. }
  559. }
  560. void RigidBody::set_linear_velocity(const Vector3 &p_velocity) {
  561. linear_velocity = p_velocity;
  562. if (state)
  563. state->set_linear_velocity(linear_velocity);
  564. else
  565. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  566. }
  567. Vector3 RigidBody::get_linear_velocity() const {
  568. return linear_velocity;
  569. }
  570. void RigidBody::set_angular_velocity(const Vector3 &p_velocity) {
  571. angular_velocity = p_velocity;
  572. if (state)
  573. state->set_angular_velocity(angular_velocity);
  574. else
  575. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
  576. }
  577. Vector3 RigidBody::get_angular_velocity() const {
  578. return angular_velocity;
  579. }
  580. void RigidBody::set_use_custom_integrator(bool p_enable) {
  581. if (custom_integrator == p_enable)
  582. return;
  583. custom_integrator = p_enable;
  584. PhysicsServer::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
  585. }
  586. bool RigidBody::is_using_custom_integrator() {
  587. return custom_integrator;
  588. }
  589. void RigidBody::set_sleeping(bool p_sleeping) {
  590. sleeping = p_sleeping;
  591. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_SLEEPING, sleeping);
  592. }
  593. void RigidBody::set_can_sleep(bool p_active) {
  594. can_sleep = p_active;
  595. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_CAN_SLEEP, p_active);
  596. }
  597. bool RigidBody::is_able_to_sleep() const {
  598. return can_sleep;
  599. }
  600. bool RigidBody::is_sleeping() const {
  601. return sleeping;
  602. }
  603. void RigidBody::set_max_contacts_reported(int p_amount) {
  604. max_contacts_reported = p_amount;
  605. PhysicsServer::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount);
  606. }
  607. int RigidBody::get_max_contacts_reported() const {
  608. return max_contacts_reported;
  609. }
  610. void RigidBody::add_central_force(const Vector3 &p_force) {
  611. PhysicsServer::get_singleton()->body_add_central_force(get_rid(), p_force);
  612. }
  613. void RigidBody::add_force(const Vector3 &p_force, const Vector3 &p_pos) {
  614. PhysicsServer::get_singleton()->body_add_force(get_rid(), p_force, p_pos);
  615. }
  616. void RigidBody::add_torque(const Vector3 &p_torque) {
  617. PhysicsServer::get_singleton()->body_add_torque(get_rid(), p_torque);
  618. }
  619. void RigidBody::apply_central_impulse(const Vector3 &p_impulse) {
  620. PhysicsServer::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
  621. }
  622. void RigidBody::apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse) {
  623. PhysicsServer::get_singleton()->body_apply_impulse(get_rid(), p_pos, p_impulse);
  624. }
  625. void RigidBody::apply_torque_impulse(const Vector3 &p_impulse) {
  626. PhysicsServer::get_singleton()->body_apply_torque_impulse(get_rid(), p_impulse);
  627. }
  628. void RigidBody::set_use_continuous_collision_detection(bool p_enable) {
  629. ccd = p_enable;
  630. PhysicsServer::get_singleton()->body_set_enable_continuous_collision_detection(get_rid(), p_enable);
  631. }
  632. bool RigidBody::is_using_continuous_collision_detection() const {
  633. return ccd;
  634. }
  635. void RigidBody::set_contact_monitor(bool p_enabled) {
  636. if (p_enabled == is_contact_monitor_enabled())
  637. return;
  638. if (!p_enabled) {
  639. if (contact_monitor->locked) {
  640. ERR_EXPLAIN("Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\",false) instead");
  641. }
  642. ERR_FAIL_COND(contact_monitor->locked);
  643. for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  644. //clean up mess
  645. Object *obj = ObjectDB::get_instance(E->key());
  646. Node *node = Object::cast_to<Node>(obj);
  647. if (node) {
  648. node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree);
  649. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
  650. }
  651. }
  652. memdelete(contact_monitor);
  653. contact_monitor = NULL;
  654. } else {
  655. contact_monitor = memnew(ContactMonitor);
  656. contact_monitor->locked = false;
  657. }
  658. }
  659. bool RigidBody::is_contact_monitor_enabled() const {
  660. return contact_monitor != NULL;
  661. }
  662. void RigidBody::set_axis_lock(PhysicsServer::BodyAxis p_axis, bool p_lock) {
  663. PhysicsServer::get_singleton()->body_set_axis_lock(get_rid(), p_axis, p_lock);
  664. }
  665. bool RigidBody::get_axis_lock(PhysicsServer::BodyAxis p_axis) const {
  666. return PhysicsServer::get_singleton()->body_is_axis_locked(get_rid(), p_axis);
  667. }
  668. Array RigidBody::get_colliding_bodies() const {
  669. ERR_FAIL_COND_V(!contact_monitor, Array());
  670. Array ret;
  671. ret.resize(contact_monitor->body_map.size());
  672. int idx = 0;
  673. for (const Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  674. Object *obj = ObjectDB::get_instance(E->key());
  675. if (!obj) {
  676. ret.resize(ret.size() - 1); //ops
  677. } else {
  678. ret[idx++] = obj;
  679. }
  680. }
  681. return ret;
  682. }
  683. String RigidBody::get_configuration_warning() const {
  684. Transform t = get_transform();
  685. String warning = CollisionObject::get_configuration_warning();
  686. if ((get_mode() == MODE_RIGID || get_mode() == MODE_CHARACTER) && (ABS(t.basis.get_axis(0).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(1).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(2).length() - 1.0) > 0.05)) {
  687. if (warning != String()) {
  688. warning += "\n";
  689. }
  690. warning += TTR("Size changes to RigidBody (in character or rigid modes) will be overridden by the physics engine when running.\nChange the size in children collision shapes instead.");
  691. }
  692. return warning;
  693. }
  694. void RigidBody::_bind_methods() {
  695. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &RigidBody::set_mode);
  696. ClassDB::bind_method(D_METHOD("get_mode"), &RigidBody::get_mode);
  697. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &RigidBody::set_mass);
  698. ClassDB::bind_method(D_METHOD("get_mass"), &RigidBody::get_mass);
  699. ClassDB::bind_method(D_METHOD("set_weight", "weight"), &RigidBody::set_weight);
  700. ClassDB::bind_method(D_METHOD("get_weight"), &RigidBody::get_weight);
  701. #ifndef DISABLE_DEPRECATED
  702. ClassDB::bind_method(D_METHOD("set_friction", "friction"), &RigidBody::set_friction);
  703. ClassDB::bind_method(D_METHOD("get_friction"), &RigidBody::get_friction);
  704. ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &RigidBody::set_bounce);
  705. ClassDB::bind_method(D_METHOD("get_bounce"), &RigidBody::get_bounce);
  706. #endif // DISABLE_DEPRECATED
  707. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &RigidBody::set_physics_material_override);
  708. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &RigidBody::get_physics_material_override);
  709. ClassDB::bind_method(D_METHOD("_reload_physics_characteristics"), &RigidBody::_reload_physics_characteristics);
  710. ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &RigidBody::set_linear_velocity);
  711. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &RigidBody::get_linear_velocity);
  712. ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &RigidBody::set_angular_velocity);
  713. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &RigidBody::get_angular_velocity);
  714. ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &RigidBody::set_gravity_scale);
  715. ClassDB::bind_method(D_METHOD("get_gravity_scale"), &RigidBody::get_gravity_scale);
  716. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &RigidBody::set_linear_damp);
  717. ClassDB::bind_method(D_METHOD("get_linear_damp"), &RigidBody::get_linear_damp);
  718. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &RigidBody::set_angular_damp);
  719. ClassDB::bind_method(D_METHOD("get_angular_damp"), &RigidBody::get_angular_damp);
  720. ClassDB::bind_method(D_METHOD("set_max_contacts_reported", "amount"), &RigidBody::set_max_contacts_reported);
  721. ClassDB::bind_method(D_METHOD("get_max_contacts_reported"), &RigidBody::get_max_contacts_reported);
  722. ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &RigidBody::set_use_custom_integrator);
  723. ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &RigidBody::is_using_custom_integrator);
  724. ClassDB::bind_method(D_METHOD("set_contact_monitor", "enabled"), &RigidBody::set_contact_monitor);
  725. ClassDB::bind_method(D_METHOD("is_contact_monitor_enabled"), &RigidBody::is_contact_monitor_enabled);
  726. ClassDB::bind_method(D_METHOD("set_use_continuous_collision_detection", "enable"), &RigidBody::set_use_continuous_collision_detection);
  727. ClassDB::bind_method(D_METHOD("is_using_continuous_collision_detection"), &RigidBody::is_using_continuous_collision_detection);
  728. ClassDB::bind_method(D_METHOD("set_axis_velocity", "axis_velocity"), &RigidBody::set_axis_velocity);
  729. ClassDB::bind_method(D_METHOD("add_central_force", "force"), &RigidBody::add_central_force);
  730. ClassDB::bind_method(D_METHOD("add_force", "force", "position"), &RigidBody::add_force);
  731. ClassDB::bind_method(D_METHOD("add_torque", "torque"), &RigidBody::add_torque);
  732. ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &RigidBody::apply_central_impulse);
  733. ClassDB::bind_method(D_METHOD("apply_impulse", "position", "impulse"), &RigidBody::apply_impulse);
  734. ClassDB::bind_method(D_METHOD("apply_torque_impulse", "impulse"), &RigidBody::apply_torque_impulse);
  735. ClassDB::bind_method(D_METHOD("set_sleeping", "sleeping"), &RigidBody::set_sleeping);
  736. ClassDB::bind_method(D_METHOD("is_sleeping"), &RigidBody::is_sleeping);
  737. ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &RigidBody::set_can_sleep);
  738. ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &RigidBody::is_able_to_sleep);
  739. ClassDB::bind_method(D_METHOD("_direct_state_changed"), &RigidBody::_direct_state_changed);
  740. ClassDB::bind_method(D_METHOD("_body_enter_tree"), &RigidBody::_body_enter_tree);
  741. ClassDB::bind_method(D_METHOD("_body_exit_tree"), &RigidBody::_body_exit_tree);
  742. ClassDB::bind_method(D_METHOD("set_axis_lock", "axis", "lock"), &RigidBody::set_axis_lock);
  743. ClassDB::bind_method(D_METHOD("get_axis_lock", "axis"), &RigidBody::get_axis_lock);
  744. ClassDB::bind_method(D_METHOD("get_colliding_bodies"), &RigidBody::get_colliding_bodies);
  745. BIND_VMETHOD(MethodInfo("_integrate_forces", PropertyInfo(Variant::OBJECT, "state", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsDirectBodyState")));
  746. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Rigid,Static,Character,Kinematic"), "set_mode", "get_mode");
  747. ADD_PROPERTY(PropertyInfo(Variant::REAL, "mass", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01"), "set_mass", "get_mass");
  748. ADD_PROPERTY(PropertyInfo(Variant::REAL, "weight", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01", PROPERTY_USAGE_EDITOR), "set_weight", "get_weight");
  749. #ifndef DISABLE_DEPRECATED
  750. ADD_PROPERTY(PropertyInfo(Variant::REAL, "friction", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_friction", "get_friction");
  751. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_bounce", "get_bounce");
  752. #endif // DISABLE_DEPRECATED
  753. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  754. ADD_PROPERTY(PropertyInfo(Variant::REAL, "gravity_scale", PROPERTY_HINT_RANGE, "-128,128,0.01"), "set_gravity_scale", "get_gravity_scale");
  755. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
  756. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "continuous_cd"), "set_use_continuous_collision_detection", "is_using_continuous_collision_detection");
  757. ADD_PROPERTY(PropertyInfo(Variant::INT, "contacts_reported"), "set_max_contacts_reported", "get_max_contacts_reported");
  758. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor"), "set_contact_monitor", "is_contact_monitor_enabled");
  759. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleeping", "is_sleeping");
  760. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
  761. ADD_GROUP("Axis Lock", "axis_lock_");
  762. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_x"), "set_axis_lock", "get_axis_lock", PhysicsServer::BODY_AXIS_LINEAR_X);
  763. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_y"), "set_axis_lock", "get_axis_lock", PhysicsServer::BODY_AXIS_LINEAR_Y);
  764. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_z"), "set_axis_lock", "get_axis_lock", PhysicsServer::BODY_AXIS_LINEAR_Z);
  765. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_x"), "set_axis_lock", "get_axis_lock", PhysicsServer::BODY_AXIS_ANGULAR_X);
  766. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_y"), "set_axis_lock", "get_axis_lock", PhysicsServer::BODY_AXIS_ANGULAR_Y);
  767. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_z"), "set_axis_lock", "get_axis_lock", PhysicsServer::BODY_AXIS_ANGULAR_Z);
  768. ADD_GROUP("Linear", "linear_");
  769. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
  770. ADD_PROPERTY(PropertyInfo(Variant::REAL, "linear_damp", PROPERTY_HINT_RANGE, "-1,128,0.01"), "set_linear_damp", "get_linear_damp");
  771. ADD_GROUP("Angular", "angular_");
  772. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
  773. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_damp", PROPERTY_HINT_RANGE, "-1,128,0.01"), "set_angular_damp", "get_angular_damp");
  774. ADD_SIGNAL(MethodInfo("body_shape_entered", PropertyInfo(Variant::INT, "body_id"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::INT, "body_shape"), PropertyInfo(Variant::INT, "local_shape")));
  775. ADD_SIGNAL(MethodInfo("body_shape_exited", PropertyInfo(Variant::INT, "body_id"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::INT, "body_shape"), PropertyInfo(Variant::INT, "local_shape")));
  776. ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  777. ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  778. ADD_SIGNAL(MethodInfo("sleeping_state_changed"));
  779. BIND_ENUM_CONSTANT(MODE_RIGID);
  780. BIND_ENUM_CONSTANT(MODE_STATIC);
  781. BIND_ENUM_CONSTANT(MODE_CHARACTER);
  782. BIND_ENUM_CONSTANT(MODE_KINEMATIC);
  783. }
  784. RigidBody::RigidBody() :
  785. PhysicsBody(PhysicsServer::BODY_MODE_RIGID) {
  786. mode = MODE_RIGID;
  787. mass = 1;
  788. max_contacts_reported = 0;
  789. state = NULL;
  790. gravity_scale = 1;
  791. linear_damp = -1;
  792. angular_damp = -1;
  793. //angular_velocity=0;
  794. sleeping = false;
  795. ccd = false;
  796. custom_integrator = false;
  797. contact_monitor = NULL;
  798. can_sleep = true;
  799. PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(), this, "_direct_state_changed");
  800. }
  801. RigidBody::~RigidBody() {
  802. if (contact_monitor)
  803. memdelete(contact_monitor);
  804. }
  805. void RigidBody::_reload_physics_characteristics() {
  806. if (physics_material_override.is_null()) {
  807. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_BOUNCE, 0);
  808. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_FRICTION, 1);
  809. } else {
  810. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  811. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  812. }
  813. }
  814. //////////////////////////////////////////////////////
  815. //////////////////////////
  816. Ref<KinematicCollision> KinematicBody::_move(const Vector3 &p_motion, bool p_infinite_inertia, bool p_test_only) {
  817. Collision col;
  818. if (move_and_collide(p_motion, p_infinite_inertia, col, p_test_only)) {
  819. if (motion_cache.is_null()) {
  820. motion_cache.instance();
  821. motion_cache->owner = this;
  822. }
  823. motion_cache->collision = col;
  824. return motion_cache;
  825. }
  826. return Ref<KinematicCollision>();
  827. }
  828. bool KinematicBody::move_and_collide(const Vector3 &p_motion, bool p_infinite_inertia, Collision &r_collision, bool p_test_only) {
  829. Transform gt = get_global_transform();
  830. PhysicsServer::MotionResult result;
  831. bool colliding = PhysicsServer::get_singleton()->body_test_motion(get_rid(), gt, p_motion, p_infinite_inertia, &result);
  832. if (colliding) {
  833. r_collision.collider_metadata = result.collider_metadata;
  834. r_collision.collider_shape = result.collider_shape;
  835. r_collision.collider_vel = result.collider_velocity;
  836. r_collision.collision = result.collision_point;
  837. r_collision.normal = result.collision_normal;
  838. r_collision.collider = result.collider_id;
  839. r_collision.collider_rid = result.collider;
  840. r_collision.travel = result.motion;
  841. r_collision.remainder = result.remainder;
  842. r_collision.local_shape = result.collision_local_shape;
  843. }
  844. for (int i = 0; i < 3; i++) {
  845. if (locked_axis & (1 << i)) {
  846. result.motion[i] = 0;
  847. }
  848. }
  849. if (!p_test_only) {
  850. gt.origin += result.motion;
  851. set_global_transform(gt);
  852. }
  853. return colliding;
  854. }
  855. //so, if you pass 45 as limit, avoid numerical precision erros when angle is 45.
  856. #define FLOOR_ANGLE_THRESHOLD 0.01
  857. Vector3 KinematicBody::move_and_slide(const Vector3 &p_linear_velocity, const Vector3 &p_floor_direction, bool p_stop_on_slope, int p_max_slides, float p_floor_max_angle, bool p_infinite_inertia) {
  858. Vector3 lv = p_linear_velocity;
  859. for (int i = 0; i < 3; i++) {
  860. if (locked_axis & (1 << i)) {
  861. lv[i] = 0;
  862. }
  863. }
  864. // Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky
  865. Vector3 motion = (floor_velocity + lv) * (Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time());
  866. on_floor = false;
  867. on_ceiling = false;
  868. on_wall = false;
  869. colliders.clear();
  870. floor_velocity = Vector3();
  871. Vector3 lv_n = p_linear_velocity.normalized();
  872. while (p_max_slides) {
  873. Collision collision;
  874. bool found_collision = false;
  875. int test_type = 0;
  876. do {
  877. bool collided;
  878. if (test_type == 0) { //collide
  879. collided = move_and_collide(motion, p_infinite_inertia, collision);
  880. if (!collided) {
  881. motion = Vector3(); //clear because no collision happened and motion completed
  882. }
  883. } else {
  884. collided = separate_raycast_shapes(p_infinite_inertia, collision);
  885. if (collided) {
  886. collision.remainder = motion; //keep
  887. collision.travel = Vector3();
  888. }
  889. }
  890. if (collided) {
  891. found_collision = true;
  892. }
  893. if (collided) {
  894. colliders.push_back(collision);
  895. motion = collision.remainder;
  896. bool is_on_slope = false;
  897. if (p_floor_direction == Vector3()) {
  898. //all is a wall
  899. on_wall = true;
  900. } else {
  901. if (collision.normal.dot(p_floor_direction) >= Math::cos(p_floor_max_angle + FLOOR_ANGLE_THRESHOLD)) { //floor
  902. on_floor = true;
  903. on_floor_body = collision.collider_rid;
  904. floor_velocity = collision.collider_vel;
  905. if (p_stop_on_slope) {
  906. if (Vector3() == lv_n + p_floor_direction) {
  907. Transform gt = get_global_transform();
  908. gt.origin -= collision.travel;
  909. set_global_transform(gt);
  910. return Vector3();
  911. }
  912. }
  913. is_on_slope = true;
  914. } else if (collision.normal.dot(-p_floor_direction) >= Math::cos(p_floor_max_angle + FLOOR_ANGLE_THRESHOLD)) { //ceiling
  915. on_ceiling = true;
  916. } else {
  917. on_wall = true;
  918. }
  919. }
  920. if (p_stop_on_slope && is_on_slope) {
  921. motion = motion.slide(p_floor_direction);
  922. lv = lv.slide(p_floor_direction);
  923. } else {
  924. Vector3 n = collision.normal;
  925. motion = motion.slide(n);
  926. lv = lv.slide(n);
  927. }
  928. for (int i = 0; i < 3; i++) {
  929. if (locked_axis & (1 << i)) {
  930. lv[i] = 0;
  931. }
  932. }
  933. }
  934. ++test_type;
  935. } while (!p_stop_on_slope && test_type < 2);
  936. if (!found_collision || motion == Vector3())
  937. break;
  938. --p_max_slides;
  939. }
  940. return lv;
  941. }
  942. Vector3 KinematicBody::move_and_slide_with_snap(const Vector3 &p_linear_velocity, const Vector3 &p_snap, const Vector3 &p_floor_direction, bool p_stop_on_slope, int p_max_slides, float p_floor_max_angle, bool p_infinite_inertia) {
  943. bool was_on_floor = on_floor;
  944. Vector3 ret = move_and_slide(p_linear_velocity, p_floor_direction, p_stop_on_slope, p_max_slides, p_floor_max_angle, p_infinite_inertia);
  945. if (!was_on_floor || p_snap == Vector3()) {
  946. return ret;
  947. }
  948. Collision col;
  949. Transform gt = get_global_transform();
  950. if (move_and_collide(p_snap, p_infinite_inertia, col, true)) {
  951. gt.origin += col.travel;
  952. if (p_floor_direction != Vector3() && Math::acos(p_floor_direction.normalized().dot(col.normal)) < p_floor_max_angle) {
  953. on_floor = true;
  954. on_floor_body = col.collider_rid;
  955. floor_velocity = col.collider_vel;
  956. }
  957. set_global_transform(gt);
  958. }
  959. return ret;
  960. }
  961. bool KinematicBody::is_on_floor() const {
  962. return on_floor;
  963. }
  964. bool KinematicBody::is_on_wall() const {
  965. return on_wall;
  966. }
  967. bool KinematicBody::is_on_ceiling() const {
  968. return on_ceiling;
  969. }
  970. Vector3 KinematicBody::get_floor_velocity() const {
  971. return floor_velocity;
  972. }
  973. bool KinematicBody::test_move(const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia) {
  974. ERR_FAIL_COND_V(!is_inside_tree(), false);
  975. return PhysicsServer::get_singleton()->body_test_motion(get_rid(), p_from, p_motion, p_infinite_inertia);
  976. }
  977. bool KinematicBody::separate_raycast_shapes(bool p_infinite_inertia, Collision &r_collision) {
  978. PhysicsServer::SeparationResult sep_res[8]; //max 8 rays
  979. Transform gt = get_global_transform();
  980. Vector3 recover;
  981. int hits = PhysicsServer::get_singleton()->body_test_ray_separation(get_rid(), gt, p_infinite_inertia, recover, sep_res, 8, margin);
  982. int deepest = -1;
  983. float deepest_depth;
  984. for (int i = 0; i < hits; i++) {
  985. if (deepest == -1 || sep_res[i].collision_depth > deepest_depth) {
  986. deepest = i;
  987. deepest_depth = sep_res[i].collision_depth;
  988. }
  989. }
  990. gt.origin += recover;
  991. set_global_transform(gt);
  992. if (deepest != -1) {
  993. r_collision.collider = sep_res[deepest].collider_id;
  994. r_collision.collider_metadata = sep_res[deepest].collider_metadata;
  995. r_collision.collider_shape = sep_res[deepest].collider_shape;
  996. r_collision.collider_vel = sep_res[deepest].collider_velocity;
  997. r_collision.collision = sep_res[deepest].collision_point;
  998. r_collision.normal = sep_res[deepest].collision_normal;
  999. r_collision.local_shape = sep_res[deepest].collision_local_shape;
  1000. r_collision.travel = recover;
  1001. r_collision.remainder = Vector3();
  1002. return true;
  1003. } else {
  1004. return false;
  1005. }
  1006. }
  1007. void KinematicBody::set_axis_lock(PhysicsServer::BodyAxis p_axis, bool p_lock) {
  1008. PhysicsServer::get_singleton()->body_set_axis_lock(get_rid(), p_axis, p_lock);
  1009. }
  1010. bool KinematicBody::get_axis_lock(PhysicsServer::BodyAxis p_axis) const {
  1011. return PhysicsServer::get_singleton()->body_is_axis_locked(get_rid(), p_axis);
  1012. }
  1013. void KinematicBody::set_safe_margin(float p_margin) {
  1014. margin = p_margin;
  1015. PhysicsServer::get_singleton()->body_set_kinematic_safe_margin(get_rid(), margin);
  1016. }
  1017. float KinematicBody::get_safe_margin() const {
  1018. return margin;
  1019. }
  1020. int KinematicBody::get_slide_count() const {
  1021. return colliders.size();
  1022. }
  1023. KinematicBody::Collision KinematicBody::get_slide_collision(int p_bounce) const {
  1024. ERR_FAIL_INDEX_V(p_bounce, colliders.size(), Collision());
  1025. return colliders[p_bounce];
  1026. }
  1027. Ref<KinematicCollision> KinematicBody::_get_slide_collision(int p_bounce) {
  1028. ERR_FAIL_INDEX_V(p_bounce, colliders.size(), Ref<KinematicCollision>());
  1029. if (p_bounce >= slide_colliders.size()) {
  1030. slide_colliders.resize(p_bounce + 1);
  1031. }
  1032. if (slide_colliders[p_bounce].is_null()) {
  1033. slide_colliders.write[p_bounce].instance();
  1034. slide_colliders.write[p_bounce]->owner = this;
  1035. }
  1036. slide_colliders.write[p_bounce]->collision = colliders[p_bounce];
  1037. return slide_colliders[p_bounce];
  1038. }
  1039. void KinematicBody::_bind_methods() {
  1040. ClassDB::bind_method(D_METHOD("move_and_collide", "rel_vec", "infinite_inertia", "test_only"), &KinematicBody::_move, DEFVAL(true), DEFVAL(false));
  1041. ClassDB::bind_method(D_METHOD("move_and_slide", "linear_velocity", "floor_normal", "stop_on_slope", "max_slides", "floor_max_angle", "infinite_inertia"), &KinematicBody::move_and_slide, DEFVAL(Vector3(0, 0, 0)), DEFVAL(false), DEFVAL(4), DEFVAL(Math::deg2rad((float)45)), DEFVAL(true));
  1042. ClassDB::bind_method(D_METHOD("move_and_slide_with_snap", "linear_velocity", "snap", "floor_normal", "stop_on_slope", "max_slides", "floor_max_angle", "infinite_inertia"), &KinematicBody::move_and_slide_with_snap, DEFVAL(Vector3(0, 0, 0)), DEFVAL(false), DEFVAL(4), DEFVAL(Math::deg2rad((float)45)), DEFVAL(true));
  1043. ClassDB::bind_method(D_METHOD("test_move", "from", "rel_vec", "infinite_inertia"), &KinematicBody::test_move, DEFVAL(true));
  1044. ClassDB::bind_method(D_METHOD("is_on_floor"), &KinematicBody::is_on_floor);
  1045. ClassDB::bind_method(D_METHOD("is_on_ceiling"), &KinematicBody::is_on_ceiling);
  1046. ClassDB::bind_method(D_METHOD("is_on_wall"), &KinematicBody::is_on_wall);
  1047. ClassDB::bind_method(D_METHOD("get_floor_velocity"), &KinematicBody::get_floor_velocity);
  1048. ClassDB::bind_method(D_METHOD("set_axis_lock", "axis", "lock"), &KinematicBody::set_axis_lock);
  1049. ClassDB::bind_method(D_METHOD("get_axis_lock", "axis"), &KinematicBody::get_axis_lock);
  1050. ClassDB::bind_method(D_METHOD("set_safe_margin", "pixels"), &KinematicBody::set_safe_margin);
  1051. ClassDB::bind_method(D_METHOD("get_safe_margin"), &KinematicBody::get_safe_margin);
  1052. ClassDB::bind_method(D_METHOD("get_slide_count"), &KinematicBody::get_slide_count);
  1053. ClassDB::bind_method(D_METHOD("get_slide_collision", "slide_idx"), &KinematicBody::_get_slide_collision);
  1054. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "move_lock_x", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_axis_lock", "get_axis_lock", PhysicsServer::BODY_AXIS_LINEAR_X);
  1055. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "move_lock_y", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_axis_lock", "get_axis_lock", PhysicsServer::BODY_AXIS_LINEAR_Y);
  1056. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "move_lock_z", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_axis_lock", "get_axis_lock", PhysicsServer::BODY_AXIS_LINEAR_Z);
  1057. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision/safe_margin", PROPERTY_HINT_RANGE, "0.001,256,0.001"), "set_safe_margin", "get_safe_margin");
  1058. }
  1059. KinematicBody::KinematicBody() :
  1060. PhysicsBody(PhysicsServer::BODY_MODE_KINEMATIC) {
  1061. margin = 0.001;
  1062. locked_axis = 0;
  1063. on_floor = false;
  1064. on_ceiling = false;
  1065. on_wall = false;
  1066. }
  1067. KinematicBody::~KinematicBody() {
  1068. if (motion_cache.is_valid()) {
  1069. motion_cache->owner = NULL;
  1070. }
  1071. for (int i = 0; i < slide_colliders.size(); i++) {
  1072. if (slide_colliders[i].is_valid()) {
  1073. slide_colliders.write[i]->owner = NULL;
  1074. }
  1075. }
  1076. }
  1077. ///////////////////////////////////////
  1078. Vector3 KinematicCollision::get_position() const {
  1079. return collision.collision;
  1080. }
  1081. Vector3 KinematicCollision::get_normal() const {
  1082. return collision.normal;
  1083. }
  1084. Vector3 KinematicCollision::get_travel() const {
  1085. return collision.travel;
  1086. }
  1087. Vector3 KinematicCollision::get_remainder() const {
  1088. return collision.remainder;
  1089. }
  1090. Object *KinematicCollision::get_local_shape() const {
  1091. ERR_FAIL_COND_V(!owner, NULL);
  1092. uint32_t ownerid = owner->shape_find_owner(collision.local_shape);
  1093. return owner->shape_owner_get_owner(ownerid);
  1094. }
  1095. Object *KinematicCollision::get_collider() const {
  1096. if (collision.collider) {
  1097. return ObjectDB::get_instance(collision.collider);
  1098. }
  1099. return NULL;
  1100. }
  1101. ObjectID KinematicCollision::get_collider_id() const {
  1102. return collision.collider;
  1103. }
  1104. Object *KinematicCollision::get_collider_shape() const {
  1105. Object *collider = get_collider();
  1106. if (collider) {
  1107. CollisionObject *obj2d = Object::cast_to<CollisionObject>(collider);
  1108. if (obj2d) {
  1109. uint32_t ownerid = obj2d->shape_find_owner(collision.collider_shape);
  1110. return obj2d->shape_owner_get_owner(ownerid);
  1111. }
  1112. }
  1113. return NULL;
  1114. }
  1115. int KinematicCollision::get_collider_shape_index() const {
  1116. return collision.collider_shape;
  1117. }
  1118. Vector3 KinematicCollision::get_collider_velocity() const {
  1119. return collision.collider_vel;
  1120. }
  1121. Variant KinematicCollision::get_collider_metadata() const {
  1122. return Variant();
  1123. }
  1124. void KinematicCollision::_bind_methods() {
  1125. ClassDB::bind_method(D_METHOD("get_position"), &KinematicCollision::get_position);
  1126. ClassDB::bind_method(D_METHOD("get_normal"), &KinematicCollision::get_normal);
  1127. ClassDB::bind_method(D_METHOD("get_travel"), &KinematicCollision::get_travel);
  1128. ClassDB::bind_method(D_METHOD("get_remainder"), &KinematicCollision::get_remainder);
  1129. ClassDB::bind_method(D_METHOD("get_local_shape"), &KinematicCollision::get_local_shape);
  1130. ClassDB::bind_method(D_METHOD("get_collider"), &KinematicCollision::get_collider);
  1131. ClassDB::bind_method(D_METHOD("get_collider_id"), &KinematicCollision::get_collider_id);
  1132. ClassDB::bind_method(D_METHOD("get_collider_shape"), &KinematicCollision::get_collider_shape);
  1133. ClassDB::bind_method(D_METHOD("get_collider_shape_index"), &KinematicCollision::get_collider_shape_index);
  1134. ClassDB::bind_method(D_METHOD("get_collider_velocity"), &KinematicCollision::get_collider_velocity);
  1135. ClassDB::bind_method(D_METHOD("get_collider_metadata"), &KinematicCollision::get_collider_metadata);
  1136. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position"), "", "get_position");
  1137. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "normal"), "", "get_normal");
  1138. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "travel"), "", "get_travel");
  1139. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "remainder"), "", "get_remainder");
  1140. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "local_shape"), "", "get_local_shape");
  1141. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider"), "", "get_collider");
  1142. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_id"), "", "get_collider_id");
  1143. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider_shape"), "", "get_collider_shape");
  1144. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_shape_index"), "", "get_collider_shape_index");
  1145. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "collider_velocity"), "", "get_collider_velocity");
  1146. ADD_PROPERTY(PropertyInfo(Variant::NIL, "collider_metadata", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "", "get_collider_metadata");
  1147. }
  1148. KinematicCollision::KinematicCollision() {
  1149. collision.collider = 0;
  1150. collision.collider_shape = 0;
  1151. collision.local_shape = 0;
  1152. owner = NULL;
  1153. }
  1154. ///////////////////////////////////////
  1155. bool PhysicalBone::JointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1156. return false;
  1157. }
  1158. bool PhysicalBone::JointData::_get(const StringName &p_name, Variant &r_ret) const {
  1159. return false;
  1160. }
  1161. void PhysicalBone::JointData::_get_property_list(List<PropertyInfo> *p_list) const {
  1162. }
  1163. bool PhysicalBone::PinJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1164. if (JointData::_set(p_name, p_value, j)) {
  1165. return true;
  1166. }
  1167. if ("joint_constraints/bias" == p_name) {
  1168. bias = p_value;
  1169. if (j.is_valid())
  1170. PhysicsServer::get_singleton()->pin_joint_set_param(j, PhysicsServer::PIN_JOINT_BIAS, bias);
  1171. } else if ("joint_constraints/damping" == p_name) {
  1172. damping = p_value;
  1173. if (j.is_valid())
  1174. PhysicsServer::get_singleton()->pin_joint_set_param(j, PhysicsServer::PIN_JOINT_DAMPING, damping);
  1175. } else if ("joint_constraints/impulse_clamp" == p_name) {
  1176. impulse_clamp = p_value;
  1177. if (j.is_valid())
  1178. PhysicsServer::get_singleton()->pin_joint_set_param(j, PhysicsServer::PIN_JOINT_IMPULSE_CLAMP, impulse_clamp);
  1179. } else {
  1180. return false;
  1181. }
  1182. return true;
  1183. }
  1184. bool PhysicalBone::PinJointData::_get(const StringName &p_name, Variant &r_ret) const {
  1185. if (JointData::_get(p_name, r_ret)) {
  1186. return true;
  1187. }
  1188. if ("joint_constraints/bias" == p_name) {
  1189. r_ret = bias;
  1190. } else if ("joint_constraints/damping" == p_name) {
  1191. r_ret = damping;
  1192. } else if ("joint_constraints/impulse_clamp" == p_name) {
  1193. r_ret = impulse_clamp;
  1194. } else {
  1195. return false;
  1196. }
  1197. return true;
  1198. }
  1199. void PhysicalBone::PinJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  1200. JointData::_get_property_list(p_list);
  1201. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/bias", PROPERTY_HINT_RANGE, "0.01,0.99,0.01"));
  1202. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/damping", PROPERTY_HINT_RANGE, "0.01,8.0,0.01"));
  1203. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/impulse_clamp", PROPERTY_HINT_RANGE, "0.0,64.0,0.01"));
  1204. }
  1205. bool PhysicalBone::ConeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1206. if (JointData::_set(p_name, p_value, j)) {
  1207. return true;
  1208. }
  1209. if ("joint_constraints/swing_span" == p_name) {
  1210. swing_span = Math::deg2rad(real_t(p_value));
  1211. if (j.is_valid())
  1212. PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::CONE_TWIST_JOINT_SWING_SPAN, swing_span);
  1213. } else if ("joint_constraints/twist_span" == p_name) {
  1214. twist_span = Math::deg2rad(real_t(p_value));
  1215. if (j.is_valid())
  1216. PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::CONE_TWIST_JOINT_TWIST_SPAN, twist_span);
  1217. } else if ("joint_constraints/bias" == p_name) {
  1218. bias = p_value;
  1219. if (j.is_valid())
  1220. PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::CONE_TWIST_JOINT_BIAS, bias);
  1221. } else if ("joint_constraints/softness" == p_name) {
  1222. softness = p_value;
  1223. if (j.is_valid())
  1224. PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::CONE_TWIST_JOINT_SOFTNESS, softness);
  1225. } else if ("joint_constraints/relaxation" == p_name) {
  1226. relaxation = p_value;
  1227. if (j.is_valid())
  1228. PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::CONE_TWIST_JOINT_RELAXATION, relaxation);
  1229. } else {
  1230. return false;
  1231. }
  1232. return true;
  1233. }
  1234. bool PhysicalBone::ConeJointData::_get(const StringName &p_name, Variant &r_ret) const {
  1235. if (JointData::_get(p_name, r_ret)) {
  1236. return true;
  1237. }
  1238. if ("joint_constraints/swing_span" == p_name) {
  1239. r_ret = Math::rad2deg(swing_span);
  1240. } else if ("joint_constraints/twist_span" == p_name) {
  1241. r_ret = Math::rad2deg(twist_span);
  1242. } else if ("joint_constraints/bias" == p_name) {
  1243. r_ret = bias;
  1244. } else if ("joint_constraints/softness" == p_name) {
  1245. r_ret = softness;
  1246. } else if ("joint_constraints/relaxation" == p_name) {
  1247. r_ret = relaxation;
  1248. } else {
  1249. return false;
  1250. }
  1251. return true;
  1252. }
  1253. void PhysicalBone::ConeJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  1254. JointData::_get_property_list(p_list);
  1255. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/swing_span", PROPERTY_HINT_RANGE, "-180,180,0.01"));
  1256. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/twist_span", PROPERTY_HINT_RANGE, "-40000,40000,0.1,or_lesser,or_greater"));
  1257. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/bias", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  1258. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  1259. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/relaxation", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  1260. }
  1261. bool PhysicalBone::HingeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1262. if (JointData::_set(p_name, p_value, j)) {
  1263. return true;
  1264. }
  1265. if ("joint_constraints/angular_limit_enabled" == p_name) {
  1266. angular_limit_enabled = p_value;
  1267. if (j.is_valid())
  1268. PhysicsServer::get_singleton()->hinge_joint_set_flag(j, PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT, angular_limit_enabled);
  1269. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  1270. angular_limit_upper = Math::deg2rad(real_t(p_value));
  1271. if (j.is_valid())
  1272. PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HINGE_JOINT_LIMIT_UPPER, angular_limit_upper);
  1273. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  1274. angular_limit_lower = Math::deg2rad(real_t(p_value));
  1275. if (j.is_valid())
  1276. PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HINGE_JOINT_LIMIT_LOWER, angular_limit_lower);
  1277. } else if ("joint_constraints/angular_limit_bias" == p_name) {
  1278. angular_limit_bias = p_value;
  1279. if (j.is_valid())
  1280. PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HINGE_JOINT_LIMIT_BIAS, angular_limit_bias);
  1281. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  1282. angular_limit_softness = p_value;
  1283. if (j.is_valid())
  1284. PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS, angular_limit_softness);
  1285. } else if ("joint_constraints/angular_limit_relaxation" == p_name) {
  1286. angular_limit_relaxation = p_value;
  1287. if (j.is_valid())
  1288. PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION, angular_limit_relaxation);
  1289. } else {
  1290. return false;
  1291. }
  1292. return true;
  1293. }
  1294. bool PhysicalBone::HingeJointData::_get(const StringName &p_name, Variant &r_ret) const {
  1295. if (JointData::_get(p_name, r_ret)) {
  1296. return true;
  1297. }
  1298. if ("joint_constraints/angular_limit_enabled" == p_name) {
  1299. r_ret = angular_limit_enabled;
  1300. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  1301. r_ret = Math::rad2deg(angular_limit_upper);
  1302. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  1303. r_ret = Math::rad2deg(angular_limit_lower);
  1304. } else if ("joint_constraints/angular_limit_bias" == p_name) {
  1305. r_ret = angular_limit_bias;
  1306. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  1307. r_ret = angular_limit_softness;
  1308. } else if ("joint_constraints/angular_limit_relaxation" == p_name) {
  1309. r_ret = angular_limit_relaxation;
  1310. } else {
  1311. return false;
  1312. }
  1313. return true;
  1314. }
  1315. void PhysicalBone::HingeJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  1316. JointData::_get_property_list(p_list);
  1317. p_list->push_back(PropertyInfo(Variant::BOOL, "joint_constraints/angular_limit_enabled"));
  1318. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_upper", PROPERTY_HINT_RANGE, "-180,180,0.01"));
  1319. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_lower", PROPERTY_HINT_RANGE, "-180,180,0.01"));
  1320. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_bias", PROPERTY_HINT_RANGE, "0.01,0.99,0.01"));
  1321. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  1322. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_relaxation", PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  1323. }
  1324. bool PhysicalBone::SliderJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1325. if (JointData::_set(p_name, p_value, j)) {
  1326. return true;
  1327. }
  1328. if ("joint_constraints/linear_limit_upper" == p_name) {
  1329. linear_limit_upper = p_value;
  1330. if (j.is_valid())
  1331. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_UPPER, linear_limit_upper);
  1332. } else if ("joint_constraints/linear_limit_lower" == p_name) {
  1333. linear_limit_lower = p_value;
  1334. if (j.is_valid())
  1335. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_LOWER, linear_limit_lower);
  1336. } else if ("joint_constraints/linear_limit_softness" == p_name) {
  1337. linear_limit_softness = p_value;
  1338. if (j.is_valid())
  1339. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, linear_limit_softness);
  1340. } else if ("joint_constraints/linear_limit_restitution" == p_name) {
  1341. linear_limit_restitution = p_value;
  1342. if (j.is_valid())
  1343. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, linear_limit_restitution);
  1344. } else if ("joint_constraints/linear_limit_damping" == p_name) {
  1345. linear_limit_damping = p_value;
  1346. if (j.is_valid())
  1347. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, linear_limit_restitution);
  1348. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  1349. angular_limit_upper = Math::deg2rad(real_t(p_value));
  1350. if (j.is_valid())
  1351. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, angular_limit_upper);
  1352. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  1353. angular_limit_lower = Math::deg2rad(real_t(p_value));
  1354. if (j.is_valid())
  1355. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, angular_limit_lower);
  1356. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  1357. angular_limit_softness = p_value;
  1358. if (j.is_valid())
  1359. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness);
  1360. } else if ("joint_constraints/angular_limit_restitution" == p_name) {
  1361. angular_limit_restitution = p_value;
  1362. if (j.is_valid())
  1363. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness);
  1364. } else if ("joint_constraints/angular_limit_damping" == p_name) {
  1365. angular_limit_damping = p_value;
  1366. if (j.is_valid())
  1367. PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, angular_limit_damping);
  1368. } else {
  1369. return false;
  1370. }
  1371. return true;
  1372. }
  1373. bool PhysicalBone::SliderJointData::_get(const StringName &p_name, Variant &r_ret) const {
  1374. if (JointData::_get(p_name, r_ret)) {
  1375. return true;
  1376. }
  1377. if ("joint_constraints/linear_limit_upper" == p_name) {
  1378. r_ret = linear_limit_upper;
  1379. } else if ("joint_constraints/linear_limit_lower" == p_name) {
  1380. r_ret = linear_limit_lower;
  1381. } else if ("joint_constraints/linear_limit_softness" == p_name) {
  1382. r_ret = linear_limit_softness;
  1383. } else if ("joint_constraints/linear_limit_restitution" == p_name) {
  1384. r_ret = linear_limit_restitution;
  1385. } else if ("joint_constraints/linear_limit_damping" == p_name) {
  1386. r_ret = linear_limit_damping;
  1387. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  1388. r_ret = Math::rad2deg(angular_limit_upper);
  1389. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  1390. r_ret = Math::rad2deg(angular_limit_lower);
  1391. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  1392. r_ret = angular_limit_softness;
  1393. } else if ("joint_constraints/angular_limit_restitution" == p_name) {
  1394. r_ret = angular_limit_restitution;
  1395. } else if ("joint_constraints/angular_limit_damping" == p_name) {
  1396. r_ret = angular_limit_damping;
  1397. } else {
  1398. return false;
  1399. }
  1400. return true;
  1401. }
  1402. void PhysicalBone::SliderJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  1403. JointData::_get_property_list(p_list);
  1404. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/linear_limit_upper"));
  1405. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/linear_limit_lower"));
  1406. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/linear_limit_softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  1407. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/linear_limit_restitution", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  1408. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/linear_limit_damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"));
  1409. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_upper", PROPERTY_HINT_RANGE, "-180,180,0.01"));
  1410. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_lower", PROPERTY_HINT_RANGE, "-180,180,0.01"));
  1411. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  1412. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_restitution", PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  1413. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_damping", PROPERTY_HINT_RANGE, "0,16.0,0.01"));
  1414. }
  1415. bool PhysicalBone::SixDOFJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1416. if (JointData::_set(p_name, p_value, j)) {
  1417. return true;
  1418. }
  1419. String path = p_name;
  1420. Vector3::Axis axis;
  1421. {
  1422. const String axis_s = path.get_slicec('/', 1);
  1423. if ("x" == axis_s) {
  1424. axis = Vector3::AXIS_X;
  1425. } else if ("y" == axis_s) {
  1426. axis = Vector3::AXIS_Y;
  1427. } else if ("z" == axis_s) {
  1428. axis = Vector3::AXIS_Z;
  1429. } else {
  1430. return false;
  1431. }
  1432. }
  1433. String var_name = path.get_slicec('/', 2);
  1434. if ("linear_limit_enabled" == var_name) {
  1435. axis_data[axis].linear_limit_enabled = p_value;
  1436. if (j.is_valid())
  1437. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, axis_data[axis].linear_limit_enabled);
  1438. } else if ("linear_limit_upper" == var_name) {
  1439. axis_data[axis].linear_limit_upper = p_value;
  1440. if (j.is_valid())
  1441. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_UPPER_LIMIT, axis_data[axis].linear_limit_upper);
  1442. } else if ("linear_limit_lower" == var_name) {
  1443. axis_data[axis].linear_limit_lower = p_value;
  1444. if (j.is_valid())
  1445. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_LOWER_LIMIT, axis_data[axis].linear_limit_lower);
  1446. } else if ("linear_limit_softness" == var_name) {
  1447. axis_data[axis].linear_limit_softness = p_value;
  1448. if (j.is_valid())
  1449. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, axis_data[axis].linear_limit_softness);
  1450. } else if ("linear_spring_enabled" == var_name) {
  1451. axis_data[axis].linear_spring_enabled = p_value;
  1452. if (j.is_valid())
  1453. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, axis_data[axis].linear_spring_enabled);
  1454. } else if ("linear_spring_stiffness" == var_name) {
  1455. axis_data[axis].linear_spring_stiffness = p_value;
  1456. if (j.is_valid())
  1457. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, axis_data[axis].linear_spring_stiffness);
  1458. } else if ("linear_spring_damping" == var_name) {
  1459. axis_data[axis].linear_spring_damping = p_value;
  1460. if (j.is_valid())
  1461. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_DAMPING, axis_data[axis].linear_spring_damping);
  1462. } else if ("linear_equilibrium_point" == var_name) {
  1463. axis_data[axis].linear_equilibrium_point = p_value;
  1464. if (j.is_valid())
  1465. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].linear_equilibrium_point);
  1466. } else if ("linear_restitution" == var_name) {
  1467. axis_data[axis].linear_restitution = p_value;
  1468. if (j.is_valid())
  1469. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_RESTITUTION, axis_data[axis].linear_restitution);
  1470. } else if ("linear_damping" == var_name) {
  1471. axis_data[axis].linear_damping = p_value;
  1472. if (j.is_valid())
  1473. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_DAMPING, axis_data[axis].linear_damping);
  1474. } else if ("angular_limit_enabled" == var_name) {
  1475. axis_data[axis].angular_limit_enabled = p_value;
  1476. if (j.is_valid())
  1477. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, axis_data[axis].angular_limit_enabled);
  1478. } else if ("angular_limit_upper" == var_name) {
  1479. axis_data[axis].angular_limit_upper = Math::deg2rad(real_t(p_value));
  1480. if (j.is_valid())
  1481. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, axis_data[axis].angular_limit_upper);
  1482. } else if ("angular_limit_lower" == var_name) {
  1483. axis_data[axis].angular_limit_lower = Math::deg2rad(real_t(p_value));
  1484. if (j.is_valid())
  1485. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, axis_data[axis].angular_limit_lower);
  1486. } else if ("angular_limit_softness" == var_name) {
  1487. axis_data[axis].angular_limit_softness = p_value;
  1488. if (j.is_valid())
  1489. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, axis_data[axis].angular_limit_softness);
  1490. } else if ("angular_restitution" == var_name) {
  1491. axis_data[axis].angular_restitution = p_value;
  1492. if (j.is_valid())
  1493. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_RESTITUTION, axis_data[axis].angular_restitution);
  1494. } else if ("angular_damping" == var_name) {
  1495. axis_data[axis].angular_damping = p_value;
  1496. if (j.is_valid())
  1497. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_DAMPING, axis_data[axis].angular_damping);
  1498. } else if ("erp" == var_name) {
  1499. axis_data[axis].erp = p_value;
  1500. if (j.is_valid())
  1501. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_ERP, axis_data[axis].erp);
  1502. } else if ("angular_spring_enabled" == var_name) {
  1503. axis_data[axis].angular_spring_enabled = p_value;
  1504. if (j.is_valid())
  1505. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, axis_data[axis].angular_spring_enabled);
  1506. } else if ("angular_spring_stiffness" == var_name) {
  1507. axis_data[axis].angular_spring_stiffness = p_value;
  1508. if (j.is_valid())
  1509. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, axis_data[axis].angular_spring_stiffness);
  1510. } else if ("angular_spring_damping" == var_name) {
  1511. axis_data[axis].angular_spring_damping = p_value;
  1512. if (j.is_valid())
  1513. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, axis_data[axis].angular_spring_damping);
  1514. } else if ("angular_equilibrium_point" == var_name) {
  1515. axis_data[axis].angular_equilibrium_point = p_value;
  1516. if (j.is_valid())
  1517. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].angular_equilibrium_point);
  1518. } else {
  1519. return false;
  1520. }
  1521. return true;
  1522. }
  1523. bool PhysicalBone::SixDOFJointData::_get(const StringName &p_name, Variant &r_ret) const {
  1524. if (JointData::_get(p_name, r_ret)) {
  1525. return true;
  1526. }
  1527. String path = p_name;
  1528. int axis;
  1529. {
  1530. const String axis_s = path.get_slicec('/', 1);
  1531. if ("x" == axis_s) {
  1532. axis = 0;
  1533. } else if ("y" == axis_s) {
  1534. axis = 1;
  1535. } else if ("z" == axis_s) {
  1536. axis = 2;
  1537. } else {
  1538. return false;
  1539. }
  1540. }
  1541. String var_name = path.get_slicec('/', 2);
  1542. if ("linear_limit_enabled" == var_name) {
  1543. r_ret = axis_data[axis].linear_limit_enabled;
  1544. } else if ("linear_limit_upper" == var_name) {
  1545. r_ret = axis_data[axis].linear_limit_upper;
  1546. } else if ("linear_limit_lower" == var_name) {
  1547. r_ret = axis_data[axis].linear_limit_lower;
  1548. } else if ("linear_limit_softness" == var_name) {
  1549. r_ret = axis_data[axis].linear_limit_softness;
  1550. } else if ("linear_spring_enabled" == var_name) {
  1551. r_ret = axis_data[axis].linear_spring_enabled;
  1552. } else if ("linear_spring_stiffness" == var_name) {
  1553. r_ret = axis_data[axis].linear_spring_stiffness;
  1554. } else if ("linear_spring_damping" == var_name) {
  1555. r_ret = axis_data[axis].linear_spring_damping;
  1556. } else if ("linear_equilibrium_point" == var_name) {
  1557. r_ret = axis_data[axis].linear_equilibrium_point;
  1558. } else if ("linear_restitution" == var_name) {
  1559. r_ret = axis_data[axis].linear_restitution;
  1560. } else if ("linear_damping" == var_name) {
  1561. r_ret = axis_data[axis].linear_damping;
  1562. } else if ("angular_limit_enabled" == var_name) {
  1563. r_ret = axis_data[axis].angular_limit_enabled;
  1564. } else if ("angular_limit_upper" == var_name) {
  1565. r_ret = Math::rad2deg(axis_data[axis].angular_limit_upper);
  1566. } else if ("angular_limit_lower" == var_name) {
  1567. r_ret = Math::rad2deg(axis_data[axis].angular_limit_lower);
  1568. } else if ("angular_limit_softness" == var_name) {
  1569. r_ret = axis_data[axis].angular_limit_softness;
  1570. } else if ("angular_restitution" == var_name) {
  1571. r_ret = axis_data[axis].angular_restitution;
  1572. } else if ("angular_damping" == var_name) {
  1573. r_ret = axis_data[axis].angular_damping;
  1574. } else if ("erp" == var_name) {
  1575. r_ret = axis_data[axis].erp;
  1576. } else if ("angular_spring_enabled" == var_name) {
  1577. r_ret = axis_data[axis].angular_spring_enabled;
  1578. } else if ("angular_spring_stiffness" == var_name) {
  1579. r_ret = axis_data[axis].angular_spring_stiffness;
  1580. } else if ("angular_spring_damping" == var_name) {
  1581. r_ret = axis_data[axis].angular_spring_damping;
  1582. } else if ("angular_equilibrium_point" == var_name) {
  1583. r_ret = axis_data[axis].angular_equilibrium_point;
  1584. } else {
  1585. return false;
  1586. }
  1587. return true;
  1588. }
  1589. void PhysicalBone::SixDOFJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  1590. const StringName axis_names[] = { "x", "y", "z" };
  1591. for (int i = 0; i < 3; ++i) {
  1592. p_list->push_back(PropertyInfo(Variant::BOOL, "joint_constraints/" + axis_names[i] + "/linear_limit_enabled"));
  1593. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_limit_upper"));
  1594. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_limit_lower"));
  1595. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_limit_softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  1596. p_list->push_back(PropertyInfo(Variant::BOOL, "joint_constraints/" + axis_names[i] + "/linear_spring_enabled"));
  1597. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_spring_stiffness"));
  1598. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_spring_damping"));
  1599. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_equilibrium_point"));
  1600. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_restitution", PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  1601. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_damping", PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  1602. p_list->push_back(PropertyInfo(Variant::BOOL, "joint_constraints/" + axis_names[i] + "/angular_limit_enabled"));
  1603. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_limit_upper", PROPERTY_HINT_RANGE, "-180,180,0.01"));
  1604. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_limit_lower", PROPERTY_HINT_RANGE, "-180,180,0.01"));
  1605. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_limit_softness", PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  1606. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_restitution", PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  1607. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_damping", PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  1608. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/erp"));
  1609. p_list->push_back(PropertyInfo(Variant::BOOL, "joint_constraints/" + axis_names[i] + "/angular_spring_enabled"));
  1610. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_spring_stiffness"));
  1611. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_spring_damping"));
  1612. p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_equilibrium_point"));
  1613. }
  1614. }
  1615. bool PhysicalBone::_set(const StringName &p_name, const Variant &p_value) {
  1616. if (p_name == "bone_name") {
  1617. set_bone_name(p_value);
  1618. return true;
  1619. }
  1620. if (joint_data) {
  1621. if (joint_data->_set(p_name, p_value)) {
  1622. #ifdef TOOLS_ENABLED
  1623. if (get_gizmo().is_valid())
  1624. get_gizmo()->redraw();
  1625. #endif
  1626. return true;
  1627. }
  1628. }
  1629. return false;
  1630. }
  1631. bool PhysicalBone::_get(const StringName &p_name, Variant &r_ret) const {
  1632. if (p_name == "bone_name") {
  1633. r_ret = get_bone_name();
  1634. return true;
  1635. }
  1636. if (joint_data) {
  1637. return joint_data->_get(p_name, r_ret);
  1638. }
  1639. return false;
  1640. }
  1641. void PhysicalBone::_get_property_list(List<PropertyInfo> *p_list) const {
  1642. Skeleton *parent = find_skeleton_parent(get_parent());
  1643. if (parent) {
  1644. String names;
  1645. for (int i = 0; i < parent->get_bone_count(); i++) {
  1646. if (i > 0)
  1647. names += ",";
  1648. names += parent->get_bone_name(i);
  1649. }
  1650. p_list->push_back(PropertyInfo(Variant::STRING, "bone_name", PROPERTY_HINT_ENUM, names));
  1651. } else {
  1652. p_list->push_back(PropertyInfo(Variant::STRING, "bone_name"));
  1653. }
  1654. if (joint_data) {
  1655. joint_data->_get_property_list(p_list);
  1656. }
  1657. }
  1658. void PhysicalBone::_notification(int p_what) {
  1659. switch (p_what) {
  1660. case NOTIFICATION_ENTER_TREE:
  1661. parent_skeleton = find_skeleton_parent(get_parent());
  1662. update_bone_id();
  1663. reset_to_rest_position();
  1664. _reset_physics_simulation_state();
  1665. break;
  1666. case NOTIFICATION_EXIT_TREE:
  1667. if (parent_skeleton) {
  1668. if (-1 != bone_id) {
  1669. parent_skeleton->unbind_physical_bone_from_bone(bone_id);
  1670. }
  1671. }
  1672. parent_skeleton = NULL;
  1673. update_bone_id();
  1674. break;
  1675. case NOTIFICATION_TRANSFORM_CHANGED:
  1676. if (Engine::get_singleton()->is_editor_hint()) {
  1677. update_offset();
  1678. }
  1679. break;
  1680. }
  1681. }
  1682. void PhysicalBone::_direct_state_changed(Object *p_state) {
  1683. if (!simulate_physics) {
  1684. return;
  1685. }
  1686. /// Update bone transform
  1687. PhysicsDirectBodyState *state;
  1688. #ifdef DEBUG_ENABLED
  1689. state = Object::cast_to<PhysicsDirectBodyState>(p_state);
  1690. #else
  1691. state = (PhysicsDirectBodyState *)p_state; //trust it
  1692. #endif
  1693. Transform global_transform(state->get_transform());
  1694. set_ignore_transform_notification(true);
  1695. set_global_transform(global_transform);
  1696. set_ignore_transform_notification(false);
  1697. // Update skeleton
  1698. if (parent_skeleton) {
  1699. if (-1 != bone_id) {
  1700. parent_skeleton->set_bone_global_pose(bone_id, parent_skeleton->get_global_transform().affine_inverse() * (global_transform * body_offset_inverse));
  1701. }
  1702. }
  1703. }
  1704. void PhysicalBone::_bind_methods() {
  1705. ClassDB::bind_method(D_METHOD("_direct_state_changed"), &PhysicalBone::_direct_state_changed);
  1706. ClassDB::bind_method(D_METHOD("set_joint_type", "joint_type"), &PhysicalBone::set_joint_type);
  1707. ClassDB::bind_method(D_METHOD("get_joint_type"), &PhysicalBone::get_joint_type);
  1708. ClassDB::bind_method(D_METHOD("set_joint_offset", "offset"), &PhysicalBone::set_joint_offset);
  1709. ClassDB::bind_method(D_METHOD("get_joint_offset"), &PhysicalBone::get_joint_offset);
  1710. ClassDB::bind_method(D_METHOD("set_body_offset", "offset"), &PhysicalBone::set_body_offset);
  1711. ClassDB::bind_method(D_METHOD("get_body_offset"), &PhysicalBone::get_body_offset);
  1712. ClassDB::bind_method(D_METHOD("is_static_body"), &PhysicalBone::is_static_body);
  1713. ClassDB::bind_method(D_METHOD("get_simulate_physics"), &PhysicalBone::get_simulate_physics);
  1714. ClassDB::bind_method(D_METHOD("is_simulating_physics"), &PhysicalBone::is_simulating_physics);
  1715. ClassDB::bind_method(D_METHOD("get_bone_id"), &PhysicalBone::get_bone_id);
  1716. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &PhysicalBone::set_mass);
  1717. ClassDB::bind_method(D_METHOD("get_mass"), &PhysicalBone::get_mass);
  1718. ClassDB::bind_method(D_METHOD("set_weight", "weight"), &PhysicalBone::set_weight);
  1719. ClassDB::bind_method(D_METHOD("get_weight"), &PhysicalBone::get_weight);
  1720. ClassDB::bind_method(D_METHOD("set_friction", "friction"), &PhysicalBone::set_friction);
  1721. ClassDB::bind_method(D_METHOD("get_friction"), &PhysicalBone::get_friction);
  1722. ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &PhysicalBone::set_bounce);
  1723. ClassDB::bind_method(D_METHOD("get_bounce"), &PhysicalBone::get_bounce);
  1724. ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &PhysicalBone::set_gravity_scale);
  1725. ClassDB::bind_method(D_METHOD("get_gravity_scale"), &PhysicalBone::get_gravity_scale);
  1726. ADD_GROUP("Joint", "joint_");
  1727. ADD_PROPERTY(PropertyInfo(Variant::INT, "joint_type", PROPERTY_HINT_ENUM, "None,PinJoint,ConeJoint,HingeJoint,SliderJoint,6DOFJoint"), "set_joint_type", "get_joint_type");
  1728. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "joint_offset"), "set_joint_offset", "get_joint_offset");
  1729. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "body_offset"), "set_body_offset", "get_body_offset");
  1730. ADD_PROPERTY(PropertyInfo(Variant::REAL, "mass", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01"), "set_mass", "get_mass");
  1731. ADD_PROPERTY(PropertyInfo(Variant::REAL, "weight", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01"), "set_weight", "get_weight");
  1732. ADD_PROPERTY(PropertyInfo(Variant::REAL, "friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_friction", "get_friction");
  1733. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_bounce", "get_bounce");
  1734. ADD_PROPERTY(PropertyInfo(Variant::REAL, "gravity_scale", PROPERTY_HINT_RANGE, "-10,10,0.01"), "set_gravity_scale", "get_gravity_scale");
  1735. BIND_ENUM_CONSTANT(JOINT_TYPE_NONE);
  1736. BIND_ENUM_CONSTANT(JOINT_TYPE_PIN);
  1737. BIND_ENUM_CONSTANT(JOINT_TYPE_CONE);
  1738. BIND_ENUM_CONSTANT(JOINT_TYPE_HINGE);
  1739. BIND_ENUM_CONSTANT(JOINT_TYPE_SLIDER);
  1740. BIND_ENUM_CONSTANT(JOINT_TYPE_6DOF);
  1741. }
  1742. Skeleton *PhysicalBone::find_skeleton_parent(Node *p_parent) {
  1743. if (!p_parent) {
  1744. return NULL;
  1745. }
  1746. Skeleton *s = Object::cast_to<Skeleton>(p_parent);
  1747. return s ? s : find_skeleton_parent(p_parent->get_parent());
  1748. }
  1749. void PhysicalBone::_fix_joint_offset() {
  1750. // Clamp joint origin to bone origin
  1751. if (parent_skeleton) {
  1752. joint_offset.origin = body_offset.affine_inverse().origin;
  1753. }
  1754. }
  1755. void PhysicalBone::_reload_joint() {
  1756. if (joint.is_valid()) {
  1757. PhysicsServer::get_singleton()->free(joint);
  1758. joint = RID();
  1759. }
  1760. if (!parent_skeleton) {
  1761. return;
  1762. }
  1763. PhysicalBone *body_a = parent_skeleton->get_physical_bone_parent(bone_id);
  1764. if (!body_a) {
  1765. return;
  1766. }
  1767. Transform joint_transf = get_global_transform() * joint_offset;
  1768. Transform local_a = body_a->get_global_transform().affine_inverse() * joint_transf;
  1769. local_a.orthonormalize();
  1770. switch (get_joint_type()) {
  1771. case JOINT_TYPE_PIN: {
  1772. joint = PhysicsServer::get_singleton()->joint_create_pin(body_a->get_rid(), local_a.origin, get_rid(), joint_offset.origin);
  1773. const PinJointData *pjd(static_cast<const PinJointData *>(joint_data));
  1774. PhysicsServer::get_singleton()->pin_joint_set_param(joint, PhysicsServer::PIN_JOINT_BIAS, pjd->bias);
  1775. PhysicsServer::get_singleton()->pin_joint_set_param(joint, PhysicsServer::PIN_JOINT_DAMPING, pjd->damping);
  1776. PhysicsServer::get_singleton()->pin_joint_set_param(joint, PhysicsServer::PIN_JOINT_IMPULSE_CLAMP, pjd->impulse_clamp);
  1777. } break;
  1778. case JOINT_TYPE_CONE: {
  1779. joint = PhysicsServer::get_singleton()->joint_create_cone_twist(body_a->get_rid(), local_a, get_rid(), joint_offset);
  1780. const ConeJointData *cjd(static_cast<const ConeJointData *>(joint_data));
  1781. PhysicsServer::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer::CONE_TWIST_JOINT_SWING_SPAN, cjd->swing_span);
  1782. PhysicsServer::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer::CONE_TWIST_JOINT_TWIST_SPAN, cjd->twist_span);
  1783. PhysicsServer::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer::CONE_TWIST_JOINT_BIAS, cjd->bias);
  1784. PhysicsServer::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer::CONE_TWIST_JOINT_SOFTNESS, cjd->softness);
  1785. PhysicsServer::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer::CONE_TWIST_JOINT_RELAXATION, cjd->relaxation);
  1786. } break;
  1787. case JOINT_TYPE_HINGE: {
  1788. joint = PhysicsServer::get_singleton()->joint_create_hinge(body_a->get_rid(), local_a, get_rid(), joint_offset);
  1789. const HingeJointData *hjd(static_cast<const HingeJointData *>(joint_data));
  1790. PhysicsServer::get_singleton()->hinge_joint_set_flag(joint, PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT, hjd->angular_limit_enabled);
  1791. PhysicsServer::get_singleton()->hinge_joint_set_param(joint, PhysicsServer::HINGE_JOINT_LIMIT_UPPER, hjd->angular_limit_upper);
  1792. PhysicsServer::get_singleton()->hinge_joint_set_param(joint, PhysicsServer::HINGE_JOINT_LIMIT_LOWER, hjd->angular_limit_lower);
  1793. PhysicsServer::get_singleton()->hinge_joint_set_param(joint, PhysicsServer::HINGE_JOINT_LIMIT_BIAS, hjd->angular_limit_bias);
  1794. PhysicsServer::get_singleton()->hinge_joint_set_param(joint, PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS, hjd->angular_limit_softness);
  1795. PhysicsServer::get_singleton()->hinge_joint_set_param(joint, PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION, hjd->angular_limit_relaxation);
  1796. } break;
  1797. case JOINT_TYPE_SLIDER: {
  1798. joint = PhysicsServer::get_singleton()->joint_create_slider(body_a->get_rid(), local_a, get_rid(), joint_offset);
  1799. const SliderJointData *sjd(static_cast<const SliderJointData *>(joint_data));
  1800. PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_UPPER, sjd->linear_limit_upper);
  1801. PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_LOWER, sjd->linear_limit_lower);
  1802. PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, sjd->linear_limit_softness);
  1803. PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, sjd->linear_limit_restitution);
  1804. PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, sjd->linear_limit_restitution);
  1805. PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, sjd->angular_limit_upper);
  1806. PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, sjd->angular_limit_lower);
  1807. PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness);
  1808. PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness);
  1809. PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, sjd->angular_limit_damping);
  1810. } break;
  1811. case JOINT_TYPE_6DOF: {
  1812. joint = PhysicsServer::get_singleton()->joint_create_generic_6dof(body_a->get_rid(), local_a, get_rid(), joint_offset);
  1813. const SixDOFJointData *g6dofjd(static_cast<const SixDOFJointData *>(joint_data));
  1814. for (int axis = 0; axis < 3; ++axis) {
  1815. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, g6dofjd->axis_data[axis].linear_limit_enabled);
  1816. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_LINEAR_UPPER_LIMIT, g6dofjd->axis_data[axis].linear_limit_upper);
  1817. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_LINEAR_LOWER_LIMIT, g6dofjd->axis_data[axis].linear_limit_lower);
  1818. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].linear_limit_softness);
  1819. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, g6dofjd->axis_data[axis].linear_spring_enabled);
  1820. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].linear_spring_stiffness);
  1821. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_DAMPING, g6dofjd->axis_data[axis].linear_spring_damping);
  1822. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].linear_equilibrium_point);
  1823. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_LINEAR_RESTITUTION, g6dofjd->axis_data[axis].linear_restitution);
  1824. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_LINEAR_DAMPING, g6dofjd->axis_data[axis].linear_damping);
  1825. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, g6dofjd->axis_data[axis].angular_limit_enabled);
  1826. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, g6dofjd->axis_data[axis].angular_limit_upper);
  1827. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, g6dofjd->axis_data[axis].angular_limit_lower);
  1828. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].angular_limit_softness);
  1829. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_RESTITUTION, g6dofjd->axis_data[axis].angular_restitution);
  1830. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_DAMPING, g6dofjd->axis_data[axis].angular_damping);
  1831. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_ERP, g6dofjd->axis_data[axis].erp);
  1832. PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, g6dofjd->axis_data[axis].angular_spring_enabled);
  1833. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].angular_spring_stiffness);
  1834. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, g6dofjd->axis_data[axis].angular_spring_damping);
  1835. PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].angular_equilibrium_point);
  1836. }
  1837. } break;
  1838. case JOINT_TYPE_NONE: {
  1839. } break;
  1840. }
  1841. }
  1842. void PhysicalBone::_on_bone_parent_changed() {
  1843. _reload_joint();
  1844. }
  1845. void PhysicalBone::_set_gizmo_move_joint(bool p_move_joint) {
  1846. #ifdef TOOLS_ENABLED
  1847. gizmo_move_joint = p_move_joint;
  1848. SpatialEditor::get_singleton()->update_transform_gizmo();
  1849. #endif
  1850. }
  1851. #ifdef TOOLS_ENABLED
  1852. Transform PhysicalBone::get_global_gizmo_transform() const {
  1853. return gizmo_move_joint ? get_global_transform() * joint_offset : get_global_transform();
  1854. }
  1855. Transform PhysicalBone::get_local_gizmo_transform() const {
  1856. return gizmo_move_joint ? get_transform() * joint_offset : get_transform();
  1857. }
  1858. #endif
  1859. const PhysicalBone::JointData *PhysicalBone::get_joint_data() const {
  1860. return joint_data;
  1861. }
  1862. Skeleton *PhysicalBone::find_skeleton_parent() {
  1863. return find_skeleton_parent(this);
  1864. }
  1865. void PhysicalBone::set_joint_type(JointType p_joint_type) {
  1866. if (p_joint_type == get_joint_type())
  1867. return;
  1868. if (joint_data)
  1869. memdelete(joint_data);
  1870. joint_data = NULL;
  1871. switch (p_joint_type) {
  1872. case JOINT_TYPE_PIN:
  1873. joint_data = memnew(PinJointData);
  1874. break;
  1875. case JOINT_TYPE_CONE:
  1876. joint_data = memnew(ConeJointData);
  1877. break;
  1878. case JOINT_TYPE_HINGE:
  1879. joint_data = memnew(HingeJointData);
  1880. break;
  1881. case JOINT_TYPE_SLIDER:
  1882. joint_data = memnew(SliderJointData);
  1883. break;
  1884. case JOINT_TYPE_6DOF:
  1885. joint_data = memnew(SixDOFJointData);
  1886. break;
  1887. case JOINT_TYPE_NONE:
  1888. break;
  1889. }
  1890. _reload_joint();
  1891. #ifdef TOOLS_ENABLED
  1892. _change_notify();
  1893. if (get_gizmo().is_valid())
  1894. get_gizmo()->redraw();
  1895. #endif
  1896. }
  1897. PhysicalBone::JointType PhysicalBone::get_joint_type() const {
  1898. return joint_data ? joint_data->get_joint_type() : JOINT_TYPE_NONE;
  1899. }
  1900. void PhysicalBone::set_joint_offset(const Transform &p_offset) {
  1901. joint_offset = p_offset;
  1902. _fix_joint_offset();
  1903. set_ignore_transform_notification(true);
  1904. reset_to_rest_position();
  1905. set_ignore_transform_notification(false);
  1906. #ifdef TOOLS_ENABLED
  1907. if (get_gizmo().is_valid())
  1908. get_gizmo()->redraw();
  1909. #endif
  1910. }
  1911. const Transform &PhysicalBone::get_body_offset() const {
  1912. return body_offset;
  1913. }
  1914. void PhysicalBone::set_body_offset(const Transform &p_offset) {
  1915. body_offset = p_offset;
  1916. body_offset_inverse = body_offset.affine_inverse();
  1917. _fix_joint_offset();
  1918. set_ignore_transform_notification(true);
  1919. reset_to_rest_position();
  1920. set_ignore_transform_notification(false);
  1921. #ifdef TOOLS_ENABLED
  1922. if (get_gizmo().is_valid())
  1923. get_gizmo()->redraw();
  1924. #endif
  1925. }
  1926. const Transform &PhysicalBone::get_joint_offset() const {
  1927. return joint_offset;
  1928. }
  1929. void PhysicalBone::set_static_body(bool p_static) {
  1930. static_body = p_static;
  1931. set_as_toplevel(!static_body);
  1932. _reset_physics_simulation_state();
  1933. }
  1934. bool PhysicalBone::is_static_body() {
  1935. return static_body;
  1936. }
  1937. void PhysicalBone::set_simulate_physics(bool p_simulate) {
  1938. if (simulate_physics == p_simulate) {
  1939. return;
  1940. }
  1941. simulate_physics = p_simulate;
  1942. _reset_physics_simulation_state();
  1943. }
  1944. bool PhysicalBone::get_simulate_physics() {
  1945. return simulate_physics;
  1946. }
  1947. bool PhysicalBone::is_simulating_physics() {
  1948. return _internal_simulate_physics && !_internal_static_body;
  1949. }
  1950. void PhysicalBone::set_bone_name(const String &p_name) {
  1951. bone_name = p_name;
  1952. bone_id = -1;
  1953. update_bone_id();
  1954. reset_to_rest_position();
  1955. }
  1956. const String &PhysicalBone::get_bone_name() const {
  1957. return bone_name;
  1958. }
  1959. void PhysicalBone::set_mass(real_t p_mass) {
  1960. ERR_FAIL_COND(p_mass <= 0);
  1961. mass = p_mass;
  1962. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_MASS, mass);
  1963. }
  1964. real_t PhysicalBone::get_mass() const {
  1965. return mass;
  1966. }
  1967. void PhysicalBone::set_weight(real_t p_weight) {
  1968. set_mass(p_weight / real_t(GLOBAL_DEF("physics/3d/default_gravity", 9.8)));
  1969. }
  1970. real_t PhysicalBone::get_weight() const {
  1971. return mass * real_t(GLOBAL_DEF("physics/3d/default_gravity", 9.8));
  1972. }
  1973. void PhysicalBone::set_friction(real_t p_friction) {
  1974. ERR_FAIL_COND(p_friction < 0 || p_friction > 1);
  1975. friction = p_friction;
  1976. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_FRICTION, friction);
  1977. }
  1978. real_t PhysicalBone::get_friction() const {
  1979. return friction;
  1980. }
  1981. void PhysicalBone::set_bounce(real_t p_bounce) {
  1982. ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
  1983. bounce = p_bounce;
  1984. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_BOUNCE, bounce);
  1985. }
  1986. real_t PhysicalBone::get_bounce() const {
  1987. return bounce;
  1988. }
  1989. void PhysicalBone::set_gravity_scale(real_t p_gravity_scale) {
  1990. gravity_scale = p_gravity_scale;
  1991. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
  1992. }
  1993. real_t PhysicalBone::get_gravity_scale() const {
  1994. return gravity_scale;
  1995. }
  1996. PhysicalBone::PhysicalBone() :
  1997. PhysicsBody(PhysicsServer::BODY_MODE_STATIC),
  1998. #ifdef TOOLS_ENABLED
  1999. gizmo_move_joint(false),
  2000. #endif
  2001. joint_data(NULL),
  2002. parent_skeleton(NULL),
  2003. static_body(false),
  2004. _internal_static_body(false),
  2005. simulate_physics(false),
  2006. _internal_simulate_physics(false),
  2007. bone_id(-1),
  2008. bone_name(""),
  2009. bounce(0),
  2010. mass(1),
  2011. friction(1),
  2012. gravity_scale(1) {
  2013. set_static_body(static_body);
  2014. _reset_physics_simulation_state();
  2015. }
  2016. PhysicalBone::~PhysicalBone() {
  2017. if (joint_data)
  2018. memdelete(joint_data);
  2019. }
  2020. void PhysicalBone::update_bone_id() {
  2021. if (!parent_skeleton) {
  2022. return;
  2023. }
  2024. const int new_bone_id = parent_skeleton->find_bone(bone_name);
  2025. if (new_bone_id != bone_id) {
  2026. if (-1 != bone_id) {
  2027. // Assert the unbind from old node
  2028. parent_skeleton->unbind_physical_bone_from_bone(bone_id);
  2029. parent_skeleton->unbind_child_node_from_bone(bone_id, this);
  2030. }
  2031. bone_id = new_bone_id;
  2032. parent_skeleton->bind_physical_bone_to_bone(bone_id, this);
  2033. _fix_joint_offset();
  2034. _internal_static_body = !static_body; // Force staticness reset
  2035. _reset_staticness_state();
  2036. }
  2037. }
  2038. void PhysicalBone::update_offset() {
  2039. #ifdef TOOLS_ENABLED
  2040. if (parent_skeleton) {
  2041. Transform bone_transform(parent_skeleton->get_global_transform());
  2042. if (-1 != bone_id)
  2043. bone_transform *= parent_skeleton->get_bone_global_pose(bone_id);
  2044. if (gizmo_move_joint) {
  2045. bone_transform *= body_offset;
  2046. set_joint_offset(bone_transform.affine_inverse() * get_global_transform());
  2047. } else {
  2048. set_body_offset(bone_transform.affine_inverse() * get_global_transform());
  2049. }
  2050. }
  2051. #endif
  2052. }
  2053. void PhysicalBone::reset_to_rest_position() {
  2054. if (parent_skeleton) {
  2055. if (-1 == bone_id) {
  2056. set_global_transform(parent_skeleton->get_global_transform() * body_offset);
  2057. } else {
  2058. set_global_transform(parent_skeleton->get_global_transform() * parent_skeleton->get_bone_global_pose(bone_id) * body_offset);
  2059. }
  2060. }
  2061. }
  2062. void PhysicalBone::_reset_physics_simulation_state() {
  2063. if (simulate_physics && !static_body) {
  2064. _start_physics_simulation();
  2065. } else {
  2066. _stop_physics_simulation();
  2067. }
  2068. _reset_staticness_state();
  2069. }
  2070. void PhysicalBone::_reset_staticness_state() {
  2071. if (parent_skeleton && -1 != bone_id) {
  2072. if (static_body && simulate_physics) { // With this check I'm sure the position of this body is updated only when it's necessary
  2073. if (_internal_static_body) {
  2074. return;
  2075. }
  2076. parent_skeleton->bind_child_node_to_bone(bone_id, this);
  2077. _internal_static_body = true;
  2078. } else {
  2079. if (!_internal_static_body) {
  2080. return;
  2081. }
  2082. parent_skeleton->unbind_child_node_from_bone(bone_id, this);
  2083. _internal_static_body = false;
  2084. }
  2085. }
  2086. }
  2087. void PhysicalBone::_start_physics_simulation() {
  2088. if (_internal_simulate_physics || !parent_skeleton) {
  2089. return;
  2090. }
  2091. reset_to_rest_position();
  2092. PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_RIGID);
  2093. PhysicsServer::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer());
  2094. PhysicsServer::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask());
  2095. PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(), this, "_direct_state_changed");
  2096. parent_skeleton->set_bone_ignore_animation(bone_id, true);
  2097. _internal_simulate_physics = true;
  2098. }
  2099. void PhysicalBone::_stop_physics_simulation() {
  2100. if (!_internal_simulate_physics || !parent_skeleton) {
  2101. return;
  2102. }
  2103. PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_STATIC);
  2104. PhysicsServer::get_singleton()->body_set_collision_layer(get_rid(), 0);
  2105. PhysicsServer::get_singleton()->body_set_collision_mask(get_rid(), 0);
  2106. PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(), NULL, "");
  2107. parent_skeleton->set_bone_ignore_animation(bone_id, false);
  2108. _internal_simulate_physics = false;
  2109. }