physics_body_3d.cpp 141 KB

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