physics_body_3d.cpp 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454
  1. /*************************************************************************/
  2. /* physics_body_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_3d.h"
  31. #include "core/core_string_names.h"
  32. #include "scene/scene_string_names.h"
  33. void PhysicsBody3D::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("move_and_collide", "distance", "test_only", "safe_margin", "max_collisions"), &PhysicsBody3D::_move, DEFVAL(false), DEFVAL(0.001), DEFVAL(1));
  35. ClassDB::bind_method(D_METHOD("test_move", "from", "distance", "collision", "safe_margin", "max_collisions"), &PhysicsBody3D::test_move, DEFVAL(Variant()), DEFVAL(0.001), DEFVAL(1));
  36. ClassDB::bind_method(D_METHOD("set_axis_lock", "axis", "lock"), &PhysicsBody3D::set_axis_lock);
  37. ClassDB::bind_method(D_METHOD("get_axis_lock", "axis"), &PhysicsBody3D::get_axis_lock);
  38. ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody3D::get_collision_exceptions);
  39. ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody3D::add_collision_exception_with);
  40. ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody3D::remove_collision_exception_with);
  41. ADD_GROUP("Axis Lock", "axis_lock_");
  42. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_x"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_LINEAR_X);
  43. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_y"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_LINEAR_Y);
  44. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_z"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_LINEAR_Z);
  45. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_x"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_ANGULAR_X);
  46. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_y"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_ANGULAR_Y);
  47. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_z"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_ANGULAR_Z);
  48. }
  49. PhysicsBody3D::PhysicsBody3D(PhysicsServer3D::BodyMode p_mode) :
  50. CollisionObject3D(PhysicsServer3D::get_singleton()->body_create(), false) {
  51. set_body_mode(p_mode);
  52. }
  53. PhysicsBody3D::~PhysicsBody3D() {
  54. if (motion_cache.is_valid()) {
  55. motion_cache->owner = nullptr;
  56. }
  57. }
  58. TypedArray<PhysicsBody3D> PhysicsBody3D::get_collision_exceptions() {
  59. List<RID> exceptions;
  60. PhysicsServer3D::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);
  61. Array ret;
  62. for (const RID &body : exceptions) {
  63. ObjectID instance_id = PhysicsServer3D::get_singleton()->body_get_object_instance_id(body);
  64. Object *obj = ObjectDB::get_instance(instance_id);
  65. PhysicsBody3D *physics_body = Object::cast_to<PhysicsBody3D>(obj);
  66. ret.append(physics_body);
  67. }
  68. return ret;
  69. }
  70. void PhysicsBody3D::add_collision_exception_with(Node *p_node) {
  71. ERR_FAIL_NULL(p_node);
  72. CollisionObject3D *collision_object = Object::cast_to<CollisionObject3D>(p_node);
  73. ERR_FAIL_COND_MSG(!collision_object, "Collision exception only works between two CollisionObject3Ds.");
  74. PhysicsServer3D::get_singleton()->body_add_collision_exception(get_rid(), collision_object->get_rid());
  75. }
  76. void PhysicsBody3D::remove_collision_exception_with(Node *p_node) {
  77. ERR_FAIL_NULL(p_node);
  78. CollisionObject3D *collision_object = Object::cast_to<CollisionObject3D>(p_node);
  79. ERR_FAIL_COND_MSG(!collision_object, "Collision exception only works between two CollisionObject3Ds.");
  80. PhysicsServer3D::get_singleton()->body_remove_collision_exception(get_rid(), collision_object->get_rid());
  81. }
  82. Ref<KinematicCollision3D> PhysicsBody3D::_move(const Vector3 &p_distance, bool p_test_only, real_t p_margin, int p_max_collisions) {
  83. PhysicsServer3D::MotionParameters parameters(get_global_transform(), p_distance, p_margin);
  84. parameters.max_collisions = p_max_collisions;
  85. parameters.recovery_as_collision = false; // Don't report collisions generated only from recovery.
  86. PhysicsServer3D::MotionResult result;
  87. if (move_and_collide(parameters, result, p_test_only)) {
  88. // Create a new instance when the cached reference is invalid or still in use in script.
  89. if (motion_cache.is_null() || motion_cache->reference_get_count() > 1) {
  90. motion_cache.instantiate();
  91. motion_cache->owner = this;
  92. }
  93. motion_cache->result = result;
  94. return motion_cache;
  95. }
  96. return Ref<KinematicCollision3D>();
  97. }
  98. bool PhysicsBody3D::move_and_collide(const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult &r_result, bool p_test_only, bool p_cancel_sliding) {
  99. bool colliding = PhysicsServer3D::get_singleton()->body_test_motion(get_rid(), p_parameters, &r_result);
  100. // Restore direction of motion to be along original motion,
  101. // in order to avoid sliding due to recovery,
  102. // but only if collision depth is low enough to avoid tunneling.
  103. if (p_cancel_sliding) {
  104. real_t motion_length = p_parameters.motion.length();
  105. real_t precision = 0.001;
  106. if (colliding) {
  107. // Can't just use margin as a threshold because collision depth is calculated on unsafe motion,
  108. // so even in normal resting cases the depth can be a bit more than the margin.
  109. precision += motion_length * (r_result.collision_unsafe_fraction - r_result.collision_safe_fraction);
  110. if (r_result.collisions[0].depth > p_parameters.margin + precision) {
  111. p_cancel_sliding = false;
  112. }
  113. }
  114. if (p_cancel_sliding) {
  115. // When motion is null, recovery is the resulting motion.
  116. Vector3 motion_normal;
  117. if (motion_length > CMP_EPSILON) {
  118. motion_normal = p_parameters.motion / motion_length;
  119. }
  120. // Check depth of recovery.
  121. real_t projected_length = r_result.travel.dot(motion_normal);
  122. Vector3 recovery = r_result.travel - motion_normal * projected_length;
  123. real_t recovery_length = recovery.length();
  124. // Fixes cases where canceling slide causes the motion to go too deep into the ground,
  125. // because we're only taking rest information into account and not general recovery.
  126. if (recovery_length < p_parameters.margin + precision) {
  127. // Apply adjustment to motion.
  128. r_result.travel = motion_normal * projected_length;
  129. r_result.remainder = p_parameters.motion - r_result.travel;
  130. }
  131. }
  132. }
  133. for (int i = 0; i < 3; i++) {
  134. if (locked_axis & (1 << i)) {
  135. r_result.travel[i] = 0;
  136. }
  137. }
  138. if (!p_test_only) {
  139. Transform3D gt = p_parameters.from;
  140. gt.origin += r_result.travel;
  141. set_global_transform(gt);
  142. }
  143. return colliding;
  144. }
  145. bool PhysicsBody3D::test_move(const Transform3D &p_from, const Vector3 &p_distance, const Ref<KinematicCollision3D> &r_collision, real_t p_margin, int p_max_collisions) {
  146. ERR_FAIL_COND_V(!is_inside_tree(), false);
  147. PhysicsServer3D::MotionResult *r = nullptr;
  148. PhysicsServer3D::MotionResult temp_result;
  149. if (r_collision.is_valid()) {
  150. // Needs const_cast because method bindings don't support non-const Ref.
  151. r = const_cast<PhysicsServer3D::MotionResult *>(&r_collision->result);
  152. } else {
  153. r = &temp_result;
  154. }
  155. PhysicsServer3D::MotionParameters parameters(p_from, p_distance, p_margin);
  156. parameters.recovery_as_collision = false; // Don't report collisions generated only from recovery.
  157. return PhysicsServer3D::get_singleton()->body_test_motion(get_rid(), parameters, r);
  158. }
  159. void PhysicsBody3D::set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool p_lock) {
  160. if (p_lock) {
  161. locked_axis |= p_axis;
  162. } else {
  163. locked_axis &= (~p_axis);
  164. }
  165. PhysicsServer3D::get_singleton()->body_set_axis_lock(get_rid(), p_axis, p_lock);
  166. }
  167. bool PhysicsBody3D::get_axis_lock(PhysicsServer3D::BodyAxis p_axis) const {
  168. return (locked_axis & p_axis);
  169. }
  170. Vector3 PhysicsBody3D::get_linear_velocity() const {
  171. return Vector3();
  172. }
  173. Vector3 PhysicsBody3D::get_angular_velocity() const {
  174. return Vector3();
  175. }
  176. real_t PhysicsBody3D::get_inverse_mass() const {
  177. return 0;
  178. }
  179. void StaticBody3D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  180. if (physics_material_override.is_valid()) {
  181. if (physics_material_override->is_connected(CoreStringNames::get_singleton()->changed, callable_mp(this, &StaticBody3D::_reload_physics_characteristics))) {
  182. physics_material_override->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &StaticBody3D::_reload_physics_characteristics));
  183. }
  184. }
  185. physics_material_override = p_physics_material_override;
  186. if (physics_material_override.is_valid()) {
  187. physics_material_override->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &StaticBody3D::_reload_physics_characteristics));
  188. }
  189. _reload_physics_characteristics();
  190. }
  191. Ref<PhysicsMaterial> StaticBody3D::get_physics_material_override() const {
  192. return physics_material_override;
  193. }
  194. void StaticBody3D::set_constant_linear_velocity(const Vector3 &p_vel) {
  195. constant_linear_velocity = p_vel;
  196. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, constant_linear_velocity);
  197. }
  198. void StaticBody3D::set_constant_angular_velocity(const Vector3 &p_vel) {
  199. constant_angular_velocity = p_vel;
  200. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY, constant_angular_velocity);
  201. }
  202. Vector3 StaticBody3D::get_constant_linear_velocity() const {
  203. return constant_linear_velocity;
  204. }
  205. Vector3 StaticBody3D::get_constant_angular_velocity() const {
  206. return constant_angular_velocity;
  207. }
  208. void StaticBody3D::_bind_methods() {
  209. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "vel"), &StaticBody3D::set_constant_linear_velocity);
  210. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "vel"), &StaticBody3D::set_constant_angular_velocity);
  211. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity"), &StaticBody3D::get_constant_linear_velocity);
  212. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity"), &StaticBody3D::get_constant_angular_velocity);
  213. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &StaticBody3D::set_physics_material_override);
  214. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &StaticBody3D::get_physics_material_override);
  215. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  216. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_linear_velocity", PROPERTY_HINT_NONE, "suffix:m/s"), "set_constant_linear_velocity", "get_constant_linear_velocity");
  217. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_angular_velocity", PROPERTY_HINT_NONE, U"radians,suffix:\u00B0/s"), "set_constant_angular_velocity", "get_constant_angular_velocity");
  218. }
  219. StaticBody3D::StaticBody3D(PhysicsServer3D::BodyMode p_mode) :
  220. PhysicsBody3D(p_mode) {
  221. }
  222. void StaticBody3D::_reload_physics_characteristics() {
  223. if (physics_material_override.is_null()) {
  224. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, 0);
  225. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, 1);
  226. } else {
  227. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  228. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  229. }
  230. }
  231. Vector3 AnimatableBody3D::get_linear_velocity() const {
  232. return linear_velocity;
  233. }
  234. Vector3 AnimatableBody3D::get_angular_velocity() const {
  235. return angular_velocity;
  236. }
  237. void AnimatableBody3D::set_sync_to_physics(bool p_enable) {
  238. if (sync_to_physics == p_enable) {
  239. return;
  240. }
  241. sync_to_physics = p_enable;
  242. _update_kinematic_motion();
  243. }
  244. bool AnimatableBody3D::is_sync_to_physics_enabled() const {
  245. return sync_to_physics;
  246. }
  247. void AnimatableBody3D::_update_kinematic_motion() {
  248. #ifdef TOOLS_ENABLED
  249. if (Engine::get_singleton()->is_editor_hint()) {
  250. return;
  251. }
  252. #endif
  253. if (sync_to_physics) {
  254. set_only_update_transform_changes(true);
  255. set_notify_local_transform(true);
  256. } else {
  257. set_only_update_transform_changes(false);
  258. set_notify_local_transform(false);
  259. }
  260. }
  261. void AnimatableBody3D::_body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state) {
  262. AnimatableBody3D *body = (AnimatableBody3D *)p_instance;
  263. body->_body_state_changed(p_state);
  264. }
  265. void AnimatableBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
  266. linear_velocity = p_state->get_linear_velocity();
  267. angular_velocity = p_state->get_angular_velocity();
  268. if (!sync_to_physics) {
  269. return;
  270. }
  271. last_valid_transform = p_state->get_transform();
  272. set_notify_local_transform(false);
  273. set_global_transform(last_valid_transform);
  274. set_notify_local_transform(true);
  275. _on_transform_changed();
  276. }
  277. void AnimatableBody3D::_notification(int p_what) {
  278. switch (p_what) {
  279. case NOTIFICATION_ENTER_TREE: {
  280. last_valid_transform = get_global_transform();
  281. _update_kinematic_motion();
  282. } break;
  283. case NOTIFICATION_EXIT_TREE: {
  284. set_only_update_transform_changes(false);
  285. set_notify_local_transform(false);
  286. } break;
  287. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  288. // Used by sync to physics, send the new transform to the physics...
  289. Transform3D new_transform = get_global_transform();
  290. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_TRANSFORM, new_transform);
  291. // ... but then revert changes.
  292. set_notify_local_transform(false);
  293. set_global_transform(last_valid_transform);
  294. set_notify_local_transform(true);
  295. _on_transform_changed();
  296. } break;
  297. }
  298. }
  299. void AnimatableBody3D::_bind_methods() {
  300. ClassDB::bind_method(D_METHOD("set_sync_to_physics", "enable"), &AnimatableBody3D::set_sync_to_physics);
  301. ClassDB::bind_method(D_METHOD("is_sync_to_physics_enabled"), &AnimatableBody3D::is_sync_to_physics_enabled);
  302. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync_to_physics"), "set_sync_to_physics", "is_sync_to_physics_enabled");
  303. }
  304. AnimatableBody3D::AnimatableBody3D() :
  305. StaticBody3D(PhysicsServer3D::BODY_MODE_KINEMATIC) {
  306. PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), this, _body_state_changed_callback);
  307. }
  308. void RigidBody3D::_body_enter_tree(ObjectID p_id) {
  309. Object *obj = ObjectDB::get_instance(p_id);
  310. Node *node = Object::cast_to<Node>(obj);
  311. ERR_FAIL_COND(!node);
  312. ERR_FAIL_COND(!contact_monitor);
  313. HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(p_id);
  314. ERR_FAIL_COND(!E);
  315. ERR_FAIL_COND(E->value.in_tree);
  316. E->value.in_tree = true;
  317. contact_monitor->locked = true;
  318. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  319. for (int i = 0; i < E->value.shapes.size(); i++) {
  320. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].local_shape);
  321. }
  322. contact_monitor->locked = false;
  323. }
  324. void RigidBody3D::_body_exit_tree(ObjectID p_id) {
  325. Object *obj = ObjectDB::get_instance(p_id);
  326. Node *node = Object::cast_to<Node>(obj);
  327. ERR_FAIL_COND(!node);
  328. ERR_FAIL_COND(!contact_monitor);
  329. HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(p_id);
  330. ERR_FAIL_COND(!E);
  331. ERR_FAIL_COND(!E->value.in_tree);
  332. E->value.in_tree = false;
  333. contact_monitor->locked = true;
  334. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  335. for (int i = 0; i < E->value.shapes.size(); i++) {
  336. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].local_shape);
  337. }
  338. contact_monitor->locked = false;
  339. }
  340. void RigidBody3D::_body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape) {
  341. bool body_in = p_status == 1;
  342. ObjectID objid = p_instance;
  343. Object *obj = ObjectDB::get_instance(objid);
  344. Node *node = Object::cast_to<Node>(obj);
  345. ERR_FAIL_COND(!contact_monitor);
  346. HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(objid);
  347. ERR_FAIL_COND(!body_in && !E);
  348. if (body_in) {
  349. if (!E) {
  350. E = contact_monitor->body_map.insert(objid, BodyState());
  351. E->value.rid = p_body;
  352. //E->value.rc=0;
  353. E->value.in_tree = node && node->is_inside_tree();
  354. if (node) {
  355. node->connect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &RigidBody3D::_body_enter_tree).bind(objid));
  356. node->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &RigidBody3D::_body_exit_tree).bind(objid));
  357. if (E->value.in_tree) {
  358. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  359. }
  360. }
  361. }
  362. //E->value.rc++;
  363. if (node) {
  364. E->value.shapes.insert(ShapePair(p_body_shape, p_local_shape));
  365. }
  366. if (E->value.in_tree) {
  367. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, p_body, node, p_body_shape, p_local_shape);
  368. }
  369. } else {
  370. //E->value.rc--;
  371. if (node) {
  372. E->value.shapes.erase(ShapePair(p_body_shape, p_local_shape));
  373. }
  374. bool in_tree = E->value.in_tree;
  375. if (E->value.shapes.is_empty()) {
  376. if (node) {
  377. node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &RigidBody3D::_body_enter_tree));
  378. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &RigidBody3D::_body_exit_tree));
  379. if (in_tree) {
  380. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  381. }
  382. }
  383. contact_monitor->body_map.remove(E);
  384. }
  385. if (node && in_tree) {
  386. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, p_body, obj, p_body_shape, p_local_shape);
  387. }
  388. }
  389. }
  390. struct _RigidBodyInOut {
  391. RID rid;
  392. ObjectID id;
  393. int shape = 0;
  394. int local_shape = 0;
  395. };
  396. void RigidBody3D::_body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state) {
  397. RigidBody3D *body = (RigidBody3D *)p_instance;
  398. body->_body_state_changed(p_state);
  399. }
  400. void RigidBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
  401. set_ignore_transform_notification(true);
  402. set_global_transform(p_state->get_transform());
  403. linear_velocity = p_state->get_linear_velocity();
  404. angular_velocity = p_state->get_angular_velocity();
  405. inverse_inertia_tensor = p_state->get_inverse_inertia_tensor();
  406. if (sleeping != p_state->is_sleeping()) {
  407. sleeping = p_state->is_sleeping();
  408. emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed);
  409. }
  410. GDVIRTUAL_CALL(_integrate_forces, p_state);
  411. set_ignore_transform_notification(false);
  412. _on_transform_changed();
  413. if (contact_monitor) {
  414. contact_monitor->locked = true;
  415. //untag all
  416. int rc = 0;
  417. for (KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
  418. for (int i = 0; i < E.value.shapes.size(); i++) {
  419. E.value.shapes[i].tagged = false;
  420. rc++;
  421. }
  422. }
  423. _RigidBodyInOut *toadd = (_RigidBodyInOut *)alloca(p_state->get_contact_count() * sizeof(_RigidBodyInOut));
  424. int toadd_count = 0;
  425. RigidBody3D_RemoveAction *toremove = (RigidBody3D_RemoveAction *)alloca(rc * sizeof(RigidBody3D_RemoveAction));
  426. int toremove_count = 0;
  427. //put the ones to add
  428. for (int i = 0; i < p_state->get_contact_count(); i++) {
  429. RID rid = p_state->get_contact_collider(i);
  430. ObjectID obj = p_state->get_contact_collider_id(i);
  431. int local_shape = p_state->get_contact_local_shape(i);
  432. int shape = p_state->get_contact_collider_shape(i);
  433. HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(obj);
  434. if (!E) {
  435. toadd[toadd_count].rid = rid;
  436. toadd[toadd_count].local_shape = local_shape;
  437. toadd[toadd_count].id = obj;
  438. toadd[toadd_count].shape = shape;
  439. toadd_count++;
  440. continue;
  441. }
  442. ShapePair sp(shape, local_shape);
  443. int idx = E->value.shapes.find(sp);
  444. if (idx == -1) {
  445. toadd[toadd_count].rid = rid;
  446. toadd[toadd_count].local_shape = local_shape;
  447. toadd[toadd_count].id = obj;
  448. toadd[toadd_count].shape = shape;
  449. toadd_count++;
  450. continue;
  451. }
  452. E->value.shapes[idx].tagged = true;
  453. }
  454. //put the ones to remove
  455. for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
  456. for (int i = 0; i < E.value.shapes.size(); i++) {
  457. if (!E.value.shapes[i].tagged) {
  458. toremove[toremove_count].rid = E.value.rid;
  459. toremove[toremove_count].body_id = E.key;
  460. toremove[toremove_count].pair = E.value.shapes[i];
  461. toremove_count++;
  462. }
  463. }
  464. }
  465. //process removals
  466. for (int i = 0; i < toremove_count; i++) {
  467. _body_inout(0, toremove[i].rid, toremove[i].body_id, toremove[i].pair.body_shape, toremove[i].pair.local_shape);
  468. }
  469. //process additions
  470. for (int i = 0; i < toadd_count; i++) {
  471. _body_inout(1, toremove[i].rid, toadd[i].id, toadd[i].shape, toadd[i].local_shape);
  472. }
  473. contact_monitor->locked = false;
  474. }
  475. }
  476. void RigidBody3D::_notification(int p_what) {
  477. #ifdef TOOLS_ENABLED
  478. switch (p_what) {
  479. case NOTIFICATION_ENTER_TREE: {
  480. if (Engine::get_singleton()->is_editor_hint()) {
  481. set_notify_local_transform(true); // Used for warnings and only in editor.
  482. }
  483. } break;
  484. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  485. if (Engine::get_singleton()->is_editor_hint()) {
  486. update_configuration_warnings();
  487. }
  488. } break;
  489. }
  490. #endif
  491. }
  492. void RigidBody3D::_apply_body_mode() {
  493. if (freeze) {
  494. switch (freeze_mode) {
  495. case FREEZE_MODE_STATIC: {
  496. set_body_mode(PhysicsServer3D::BODY_MODE_STATIC);
  497. } break;
  498. case FREEZE_MODE_KINEMATIC: {
  499. set_body_mode(PhysicsServer3D::BODY_MODE_KINEMATIC);
  500. } break;
  501. }
  502. } else if (lock_rotation) {
  503. set_body_mode(PhysicsServer3D::BODY_MODE_RIGID_LINEAR);
  504. } else {
  505. set_body_mode(PhysicsServer3D::BODY_MODE_RIGID);
  506. }
  507. }
  508. void RigidBody3D::set_lock_rotation_enabled(bool p_lock_rotation) {
  509. if (p_lock_rotation == lock_rotation) {
  510. return;
  511. }
  512. lock_rotation = p_lock_rotation;
  513. _apply_body_mode();
  514. }
  515. bool RigidBody3D::is_lock_rotation_enabled() const {
  516. return lock_rotation;
  517. }
  518. void RigidBody3D::set_freeze_enabled(bool p_freeze) {
  519. if (p_freeze == freeze) {
  520. return;
  521. }
  522. freeze = p_freeze;
  523. _apply_body_mode();
  524. }
  525. bool RigidBody3D::is_freeze_enabled() const {
  526. return freeze;
  527. }
  528. void RigidBody3D::set_freeze_mode(FreezeMode p_freeze_mode) {
  529. if (p_freeze_mode == freeze_mode) {
  530. return;
  531. }
  532. freeze_mode = p_freeze_mode;
  533. _apply_body_mode();
  534. }
  535. RigidBody3D::FreezeMode RigidBody3D::get_freeze_mode() const {
  536. return freeze_mode;
  537. }
  538. void RigidBody3D::set_mass(real_t p_mass) {
  539. ERR_FAIL_COND(p_mass <= 0);
  540. mass = p_mass;
  541. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_MASS, mass);
  542. }
  543. real_t RigidBody3D::get_mass() const {
  544. return mass;
  545. }
  546. void RigidBody3D::set_inertia(const Vector3 &p_inertia) {
  547. ERR_FAIL_COND(p_inertia.x < 0);
  548. ERR_FAIL_COND(p_inertia.y < 0);
  549. ERR_FAIL_COND(p_inertia.z < 0);
  550. inertia = p_inertia;
  551. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_INERTIA, inertia);
  552. }
  553. const Vector3 &RigidBody3D::get_inertia() const {
  554. return inertia;
  555. }
  556. void RigidBody3D::set_center_of_mass_mode(CenterOfMassMode p_mode) {
  557. if (center_of_mass_mode == p_mode) {
  558. return;
  559. }
  560. center_of_mass_mode = p_mode;
  561. switch (center_of_mass_mode) {
  562. case CENTER_OF_MASS_MODE_AUTO: {
  563. center_of_mass = Vector3();
  564. PhysicsServer3D::get_singleton()->body_reset_mass_properties(get_rid());
  565. if (inertia != Vector3()) {
  566. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_INERTIA, inertia);
  567. }
  568. } break;
  569. case CENTER_OF_MASS_MODE_CUSTOM: {
  570. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS, center_of_mass);
  571. } break;
  572. }
  573. }
  574. RigidBody3D::CenterOfMassMode RigidBody3D::get_center_of_mass_mode() const {
  575. return center_of_mass_mode;
  576. }
  577. void RigidBody3D::set_center_of_mass(const Vector3 &p_center_of_mass) {
  578. if (center_of_mass == p_center_of_mass) {
  579. return;
  580. }
  581. ERR_FAIL_COND(center_of_mass_mode != CENTER_OF_MASS_MODE_CUSTOM);
  582. center_of_mass = p_center_of_mass;
  583. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS, center_of_mass);
  584. }
  585. const Vector3 &RigidBody3D::get_center_of_mass() const {
  586. return center_of_mass;
  587. }
  588. void RigidBody3D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  589. if (physics_material_override.is_valid()) {
  590. if (physics_material_override->is_connected(CoreStringNames::get_singleton()->changed, callable_mp(this, &RigidBody3D::_reload_physics_characteristics))) {
  591. physics_material_override->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &RigidBody3D::_reload_physics_characteristics));
  592. }
  593. }
  594. physics_material_override = p_physics_material_override;
  595. if (physics_material_override.is_valid()) {
  596. physics_material_override->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &RigidBody3D::_reload_physics_characteristics));
  597. }
  598. _reload_physics_characteristics();
  599. }
  600. Ref<PhysicsMaterial> RigidBody3D::get_physics_material_override() const {
  601. return physics_material_override;
  602. }
  603. void RigidBody3D::set_gravity_scale(real_t p_gravity_scale) {
  604. gravity_scale = p_gravity_scale;
  605. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
  606. }
  607. real_t RigidBody3D::get_gravity_scale() const {
  608. return gravity_scale;
  609. }
  610. void RigidBody3D::set_linear_damp_mode(DampMode p_mode) {
  611. linear_damp_mode = p_mode;
  612. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE, linear_damp_mode);
  613. }
  614. RigidBody3D::DampMode RigidBody3D::get_linear_damp_mode() const {
  615. return linear_damp_mode;
  616. }
  617. void RigidBody3D::set_angular_damp_mode(DampMode p_mode) {
  618. angular_damp_mode = p_mode;
  619. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE, angular_damp_mode);
  620. }
  621. RigidBody3D::DampMode RigidBody3D::get_angular_damp_mode() const {
  622. return angular_damp_mode;
  623. }
  624. void RigidBody3D::set_linear_damp(real_t p_linear_damp) {
  625. ERR_FAIL_COND(p_linear_damp < 0.0);
  626. linear_damp = p_linear_damp;
  627. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP, linear_damp);
  628. }
  629. real_t RigidBody3D::get_linear_damp() const {
  630. return linear_damp;
  631. }
  632. void RigidBody3D::set_angular_damp(real_t p_angular_damp) {
  633. ERR_FAIL_COND(p_angular_damp < 0.0);
  634. angular_damp = p_angular_damp;
  635. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP, angular_damp);
  636. }
  637. real_t RigidBody3D::get_angular_damp() const {
  638. return angular_damp;
  639. }
  640. void RigidBody3D::set_axis_velocity(const Vector3 &p_axis) {
  641. Vector3 axis = p_axis.normalized();
  642. linear_velocity -= axis * axis.dot(linear_velocity);
  643. linear_velocity += p_axis;
  644. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  645. }
  646. void RigidBody3D::set_linear_velocity(const Vector3 &p_velocity) {
  647. linear_velocity = p_velocity;
  648. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  649. }
  650. Vector3 RigidBody3D::get_linear_velocity() const {
  651. return linear_velocity;
  652. }
  653. void RigidBody3D::set_angular_velocity(const Vector3 &p_velocity) {
  654. angular_velocity = p_velocity;
  655. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
  656. }
  657. Vector3 RigidBody3D::get_angular_velocity() const {
  658. return angular_velocity;
  659. }
  660. Basis RigidBody3D::get_inverse_inertia_tensor() const {
  661. return inverse_inertia_tensor;
  662. }
  663. void RigidBody3D::set_use_custom_integrator(bool p_enable) {
  664. if (custom_integrator == p_enable) {
  665. return;
  666. }
  667. custom_integrator = p_enable;
  668. PhysicsServer3D::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
  669. }
  670. bool RigidBody3D::is_using_custom_integrator() {
  671. return custom_integrator;
  672. }
  673. void RigidBody3D::set_sleeping(bool p_sleeping) {
  674. sleeping = p_sleeping;
  675. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_SLEEPING, sleeping);
  676. }
  677. void RigidBody3D::set_can_sleep(bool p_active) {
  678. can_sleep = p_active;
  679. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_CAN_SLEEP, p_active);
  680. }
  681. bool RigidBody3D::is_able_to_sleep() const {
  682. return can_sleep;
  683. }
  684. bool RigidBody3D::is_sleeping() const {
  685. return sleeping;
  686. }
  687. void RigidBody3D::set_max_contacts_reported(int p_amount) {
  688. max_contacts_reported = p_amount;
  689. PhysicsServer3D::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount);
  690. }
  691. int RigidBody3D::get_max_contacts_reported() const {
  692. return max_contacts_reported;
  693. }
  694. int RigidBody3D::get_contact_count() const {
  695. PhysicsDirectBodyState3D *bs = PhysicsServer3D::get_singleton()->body_get_direct_state(get_rid());
  696. ERR_FAIL_NULL_V(bs, 0);
  697. return bs->get_contact_count();
  698. }
  699. void RigidBody3D::apply_central_impulse(const Vector3 &p_impulse) {
  700. PhysicsServer3D::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
  701. }
  702. void RigidBody3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) {
  703. PhysicsServer3D *singleton = PhysicsServer3D::get_singleton();
  704. singleton->body_apply_impulse(get_rid(), p_impulse, p_position);
  705. }
  706. void RigidBody3D::apply_torque_impulse(const Vector3 &p_impulse) {
  707. PhysicsServer3D::get_singleton()->body_apply_torque_impulse(get_rid(), p_impulse);
  708. }
  709. void RigidBody3D::apply_central_force(const Vector3 &p_force) {
  710. PhysicsServer3D::get_singleton()->body_apply_central_force(get_rid(), p_force);
  711. }
  712. void RigidBody3D::apply_force(const Vector3 &p_force, const Vector3 &p_position) {
  713. PhysicsServer3D *singleton = PhysicsServer3D::get_singleton();
  714. singleton->body_apply_force(get_rid(), p_force, p_position);
  715. }
  716. void RigidBody3D::apply_torque(const Vector3 &p_torque) {
  717. PhysicsServer3D::get_singleton()->body_apply_torque(get_rid(), p_torque);
  718. }
  719. void RigidBody3D::add_constant_central_force(const Vector3 &p_force) {
  720. PhysicsServer3D::get_singleton()->body_add_constant_central_force(get_rid(), p_force);
  721. }
  722. void RigidBody3D::add_constant_force(const Vector3 &p_force, const Vector3 &p_position) {
  723. PhysicsServer3D *singleton = PhysicsServer3D::get_singleton();
  724. singleton->body_add_constant_force(get_rid(), p_force, p_position);
  725. }
  726. void RigidBody3D::add_constant_torque(const Vector3 &p_torque) {
  727. PhysicsServer3D::get_singleton()->body_add_constant_torque(get_rid(), p_torque);
  728. }
  729. void RigidBody3D::set_constant_force(const Vector3 &p_force) {
  730. PhysicsServer3D::get_singleton()->body_set_constant_force(get_rid(), p_force);
  731. }
  732. Vector3 RigidBody3D::get_constant_force() const {
  733. return PhysicsServer3D::get_singleton()->body_get_constant_force(get_rid());
  734. }
  735. void RigidBody3D::set_constant_torque(const Vector3 &p_torque) {
  736. PhysicsServer3D::get_singleton()->body_set_constant_torque(get_rid(), p_torque);
  737. }
  738. Vector3 RigidBody3D::get_constant_torque() const {
  739. return PhysicsServer3D::get_singleton()->body_get_constant_torque(get_rid());
  740. }
  741. void RigidBody3D::set_use_continuous_collision_detection(bool p_enable) {
  742. ccd = p_enable;
  743. PhysicsServer3D::get_singleton()->body_set_enable_continuous_collision_detection(get_rid(), p_enable);
  744. }
  745. bool RigidBody3D::is_using_continuous_collision_detection() const {
  746. return ccd;
  747. }
  748. void RigidBody3D::set_contact_monitor(bool p_enabled) {
  749. if (p_enabled == is_contact_monitor_enabled()) {
  750. return;
  751. }
  752. if (!p_enabled) {
  753. ERR_FAIL_COND_MSG(contact_monitor->locked, "Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\", false) instead.");
  754. for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
  755. //clean up mess
  756. Object *obj = ObjectDB::get_instance(E.key);
  757. Node *node = Object::cast_to<Node>(obj);
  758. if (node) {
  759. node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &RigidBody3D::_body_enter_tree));
  760. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &RigidBody3D::_body_exit_tree));
  761. }
  762. }
  763. memdelete(contact_monitor);
  764. contact_monitor = nullptr;
  765. } else {
  766. contact_monitor = memnew(ContactMonitor);
  767. contact_monitor->locked = false;
  768. }
  769. }
  770. bool RigidBody3D::is_contact_monitor_enabled() const {
  771. return contact_monitor != nullptr;
  772. }
  773. TypedArray<Node3D> RigidBody3D::get_colliding_bodies() const {
  774. ERR_FAIL_COND_V(!contact_monitor, TypedArray<Node3D>());
  775. TypedArray<Node3D> ret;
  776. ret.resize(contact_monitor->body_map.size());
  777. int idx = 0;
  778. for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
  779. Object *obj = ObjectDB::get_instance(E.key);
  780. if (!obj) {
  781. ret.resize(ret.size() - 1); //ops
  782. } else {
  783. ret[idx++] = obj;
  784. }
  785. }
  786. return ret;
  787. }
  788. TypedArray<String> RigidBody3D::get_configuration_warnings() const {
  789. Transform3D t = get_transform();
  790. TypedArray<String> warnings = Node::get_configuration_warnings();
  791. if (ABS(t.basis.get_column(0).length() - 1.0) > 0.05 || ABS(t.basis.get_column(1).length() - 1.0) > 0.05 || ABS(t.basis.get_column(2).length() - 1.0) > 0.05) {
  792. warnings.push_back(RTR("Size changes to RigidBody will be overridden by the physics engine when running.\nChange the size in children collision shapes instead."));
  793. }
  794. return warnings;
  795. }
  796. void RigidBody3D::_bind_methods() {
  797. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &RigidBody3D::set_mass);
  798. ClassDB::bind_method(D_METHOD("get_mass"), &RigidBody3D::get_mass);
  799. ClassDB::bind_method(D_METHOD("set_inertia", "inertia"), &RigidBody3D::set_inertia);
  800. ClassDB::bind_method(D_METHOD("get_inertia"), &RigidBody3D::get_inertia);
  801. ClassDB::bind_method(D_METHOD("set_center_of_mass_mode", "mode"), &RigidBody3D::set_center_of_mass_mode);
  802. ClassDB::bind_method(D_METHOD("get_center_of_mass_mode"), &RigidBody3D::get_center_of_mass_mode);
  803. ClassDB::bind_method(D_METHOD("set_center_of_mass", "center_of_mass"), &RigidBody3D::set_center_of_mass);
  804. ClassDB::bind_method(D_METHOD("get_center_of_mass"), &RigidBody3D::get_center_of_mass);
  805. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &RigidBody3D::set_physics_material_override);
  806. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &RigidBody3D::get_physics_material_override);
  807. ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &RigidBody3D::set_linear_velocity);
  808. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &RigidBody3D::get_linear_velocity);
  809. ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &RigidBody3D::set_angular_velocity);
  810. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &RigidBody3D::get_angular_velocity);
  811. ClassDB::bind_method(D_METHOD("get_inverse_inertia_tensor"), &RigidBody3D::get_inverse_inertia_tensor);
  812. ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &RigidBody3D::set_gravity_scale);
  813. ClassDB::bind_method(D_METHOD("get_gravity_scale"), &RigidBody3D::get_gravity_scale);
  814. ClassDB::bind_method(D_METHOD("set_linear_damp_mode", "linear_damp_mode"), &RigidBody3D::set_linear_damp_mode);
  815. ClassDB::bind_method(D_METHOD("get_linear_damp_mode"), &RigidBody3D::get_linear_damp_mode);
  816. ClassDB::bind_method(D_METHOD("set_angular_damp_mode", "angular_damp_mode"), &RigidBody3D::set_angular_damp_mode);
  817. ClassDB::bind_method(D_METHOD("get_angular_damp_mode"), &RigidBody3D::get_angular_damp_mode);
  818. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &RigidBody3D::set_linear_damp);
  819. ClassDB::bind_method(D_METHOD("get_linear_damp"), &RigidBody3D::get_linear_damp);
  820. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &RigidBody3D::set_angular_damp);
  821. ClassDB::bind_method(D_METHOD("get_angular_damp"), &RigidBody3D::get_angular_damp);
  822. ClassDB::bind_method(D_METHOD("set_max_contacts_reported", "amount"), &RigidBody3D::set_max_contacts_reported);
  823. ClassDB::bind_method(D_METHOD("get_max_contacts_reported"), &RigidBody3D::get_max_contacts_reported);
  824. ClassDB::bind_method(D_METHOD("get_contact_count"), &RigidBody3D::get_contact_count);
  825. ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &RigidBody3D::set_use_custom_integrator);
  826. ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &RigidBody3D::is_using_custom_integrator);
  827. ClassDB::bind_method(D_METHOD("set_contact_monitor", "enabled"), &RigidBody3D::set_contact_monitor);
  828. ClassDB::bind_method(D_METHOD("is_contact_monitor_enabled"), &RigidBody3D::is_contact_monitor_enabled);
  829. ClassDB::bind_method(D_METHOD("set_use_continuous_collision_detection", "enable"), &RigidBody3D::set_use_continuous_collision_detection);
  830. ClassDB::bind_method(D_METHOD("is_using_continuous_collision_detection"), &RigidBody3D::is_using_continuous_collision_detection);
  831. ClassDB::bind_method(D_METHOD("set_axis_velocity", "axis_velocity"), &RigidBody3D::set_axis_velocity);
  832. ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &RigidBody3D::apply_central_impulse);
  833. ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &RigidBody3D::apply_impulse, Vector3());
  834. ClassDB::bind_method(D_METHOD("apply_torque_impulse", "impulse"), &RigidBody3D::apply_torque_impulse);
  835. ClassDB::bind_method(D_METHOD("apply_central_force", "force"), &RigidBody3D::apply_central_force);
  836. ClassDB::bind_method(D_METHOD("apply_force", "force", "position"), &RigidBody3D::apply_force, Vector3());
  837. ClassDB::bind_method(D_METHOD("apply_torque", "torque"), &RigidBody3D::apply_torque);
  838. ClassDB::bind_method(D_METHOD("add_constant_central_force", "force"), &RigidBody3D::add_constant_central_force);
  839. ClassDB::bind_method(D_METHOD("add_constant_force", "force", "position"), &RigidBody3D::add_constant_force, Vector3());
  840. ClassDB::bind_method(D_METHOD("add_constant_torque", "torque"), &RigidBody3D::add_constant_torque);
  841. ClassDB::bind_method(D_METHOD("set_constant_force", "force"), &RigidBody3D::set_constant_force);
  842. ClassDB::bind_method(D_METHOD("get_constant_force"), &RigidBody3D::get_constant_force);
  843. ClassDB::bind_method(D_METHOD("set_constant_torque", "torque"), &RigidBody3D::set_constant_torque);
  844. ClassDB::bind_method(D_METHOD("get_constant_torque"), &RigidBody3D::get_constant_torque);
  845. ClassDB::bind_method(D_METHOD("set_sleeping", "sleeping"), &RigidBody3D::set_sleeping);
  846. ClassDB::bind_method(D_METHOD("is_sleeping"), &RigidBody3D::is_sleeping);
  847. ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &RigidBody3D::set_can_sleep);
  848. ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &RigidBody3D::is_able_to_sleep);
  849. ClassDB::bind_method(D_METHOD("set_lock_rotation_enabled", "lock_rotation"), &RigidBody3D::set_lock_rotation_enabled);
  850. ClassDB::bind_method(D_METHOD("is_lock_rotation_enabled"), &RigidBody3D::is_lock_rotation_enabled);
  851. ClassDB::bind_method(D_METHOD("set_freeze_enabled", "freeze_mode"), &RigidBody3D::set_freeze_enabled);
  852. ClassDB::bind_method(D_METHOD("is_freeze_enabled"), &RigidBody3D::is_freeze_enabled);
  853. ClassDB::bind_method(D_METHOD("set_freeze_mode", "freeze_mode"), &RigidBody3D::set_freeze_mode);
  854. ClassDB::bind_method(D_METHOD("get_freeze_mode"), &RigidBody3D::get_freeze_mode);
  855. ClassDB::bind_method(D_METHOD("get_colliding_bodies"), &RigidBody3D::get_colliding_bodies);
  856. GDVIRTUAL_BIND(_integrate_forces, "state");
  857. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass", PROPERTY_HINT_RANGE, "0.01,1000,0.01,or_greater,exp,suffix:kg"), "set_mass", "get_mass");
  858. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "inertia", PROPERTY_HINT_RANGE, U"0,1000,0.01,or_greater,exp,suffix:kg\u22C5m\u00B2"), "set_inertia", "get_inertia");
  859. ADD_PROPERTY(PropertyInfo(Variant::INT, "center_of_mass_mode", PROPERTY_HINT_ENUM, "Auto,Custom", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_center_of_mass_mode", "get_center_of_mass_mode");
  860. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_of_mass", PROPERTY_HINT_RANGE, "-10,10,0.01,or_lesser,or_greater,suffix:m"), "set_center_of_mass", "get_center_of_mass");
  861. ADD_LINKED_PROPERTY("center_of_mass_mode", "center_of_mass");
  862. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  863. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_scale", PROPERTY_HINT_RANGE, "-128,128,0.01"), "set_gravity_scale", "get_gravity_scale");
  864. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
  865. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "continuous_cd"), "set_use_continuous_collision_detection", "is_using_continuous_collision_detection");
  866. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_contacts_reported", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), "set_max_contacts_reported", "get_max_contacts_reported");
  867. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor"), "set_contact_monitor", "is_contact_monitor_enabled");
  868. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleeping", "is_sleeping");
  869. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
  870. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "lock_rotation"), "set_lock_rotation_enabled", "is_lock_rotation_enabled");
  871. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "freeze"), "set_freeze_enabled", "is_freeze_enabled");
  872. ADD_PROPERTY(PropertyInfo(Variant::INT, "freeze_mode", PROPERTY_HINT_ENUM, "Static,Kinematic"), "set_freeze_mode", "get_freeze_mode");
  873. ADD_GROUP("Linear", "linear_");
  874. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity", PROPERTY_HINT_NONE, "suffix:m/s"), "set_linear_velocity", "get_linear_velocity");
  875. ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_linear_damp_mode", "get_linear_damp_mode");
  876. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
  877. ADD_GROUP("Angular", "angular_");
  878. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity", PROPERTY_HINT_NONE, U"radians,suffix:\u00B0/s"), "set_angular_velocity", "get_angular_velocity");
  879. ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_angular_damp_mode", "get_angular_damp_mode");
  880. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
  881. ADD_GROUP("Constant Forces", "constant_");
  882. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_force"), "set_constant_force", "get_constant_force");
  883. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_torque"), "set_constant_torque", "get_constant_torque");
  884. ADD_SIGNAL(MethodInfo("body_shape_entered", PropertyInfo(Variant::RID, "body_rid"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::INT, "body_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  885. ADD_SIGNAL(MethodInfo("body_shape_exited", PropertyInfo(Variant::RID, "body_rid"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::INT, "body_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  886. ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  887. ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  888. ADD_SIGNAL(MethodInfo("sleeping_state_changed"));
  889. BIND_ENUM_CONSTANT(FREEZE_MODE_STATIC);
  890. BIND_ENUM_CONSTANT(FREEZE_MODE_KINEMATIC);
  891. BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_AUTO);
  892. BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_CUSTOM);
  893. BIND_ENUM_CONSTANT(DAMP_MODE_COMBINE);
  894. BIND_ENUM_CONSTANT(DAMP_MODE_REPLACE);
  895. }
  896. void RigidBody3D::_validate_property(PropertyInfo &p_property) const {
  897. if (center_of_mass_mode != CENTER_OF_MASS_MODE_CUSTOM) {
  898. if (p_property.name == "center_of_mass") {
  899. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  900. }
  901. }
  902. }
  903. RigidBody3D::RigidBody3D() :
  904. PhysicsBody3D(PhysicsServer3D::BODY_MODE_RIGID) {
  905. PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), this, _body_state_changed_callback);
  906. }
  907. RigidBody3D::~RigidBody3D() {
  908. if (contact_monitor) {
  909. memdelete(contact_monitor);
  910. }
  911. }
  912. void RigidBody3D::_reload_physics_characteristics() {
  913. if (physics_material_override.is_null()) {
  914. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, 0);
  915. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, 1);
  916. } else {
  917. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  918. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  919. }
  920. }
  921. ///////////////////////////////////////
  922. //so, if you pass 45 as limit, avoid numerical precision errors when angle is 45.
  923. #define FLOOR_ANGLE_THRESHOLD 0.01
  924. bool CharacterBody3D::move_and_slide() {
  925. // Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky
  926. double delta = Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time();
  927. for (int i = 0; i < 3; i++) {
  928. if (locked_axis & (1 << i)) {
  929. velocity[i] = 0.0;
  930. }
  931. }
  932. Transform3D gt = get_global_transform();
  933. previous_position = gt.origin;
  934. Vector3 current_platform_velocity = platform_velocity;
  935. if ((collision_state.floor || collision_state.wall) && platform_rid.is_valid()) {
  936. bool excluded = false;
  937. if (collision_state.floor) {
  938. excluded = (platform_floor_layers & platform_layer) == 0;
  939. } else if (collision_state.wall) {
  940. excluded = (platform_wall_layers & platform_layer) == 0;
  941. }
  942. if (!excluded) {
  943. //this approach makes sure there is less delay between the actual body velocity and the one we saved
  944. PhysicsDirectBodyState3D *bs = PhysicsServer3D::get_singleton()->body_get_direct_state(platform_rid);
  945. if (bs) {
  946. Vector3 local_position = gt.origin - bs->get_transform().origin;
  947. current_platform_velocity = bs->get_velocity_at_local_position(local_position);
  948. } else {
  949. // Body is removed or destroyed, invalidate floor.
  950. current_platform_velocity = Vector3();
  951. platform_rid = RID();
  952. }
  953. } else {
  954. current_platform_velocity = Vector3();
  955. }
  956. }
  957. motion_results.clear();
  958. bool was_on_floor = collision_state.floor;
  959. collision_state.state = 0;
  960. last_motion = Vector3();
  961. if (!current_platform_velocity.is_equal_approx(Vector3())) {
  962. PhysicsServer3D::MotionParameters parameters(get_global_transform(), current_platform_velocity * delta, margin);
  963. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  964. parameters.exclude_bodies.insert(platform_rid);
  965. if (platform_object_id.is_valid()) {
  966. parameters.exclude_objects.insert(platform_object_id);
  967. }
  968. PhysicsServer3D::MotionResult floor_result;
  969. if (move_and_collide(parameters, floor_result, false, false)) {
  970. motion_results.push_back(floor_result);
  971. CollisionState result_state;
  972. _set_collision_direction(floor_result, result_state);
  973. }
  974. }
  975. if (motion_mode == MOTION_MODE_GROUNDED) {
  976. _move_and_slide_grounded(delta, was_on_floor);
  977. } else {
  978. _move_and_slide_floating(delta);
  979. }
  980. // Compute real velocity.
  981. real_velocity = get_position_delta() / delta;
  982. if (platform_on_leave != PLATFORM_ON_LEAVE_DO_NOTHING) {
  983. // Add last platform velocity when just left a moving platform.
  984. if (!collision_state.floor && !collision_state.wall) {
  985. if (platform_on_leave == PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY && current_platform_velocity.dot(up_direction) < 0) {
  986. current_platform_velocity = current_platform_velocity.slide(up_direction);
  987. }
  988. velocity += current_platform_velocity;
  989. }
  990. }
  991. return motion_results.size() > 0;
  992. }
  993. void CharacterBody3D::_move_and_slide_grounded(double p_delta, bool p_was_on_floor) {
  994. Vector3 motion = velocity * p_delta;
  995. Vector3 motion_slide_up = motion.slide(up_direction);
  996. Vector3 prev_floor_normal = floor_normal;
  997. platform_rid = RID();
  998. platform_object_id = ObjectID();
  999. platform_velocity = Vector3();
  1000. platform_ceiling_velocity = Vector3();
  1001. floor_normal = Vector3();
  1002. wall_normal = Vector3();
  1003. ceiling_normal = Vector3();
  1004. // No sliding on first attempt to keep floor motion stable when possible,
  1005. // When stop on slope is enabled or when there is no up direction.
  1006. bool sliding_enabled = !floor_stop_on_slope;
  1007. // Constant speed can be applied only the first time sliding is enabled.
  1008. bool can_apply_constant_speed = sliding_enabled;
  1009. // If the platform's ceiling push down the body.
  1010. bool apply_ceiling_velocity = false;
  1011. bool first_slide = true;
  1012. bool vel_dir_facing_up = velocity.dot(up_direction) > 0;
  1013. Vector3 total_travel;
  1014. for (int iteration = 0; iteration < max_slides; ++iteration) {
  1015. PhysicsServer3D::MotionParameters parameters(get_global_transform(), motion, margin);
  1016. parameters.max_collisions = 4;
  1017. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  1018. PhysicsServer3D::MotionResult result;
  1019. bool collided = move_and_collide(parameters, result, false, !sliding_enabled);
  1020. last_motion = result.travel;
  1021. if (collided) {
  1022. motion_results.push_back(result);
  1023. CollisionState previous_state = collision_state;
  1024. CollisionState result_state;
  1025. _set_collision_direction(result, result_state);
  1026. // If we hit a ceiling platform, we set the vertical velocity to at least the platform one.
  1027. if (collision_state.ceiling && platform_ceiling_velocity != Vector3() && platform_ceiling_velocity.dot(up_direction) < 0) {
  1028. // If ceiling sliding is on, only apply when the ceiling is flat or when the motion is upward.
  1029. if (!slide_on_ceiling || motion.dot(up_direction) < 0 || (ceiling_normal + up_direction).length() < 0.01) {
  1030. apply_ceiling_velocity = true;
  1031. Vector3 ceiling_vertical_velocity = up_direction * up_direction.dot(platform_ceiling_velocity);
  1032. Vector3 motion_vertical_velocity = up_direction * up_direction.dot(velocity);
  1033. if (motion_vertical_velocity.dot(up_direction) > 0 || ceiling_vertical_velocity.length_squared() > motion_vertical_velocity.length_squared()) {
  1034. velocity = ceiling_vertical_velocity + velocity.slide(up_direction);
  1035. }
  1036. }
  1037. }
  1038. if (collision_state.floor && floor_stop_on_slope && (velocity.normalized() + up_direction).length() < 0.01) {
  1039. Transform3D gt = get_global_transform();
  1040. if (result.travel.length() <= margin + CMP_EPSILON) {
  1041. gt.origin -= result.travel;
  1042. }
  1043. set_global_transform(gt);
  1044. velocity = Vector3();
  1045. motion = Vector3();
  1046. last_motion = Vector3();
  1047. break;
  1048. }
  1049. if (result.remainder.is_equal_approx(Vector3())) {
  1050. motion = Vector3();
  1051. break;
  1052. }
  1053. // Apply regular sliding by default.
  1054. bool apply_default_sliding = true;
  1055. // Wall collision checks.
  1056. if (result_state.wall && (motion_slide_up.dot(wall_normal) <= 0)) {
  1057. // Move on floor only checks.
  1058. if (floor_block_on_wall) {
  1059. // Needs horizontal motion from current motion instead of motion_slide_up
  1060. // to properly test the angle and avoid standing on slopes
  1061. Vector3 horizontal_motion = motion.slide(up_direction);
  1062. Vector3 horizontal_normal = wall_normal.slide(up_direction).normalized();
  1063. real_t motion_angle = Math::abs(Math::acos(-horizontal_normal.dot(horizontal_motion.normalized())));
  1064. // Avoid to move forward on a wall if floor_block_on_wall is true.
  1065. // Applies only when the motion angle is under 90 degrees,
  1066. // in order to avoid blocking lateral motion along a wall.
  1067. if (motion_angle < .5 * Math_PI) {
  1068. apply_default_sliding = false;
  1069. if (p_was_on_floor && !vel_dir_facing_up) {
  1070. // Cancel the motion.
  1071. Transform3D gt = get_global_transform();
  1072. real_t travel_total = result.travel.length();
  1073. real_t cancel_dist_max = MIN(0.1, margin * 20);
  1074. if (travel_total <= margin + CMP_EPSILON) {
  1075. gt.origin -= result.travel;
  1076. result.travel = Vector3(); // Cancel for constant speed computation.
  1077. } else if (travel_total < cancel_dist_max) { // If the movement is large the body can be prevented from reaching the walls.
  1078. gt.origin -= result.travel.slide(up_direction);
  1079. // Keep remaining motion in sync with amount canceled.
  1080. motion = motion.slide(up_direction);
  1081. result.travel = Vector3();
  1082. } else {
  1083. // Travel is too high to be safely cancelled, we take it into account.
  1084. result.travel = result.travel.slide(up_direction);
  1085. motion = motion.normalized() * result.travel.length();
  1086. }
  1087. set_global_transform(gt);
  1088. // Determines if you are on the ground, and limits the possibility of climbing on the walls because of the approximations.
  1089. _snap_on_floor(true, false);
  1090. } else {
  1091. // If the movement is not cancelled we only keep the remaining.
  1092. motion = result.remainder;
  1093. }
  1094. // Apply slide on forward in order to allow only lateral motion on next step.
  1095. Vector3 forward = wall_normal.slide(up_direction).normalized();
  1096. motion = motion.slide(forward);
  1097. // Scales the horizontal velocity according to the wall slope.
  1098. if (vel_dir_facing_up) {
  1099. Vector3 slide_motion = velocity.slide(result.collisions[0].normal);
  1100. // Keeps the vertical motion from velocity and add the horizontal motion of the projection.
  1101. velocity = up_direction * up_direction.dot(velocity) + slide_motion.slide(up_direction);
  1102. } else {
  1103. velocity = velocity.slide(forward);
  1104. }
  1105. // Allow only lateral motion along previous floor when already on floor.
  1106. // Fixes slowing down when moving in diagonal against an inclined wall.
  1107. if (p_was_on_floor && !vel_dir_facing_up && (motion.dot(up_direction) > 0.0)) {
  1108. // Slide along the corner between the wall and previous floor.
  1109. Vector3 floor_side = prev_floor_normal.cross(wall_normal);
  1110. if (floor_side != Vector3()) {
  1111. motion = floor_side * motion.dot(floor_side);
  1112. }
  1113. }
  1114. // Stop all motion when a second wall is hit (unless sliding down or jumping),
  1115. // in order to avoid jittering in corner cases.
  1116. bool stop_all_motion = previous_state.wall && !vel_dir_facing_up;
  1117. // Allow sliding when the body falls.
  1118. if (!collision_state.floor && motion.dot(up_direction) < 0) {
  1119. Vector3 slide_motion = motion.slide(wall_normal);
  1120. // Test again to allow sliding only if the result goes downwards.
  1121. // Fixes jittering issues at the bottom of inclined walls.
  1122. if (slide_motion.dot(up_direction) < 0) {
  1123. stop_all_motion = false;
  1124. motion = slide_motion;
  1125. }
  1126. }
  1127. if (stop_all_motion) {
  1128. motion = Vector3();
  1129. velocity = Vector3();
  1130. }
  1131. }
  1132. }
  1133. // Stop horizontal motion when under wall slide threshold.
  1134. if (p_was_on_floor && (wall_min_slide_angle > 0.0) && result_state.wall) {
  1135. Vector3 horizontal_normal = wall_normal.slide(up_direction).normalized();
  1136. real_t motion_angle = Math::abs(Math::acos(-horizontal_normal.dot(motion_slide_up.normalized())));
  1137. if (motion_angle < wall_min_slide_angle) {
  1138. motion = up_direction * motion.dot(up_direction);
  1139. velocity = up_direction * velocity.dot(up_direction);
  1140. apply_default_sliding = false;
  1141. }
  1142. }
  1143. }
  1144. if (apply_default_sliding) {
  1145. // Regular sliding, the last part of the test handle the case when you don't want to slide on the ceiling.
  1146. if ((sliding_enabled || !collision_state.floor) && (!collision_state.ceiling || slide_on_ceiling || !vel_dir_facing_up) && !apply_ceiling_velocity) {
  1147. const PhysicsServer3D::MotionCollision &collision = result.collisions[0];
  1148. Vector3 slide_motion = result.remainder.slide(collision.normal);
  1149. if (collision_state.floor && !collision_state.wall && !motion_slide_up.is_equal_approx(Vector3())) {
  1150. // Slide using the intersection between the motion plane and the floor plane,
  1151. // in order to keep the direction intact.
  1152. real_t motion_length = slide_motion.length();
  1153. slide_motion = up_direction.cross(result.remainder).cross(floor_normal);
  1154. // Keep the length from default slide to change speed in slopes by default,
  1155. // when constant speed is not enabled.
  1156. slide_motion.normalize();
  1157. slide_motion *= motion_length;
  1158. }
  1159. if (slide_motion.dot(velocity) > 0.0) {
  1160. motion = slide_motion;
  1161. } else {
  1162. motion = Vector3();
  1163. }
  1164. if (slide_on_ceiling && result_state.ceiling) {
  1165. // Apply slide only in the direction of the input motion, otherwise just stop to avoid jittering when moving against a wall.
  1166. if (vel_dir_facing_up) {
  1167. velocity = velocity.slide(collision.normal);
  1168. } else {
  1169. // Avoid acceleration in slope when falling.
  1170. velocity = up_direction * up_direction.dot(velocity);
  1171. }
  1172. }
  1173. }
  1174. // No sliding on first attempt to keep floor motion stable when possible.
  1175. else {
  1176. motion = result.remainder;
  1177. if (result_state.ceiling && !slide_on_ceiling && vel_dir_facing_up) {
  1178. velocity = velocity.slide(up_direction);
  1179. motion = motion.slide(up_direction);
  1180. }
  1181. }
  1182. }
  1183. total_travel += result.travel;
  1184. // Apply Constant Speed.
  1185. if (p_was_on_floor && floor_constant_speed && can_apply_constant_speed && collision_state.floor && !motion.is_equal_approx(Vector3())) {
  1186. Vector3 travel_slide_up = total_travel.slide(up_direction);
  1187. motion = motion.normalized() * MAX(0, (motion_slide_up.length() - travel_slide_up.length()));
  1188. }
  1189. }
  1190. // When you move forward in a downward slope you don’t collide because you will be in the air.
  1191. // This test ensures that constant speed is applied, only if the player is still on the ground after the snap is applied.
  1192. else if (floor_constant_speed && first_slide && _on_floor_if_snapped(p_was_on_floor, vel_dir_facing_up)) {
  1193. can_apply_constant_speed = false;
  1194. sliding_enabled = true;
  1195. Transform3D gt = get_global_transform();
  1196. gt.origin = gt.origin - result.travel;
  1197. set_global_transform(gt);
  1198. // Slide using the intersection between the motion plane and the floor plane,
  1199. // in order to keep the direction intact.
  1200. Vector3 motion_slide_norm = up_direction.cross(motion).cross(prev_floor_normal);
  1201. motion_slide_norm.normalize();
  1202. motion = motion_slide_norm * (motion_slide_up.length());
  1203. collided = true;
  1204. }
  1205. if (!collided || motion.is_equal_approx(Vector3())) {
  1206. break;
  1207. }
  1208. can_apply_constant_speed = !can_apply_constant_speed && !sliding_enabled;
  1209. sliding_enabled = true;
  1210. first_slide = false;
  1211. }
  1212. _snap_on_floor(p_was_on_floor, vel_dir_facing_up);
  1213. // Reset the gravity accumulation when touching the ground.
  1214. if (collision_state.floor && !vel_dir_facing_up) {
  1215. velocity = velocity.slide(up_direction);
  1216. }
  1217. }
  1218. void CharacterBody3D::_move_and_slide_floating(double p_delta) {
  1219. Vector3 motion = velocity * p_delta;
  1220. platform_rid = RID();
  1221. platform_object_id = ObjectID();
  1222. floor_normal = Vector3();
  1223. platform_velocity = Vector3();
  1224. bool first_slide = true;
  1225. for (int iteration = 0; iteration < max_slides; ++iteration) {
  1226. PhysicsServer3D::MotionParameters parameters(get_global_transform(), motion, margin);
  1227. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  1228. PhysicsServer3D::MotionResult result;
  1229. bool collided = move_and_collide(parameters, result, false, false);
  1230. last_motion = result.travel;
  1231. if (collided) {
  1232. motion_results.push_back(result);
  1233. CollisionState result_state;
  1234. _set_collision_direction(result, result_state);
  1235. if (result.remainder.is_equal_approx(Vector3())) {
  1236. motion = Vector3();
  1237. break;
  1238. }
  1239. if (wall_min_slide_angle != 0 && Math::acos(wall_normal.dot(-velocity.normalized())) < wall_min_slide_angle + FLOOR_ANGLE_THRESHOLD) {
  1240. motion = Vector3();
  1241. if (result.travel.length() < margin + CMP_EPSILON) {
  1242. Transform3D gt = get_global_transform();
  1243. gt.origin -= result.travel;
  1244. set_global_transform(gt);
  1245. }
  1246. } else if (first_slide) {
  1247. Vector3 motion_slide_norm = result.remainder.slide(wall_normal).normalized();
  1248. motion = motion_slide_norm * (motion.length() - result.travel.length());
  1249. } else {
  1250. motion = result.remainder.slide(wall_normal);
  1251. }
  1252. if (motion.dot(velocity) <= 0.0) {
  1253. motion = Vector3();
  1254. }
  1255. }
  1256. if (!collided || motion.is_equal_approx(Vector3())) {
  1257. break;
  1258. }
  1259. first_slide = false;
  1260. }
  1261. }
  1262. void CharacterBody3D::_snap_on_floor(bool p_was_on_floor, bool p_vel_dir_facing_up) {
  1263. if (collision_state.floor || !p_was_on_floor || p_vel_dir_facing_up) {
  1264. return;
  1265. }
  1266. // Snap by at least collision margin to keep floor state consistent.
  1267. real_t length = MAX(floor_snap_length, margin);
  1268. PhysicsServer3D::MotionParameters parameters(get_global_transform(), -up_direction * length, margin);
  1269. parameters.max_collisions = 4;
  1270. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  1271. parameters.collide_separation_ray = true;
  1272. PhysicsServer3D::MotionResult result;
  1273. if (move_and_collide(parameters, result, true, false)) {
  1274. CollisionState result_state;
  1275. // Apply direction for floor only.
  1276. _set_collision_direction(result, result_state, CollisionState(true, false, false));
  1277. if (result_state.floor) {
  1278. if (floor_stop_on_slope) {
  1279. // move and collide may stray the object a bit because of pre un-stucking,
  1280. // so only ensure that motion happens on floor direction in this case.
  1281. if (result.travel.length() > margin) {
  1282. result.travel = up_direction * up_direction.dot(result.travel);
  1283. } else {
  1284. result.travel = Vector3();
  1285. }
  1286. }
  1287. parameters.from.origin += result.travel;
  1288. set_global_transform(parameters.from);
  1289. }
  1290. }
  1291. }
  1292. bool CharacterBody3D::_on_floor_if_snapped(bool p_was_on_floor, bool p_vel_dir_facing_up) {
  1293. if (up_direction == Vector3() || collision_state.floor || !p_was_on_floor || p_vel_dir_facing_up) {
  1294. return false;
  1295. }
  1296. // Snap by at least collision margin to keep floor state consistent.
  1297. real_t length = MAX(floor_snap_length, margin);
  1298. PhysicsServer3D::MotionParameters parameters(get_global_transform(), -up_direction * length, margin);
  1299. parameters.max_collisions = 4;
  1300. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  1301. parameters.collide_separation_ray = true;
  1302. PhysicsServer3D::MotionResult result;
  1303. if (move_and_collide(parameters, result, true, false)) {
  1304. CollisionState result_state;
  1305. // Don't apply direction for any type.
  1306. _set_collision_direction(result, result_state, CollisionState());
  1307. return result_state.floor;
  1308. }
  1309. return false;
  1310. }
  1311. void CharacterBody3D::_set_collision_direction(const PhysicsServer3D::MotionResult &p_result, CollisionState &r_state, CollisionState p_apply_state) {
  1312. r_state.state = 0;
  1313. real_t wall_depth = -1.0;
  1314. real_t floor_depth = -1.0;
  1315. bool was_on_wall = collision_state.wall;
  1316. Vector3 prev_wall_normal = wall_normal;
  1317. int wall_collision_count = 0;
  1318. Vector3 combined_wall_normal;
  1319. Vector3 tmp_wall_col; // Avoid duplicate on average calculation.
  1320. for (int i = p_result.collision_count - 1; i >= 0; i--) {
  1321. const PhysicsServer3D::MotionCollision &collision = p_result.collisions[i];
  1322. if (motion_mode == MOTION_MODE_GROUNDED) {
  1323. // Check if any collision is floor.
  1324. real_t floor_angle = collision.get_angle(up_direction);
  1325. if (floor_angle <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) {
  1326. r_state.floor = true;
  1327. if (p_apply_state.floor && collision.depth > floor_depth) {
  1328. collision_state.floor = true;
  1329. floor_normal = collision.normal;
  1330. floor_depth = collision.depth;
  1331. _set_platform_data(collision);
  1332. }
  1333. continue;
  1334. }
  1335. // Check if any collision is ceiling.
  1336. real_t ceiling_angle = collision.get_angle(-up_direction);
  1337. if (ceiling_angle <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) {
  1338. r_state.ceiling = true;
  1339. if (p_apply_state.ceiling) {
  1340. platform_ceiling_velocity = collision.collider_velocity;
  1341. ceiling_normal = collision.normal;
  1342. collision_state.ceiling = true;
  1343. }
  1344. continue;
  1345. }
  1346. }
  1347. // Collision is wall by default.
  1348. r_state.wall = true;
  1349. if (p_apply_state.wall && collision.depth > wall_depth) {
  1350. collision_state.wall = true;
  1351. wall_depth = collision.depth;
  1352. wall_normal = collision.normal;
  1353. // Don't apply wall velocity when the collider is a CharacterBody3D.
  1354. if (Object::cast_to<CharacterBody3D>(ObjectDB::get_instance(collision.collider_id)) == nullptr) {
  1355. _set_platform_data(collision);
  1356. }
  1357. }
  1358. // Collect normal for calculating average.
  1359. if (!collision.normal.is_equal_approx(tmp_wall_col)) {
  1360. tmp_wall_col = collision.normal;
  1361. combined_wall_normal += collision.normal;
  1362. wall_collision_count++;
  1363. }
  1364. }
  1365. if (r_state.wall) {
  1366. if (wall_collision_count > 1 && !r_state.floor) {
  1367. // Check if wall normals cancel out to floor support.
  1368. if (!r_state.floor && motion_mode == MOTION_MODE_GROUNDED) {
  1369. combined_wall_normal.normalize();
  1370. real_t floor_angle = Math::acos(combined_wall_normal.dot(up_direction));
  1371. if (floor_angle <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) {
  1372. r_state.floor = true;
  1373. r_state.wall = false;
  1374. if (p_apply_state.floor) {
  1375. collision_state.floor = true;
  1376. floor_normal = combined_wall_normal;
  1377. }
  1378. if (p_apply_state.wall) {
  1379. collision_state.wall = was_on_wall;
  1380. wall_normal = prev_wall_normal;
  1381. }
  1382. return;
  1383. }
  1384. }
  1385. }
  1386. }
  1387. }
  1388. void CharacterBody3D::_set_platform_data(const PhysicsServer3D::MotionCollision &p_collision) {
  1389. platform_rid = p_collision.collider;
  1390. platform_object_id = p_collision.collider_id;
  1391. platform_velocity = p_collision.collider_velocity;
  1392. platform_layer = PhysicsServer3D::get_singleton()->body_get_collision_layer(platform_rid);
  1393. }
  1394. void CharacterBody3D::set_safe_margin(real_t p_margin) {
  1395. margin = p_margin;
  1396. }
  1397. real_t CharacterBody3D::get_safe_margin() const {
  1398. return margin;
  1399. }
  1400. const Vector3 &CharacterBody3D::get_velocity() const {
  1401. return velocity;
  1402. }
  1403. void CharacterBody3D::set_velocity(const Vector3 &p_velocity) {
  1404. velocity = p_velocity;
  1405. }
  1406. bool CharacterBody3D::is_on_floor() const {
  1407. return collision_state.floor;
  1408. }
  1409. bool CharacterBody3D::is_on_floor_only() const {
  1410. return collision_state.floor && !collision_state.wall && !collision_state.ceiling;
  1411. }
  1412. bool CharacterBody3D::is_on_wall() const {
  1413. return collision_state.wall;
  1414. }
  1415. bool CharacterBody3D::is_on_wall_only() const {
  1416. return collision_state.wall && !collision_state.floor && !collision_state.ceiling;
  1417. }
  1418. bool CharacterBody3D::is_on_ceiling() const {
  1419. return collision_state.ceiling;
  1420. }
  1421. bool CharacterBody3D::is_on_ceiling_only() const {
  1422. return collision_state.ceiling && !collision_state.floor && !collision_state.wall;
  1423. }
  1424. const Vector3 &CharacterBody3D::get_floor_normal() const {
  1425. return floor_normal;
  1426. }
  1427. const Vector3 &CharacterBody3D::get_wall_normal() const {
  1428. return wall_normal;
  1429. }
  1430. const Vector3 &CharacterBody3D::get_last_motion() const {
  1431. return last_motion;
  1432. }
  1433. Vector3 CharacterBody3D::get_position_delta() const {
  1434. return get_transform().origin - previous_position;
  1435. }
  1436. const Vector3 &CharacterBody3D::get_real_velocity() const {
  1437. return real_velocity;
  1438. }
  1439. real_t CharacterBody3D::get_floor_angle(const Vector3 &p_up_direction) const {
  1440. ERR_FAIL_COND_V(p_up_direction == Vector3(), 0);
  1441. return Math::acos(floor_normal.dot(p_up_direction));
  1442. }
  1443. const Vector3 &CharacterBody3D::get_platform_velocity() const {
  1444. return platform_velocity;
  1445. }
  1446. Vector3 CharacterBody3D::get_linear_velocity() const {
  1447. return get_real_velocity();
  1448. }
  1449. int CharacterBody3D::get_slide_collision_count() const {
  1450. return motion_results.size();
  1451. }
  1452. PhysicsServer3D::MotionResult CharacterBody3D::get_slide_collision(int p_bounce) const {
  1453. ERR_FAIL_INDEX_V(p_bounce, motion_results.size(), PhysicsServer3D::MotionResult());
  1454. return motion_results[p_bounce];
  1455. }
  1456. Ref<KinematicCollision3D> CharacterBody3D::_get_slide_collision(int p_bounce) {
  1457. ERR_FAIL_INDEX_V(p_bounce, motion_results.size(), Ref<KinematicCollision3D>());
  1458. if (p_bounce >= slide_colliders.size()) {
  1459. slide_colliders.resize(p_bounce + 1);
  1460. }
  1461. // Create a new instance when the cached reference is invalid or still in use in script.
  1462. if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->reference_get_count() > 1) {
  1463. slide_colliders.write[p_bounce].instantiate();
  1464. slide_colliders.write[p_bounce]->owner = this;
  1465. }
  1466. slide_colliders.write[p_bounce]->result = motion_results[p_bounce];
  1467. return slide_colliders[p_bounce];
  1468. }
  1469. Ref<KinematicCollision3D> CharacterBody3D::_get_last_slide_collision() {
  1470. if (motion_results.size() == 0) {
  1471. return Ref<KinematicCollision3D>();
  1472. }
  1473. return _get_slide_collision(motion_results.size() - 1);
  1474. }
  1475. bool CharacterBody3D::is_floor_stop_on_slope_enabled() const {
  1476. return floor_stop_on_slope;
  1477. }
  1478. void CharacterBody3D::set_floor_stop_on_slope_enabled(bool p_enabled) {
  1479. floor_stop_on_slope = p_enabled;
  1480. }
  1481. bool CharacterBody3D::is_floor_constant_speed_enabled() const {
  1482. return floor_constant_speed;
  1483. }
  1484. void CharacterBody3D::set_floor_constant_speed_enabled(bool p_enabled) {
  1485. floor_constant_speed = p_enabled;
  1486. }
  1487. bool CharacterBody3D::is_floor_block_on_wall_enabled() const {
  1488. return floor_block_on_wall;
  1489. }
  1490. void CharacterBody3D::set_floor_block_on_wall_enabled(bool p_enabled) {
  1491. floor_block_on_wall = p_enabled;
  1492. }
  1493. bool CharacterBody3D::is_slide_on_ceiling_enabled() const {
  1494. return slide_on_ceiling;
  1495. }
  1496. void CharacterBody3D::set_slide_on_ceiling_enabled(bool p_enabled) {
  1497. slide_on_ceiling = p_enabled;
  1498. }
  1499. uint32_t CharacterBody3D::get_platform_floor_layers() const {
  1500. return platform_floor_layers;
  1501. }
  1502. void CharacterBody3D::set_platform_floor_layers(uint32_t p_exclude_layers) {
  1503. platform_floor_layers = p_exclude_layers;
  1504. }
  1505. uint32_t CharacterBody3D::get_platform_wall_layers() const {
  1506. return platform_wall_layers;
  1507. }
  1508. void CharacterBody3D::set_platform_wall_layers(uint32_t p_exclude_layers) {
  1509. platform_wall_layers = p_exclude_layers;
  1510. }
  1511. void CharacterBody3D::set_motion_mode(MotionMode p_mode) {
  1512. motion_mode = p_mode;
  1513. }
  1514. CharacterBody3D::MotionMode CharacterBody3D::get_motion_mode() const {
  1515. return motion_mode;
  1516. }
  1517. void CharacterBody3D::set_platform_on_leave(PlatformOnLeave p_on_leave_apply_velocity) {
  1518. platform_on_leave = p_on_leave_apply_velocity;
  1519. }
  1520. CharacterBody3D::PlatformOnLeave CharacterBody3D::get_platform_on_leave() const {
  1521. return platform_on_leave;
  1522. }
  1523. int CharacterBody3D::get_max_slides() const {
  1524. return max_slides;
  1525. }
  1526. void CharacterBody3D::set_max_slides(int p_max_slides) {
  1527. ERR_FAIL_COND(p_max_slides < 1);
  1528. max_slides = p_max_slides;
  1529. }
  1530. real_t CharacterBody3D::get_floor_max_angle() const {
  1531. return floor_max_angle;
  1532. }
  1533. void CharacterBody3D::set_floor_max_angle(real_t p_radians) {
  1534. floor_max_angle = p_radians;
  1535. }
  1536. real_t CharacterBody3D::get_floor_snap_length() {
  1537. return floor_snap_length;
  1538. }
  1539. void CharacterBody3D::set_floor_snap_length(real_t p_floor_snap_length) {
  1540. ERR_FAIL_COND(p_floor_snap_length < 0);
  1541. floor_snap_length = p_floor_snap_length;
  1542. }
  1543. real_t CharacterBody3D::get_wall_min_slide_angle() const {
  1544. return wall_min_slide_angle;
  1545. }
  1546. void CharacterBody3D::set_wall_min_slide_angle(real_t p_radians) {
  1547. wall_min_slide_angle = p_radians;
  1548. }
  1549. const Vector3 &CharacterBody3D::get_up_direction() const {
  1550. return up_direction;
  1551. }
  1552. void CharacterBody3D::set_up_direction(const Vector3 &p_up_direction) {
  1553. ERR_FAIL_COND_MSG(p_up_direction == Vector3(), "up_direction can't be equal to Vector3.ZERO, consider using Floating motion mode instead.");
  1554. up_direction = p_up_direction.normalized();
  1555. }
  1556. void CharacterBody3D::_notification(int p_what) {
  1557. switch (p_what) {
  1558. case NOTIFICATION_ENTER_TREE: {
  1559. // Reset move_and_slide() data.
  1560. collision_state.state = 0;
  1561. platform_rid = RID();
  1562. platform_object_id = ObjectID();
  1563. motion_results.clear();
  1564. platform_velocity = Vector3();
  1565. } break;
  1566. }
  1567. }
  1568. void CharacterBody3D::_bind_methods() {
  1569. ClassDB::bind_method(D_METHOD("move_and_slide"), &CharacterBody3D::move_and_slide);
  1570. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &CharacterBody3D::set_velocity);
  1571. ClassDB::bind_method(D_METHOD("get_velocity"), &CharacterBody3D::get_velocity);
  1572. ClassDB::bind_method(D_METHOD("set_safe_margin", "margin"), &CharacterBody3D::set_safe_margin);
  1573. ClassDB::bind_method(D_METHOD("get_safe_margin"), &CharacterBody3D::get_safe_margin);
  1574. ClassDB::bind_method(D_METHOD("is_floor_stop_on_slope_enabled"), &CharacterBody3D::is_floor_stop_on_slope_enabled);
  1575. ClassDB::bind_method(D_METHOD("set_floor_stop_on_slope_enabled", "enabled"), &CharacterBody3D::set_floor_stop_on_slope_enabled);
  1576. ClassDB::bind_method(D_METHOD("set_floor_constant_speed_enabled", "enabled"), &CharacterBody3D::set_floor_constant_speed_enabled);
  1577. ClassDB::bind_method(D_METHOD("is_floor_constant_speed_enabled"), &CharacterBody3D::is_floor_constant_speed_enabled);
  1578. ClassDB::bind_method(D_METHOD("set_floor_block_on_wall_enabled", "enabled"), &CharacterBody3D::set_floor_block_on_wall_enabled);
  1579. ClassDB::bind_method(D_METHOD("is_floor_block_on_wall_enabled"), &CharacterBody3D::is_floor_block_on_wall_enabled);
  1580. ClassDB::bind_method(D_METHOD("set_slide_on_ceiling_enabled", "enabled"), &CharacterBody3D::set_slide_on_ceiling_enabled);
  1581. ClassDB::bind_method(D_METHOD("is_slide_on_ceiling_enabled"), &CharacterBody3D::is_slide_on_ceiling_enabled);
  1582. ClassDB::bind_method(D_METHOD("set_platform_floor_layers", "exclude_layer"), &CharacterBody3D::set_platform_floor_layers);
  1583. ClassDB::bind_method(D_METHOD("get_platform_floor_layers"), &CharacterBody3D::get_platform_floor_layers);
  1584. ClassDB::bind_method(D_METHOD("set_platform_wall_layers", "exclude_layer"), &CharacterBody3D::set_platform_wall_layers);
  1585. ClassDB::bind_method(D_METHOD("get_platform_wall_layers"), &CharacterBody3D::get_platform_wall_layers);
  1586. ClassDB::bind_method(D_METHOD("get_max_slides"), &CharacterBody3D::get_max_slides);
  1587. ClassDB::bind_method(D_METHOD("set_max_slides", "max_slides"), &CharacterBody3D::set_max_slides);
  1588. ClassDB::bind_method(D_METHOD("get_floor_max_angle"), &CharacterBody3D::get_floor_max_angle);
  1589. ClassDB::bind_method(D_METHOD("set_floor_max_angle", "radians"), &CharacterBody3D::set_floor_max_angle);
  1590. ClassDB::bind_method(D_METHOD("get_floor_snap_length"), &CharacterBody3D::get_floor_snap_length);
  1591. ClassDB::bind_method(D_METHOD("set_floor_snap_length", "floor_snap_length"), &CharacterBody3D::set_floor_snap_length);
  1592. ClassDB::bind_method(D_METHOD("get_wall_min_slide_angle"), &CharacterBody3D::get_wall_min_slide_angle);
  1593. ClassDB::bind_method(D_METHOD("set_wall_min_slide_angle", "radians"), &CharacterBody3D::set_wall_min_slide_angle);
  1594. ClassDB::bind_method(D_METHOD("get_up_direction"), &CharacterBody3D::get_up_direction);
  1595. ClassDB::bind_method(D_METHOD("set_up_direction", "up_direction"), &CharacterBody3D::set_up_direction);
  1596. ClassDB::bind_method(D_METHOD("set_motion_mode", "mode"), &CharacterBody3D::set_motion_mode);
  1597. ClassDB::bind_method(D_METHOD("get_motion_mode"), &CharacterBody3D::get_motion_mode);
  1598. ClassDB::bind_method(D_METHOD("set_platform_on_leave", "on_leave_apply_velocity"), &CharacterBody3D::set_platform_on_leave);
  1599. ClassDB::bind_method(D_METHOD("get_platform_on_leave"), &CharacterBody3D::get_platform_on_leave);
  1600. ClassDB::bind_method(D_METHOD("is_on_floor"), &CharacterBody3D::is_on_floor);
  1601. ClassDB::bind_method(D_METHOD("is_on_floor_only"), &CharacterBody3D::is_on_floor_only);
  1602. ClassDB::bind_method(D_METHOD("is_on_ceiling"), &CharacterBody3D::is_on_ceiling);
  1603. ClassDB::bind_method(D_METHOD("is_on_ceiling_only"), &CharacterBody3D::is_on_ceiling_only);
  1604. ClassDB::bind_method(D_METHOD("is_on_wall"), &CharacterBody3D::is_on_wall);
  1605. ClassDB::bind_method(D_METHOD("is_on_wall_only"), &CharacterBody3D::is_on_wall_only);
  1606. ClassDB::bind_method(D_METHOD("get_floor_normal"), &CharacterBody3D::get_floor_normal);
  1607. ClassDB::bind_method(D_METHOD("get_wall_normal"), &CharacterBody3D::get_wall_normal);
  1608. ClassDB::bind_method(D_METHOD("get_last_motion"), &CharacterBody3D::get_last_motion);
  1609. ClassDB::bind_method(D_METHOD("get_position_delta"), &CharacterBody3D::get_position_delta);
  1610. ClassDB::bind_method(D_METHOD("get_real_velocity"), &CharacterBody3D::get_real_velocity);
  1611. ClassDB::bind_method(D_METHOD("get_floor_angle", "up_direction"), &CharacterBody3D::get_floor_angle, DEFVAL(Vector3(0.0, 1.0, 0.0)));
  1612. ClassDB::bind_method(D_METHOD("get_platform_velocity"), &CharacterBody3D::get_platform_velocity);
  1613. ClassDB::bind_method(D_METHOD("get_slide_collision_count"), &CharacterBody3D::get_slide_collision_count);
  1614. ClassDB::bind_method(D_METHOD("get_slide_collision", "slide_idx"), &CharacterBody3D::_get_slide_collision);
  1615. ClassDB::bind_method(D_METHOD("get_last_slide_collision"), &CharacterBody3D::_get_last_slide_collision);
  1616. ADD_PROPERTY(PropertyInfo(Variant::INT, "motion_mode", PROPERTY_HINT_ENUM, "Grounded,Floating", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_motion_mode", "get_motion_mode");
  1617. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "up_direction"), "set_up_direction", "get_up_direction");
  1618. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "slide_on_ceiling"), "set_slide_on_ceiling_enabled", "is_slide_on_ceiling_enabled");
  1619. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "velocity", PROPERTY_HINT_NONE, "suffix:m/s", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
  1620. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_slides", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_max_slides", "get_max_slides");
  1621. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wall_min_slide_angle", PROPERTY_HINT_RANGE, "0,180,0.1,radians", PROPERTY_USAGE_DEFAULT), "set_wall_min_slide_angle", "get_wall_min_slide_angle");
  1622. ADD_GROUP("Floor", "floor_");
  1623. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_stop_on_slope"), "set_floor_stop_on_slope_enabled", "is_floor_stop_on_slope_enabled");
  1624. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_constant_speed"), "set_floor_constant_speed_enabled", "is_floor_constant_speed_enabled");
  1625. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_block_on_wall"), "set_floor_block_on_wall_enabled", "is_floor_block_on_wall_enabled");
  1626. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "floor_max_angle", PROPERTY_HINT_RANGE, "0,180,0.1,radians"), "set_floor_max_angle", "get_floor_max_angle");
  1627. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "floor_snap_length", PROPERTY_HINT_RANGE, "0,1,0.01,or_greater,suffix:m"), "set_floor_snap_length", "get_floor_snap_length");
  1628. ADD_GROUP("Moving Platform", "platform_");
  1629. ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_on_leave", PROPERTY_HINT_ENUM, "Add Velocity,Add Upward Velocity,Do Nothing", PROPERTY_USAGE_DEFAULT), "set_platform_on_leave", "get_platform_on_leave");
  1630. ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_floor_layers", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_platform_floor_layers", "get_platform_floor_layers");
  1631. ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_wall_layers", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_platform_wall_layers", "get_platform_wall_layers");
  1632. ADD_GROUP("Collision", "");
  1633. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "safe_margin", PROPERTY_HINT_RANGE, "0.001,256,0.001,suffix:m"), "set_safe_margin", "get_safe_margin");
  1634. BIND_ENUM_CONSTANT(MOTION_MODE_GROUNDED);
  1635. BIND_ENUM_CONSTANT(MOTION_MODE_FLOATING);
  1636. BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_ADD_VELOCITY);
  1637. BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY);
  1638. BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_DO_NOTHING);
  1639. }
  1640. void CharacterBody3D::_validate_property(PropertyInfo &p_property) const {
  1641. if (motion_mode == MOTION_MODE_FLOATING) {
  1642. if (p_property.name.begins_with("floor_") || p_property.name == "up_direction" || p_property.name == "slide_on_ceiling") {
  1643. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1644. }
  1645. }
  1646. }
  1647. CharacterBody3D::CharacterBody3D() :
  1648. PhysicsBody3D(PhysicsServer3D::BODY_MODE_KINEMATIC) {
  1649. }
  1650. CharacterBody3D::~CharacterBody3D() {
  1651. for (int i = 0; i < slide_colliders.size(); i++) {
  1652. if (slide_colliders[i].is_valid()) {
  1653. slide_colliders.write[i]->owner = nullptr;
  1654. }
  1655. }
  1656. }
  1657. ///////////////////////////////////////
  1658. Vector3 KinematicCollision3D::get_travel() const {
  1659. return result.travel;
  1660. }
  1661. Vector3 KinematicCollision3D::get_remainder() const {
  1662. return result.remainder;
  1663. }
  1664. int KinematicCollision3D::get_collision_count() const {
  1665. return result.collision_count;
  1666. }
  1667. real_t KinematicCollision3D::get_depth() const {
  1668. return result.collision_depth;
  1669. }
  1670. Vector3 KinematicCollision3D::get_position(int p_collision_index) const {
  1671. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3());
  1672. return result.collisions[p_collision_index].position;
  1673. }
  1674. Vector3 KinematicCollision3D::get_normal(int p_collision_index) const {
  1675. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3());
  1676. return result.collisions[p_collision_index].normal;
  1677. }
  1678. real_t KinematicCollision3D::get_angle(int p_collision_index, const Vector3 &p_up_direction) const {
  1679. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, 0.0);
  1680. ERR_FAIL_COND_V(p_up_direction == Vector3(), 0);
  1681. return result.collisions[p_collision_index].get_angle(p_up_direction);
  1682. }
  1683. Object *KinematicCollision3D::get_local_shape(int p_collision_index) const {
  1684. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, nullptr);
  1685. if (!owner) {
  1686. return nullptr;
  1687. }
  1688. uint32_t ownerid = owner->shape_find_owner(result.collisions[p_collision_index].local_shape);
  1689. return owner->shape_owner_get_owner(ownerid);
  1690. }
  1691. Object *KinematicCollision3D::get_collider(int p_collision_index) const {
  1692. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, nullptr);
  1693. if (result.collisions[p_collision_index].collider_id.is_valid()) {
  1694. return ObjectDB::get_instance(result.collisions[p_collision_index].collider_id);
  1695. }
  1696. return nullptr;
  1697. }
  1698. ObjectID KinematicCollision3D::get_collider_id(int p_collision_index) const {
  1699. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, ObjectID());
  1700. return result.collisions[p_collision_index].collider_id;
  1701. }
  1702. RID KinematicCollision3D::get_collider_rid(int p_collision_index) const {
  1703. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, RID());
  1704. return result.collisions[p_collision_index].collider;
  1705. }
  1706. Object *KinematicCollision3D::get_collider_shape(int p_collision_index) const {
  1707. Object *collider = get_collider(p_collision_index);
  1708. if (collider) {
  1709. CollisionObject3D *obj2d = Object::cast_to<CollisionObject3D>(collider);
  1710. if (obj2d) {
  1711. uint32_t ownerid = obj2d->shape_find_owner(result.collisions[p_collision_index].collider_shape);
  1712. return obj2d->shape_owner_get_owner(ownerid);
  1713. }
  1714. }
  1715. return nullptr;
  1716. }
  1717. int KinematicCollision3D::get_collider_shape_index(int p_collision_index) const {
  1718. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, 0);
  1719. return result.collisions[p_collision_index].collider_shape;
  1720. }
  1721. Vector3 KinematicCollision3D::get_collider_velocity(int p_collision_index) const {
  1722. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3());
  1723. return result.collisions[p_collision_index].collider_velocity;
  1724. }
  1725. void KinematicCollision3D::_bind_methods() {
  1726. ClassDB::bind_method(D_METHOD("get_travel"), &KinematicCollision3D::get_travel);
  1727. ClassDB::bind_method(D_METHOD("get_remainder"), &KinematicCollision3D::get_remainder);
  1728. ClassDB::bind_method(D_METHOD("get_depth"), &KinematicCollision3D::get_depth);
  1729. ClassDB::bind_method(D_METHOD("get_collision_count"), &KinematicCollision3D::get_collision_count);
  1730. ClassDB::bind_method(D_METHOD("get_position", "collision_index"), &KinematicCollision3D::get_position, DEFVAL(0));
  1731. ClassDB::bind_method(D_METHOD("get_normal", "collision_index"), &KinematicCollision3D::get_normal, DEFVAL(0));
  1732. ClassDB::bind_method(D_METHOD("get_angle", "collision_index", "up_direction"), &KinematicCollision3D::get_angle, DEFVAL(0), DEFVAL(Vector3(0.0, 1.0, 0.0)));
  1733. ClassDB::bind_method(D_METHOD("get_local_shape", "collision_index"), &KinematicCollision3D::get_local_shape, DEFVAL(0));
  1734. ClassDB::bind_method(D_METHOD("get_collider", "collision_index"), &KinematicCollision3D::get_collider, DEFVAL(0));
  1735. ClassDB::bind_method(D_METHOD("get_collider_id", "collision_index"), &KinematicCollision3D::get_collider_id, DEFVAL(0));
  1736. ClassDB::bind_method(D_METHOD("get_collider_rid", "collision_index"), &KinematicCollision3D::get_collider_rid, DEFVAL(0));
  1737. ClassDB::bind_method(D_METHOD("get_collider_shape", "collision_index"), &KinematicCollision3D::get_collider_shape, DEFVAL(0));
  1738. ClassDB::bind_method(D_METHOD("get_collider_shape_index", "collision_index"), &KinematicCollision3D::get_collider_shape_index, DEFVAL(0));
  1739. ClassDB::bind_method(D_METHOD("get_collider_velocity", "collision_index"), &KinematicCollision3D::get_collider_velocity, DEFVAL(0));
  1740. }
  1741. ///////////////////////////////////////
  1742. bool PhysicalBone3D::JointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1743. return false;
  1744. }
  1745. bool PhysicalBone3D::JointData::_get(const StringName &p_name, Variant &r_ret) const {
  1746. return false;
  1747. }
  1748. void PhysicalBone3D::JointData::_get_property_list(List<PropertyInfo> *p_list) const {
  1749. }
  1750. void PhysicalBone3D::apply_central_impulse(const Vector3 &p_impulse) {
  1751. PhysicsServer3D::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
  1752. }
  1753. void PhysicalBone3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) {
  1754. PhysicsServer3D::get_singleton()->body_apply_impulse(get_rid(), p_impulse, p_position);
  1755. }
  1756. void PhysicalBone3D::set_linear_velocity(const Vector3 &p_velocity) {
  1757. linear_velocity = p_velocity;
  1758. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  1759. }
  1760. Vector3 PhysicalBone3D::get_linear_velocity() const {
  1761. return linear_velocity;
  1762. }
  1763. void PhysicalBone3D::set_angular_velocity(const Vector3 &p_velocity) {
  1764. angular_velocity = p_velocity;
  1765. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
  1766. }
  1767. Vector3 PhysicalBone3D::get_angular_velocity() const {
  1768. return angular_velocity;
  1769. }
  1770. void PhysicalBone3D::set_use_custom_integrator(bool p_enable) {
  1771. if (custom_integrator == p_enable) {
  1772. return;
  1773. }
  1774. custom_integrator = p_enable;
  1775. PhysicsServer3D::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
  1776. }
  1777. bool PhysicalBone3D::is_using_custom_integrator() {
  1778. return custom_integrator;
  1779. }
  1780. void PhysicalBone3D::reset_physics_simulation_state() {
  1781. if (simulate_physics) {
  1782. _start_physics_simulation();
  1783. } else {
  1784. _stop_physics_simulation();
  1785. }
  1786. }
  1787. void PhysicalBone3D::reset_to_rest_position() {
  1788. if (parent_skeleton) {
  1789. if (-1 == bone_id) {
  1790. set_global_transform(parent_skeleton->get_global_transform() * body_offset);
  1791. } else {
  1792. set_global_transform(parent_skeleton->get_global_transform() * parent_skeleton->get_bone_global_pose(bone_id) * body_offset);
  1793. }
  1794. }
  1795. }
  1796. bool PhysicalBone3D::PinJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1797. if (JointData::_set(p_name, p_value, j)) {
  1798. return true;
  1799. }
  1800. if ("joint_constraints/bias" == p_name) {
  1801. bias = p_value;
  1802. if (j.is_valid()) {
  1803. PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_BIAS, bias);
  1804. }
  1805. } else if ("joint_constraints/damping" == p_name) {
  1806. damping = p_value;
  1807. if (j.is_valid()) {
  1808. PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_DAMPING, damping);
  1809. }
  1810. } else if ("joint_constraints/impulse_clamp" == p_name) {
  1811. impulse_clamp = p_value;
  1812. if (j.is_valid()) {
  1813. PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP, impulse_clamp);
  1814. }
  1815. } else {
  1816. return false;
  1817. }
  1818. return true;
  1819. }
  1820. bool PhysicalBone3D::PinJointData::_get(const StringName &p_name, Variant &r_ret) const {
  1821. if (JointData::_get(p_name, r_ret)) {
  1822. return true;
  1823. }
  1824. if ("joint_constraints/bias" == p_name) {
  1825. r_ret = bias;
  1826. } else if ("joint_constraints/damping" == p_name) {
  1827. r_ret = damping;
  1828. } else if ("joint_constraints/impulse_clamp" == p_name) {
  1829. r_ret = impulse_clamp;
  1830. } else {
  1831. return false;
  1832. }
  1833. return true;
  1834. }
  1835. void PhysicalBone3D::PinJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  1836. JointData::_get_property_list(p_list);
  1837. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/bias"), PROPERTY_HINT_RANGE, "0.01,0.99,0.01"));
  1838. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/damping"), PROPERTY_HINT_RANGE, "0.01,8.0,0.01"));
  1839. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/impulse_clamp"), PROPERTY_HINT_RANGE, "0.0,64.0,0.01"));
  1840. }
  1841. bool PhysicalBone3D::ConeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1842. if (JointData::_set(p_name, p_value, j)) {
  1843. return true;
  1844. }
  1845. if ("joint_constraints/swing_span" == p_name) {
  1846. swing_span = Math::deg_to_rad(real_t(p_value));
  1847. if (j.is_valid()) {
  1848. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN, swing_span);
  1849. }
  1850. } else if ("joint_constraints/twist_span" == p_name) {
  1851. twist_span = Math::deg_to_rad(real_t(p_value));
  1852. if (j.is_valid()) {
  1853. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN, twist_span);
  1854. }
  1855. } else if ("joint_constraints/bias" == p_name) {
  1856. bias = p_value;
  1857. if (j.is_valid()) {
  1858. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_BIAS, bias);
  1859. }
  1860. } else if ("joint_constraints/softness" == p_name) {
  1861. softness = p_value;
  1862. if (j.is_valid()) {
  1863. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS, softness);
  1864. }
  1865. } else if ("joint_constraints/relaxation" == p_name) {
  1866. relaxation = p_value;
  1867. if (j.is_valid()) {
  1868. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION, relaxation);
  1869. }
  1870. } else {
  1871. return false;
  1872. }
  1873. return true;
  1874. }
  1875. bool PhysicalBone3D::ConeJointData::_get(const StringName &p_name, Variant &r_ret) const {
  1876. if (JointData::_get(p_name, r_ret)) {
  1877. return true;
  1878. }
  1879. if ("joint_constraints/swing_span" == p_name) {
  1880. r_ret = Math::rad_to_deg(swing_span);
  1881. } else if ("joint_constraints/twist_span" == p_name) {
  1882. r_ret = Math::rad_to_deg(twist_span);
  1883. } else if ("joint_constraints/bias" == p_name) {
  1884. r_ret = bias;
  1885. } else if ("joint_constraints/softness" == p_name) {
  1886. r_ret = softness;
  1887. } else if ("joint_constraints/relaxation" == p_name) {
  1888. r_ret = relaxation;
  1889. } else {
  1890. return false;
  1891. }
  1892. return true;
  1893. }
  1894. void PhysicalBone3D::ConeJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  1895. JointData::_get_property_list(p_list);
  1896. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/swing_span"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  1897. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/twist_span"), PROPERTY_HINT_RANGE, "-40000,40000,0.1,or_lesser,or_greater"));
  1898. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/bias"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  1899. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/softness"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  1900. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/relaxation"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  1901. }
  1902. bool PhysicalBone3D::HingeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1903. if (JointData::_set(p_name, p_value, j)) {
  1904. return true;
  1905. }
  1906. if ("joint_constraints/angular_limit_enabled" == p_name) {
  1907. angular_limit_enabled = p_value;
  1908. if (j.is_valid()) {
  1909. PhysicsServer3D::get_singleton()->hinge_joint_set_flag(j, PhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT, angular_limit_enabled);
  1910. }
  1911. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  1912. angular_limit_upper = Math::deg_to_rad(real_t(p_value));
  1913. if (j.is_valid()) {
  1914. PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_UPPER, angular_limit_upper);
  1915. }
  1916. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  1917. angular_limit_lower = Math::deg_to_rad(real_t(p_value));
  1918. if (j.is_valid()) {
  1919. PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_LOWER, angular_limit_lower);
  1920. }
  1921. } else if ("joint_constraints/angular_limit_bias" == p_name) {
  1922. angular_limit_bias = p_value;
  1923. if (j.is_valid()) {
  1924. PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_BIAS, angular_limit_bias);
  1925. }
  1926. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  1927. angular_limit_softness = p_value;
  1928. if (j.is_valid()) {
  1929. PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_SOFTNESS, angular_limit_softness);
  1930. }
  1931. } else if ("joint_constraints/angular_limit_relaxation" == p_name) {
  1932. angular_limit_relaxation = p_value;
  1933. if (j.is_valid()) {
  1934. PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_RELAXATION, angular_limit_relaxation);
  1935. }
  1936. } else {
  1937. return false;
  1938. }
  1939. return true;
  1940. }
  1941. bool PhysicalBone3D::HingeJointData::_get(const StringName &p_name, Variant &r_ret) const {
  1942. if (JointData::_get(p_name, r_ret)) {
  1943. return true;
  1944. }
  1945. if ("joint_constraints/angular_limit_enabled" == p_name) {
  1946. r_ret = angular_limit_enabled;
  1947. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  1948. r_ret = Math::rad_to_deg(angular_limit_upper);
  1949. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  1950. r_ret = Math::rad_to_deg(angular_limit_lower);
  1951. } else if ("joint_constraints/angular_limit_bias" == p_name) {
  1952. r_ret = angular_limit_bias;
  1953. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  1954. r_ret = angular_limit_softness;
  1955. } else if ("joint_constraints/angular_limit_relaxation" == p_name) {
  1956. r_ret = angular_limit_relaxation;
  1957. } else {
  1958. return false;
  1959. }
  1960. return true;
  1961. }
  1962. void PhysicalBone3D::HingeJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  1963. JointData::_get_property_list(p_list);
  1964. p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("joint_constraints/angular_limit_enabled")));
  1965. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_upper"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  1966. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_lower"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  1967. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_bias"), PROPERTY_HINT_RANGE, "0.01,0.99,0.01"));
  1968. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  1969. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_relaxation"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  1970. }
  1971. bool PhysicalBone3D::SliderJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  1972. if (JointData::_set(p_name, p_value, j)) {
  1973. return true;
  1974. }
  1975. if ("joint_constraints/linear_limit_upper" == p_name) {
  1976. linear_limit_upper = p_value;
  1977. if (j.is_valid()) {
  1978. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER, linear_limit_upper);
  1979. }
  1980. } else if ("joint_constraints/linear_limit_lower" == p_name) {
  1981. linear_limit_lower = p_value;
  1982. if (j.is_valid()) {
  1983. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER, linear_limit_lower);
  1984. }
  1985. } else if ("joint_constraints/linear_limit_softness" == p_name) {
  1986. linear_limit_softness = p_value;
  1987. if (j.is_valid()) {
  1988. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, linear_limit_softness);
  1989. }
  1990. } else if ("joint_constraints/linear_limit_restitution" == p_name) {
  1991. linear_limit_restitution = p_value;
  1992. if (j.is_valid()) {
  1993. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, linear_limit_restitution);
  1994. }
  1995. } else if ("joint_constraints/linear_limit_damping" == p_name) {
  1996. linear_limit_damping = p_value;
  1997. if (j.is_valid()) {
  1998. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, linear_limit_restitution);
  1999. }
  2000. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  2001. angular_limit_upper = Math::deg_to_rad(real_t(p_value));
  2002. if (j.is_valid()) {
  2003. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, angular_limit_upper);
  2004. }
  2005. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  2006. angular_limit_lower = Math::deg_to_rad(real_t(p_value));
  2007. if (j.is_valid()) {
  2008. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, angular_limit_lower);
  2009. }
  2010. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  2011. angular_limit_softness = p_value;
  2012. if (j.is_valid()) {
  2013. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness);
  2014. }
  2015. } else if ("joint_constraints/angular_limit_restitution" == p_name) {
  2016. angular_limit_restitution = p_value;
  2017. if (j.is_valid()) {
  2018. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness);
  2019. }
  2020. } else if ("joint_constraints/angular_limit_damping" == p_name) {
  2021. angular_limit_damping = p_value;
  2022. if (j.is_valid()) {
  2023. PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, angular_limit_damping);
  2024. }
  2025. } else {
  2026. return false;
  2027. }
  2028. return true;
  2029. }
  2030. bool PhysicalBone3D::SliderJointData::_get(const StringName &p_name, Variant &r_ret) const {
  2031. if (JointData::_get(p_name, r_ret)) {
  2032. return true;
  2033. }
  2034. if ("joint_constraints/linear_limit_upper" == p_name) {
  2035. r_ret = linear_limit_upper;
  2036. } else if ("joint_constraints/linear_limit_lower" == p_name) {
  2037. r_ret = linear_limit_lower;
  2038. } else if ("joint_constraints/linear_limit_softness" == p_name) {
  2039. r_ret = linear_limit_softness;
  2040. } else if ("joint_constraints/linear_limit_restitution" == p_name) {
  2041. r_ret = linear_limit_restitution;
  2042. } else if ("joint_constraints/linear_limit_damping" == p_name) {
  2043. r_ret = linear_limit_damping;
  2044. } else if ("joint_constraints/angular_limit_upper" == p_name) {
  2045. r_ret = Math::rad_to_deg(angular_limit_upper);
  2046. } else if ("joint_constraints/angular_limit_lower" == p_name) {
  2047. r_ret = Math::rad_to_deg(angular_limit_lower);
  2048. } else if ("joint_constraints/angular_limit_softness" == p_name) {
  2049. r_ret = angular_limit_softness;
  2050. } else if ("joint_constraints/angular_limit_restitution" == p_name) {
  2051. r_ret = angular_limit_restitution;
  2052. } else if ("joint_constraints/angular_limit_damping" == p_name) {
  2053. r_ret = angular_limit_damping;
  2054. } else {
  2055. return false;
  2056. }
  2057. return true;
  2058. }
  2059. void PhysicalBone3D::SliderJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  2060. JointData::_get_property_list(p_list);
  2061. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_upper")));
  2062. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_lower")));
  2063. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  2064. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_restitution"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  2065. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_damping"), PROPERTY_HINT_RANGE, "0,16.0,0.01"));
  2066. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_upper"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  2067. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_lower"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  2068. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  2069. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_restitution"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
  2070. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_damping"), PROPERTY_HINT_RANGE, "0,16.0,0.01"));
  2071. }
  2072. bool PhysicalBone3D::SixDOFJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
  2073. if (JointData::_set(p_name, p_value, j)) {
  2074. return true;
  2075. }
  2076. String path = p_name;
  2077. if (!path.begins_with("joint_constraints/")) {
  2078. return false;
  2079. }
  2080. Vector3::Axis axis;
  2081. {
  2082. const String axis_s = path.get_slicec('/', 1);
  2083. if ("x" == axis_s) {
  2084. axis = Vector3::AXIS_X;
  2085. } else if ("y" == axis_s) {
  2086. axis = Vector3::AXIS_Y;
  2087. } else if ("z" == axis_s) {
  2088. axis = Vector3::AXIS_Z;
  2089. } else {
  2090. return false;
  2091. }
  2092. }
  2093. String var_name = path.get_slicec('/', 2);
  2094. if ("linear_limit_enabled" == var_name) {
  2095. axis_data[axis].linear_limit_enabled = p_value;
  2096. if (j.is_valid()) {
  2097. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, axis_data[axis].linear_limit_enabled);
  2098. }
  2099. } else if ("linear_limit_upper" == var_name) {
  2100. axis_data[axis].linear_limit_upper = p_value;
  2101. if (j.is_valid()) {
  2102. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_UPPER_LIMIT, axis_data[axis].linear_limit_upper);
  2103. }
  2104. } else if ("linear_limit_lower" == var_name) {
  2105. axis_data[axis].linear_limit_lower = p_value;
  2106. if (j.is_valid()) {
  2107. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_LOWER_LIMIT, axis_data[axis].linear_limit_lower);
  2108. }
  2109. } else if ("linear_limit_softness" == var_name) {
  2110. axis_data[axis].linear_limit_softness = p_value;
  2111. if (j.is_valid()) {
  2112. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, axis_data[axis].linear_limit_softness);
  2113. }
  2114. } else if ("linear_spring_enabled" == var_name) {
  2115. axis_data[axis].linear_spring_enabled = p_value;
  2116. if (j.is_valid()) {
  2117. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, axis_data[axis].linear_spring_enabled);
  2118. }
  2119. } else if ("linear_spring_stiffness" == var_name) {
  2120. axis_data[axis].linear_spring_stiffness = p_value;
  2121. if (j.is_valid()) {
  2122. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, axis_data[axis].linear_spring_stiffness);
  2123. }
  2124. } else if ("linear_spring_damping" == var_name) {
  2125. axis_data[axis].linear_spring_damping = p_value;
  2126. if (j.is_valid()) {
  2127. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_DAMPING, axis_data[axis].linear_spring_damping);
  2128. }
  2129. } else if ("linear_equilibrium_point" == var_name) {
  2130. axis_data[axis].linear_equilibrium_point = p_value;
  2131. if (j.is_valid()) {
  2132. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].linear_equilibrium_point);
  2133. }
  2134. } else if ("linear_restitution" == var_name) {
  2135. axis_data[axis].linear_restitution = p_value;
  2136. if (j.is_valid()) {
  2137. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_RESTITUTION, axis_data[axis].linear_restitution);
  2138. }
  2139. } else if ("linear_damping" == var_name) {
  2140. axis_data[axis].linear_damping = p_value;
  2141. if (j.is_valid()) {
  2142. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_DAMPING, axis_data[axis].linear_damping);
  2143. }
  2144. } else if ("angular_limit_enabled" == var_name) {
  2145. axis_data[axis].angular_limit_enabled = p_value;
  2146. if (j.is_valid()) {
  2147. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, axis_data[axis].angular_limit_enabled);
  2148. }
  2149. } else if ("angular_limit_upper" == var_name) {
  2150. axis_data[axis].angular_limit_upper = Math::deg_to_rad(real_t(p_value));
  2151. if (j.is_valid()) {
  2152. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, axis_data[axis].angular_limit_upper);
  2153. }
  2154. } else if ("angular_limit_lower" == var_name) {
  2155. axis_data[axis].angular_limit_lower = Math::deg_to_rad(real_t(p_value));
  2156. if (j.is_valid()) {
  2157. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, axis_data[axis].angular_limit_lower);
  2158. }
  2159. } else if ("angular_limit_softness" == var_name) {
  2160. axis_data[axis].angular_limit_softness = p_value;
  2161. if (j.is_valid()) {
  2162. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, axis_data[axis].angular_limit_softness);
  2163. }
  2164. } else if ("angular_restitution" == var_name) {
  2165. axis_data[axis].angular_restitution = p_value;
  2166. if (j.is_valid()) {
  2167. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_RESTITUTION, axis_data[axis].angular_restitution);
  2168. }
  2169. } else if ("angular_damping" == var_name) {
  2170. axis_data[axis].angular_damping = p_value;
  2171. if (j.is_valid()) {
  2172. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_DAMPING, axis_data[axis].angular_damping);
  2173. }
  2174. } else if ("erp" == var_name) {
  2175. axis_data[axis].erp = p_value;
  2176. if (j.is_valid()) {
  2177. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_ERP, axis_data[axis].erp);
  2178. }
  2179. } else if ("angular_spring_enabled" == var_name) {
  2180. axis_data[axis].angular_spring_enabled = p_value;
  2181. if (j.is_valid()) {
  2182. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, axis_data[axis].angular_spring_enabled);
  2183. }
  2184. } else if ("angular_spring_stiffness" == var_name) {
  2185. axis_data[axis].angular_spring_stiffness = p_value;
  2186. if (j.is_valid()) {
  2187. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, axis_data[axis].angular_spring_stiffness);
  2188. }
  2189. } else if ("angular_spring_damping" == var_name) {
  2190. axis_data[axis].angular_spring_damping = p_value;
  2191. if (j.is_valid()) {
  2192. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, axis_data[axis].angular_spring_damping);
  2193. }
  2194. } else if ("angular_equilibrium_point" == var_name) {
  2195. axis_data[axis].angular_equilibrium_point = p_value;
  2196. if (j.is_valid()) {
  2197. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].angular_equilibrium_point);
  2198. }
  2199. } else {
  2200. return false;
  2201. }
  2202. return true;
  2203. }
  2204. bool PhysicalBone3D::SixDOFJointData::_get(const StringName &p_name, Variant &r_ret) const {
  2205. if (JointData::_get(p_name, r_ret)) {
  2206. return true;
  2207. }
  2208. String path = p_name;
  2209. if (!path.begins_with("joint_constraints/")) {
  2210. return false;
  2211. }
  2212. int axis;
  2213. {
  2214. const String axis_s = path.get_slicec('/', 1);
  2215. if ("x" == axis_s) {
  2216. axis = 0;
  2217. } else if ("y" == axis_s) {
  2218. axis = 1;
  2219. } else if ("z" == axis_s) {
  2220. axis = 2;
  2221. } else {
  2222. return false;
  2223. }
  2224. }
  2225. String var_name = path.get_slicec('/', 2);
  2226. if ("linear_limit_enabled" == var_name) {
  2227. r_ret = axis_data[axis].linear_limit_enabled;
  2228. } else if ("linear_limit_upper" == var_name) {
  2229. r_ret = axis_data[axis].linear_limit_upper;
  2230. } else if ("linear_limit_lower" == var_name) {
  2231. r_ret = axis_data[axis].linear_limit_lower;
  2232. } else if ("linear_limit_softness" == var_name) {
  2233. r_ret = axis_data[axis].linear_limit_softness;
  2234. } else if ("linear_spring_enabled" == var_name) {
  2235. r_ret = axis_data[axis].linear_spring_enabled;
  2236. } else if ("linear_spring_stiffness" == var_name) {
  2237. r_ret = axis_data[axis].linear_spring_stiffness;
  2238. } else if ("linear_spring_damping" == var_name) {
  2239. r_ret = axis_data[axis].linear_spring_damping;
  2240. } else if ("linear_equilibrium_point" == var_name) {
  2241. r_ret = axis_data[axis].linear_equilibrium_point;
  2242. } else if ("linear_restitution" == var_name) {
  2243. r_ret = axis_data[axis].linear_restitution;
  2244. } else if ("linear_damping" == var_name) {
  2245. r_ret = axis_data[axis].linear_damping;
  2246. } else if ("angular_limit_enabled" == var_name) {
  2247. r_ret = axis_data[axis].angular_limit_enabled;
  2248. } else if ("angular_limit_upper" == var_name) {
  2249. r_ret = Math::rad_to_deg(axis_data[axis].angular_limit_upper);
  2250. } else if ("angular_limit_lower" == var_name) {
  2251. r_ret = Math::rad_to_deg(axis_data[axis].angular_limit_lower);
  2252. } else if ("angular_limit_softness" == var_name) {
  2253. r_ret = axis_data[axis].angular_limit_softness;
  2254. } else if ("angular_restitution" == var_name) {
  2255. r_ret = axis_data[axis].angular_restitution;
  2256. } else if ("angular_damping" == var_name) {
  2257. r_ret = axis_data[axis].angular_damping;
  2258. } else if ("erp" == var_name) {
  2259. r_ret = axis_data[axis].erp;
  2260. } else if ("angular_spring_enabled" == var_name) {
  2261. r_ret = axis_data[axis].angular_spring_enabled;
  2262. } else if ("angular_spring_stiffness" == var_name) {
  2263. r_ret = axis_data[axis].angular_spring_stiffness;
  2264. } else if ("angular_spring_damping" == var_name) {
  2265. r_ret = axis_data[axis].angular_spring_damping;
  2266. } else if ("angular_equilibrium_point" == var_name) {
  2267. r_ret = axis_data[axis].angular_equilibrium_point;
  2268. } else {
  2269. return false;
  2270. }
  2271. return true;
  2272. }
  2273. void PhysicalBone3D::SixDOFJointData::_get_property_list(List<PropertyInfo> *p_list) const {
  2274. const StringName axis_names[] = { PNAME("x"), PNAME("y"), PNAME("z") };
  2275. for (int i = 0; i < 3; ++i) {
  2276. const String prefix = vformat("%s/%s/", PNAME("joint_constraints"), axis_names[i]);
  2277. p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("linear_limit_enabled")));
  2278. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_upper")));
  2279. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_lower")));
  2280. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  2281. p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("linear_spring_enabled")));
  2282. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_spring_stiffness")));
  2283. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_spring_damping")));
  2284. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_equilibrium_point")));
  2285. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_restitution"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  2286. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_damping"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  2287. p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("angular_limit_enabled")));
  2288. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_upper"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  2289. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_lower"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
  2290. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  2291. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_restitution"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  2292. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_damping"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
  2293. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("erp")));
  2294. p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("angular_spring_enabled")));
  2295. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_spring_stiffness")));
  2296. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_spring_damping")));
  2297. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_equilibrium_point")));
  2298. }
  2299. }
  2300. bool PhysicalBone3D::_set(const StringName &p_name, const Variant &p_value) {
  2301. if (p_name == "bone_name") {
  2302. set_bone_name(p_value);
  2303. return true;
  2304. }
  2305. if (joint_data) {
  2306. if (joint_data->_set(p_name, p_value, joint)) {
  2307. #ifdef TOOLS_ENABLED
  2308. update_gizmos();
  2309. #endif
  2310. return true;
  2311. }
  2312. }
  2313. return false;
  2314. }
  2315. bool PhysicalBone3D::_get(const StringName &p_name, Variant &r_ret) const {
  2316. if (p_name == "bone_name") {
  2317. r_ret = get_bone_name();
  2318. return true;
  2319. }
  2320. if (joint_data) {
  2321. return joint_data->_get(p_name, r_ret);
  2322. }
  2323. return false;
  2324. }
  2325. void PhysicalBone3D::_get_property_list(List<PropertyInfo> *p_list) const {
  2326. Skeleton3D *parent = find_skeleton_parent(get_parent());
  2327. if (parent) {
  2328. String names;
  2329. for (int i = 0; i < parent->get_bone_count(); i++) {
  2330. if (i > 0) {
  2331. names += ",";
  2332. }
  2333. names += parent->get_bone_name(i);
  2334. }
  2335. p_list->push_back(PropertyInfo(Variant::STRING_NAME, PNAME("bone_name"), PROPERTY_HINT_ENUM, names));
  2336. } else {
  2337. p_list->push_back(PropertyInfo(Variant::STRING_NAME, PNAME("bone_name")));
  2338. }
  2339. if (joint_data) {
  2340. joint_data->_get_property_list(p_list);
  2341. }
  2342. }
  2343. void PhysicalBone3D::_notification(int p_what) {
  2344. switch (p_what) {
  2345. case NOTIFICATION_ENTER_TREE:
  2346. parent_skeleton = find_skeleton_parent(get_parent());
  2347. update_bone_id();
  2348. reset_to_rest_position();
  2349. reset_physics_simulation_state();
  2350. if (joint_data) {
  2351. _reload_joint();
  2352. }
  2353. break;
  2354. case NOTIFICATION_EXIT_TREE: {
  2355. if (parent_skeleton) {
  2356. if (-1 != bone_id) {
  2357. parent_skeleton->unbind_physical_bone_from_bone(bone_id);
  2358. bone_id = -1;
  2359. }
  2360. }
  2361. parent_skeleton = nullptr;
  2362. PhysicsServer3D::get_singleton()->joint_clear(joint);
  2363. } break;
  2364. case NOTIFICATION_TRANSFORM_CHANGED: {
  2365. if (Engine::get_singleton()->is_editor_hint()) {
  2366. update_offset();
  2367. }
  2368. } break;
  2369. }
  2370. }
  2371. void PhysicalBone3D::_body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state) {
  2372. PhysicalBone3D *bone = (PhysicalBone3D *)p_instance;
  2373. bone->_body_state_changed(p_state);
  2374. }
  2375. void PhysicalBone3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
  2376. if (!simulate_physics || !_internal_simulate_physics) {
  2377. return;
  2378. }
  2379. linear_velocity = p_state->get_linear_velocity();
  2380. angular_velocity = p_state->get_angular_velocity();
  2381. GDVIRTUAL_CALL(_integrate_forces, p_state);
  2382. /// Update bone transform.
  2383. Transform3D global_transform(p_state->get_transform());
  2384. set_ignore_transform_notification(true);
  2385. set_global_transform(global_transform);
  2386. set_ignore_transform_notification(false);
  2387. _on_transform_changed();
  2388. // Update skeleton
  2389. if (parent_skeleton) {
  2390. if (-1 != bone_id) {
  2391. parent_skeleton->set_bone_global_pose_override(bone_id, parent_skeleton->get_global_transform().affine_inverse() * (global_transform * body_offset_inverse), 1.0, true);
  2392. }
  2393. }
  2394. }
  2395. void PhysicalBone3D::_bind_methods() {
  2396. ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &PhysicalBone3D::apply_central_impulse);
  2397. ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &PhysicalBone3D::apply_impulse, Vector3());
  2398. ClassDB::bind_method(D_METHOD("set_joint_type", "joint_type"), &PhysicalBone3D::set_joint_type);
  2399. ClassDB::bind_method(D_METHOD("get_joint_type"), &PhysicalBone3D::get_joint_type);
  2400. ClassDB::bind_method(D_METHOD("set_joint_offset", "offset"), &PhysicalBone3D::set_joint_offset);
  2401. ClassDB::bind_method(D_METHOD("get_joint_offset"), &PhysicalBone3D::get_joint_offset);
  2402. ClassDB::bind_method(D_METHOD("set_joint_rotation", "euler"), &PhysicalBone3D::set_joint_rotation);
  2403. ClassDB::bind_method(D_METHOD("get_joint_rotation"), &PhysicalBone3D::get_joint_rotation);
  2404. ClassDB::bind_method(D_METHOD("set_body_offset", "offset"), &PhysicalBone3D::set_body_offset);
  2405. ClassDB::bind_method(D_METHOD("get_body_offset"), &PhysicalBone3D::get_body_offset);
  2406. ClassDB::bind_method(D_METHOD("get_simulate_physics"), &PhysicalBone3D::get_simulate_physics);
  2407. ClassDB::bind_method(D_METHOD("is_simulating_physics"), &PhysicalBone3D::is_simulating_physics);
  2408. ClassDB::bind_method(D_METHOD("get_bone_id"), &PhysicalBone3D::get_bone_id);
  2409. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &PhysicalBone3D::set_mass);
  2410. ClassDB::bind_method(D_METHOD("get_mass"), &PhysicalBone3D::get_mass);
  2411. ClassDB::bind_method(D_METHOD("set_friction", "friction"), &PhysicalBone3D::set_friction);
  2412. ClassDB::bind_method(D_METHOD("get_friction"), &PhysicalBone3D::get_friction);
  2413. ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &PhysicalBone3D::set_bounce);
  2414. ClassDB::bind_method(D_METHOD("get_bounce"), &PhysicalBone3D::get_bounce);
  2415. ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &PhysicalBone3D::set_gravity_scale);
  2416. ClassDB::bind_method(D_METHOD("get_gravity_scale"), &PhysicalBone3D::get_gravity_scale);
  2417. ClassDB::bind_method(D_METHOD("set_linear_damp_mode", "linear_damp_mode"), &PhysicalBone3D::set_linear_damp_mode);
  2418. ClassDB::bind_method(D_METHOD("get_linear_damp_mode"), &PhysicalBone3D::get_linear_damp_mode);
  2419. ClassDB::bind_method(D_METHOD("set_angular_damp_mode", "angular_damp_mode"), &PhysicalBone3D::set_angular_damp_mode);
  2420. ClassDB::bind_method(D_METHOD("get_angular_damp_mode"), &PhysicalBone3D::get_angular_damp_mode);
  2421. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &PhysicalBone3D::set_linear_damp);
  2422. ClassDB::bind_method(D_METHOD("get_linear_damp"), &PhysicalBone3D::get_linear_damp);
  2423. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &PhysicalBone3D::set_angular_damp);
  2424. ClassDB::bind_method(D_METHOD("get_angular_damp"), &PhysicalBone3D::get_angular_damp);
  2425. ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &PhysicalBone3D::set_linear_velocity);
  2426. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &PhysicalBone3D::get_linear_velocity);
  2427. ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &PhysicalBone3D::set_angular_velocity);
  2428. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &PhysicalBone3D::get_angular_velocity);
  2429. ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &PhysicalBone3D::set_use_custom_integrator);
  2430. ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &PhysicalBone3D::is_using_custom_integrator);
  2431. ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &PhysicalBone3D::set_can_sleep);
  2432. ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &PhysicalBone3D::is_able_to_sleep);
  2433. GDVIRTUAL_BIND(_integrate_forces, "state");
  2434. ADD_GROUP("Joint", "joint_");
  2435. ADD_PROPERTY(PropertyInfo(Variant::INT, "joint_type", PROPERTY_HINT_ENUM, "None,PinJoint,ConeJoint,HingeJoint,SliderJoint,6DOFJoint"), "set_joint_type", "get_joint_type");
  2436. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "joint_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_joint_offset", "get_joint_offset");
  2437. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "joint_rotation", PROPERTY_HINT_RANGE, "-360,360,0.01,or_lesser,or_greater,radians"), "set_joint_rotation", "get_joint_rotation");
  2438. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "body_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_body_offset", "get_body_offset");
  2439. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass", PROPERTY_HINT_RANGE, "0.01,1000,0.01,or_greater,exp,suffix:kg"), "set_mass", "get_mass");
  2440. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_friction", "get_friction");
  2441. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_bounce", "get_bounce");
  2442. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_scale", PROPERTY_HINT_RANGE, "-10,10,0.01"), "set_gravity_scale", "get_gravity_scale");
  2443. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
  2444. ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_linear_damp_mode", "get_linear_damp_mode");
  2445. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
  2446. ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_angular_damp_mode", "get_angular_damp_mode");
  2447. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
  2448. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity", PROPERTY_HINT_NONE, "suffix:m/s"), "set_linear_velocity", "get_linear_velocity");
  2449. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity", PROPERTY_HINT_NONE, U"radians,suffix:\u00B0/s"), "set_angular_velocity", "get_angular_velocity");
  2450. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
  2451. BIND_ENUM_CONSTANT(DAMP_MODE_COMBINE);
  2452. BIND_ENUM_CONSTANT(DAMP_MODE_REPLACE);
  2453. BIND_ENUM_CONSTANT(JOINT_TYPE_NONE);
  2454. BIND_ENUM_CONSTANT(JOINT_TYPE_PIN);
  2455. BIND_ENUM_CONSTANT(JOINT_TYPE_CONE);
  2456. BIND_ENUM_CONSTANT(JOINT_TYPE_HINGE);
  2457. BIND_ENUM_CONSTANT(JOINT_TYPE_SLIDER);
  2458. BIND_ENUM_CONSTANT(JOINT_TYPE_6DOF);
  2459. }
  2460. Skeleton3D *PhysicalBone3D::find_skeleton_parent(Node *p_parent) {
  2461. if (!p_parent) {
  2462. return nullptr;
  2463. }
  2464. Skeleton3D *s = Object::cast_to<Skeleton3D>(p_parent);
  2465. return s ? s : find_skeleton_parent(p_parent->get_parent());
  2466. }
  2467. void PhysicalBone3D::_update_joint_offset() {
  2468. _fix_joint_offset();
  2469. set_ignore_transform_notification(true);
  2470. reset_to_rest_position();
  2471. set_ignore_transform_notification(false);
  2472. #ifdef TOOLS_ENABLED
  2473. update_gizmos();
  2474. #endif
  2475. }
  2476. void PhysicalBone3D::_fix_joint_offset() {
  2477. // Clamp joint origin to bone origin
  2478. if (parent_skeleton) {
  2479. joint_offset.origin = body_offset.affine_inverse().origin;
  2480. }
  2481. }
  2482. void PhysicalBone3D::_reload_joint() {
  2483. if (!parent_skeleton) {
  2484. PhysicsServer3D::get_singleton()->joint_clear(joint);
  2485. return;
  2486. }
  2487. PhysicalBone3D *body_a = parent_skeleton->get_physical_bone_parent(bone_id);
  2488. if (!body_a) {
  2489. PhysicsServer3D::get_singleton()->joint_clear(joint);
  2490. return;
  2491. }
  2492. Transform3D joint_transf = get_global_transform() * joint_offset;
  2493. Transform3D local_a = body_a->get_global_transform().affine_inverse() * joint_transf;
  2494. local_a.orthonormalize();
  2495. switch (get_joint_type()) {
  2496. case JOINT_TYPE_PIN: {
  2497. PhysicsServer3D::get_singleton()->joint_make_pin(joint, body_a->get_rid(), local_a.origin, get_rid(), joint_offset.origin);
  2498. const PinJointData *pjd(static_cast<const PinJointData *>(joint_data));
  2499. PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_BIAS, pjd->bias);
  2500. PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_DAMPING, pjd->damping);
  2501. PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP, pjd->impulse_clamp);
  2502. } break;
  2503. case JOINT_TYPE_CONE: {
  2504. PhysicsServer3D::get_singleton()->joint_make_cone_twist(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
  2505. const ConeJointData *cjd(static_cast<const ConeJointData *>(joint_data));
  2506. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN, cjd->swing_span);
  2507. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN, cjd->twist_span);
  2508. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_BIAS, cjd->bias);
  2509. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS, cjd->softness);
  2510. PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION, cjd->relaxation);
  2511. } break;
  2512. case JOINT_TYPE_HINGE: {
  2513. PhysicsServer3D::get_singleton()->joint_make_hinge(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
  2514. const HingeJointData *hjd(static_cast<const HingeJointData *>(joint_data));
  2515. PhysicsServer3D::get_singleton()->hinge_joint_set_flag(joint, PhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT, hjd->angular_limit_enabled);
  2516. PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_UPPER, hjd->angular_limit_upper);
  2517. PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_LOWER, hjd->angular_limit_lower);
  2518. PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_BIAS, hjd->angular_limit_bias);
  2519. PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_SOFTNESS, hjd->angular_limit_softness);
  2520. PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_RELAXATION, hjd->angular_limit_relaxation);
  2521. } break;
  2522. case JOINT_TYPE_SLIDER: {
  2523. PhysicsServer3D::get_singleton()->joint_make_slider(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
  2524. const SliderJointData *sjd(static_cast<const SliderJointData *>(joint_data));
  2525. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER, sjd->linear_limit_upper);
  2526. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER, sjd->linear_limit_lower);
  2527. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, sjd->linear_limit_softness);
  2528. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, sjd->linear_limit_restitution);
  2529. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, sjd->linear_limit_restitution);
  2530. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, sjd->angular_limit_upper);
  2531. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, sjd->angular_limit_lower);
  2532. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness);
  2533. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness);
  2534. PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, sjd->angular_limit_damping);
  2535. } break;
  2536. case JOINT_TYPE_6DOF: {
  2537. PhysicsServer3D::get_singleton()->joint_make_generic_6dof(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
  2538. const SixDOFJointData *g6dofjd(static_cast<const SixDOFJointData *>(joint_data));
  2539. for (int axis = 0; axis < 3; ++axis) {
  2540. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, g6dofjd->axis_data[axis].linear_limit_enabled);
  2541. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_UPPER_LIMIT, g6dofjd->axis_data[axis].linear_limit_upper);
  2542. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_LOWER_LIMIT, g6dofjd->axis_data[axis].linear_limit_lower);
  2543. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].linear_limit_softness);
  2544. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, g6dofjd->axis_data[axis].linear_spring_enabled);
  2545. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].linear_spring_stiffness);
  2546. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_DAMPING, g6dofjd->axis_data[axis].linear_spring_damping);
  2547. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].linear_equilibrium_point);
  2548. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_RESTITUTION, g6dofjd->axis_data[axis].linear_restitution);
  2549. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_DAMPING, g6dofjd->axis_data[axis].linear_damping);
  2550. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, g6dofjd->axis_data[axis].angular_limit_enabled);
  2551. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, g6dofjd->axis_data[axis].angular_limit_upper);
  2552. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, g6dofjd->axis_data[axis].angular_limit_lower);
  2553. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].angular_limit_softness);
  2554. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_RESTITUTION, g6dofjd->axis_data[axis].angular_restitution);
  2555. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_DAMPING, g6dofjd->axis_data[axis].angular_damping);
  2556. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_ERP, g6dofjd->axis_data[axis].erp);
  2557. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, g6dofjd->axis_data[axis].angular_spring_enabled);
  2558. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].angular_spring_stiffness);
  2559. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, g6dofjd->axis_data[axis].angular_spring_damping);
  2560. PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].angular_equilibrium_point);
  2561. }
  2562. } break;
  2563. case JOINT_TYPE_NONE: {
  2564. } break;
  2565. }
  2566. }
  2567. void PhysicalBone3D::_on_bone_parent_changed() {
  2568. _reload_joint();
  2569. }
  2570. #ifdef TOOLS_ENABLED
  2571. void PhysicalBone3D::_set_gizmo_move_joint(bool p_move_joint) {
  2572. gizmo_move_joint = p_move_joint;
  2573. }
  2574. Transform3D PhysicalBone3D::get_global_gizmo_transform() const {
  2575. return gizmo_move_joint ? get_global_transform() * joint_offset : get_global_transform();
  2576. }
  2577. Transform3D PhysicalBone3D::get_local_gizmo_transform() const {
  2578. return gizmo_move_joint ? get_transform() * joint_offset : get_transform();
  2579. }
  2580. #endif
  2581. const PhysicalBone3D::JointData *PhysicalBone3D::get_joint_data() const {
  2582. return joint_data;
  2583. }
  2584. Skeleton3D *PhysicalBone3D::find_skeleton_parent() {
  2585. return find_skeleton_parent(this);
  2586. }
  2587. void PhysicalBone3D::set_joint_type(JointType p_joint_type) {
  2588. if (p_joint_type == get_joint_type()) {
  2589. return;
  2590. }
  2591. if (joint_data) {
  2592. memdelete(joint_data);
  2593. }
  2594. joint_data = nullptr;
  2595. switch (p_joint_type) {
  2596. case JOINT_TYPE_PIN:
  2597. joint_data = memnew(PinJointData);
  2598. break;
  2599. case JOINT_TYPE_CONE:
  2600. joint_data = memnew(ConeJointData);
  2601. break;
  2602. case JOINT_TYPE_HINGE:
  2603. joint_data = memnew(HingeJointData);
  2604. break;
  2605. case JOINT_TYPE_SLIDER:
  2606. joint_data = memnew(SliderJointData);
  2607. break;
  2608. case JOINT_TYPE_6DOF:
  2609. joint_data = memnew(SixDOFJointData);
  2610. break;
  2611. case JOINT_TYPE_NONE:
  2612. break;
  2613. }
  2614. _reload_joint();
  2615. #ifdef TOOLS_ENABLED
  2616. notify_property_list_changed();
  2617. update_gizmos();
  2618. #endif
  2619. }
  2620. PhysicalBone3D::JointType PhysicalBone3D::get_joint_type() const {
  2621. return joint_data ? joint_data->get_joint_type() : JOINT_TYPE_NONE;
  2622. }
  2623. void PhysicalBone3D::set_joint_offset(const Transform3D &p_offset) {
  2624. joint_offset = p_offset;
  2625. _update_joint_offset();
  2626. }
  2627. const Transform3D &PhysicalBone3D::get_joint_offset() const {
  2628. return joint_offset;
  2629. }
  2630. void PhysicalBone3D::set_joint_rotation(const Vector3 &p_euler_rad) {
  2631. joint_offset.basis.set_euler_scale(p_euler_rad, joint_offset.basis.get_scale());
  2632. _update_joint_offset();
  2633. }
  2634. Vector3 PhysicalBone3D::get_joint_rotation() const {
  2635. return joint_offset.basis.get_euler_normalized();
  2636. }
  2637. const Transform3D &PhysicalBone3D::get_body_offset() const {
  2638. return body_offset;
  2639. }
  2640. void PhysicalBone3D::set_body_offset(const Transform3D &p_offset) {
  2641. body_offset = p_offset;
  2642. body_offset_inverse = body_offset.affine_inverse();
  2643. _update_joint_offset();
  2644. }
  2645. void PhysicalBone3D::set_simulate_physics(bool p_simulate) {
  2646. if (simulate_physics == p_simulate) {
  2647. return;
  2648. }
  2649. simulate_physics = p_simulate;
  2650. reset_physics_simulation_state();
  2651. }
  2652. bool PhysicalBone3D::get_simulate_physics() {
  2653. return simulate_physics;
  2654. }
  2655. bool PhysicalBone3D::is_simulating_physics() {
  2656. return _internal_simulate_physics;
  2657. }
  2658. void PhysicalBone3D::set_bone_name(const String &p_name) {
  2659. bone_name = p_name;
  2660. bone_id = -1;
  2661. update_bone_id();
  2662. reset_to_rest_position();
  2663. }
  2664. const String &PhysicalBone3D::get_bone_name() const {
  2665. return bone_name;
  2666. }
  2667. void PhysicalBone3D::set_mass(real_t p_mass) {
  2668. ERR_FAIL_COND(p_mass <= 0);
  2669. mass = p_mass;
  2670. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_MASS, mass);
  2671. }
  2672. real_t PhysicalBone3D::get_mass() const {
  2673. return mass;
  2674. }
  2675. void PhysicalBone3D::set_friction(real_t p_friction) {
  2676. ERR_FAIL_COND(p_friction < 0 || p_friction > 1);
  2677. friction = p_friction;
  2678. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, friction);
  2679. }
  2680. real_t PhysicalBone3D::get_friction() const {
  2681. return friction;
  2682. }
  2683. void PhysicalBone3D::set_bounce(real_t p_bounce) {
  2684. ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
  2685. bounce = p_bounce;
  2686. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, bounce);
  2687. }
  2688. real_t PhysicalBone3D::get_bounce() const {
  2689. return bounce;
  2690. }
  2691. void PhysicalBone3D::set_gravity_scale(real_t p_gravity_scale) {
  2692. gravity_scale = p_gravity_scale;
  2693. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
  2694. }
  2695. real_t PhysicalBone3D::get_gravity_scale() const {
  2696. return gravity_scale;
  2697. }
  2698. void PhysicalBone3D::set_linear_damp_mode(DampMode p_mode) {
  2699. linear_damp_mode = p_mode;
  2700. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE, linear_damp_mode);
  2701. }
  2702. PhysicalBone3D::DampMode PhysicalBone3D::get_linear_damp_mode() const {
  2703. return linear_damp_mode;
  2704. }
  2705. void PhysicalBone3D::set_angular_damp_mode(DampMode p_mode) {
  2706. angular_damp_mode = p_mode;
  2707. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE, angular_damp_mode);
  2708. }
  2709. PhysicalBone3D::DampMode PhysicalBone3D::get_angular_damp_mode() const {
  2710. return angular_damp_mode;
  2711. }
  2712. void PhysicalBone3D::set_linear_damp(real_t p_linear_damp) {
  2713. ERR_FAIL_COND(p_linear_damp < 0);
  2714. linear_damp = p_linear_damp;
  2715. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP, linear_damp);
  2716. }
  2717. real_t PhysicalBone3D::get_linear_damp() const {
  2718. return linear_damp;
  2719. }
  2720. void PhysicalBone3D::set_angular_damp(real_t p_angular_damp) {
  2721. ERR_FAIL_COND(p_angular_damp < 0);
  2722. angular_damp = p_angular_damp;
  2723. PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP, angular_damp);
  2724. }
  2725. real_t PhysicalBone3D::get_angular_damp() const {
  2726. return angular_damp;
  2727. }
  2728. void PhysicalBone3D::set_can_sleep(bool p_active) {
  2729. can_sleep = p_active;
  2730. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_CAN_SLEEP, p_active);
  2731. }
  2732. bool PhysicalBone3D::is_able_to_sleep() const {
  2733. return can_sleep;
  2734. }
  2735. PhysicalBone3D::PhysicalBone3D() :
  2736. PhysicsBody3D(PhysicsServer3D::BODY_MODE_STATIC) {
  2737. joint = PhysicsServer3D::get_singleton()->joint_create();
  2738. reset_physics_simulation_state();
  2739. }
  2740. PhysicalBone3D::~PhysicalBone3D() {
  2741. if (joint_data) {
  2742. memdelete(joint_data);
  2743. }
  2744. PhysicsServer3D::get_singleton()->free(joint);
  2745. }
  2746. void PhysicalBone3D::update_bone_id() {
  2747. if (!parent_skeleton) {
  2748. return;
  2749. }
  2750. const int new_bone_id = parent_skeleton->find_bone(bone_name);
  2751. if (new_bone_id != bone_id) {
  2752. if (-1 != bone_id) {
  2753. // Assert the unbind from old node
  2754. parent_skeleton->unbind_physical_bone_from_bone(bone_id);
  2755. }
  2756. bone_id = new_bone_id;
  2757. parent_skeleton->bind_physical_bone_to_bone(bone_id, this);
  2758. _fix_joint_offset();
  2759. reset_physics_simulation_state();
  2760. }
  2761. }
  2762. void PhysicalBone3D::update_offset() {
  2763. #ifdef TOOLS_ENABLED
  2764. if (parent_skeleton) {
  2765. Transform3D bone_transform(parent_skeleton->get_global_transform());
  2766. if (-1 != bone_id) {
  2767. bone_transform *= parent_skeleton->get_bone_global_pose(bone_id);
  2768. }
  2769. if (gizmo_move_joint) {
  2770. bone_transform *= body_offset;
  2771. set_joint_offset(bone_transform.affine_inverse() * get_global_transform());
  2772. } else {
  2773. set_body_offset(bone_transform.affine_inverse() * get_global_transform());
  2774. }
  2775. }
  2776. #endif
  2777. }
  2778. void PhysicalBone3D::_start_physics_simulation() {
  2779. if (_internal_simulate_physics || !parent_skeleton) {
  2780. return;
  2781. }
  2782. reset_to_rest_position();
  2783. set_body_mode(PhysicsServer3D::BODY_MODE_RIGID);
  2784. PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer());
  2785. PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask());
  2786. PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), get_collision_priority());
  2787. PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), this, _body_state_changed_callback);
  2788. set_as_top_level(true);
  2789. _internal_simulate_physics = true;
  2790. }
  2791. void PhysicalBone3D::_stop_physics_simulation() {
  2792. if (!parent_skeleton) {
  2793. return;
  2794. }
  2795. if (parent_skeleton->get_animate_physical_bones()) {
  2796. set_body_mode(PhysicsServer3D::BODY_MODE_KINEMATIC);
  2797. PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer());
  2798. PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask());
  2799. PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), get_collision_priority());
  2800. } else {
  2801. set_body_mode(PhysicsServer3D::BODY_MODE_STATIC);
  2802. PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), 0);
  2803. PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), 0);
  2804. PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), 1.0);
  2805. }
  2806. if (_internal_simulate_physics) {
  2807. PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), nullptr, nullptr);
  2808. parent_skeleton->set_bone_global_pose_override(bone_id, Transform3D(), 0.0, false);
  2809. set_as_top_level(false);
  2810. _internal_simulate_physics = false;
  2811. }
  2812. }