physics_body_3d.cpp 141 KB

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