2
0

physics_body_3d.cpp 140 KB

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