2
0

physics_body.cpp 102 KB

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