2
0

physics_body_2d.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  1. /*************************************************************************/
  2. /* physics_body_2d.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_2d.h"
  31. #include "core/core_string_names.h"
  32. #include "core/engine.h"
  33. #include "core/list.h"
  34. #include "core/math/math_funcs.h"
  35. #include "core/method_bind_ext.gen.inc"
  36. #include "core/object.h"
  37. #include "core/rid.h"
  38. #include "scene/scene_string_names.h"
  39. void PhysicsBody2D::_notification(int p_what) {
  40. }
  41. void PhysicsBody2D::_set_layers(uint32_t p_mask) {
  42. set_collision_layer(p_mask);
  43. set_collision_mask(p_mask);
  44. }
  45. uint32_t PhysicsBody2D::_get_layers() const {
  46. return get_collision_layer();
  47. }
  48. void PhysicsBody2D::_bind_methods() {
  49. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &PhysicsBody2D::set_collision_layer);
  50. ClassDB::bind_method(D_METHOD("get_collision_layer"), &PhysicsBody2D::get_collision_layer);
  51. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &PhysicsBody2D::set_collision_mask);
  52. ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsBody2D::get_collision_mask);
  53. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &PhysicsBody2D::set_collision_mask_bit);
  54. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &PhysicsBody2D::get_collision_mask_bit);
  55. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &PhysicsBody2D::set_collision_layer_bit);
  56. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &PhysicsBody2D::get_collision_layer_bit);
  57. ClassDB::bind_method(D_METHOD("_set_layers", "mask"), &PhysicsBody2D::_set_layers);
  58. ClassDB::bind_method(D_METHOD("_get_layers"), &PhysicsBody2D::_get_layers);
  59. ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody2D::get_collision_exceptions);
  60. ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody2D::add_collision_exception_with);
  61. ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody2D::remove_collision_exception_with);
  62. ADD_PROPERTY(PropertyInfo(Variant::INT, "layers", PROPERTY_HINT_LAYERS_2D_PHYSICS, "", 0), "_set_layers", "_get_layers"); //for backwards compat
  63. ADD_GROUP("Collision", "collision_");
  64. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_layer", "get_collision_layer");
  65. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  66. }
  67. void PhysicsBody2D::set_collision_layer(uint32_t p_layer) {
  68. collision_layer = p_layer;
  69. Physics2DServer::get_singleton()->body_set_collision_layer(get_rid(), p_layer);
  70. }
  71. uint32_t PhysicsBody2D::get_collision_layer() const {
  72. return collision_layer;
  73. }
  74. void PhysicsBody2D::set_collision_mask(uint32_t p_mask) {
  75. collision_mask = p_mask;
  76. Physics2DServer::get_singleton()->body_set_collision_mask(get_rid(), p_mask);
  77. }
  78. uint32_t PhysicsBody2D::get_collision_mask() const {
  79. return collision_mask;
  80. }
  81. void PhysicsBody2D::set_collision_mask_bit(int p_bit, bool p_value) {
  82. uint32_t mask = get_collision_mask();
  83. if (p_value)
  84. mask |= 1 << p_bit;
  85. else
  86. mask &= ~(1 << p_bit);
  87. set_collision_mask(mask);
  88. }
  89. bool PhysicsBody2D::get_collision_mask_bit(int p_bit) const {
  90. return get_collision_mask() & (1 << p_bit);
  91. }
  92. void PhysicsBody2D::set_collision_layer_bit(int p_bit, bool p_value) {
  93. uint32_t collision_layer = get_collision_layer();
  94. if (p_value)
  95. collision_layer |= 1 << p_bit;
  96. else
  97. collision_layer &= ~(1 << p_bit);
  98. set_collision_layer(collision_layer);
  99. }
  100. bool PhysicsBody2D::get_collision_layer_bit(int p_bit) const {
  101. return get_collision_layer() & (1 << p_bit);
  102. }
  103. PhysicsBody2D::PhysicsBody2D(Physics2DServer::BodyMode p_mode) :
  104. CollisionObject2D(Physics2DServer::get_singleton()->body_create(), false) {
  105. Physics2DServer::get_singleton()->body_set_mode(get_rid(), p_mode);
  106. collision_layer = 1;
  107. collision_mask = 1;
  108. set_pickable(false);
  109. }
  110. Array PhysicsBody2D::get_collision_exceptions() {
  111. List<RID> exceptions;
  112. Physics2DServer::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);
  113. Array ret;
  114. for (List<RID>::Element *E = exceptions.front(); E; E = E->next()) {
  115. RID body = E->get();
  116. ObjectID instance_id = Physics2DServer::get_singleton()->body_get_object_instance_id(body);
  117. Object *obj = ObjectDB::get_instance(instance_id);
  118. PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(obj);
  119. ret.append(physics_body);
  120. }
  121. return ret;
  122. }
  123. void PhysicsBody2D::add_collision_exception_with(Node *p_node) {
  124. ERR_FAIL_NULL(p_node);
  125. PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);
  126. if (!physics_body) {
  127. ERR_EXPLAIN("Collision exception only works between two objects of PhysicsBody type");
  128. }
  129. ERR_FAIL_COND(!physics_body);
  130. Physics2DServer::get_singleton()->body_add_collision_exception(get_rid(), physics_body->get_rid());
  131. }
  132. void PhysicsBody2D::remove_collision_exception_with(Node *p_node) {
  133. ERR_FAIL_NULL(p_node);
  134. PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);
  135. if (!physics_body) {
  136. ERR_EXPLAIN("Collision exception only works between two objects of PhysicsBody type");
  137. }
  138. ERR_FAIL_COND(!physics_body);
  139. Physics2DServer::get_singleton()->body_remove_collision_exception(get_rid(), physics_body->get_rid());
  140. }
  141. void StaticBody2D::set_constant_linear_velocity(const Vector2 &p_vel) {
  142. constant_linear_velocity = p_vel;
  143. Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_LINEAR_VELOCITY, constant_linear_velocity);
  144. }
  145. void StaticBody2D::set_constant_angular_velocity(real_t p_vel) {
  146. constant_angular_velocity = p_vel;
  147. Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_ANGULAR_VELOCITY, constant_angular_velocity);
  148. }
  149. Vector2 StaticBody2D::get_constant_linear_velocity() const {
  150. return constant_linear_velocity;
  151. }
  152. real_t StaticBody2D::get_constant_angular_velocity() const {
  153. return constant_angular_velocity;
  154. }
  155. #ifndef DISABLE_DEPRECATED
  156. void StaticBody2D::set_friction(real_t p_friction) {
  157. if (p_friction == 1.0) { // default value, don't create an override for that
  158. return;
  159. }
  160. ERR_EXPLAIN("The method set_friction has been deprecated and will be removed in the future, use physics material instead.")
  161. WARN_DEPRECATED
  162. ERR_FAIL_COND(p_friction < 0 || p_friction > 1);
  163. if (physics_material_override.is_null()) {
  164. physics_material_override.instance();
  165. set_physics_material_override(physics_material_override);
  166. }
  167. physics_material_override->set_friction(p_friction);
  168. }
  169. real_t StaticBody2D::get_friction() const {
  170. ERR_EXPLAIN("The method get_friction has been deprecated and will be removed in the future, use physics material instead.")
  171. WARN_DEPRECATED
  172. if (physics_material_override.is_null()) {
  173. return 1;
  174. }
  175. return physics_material_override->get_friction();
  176. }
  177. void StaticBody2D::set_bounce(real_t p_bounce) {
  178. if (p_bounce == 0.0) { // default value, don't create an override for that
  179. return;
  180. }
  181. ERR_EXPLAIN("The method set_bounce has been deprecated and will be removed in the future, use physics material instead.")
  182. WARN_DEPRECATED
  183. ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
  184. if (physics_material_override.is_null()) {
  185. physics_material_override.instance();
  186. set_physics_material_override(physics_material_override);
  187. }
  188. physics_material_override->set_bounce(p_bounce);
  189. }
  190. real_t StaticBody2D::get_bounce() const {
  191. ERR_EXPLAIN("The method get_bounce has been deprecated and will be removed in the future, use physics material instead.")
  192. WARN_DEPRECATED
  193. if (physics_material_override.is_null()) {
  194. return 0;
  195. }
  196. return physics_material_override->get_bounce();
  197. }
  198. #endif // DISABLE_DEPRECATED
  199. void StaticBody2D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  200. if (physics_material_override.is_valid()) {
  201. if (physics_material_override->is_connected(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics"))
  202. physics_material_override->disconnect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
  203. }
  204. physics_material_override = p_physics_material_override;
  205. if (physics_material_override.is_valid()) {
  206. physics_material_override->connect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
  207. }
  208. _reload_physics_characteristics();
  209. }
  210. Ref<PhysicsMaterial> StaticBody2D::get_physics_material_override() const {
  211. return physics_material_override;
  212. }
  213. void StaticBody2D::_bind_methods() {
  214. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "vel"), &StaticBody2D::set_constant_linear_velocity);
  215. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "vel"), &StaticBody2D::set_constant_angular_velocity);
  216. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity"), &StaticBody2D::get_constant_linear_velocity);
  217. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity"), &StaticBody2D::get_constant_angular_velocity);
  218. #ifndef DISABLE_DEPRECATED
  219. ClassDB::bind_method(D_METHOD("set_friction", "friction"), &StaticBody2D::set_friction);
  220. ClassDB::bind_method(D_METHOD("get_friction"), &StaticBody2D::get_friction);
  221. ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &StaticBody2D::set_bounce);
  222. ClassDB::bind_method(D_METHOD("get_bounce"), &StaticBody2D::get_bounce);
  223. #endif // DISABLE_DEPRECATED
  224. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &StaticBody2D::set_physics_material_override);
  225. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &StaticBody2D::get_physics_material_override);
  226. ClassDB::bind_method(D_METHOD("_reload_physics_characteristics"), &StaticBody2D::_reload_physics_characteristics);
  227. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "constant_linear_velocity"), "set_constant_linear_velocity", "get_constant_linear_velocity");
  228. ADD_PROPERTY(PropertyInfo(Variant::REAL, "constant_angular_velocity"), "set_constant_angular_velocity", "get_constant_angular_velocity");
  229. #ifndef DISABLE_DEPRECATED
  230. ADD_PROPERTY(PropertyInfo(Variant::REAL, "friction", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_friction", "get_friction");
  231. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_bounce", "get_bounce");
  232. #endif // DISABLE_DEPRECATED
  233. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  234. }
  235. StaticBody2D::StaticBody2D() :
  236. PhysicsBody2D(Physics2DServer::BODY_MODE_STATIC) {
  237. constant_angular_velocity = 0;
  238. }
  239. StaticBody2D::~StaticBody2D() {
  240. }
  241. void StaticBody2D::_reload_physics_characteristics() {
  242. if (physics_material_override.is_null()) {
  243. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_BOUNCE, 0);
  244. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_FRICTION, 1);
  245. } else {
  246. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  247. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  248. }
  249. }
  250. void RigidBody2D::_body_enter_tree(ObjectID p_id) {
  251. Object *obj = ObjectDB::get_instance(p_id);
  252. Node *node = Object::cast_to<Node>(obj);
  253. ERR_FAIL_COND(!node);
  254. ERR_FAIL_COND(!contact_monitor);
  255. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(p_id);
  256. ERR_FAIL_COND(!E);
  257. ERR_FAIL_COND(E->get().in_scene);
  258. contact_monitor->locked = true;
  259. E->get().in_scene = true;
  260. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  261. for (int i = 0; i < E->get().shapes.size(); i++) {
  262. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].local_shape);
  263. }
  264. contact_monitor->locked = false;
  265. }
  266. void RigidBody2D::_body_exit_tree(ObjectID p_id) {
  267. Object *obj = ObjectDB::get_instance(p_id);
  268. Node *node = Object::cast_to<Node>(obj);
  269. ERR_FAIL_COND(!node);
  270. ERR_FAIL_COND(!contact_monitor);
  271. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(p_id);
  272. ERR_FAIL_COND(!E);
  273. ERR_FAIL_COND(!E->get().in_scene);
  274. E->get().in_scene = false;
  275. contact_monitor->locked = true;
  276. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  277. for (int i = 0; i < E->get().shapes.size(); i++) {
  278. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].local_shape);
  279. }
  280. contact_monitor->locked = false;
  281. }
  282. void RigidBody2D::_body_inout(int p_status, ObjectID p_instance, int p_body_shape, int p_local_shape) {
  283. bool body_in = p_status == 1;
  284. ObjectID objid = p_instance;
  285. Object *obj = ObjectDB::get_instance(objid);
  286. Node *node = Object::cast_to<Node>(obj);
  287. ERR_FAIL_COND(!contact_monitor);
  288. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(objid);
  289. ERR_FAIL_COND(!body_in && !E);
  290. if (body_in) {
  291. if (!E) {
  292. E = contact_monitor->body_map.insert(objid, BodyState());
  293. //E->get().rc=0;
  294. E->get().in_scene = node && node->is_inside_tree();
  295. if (node) {
  296. node->connect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree, make_binds(objid));
  297. node->connect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree, make_binds(objid));
  298. if (E->get().in_scene) {
  299. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  300. }
  301. }
  302. //E->get().rc++;
  303. }
  304. if (node)
  305. E->get().shapes.insert(ShapePair(p_body_shape, p_local_shape));
  306. if (E->get().in_scene) {
  307. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, objid, node, p_body_shape, p_local_shape);
  308. }
  309. } else {
  310. //E->get().rc--;
  311. if (node)
  312. E->get().shapes.erase(ShapePair(p_body_shape, p_local_shape));
  313. bool in_scene = E->get().in_scene;
  314. if (E->get().shapes.empty()) {
  315. if (node) {
  316. node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree);
  317. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
  318. if (in_scene)
  319. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  320. }
  321. contact_monitor->body_map.erase(E);
  322. }
  323. if (node && in_scene) {
  324. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, objid, node, p_body_shape, p_local_shape);
  325. }
  326. }
  327. }
  328. struct _RigidBody2DInOut {
  329. ObjectID id;
  330. int shape;
  331. int local_shape;
  332. };
  333. bool RigidBody2D::_test_motion(const Vector2 &p_motion, bool p_infinite_inertia, float p_margin, const Ref<Physics2DTestMotionResult> &p_result) {
  334. Physics2DServer::MotionResult *r = NULL;
  335. if (p_result.is_valid())
  336. r = p_result->get_result_ptr();
  337. return Physics2DServer::get_singleton()->body_test_motion(get_rid(), get_global_transform(), p_motion, p_infinite_inertia, p_margin, r);
  338. }
  339. void RigidBody2D::_direct_state_changed(Object *p_state) {
  340. #ifdef DEBUG_ENABLED
  341. state = Object::cast_to<Physics2DDirectBodyState>(p_state);
  342. #else
  343. state = (Physics2DDirectBodyState *)p_state; //trust it
  344. #endif
  345. set_block_transform_notify(true); // don't want notify (would feedback loop)
  346. if (mode != MODE_KINEMATIC)
  347. set_global_transform(state->get_transform());
  348. linear_velocity = state->get_linear_velocity();
  349. angular_velocity = state->get_angular_velocity();
  350. if (sleeping != state->is_sleeping()) {
  351. sleeping = state->is_sleeping();
  352. emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed);
  353. }
  354. if (get_script_instance())
  355. get_script_instance()->call("_integrate_forces", state);
  356. set_block_transform_notify(false); // want it back
  357. if (contact_monitor) {
  358. contact_monitor->locked = true;
  359. //untag all
  360. int rc = 0;
  361. for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  362. for (int i = 0; i < E->get().shapes.size(); i++) {
  363. E->get().shapes[i].tagged = false;
  364. rc++;
  365. }
  366. }
  367. _RigidBody2DInOut *toadd = (_RigidBody2DInOut *)alloca(state->get_contact_count() * sizeof(_RigidBody2DInOut));
  368. int toadd_count = 0; //state->get_contact_count();
  369. RigidBody2D_RemoveAction *toremove = (RigidBody2D_RemoveAction *)alloca(rc * sizeof(RigidBody2D_RemoveAction));
  370. int toremove_count = 0;
  371. //put the ones to add
  372. for (int i = 0; i < state->get_contact_count(); i++) {
  373. ObjectID obj = state->get_contact_collider_id(i);
  374. int local_shape = state->get_contact_local_shape(i);
  375. int shape = state->get_contact_collider_shape(i);
  376. //bool found=false;
  377. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(obj);
  378. if (!E) {
  379. toadd[toadd_count].local_shape = local_shape;
  380. toadd[toadd_count].id = obj;
  381. toadd[toadd_count].shape = shape;
  382. toadd_count++;
  383. continue;
  384. }
  385. ShapePair sp(shape, local_shape);
  386. int idx = E->get().shapes.find(sp);
  387. if (idx == -1) {
  388. toadd[toadd_count].local_shape = local_shape;
  389. toadd[toadd_count].id = obj;
  390. toadd[toadd_count].shape = shape;
  391. toadd_count++;
  392. continue;
  393. }
  394. E->get().shapes[idx].tagged = true;
  395. }
  396. //put the ones to remove
  397. for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  398. for (int i = 0; i < E->get().shapes.size(); i++) {
  399. if (!E->get().shapes[i].tagged) {
  400. toremove[toremove_count].body_id = E->key();
  401. toremove[toremove_count].pair = E->get().shapes[i];
  402. toremove_count++;
  403. }
  404. }
  405. }
  406. //process remotions
  407. for (int i = 0; i < toremove_count; i++) {
  408. _body_inout(0, toremove[i].body_id, toremove[i].pair.body_shape, toremove[i].pair.local_shape);
  409. }
  410. //process aditions
  411. for (int i = 0; i < toadd_count; i++) {
  412. _body_inout(1, toadd[i].id, toadd[i].shape, toadd[i].local_shape);
  413. }
  414. contact_monitor->locked = false;
  415. }
  416. state = NULL;
  417. }
  418. void RigidBody2D::set_mode(Mode p_mode) {
  419. mode = p_mode;
  420. switch (p_mode) {
  421. case MODE_RIGID: {
  422. Physics2DServer::get_singleton()->body_set_mode(get_rid(), Physics2DServer::BODY_MODE_RIGID);
  423. } break;
  424. case MODE_STATIC: {
  425. Physics2DServer::get_singleton()->body_set_mode(get_rid(), Physics2DServer::BODY_MODE_STATIC);
  426. } break;
  427. case MODE_KINEMATIC: {
  428. Physics2DServer::get_singleton()->body_set_mode(get_rid(), Physics2DServer::BODY_MODE_KINEMATIC);
  429. } break;
  430. case MODE_CHARACTER: {
  431. Physics2DServer::get_singleton()->body_set_mode(get_rid(), Physics2DServer::BODY_MODE_CHARACTER);
  432. } break;
  433. }
  434. }
  435. RigidBody2D::Mode RigidBody2D::get_mode() const {
  436. return mode;
  437. }
  438. void RigidBody2D::set_mass(real_t p_mass) {
  439. ERR_FAIL_COND(p_mass <= 0);
  440. mass = p_mass;
  441. _change_notify("mass");
  442. _change_notify("weight");
  443. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_MASS, mass);
  444. }
  445. real_t RigidBody2D::get_mass() const {
  446. return mass;
  447. }
  448. void RigidBody2D::set_inertia(real_t p_inertia) {
  449. ERR_FAIL_COND(p_inertia <= 0);
  450. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_INERTIA, p_inertia);
  451. }
  452. real_t RigidBody2D::get_inertia() const {
  453. return Physics2DServer::get_singleton()->body_get_param(get_rid(), Physics2DServer::BODY_PARAM_INERTIA);
  454. }
  455. void RigidBody2D::set_weight(real_t p_weight) {
  456. set_mass(p_weight / (real_t(GLOBAL_DEF("physics/2d/default_gravity", 98)) / 10));
  457. }
  458. real_t RigidBody2D::get_weight() const {
  459. return mass * (real_t(GLOBAL_DEF("physics/2d/default_gravity", 98)) / 10);
  460. }
  461. #ifndef DISABLE_DEPRECATED
  462. void RigidBody2D::set_friction(real_t p_friction) {
  463. if (p_friction == 1.0) { // default value, don't create an override for that
  464. return;
  465. }
  466. ERR_EXPLAIN("The method set_friction has been deprecated and will be removed in the future, use physics material instead.")
  467. WARN_DEPRECATED
  468. ERR_FAIL_COND(p_friction < 0 || p_friction > 1);
  469. if (physics_material_override.is_null()) {
  470. physics_material_override.instance();
  471. set_physics_material_override(physics_material_override);
  472. }
  473. physics_material_override->set_friction(p_friction);
  474. }
  475. real_t RigidBody2D::get_friction() const {
  476. ERR_EXPLAIN("The method get_friction has been deprecated and will be removed in the future, use physics material instead.")
  477. WARN_DEPRECATED
  478. if (physics_material_override.is_null()) {
  479. return 1;
  480. }
  481. return physics_material_override->get_friction();
  482. }
  483. void RigidBody2D::set_bounce(real_t p_bounce) {
  484. if (p_bounce == 0.0) { // default value, don't create an override for that
  485. return;
  486. }
  487. ERR_EXPLAIN("The method set_bounce has been deprecated and will be removed in the future, use physics material instead.")
  488. WARN_DEPRECATED
  489. ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
  490. if (physics_material_override.is_null()) {
  491. physics_material_override.instance();
  492. set_physics_material_override(physics_material_override);
  493. }
  494. physics_material_override->set_bounce(p_bounce);
  495. }
  496. real_t RigidBody2D::get_bounce() const {
  497. ERR_EXPLAIN("The method get_bounce has been deprecated and will be removed in the future, use physics material instead.")
  498. WARN_DEPRECATED
  499. if (physics_material_override.is_null()) {
  500. return 0;
  501. }
  502. return physics_material_override->get_bounce();
  503. }
  504. #endif // DISABLE_DEPRECATED
  505. void RigidBody2D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  506. if (physics_material_override.is_valid()) {
  507. if (physics_material_override->is_connected(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics"))
  508. physics_material_override->disconnect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
  509. }
  510. physics_material_override = p_physics_material_override;
  511. if (physics_material_override.is_valid()) {
  512. physics_material_override->connect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
  513. }
  514. _reload_physics_characteristics();
  515. }
  516. Ref<PhysicsMaterial> RigidBody2D::get_physics_material_override() const {
  517. return physics_material_override;
  518. }
  519. void RigidBody2D::set_gravity_scale(real_t p_gravity_scale) {
  520. gravity_scale = p_gravity_scale;
  521. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
  522. }
  523. real_t RigidBody2D::get_gravity_scale() const {
  524. return gravity_scale;
  525. }
  526. void RigidBody2D::set_linear_damp(real_t p_linear_damp) {
  527. ERR_FAIL_COND(p_linear_damp < -1);
  528. linear_damp = p_linear_damp;
  529. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_LINEAR_DAMP, linear_damp);
  530. }
  531. real_t RigidBody2D::get_linear_damp() const {
  532. return linear_damp;
  533. }
  534. void RigidBody2D::set_angular_damp(real_t p_angular_damp) {
  535. ERR_FAIL_COND(p_angular_damp < -1);
  536. angular_damp = p_angular_damp;
  537. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_ANGULAR_DAMP, angular_damp);
  538. }
  539. real_t RigidBody2D::get_angular_damp() const {
  540. return angular_damp;
  541. }
  542. void RigidBody2D::set_axis_velocity(const Vector2 &p_axis) {
  543. Vector2 v = state ? state->get_linear_velocity() : linear_velocity;
  544. Vector2 axis = p_axis.normalized();
  545. v -= axis * axis.dot(v);
  546. v += p_axis;
  547. if (state) {
  548. set_linear_velocity(v);
  549. } else {
  550. Physics2DServer::get_singleton()->body_set_axis_velocity(get_rid(), p_axis);
  551. linear_velocity = v;
  552. }
  553. }
  554. void RigidBody2D::set_linear_velocity(const Vector2 &p_velocity) {
  555. linear_velocity = p_velocity;
  556. if (state)
  557. state->set_linear_velocity(linear_velocity);
  558. else {
  559. Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  560. }
  561. }
  562. Vector2 RigidBody2D::get_linear_velocity() const {
  563. return linear_velocity;
  564. }
  565. void RigidBody2D::set_angular_velocity(real_t p_velocity) {
  566. angular_velocity = p_velocity;
  567. if (state)
  568. state->set_angular_velocity(angular_velocity);
  569. else
  570. Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
  571. }
  572. real_t RigidBody2D::get_angular_velocity() const {
  573. return angular_velocity;
  574. }
  575. void RigidBody2D::set_use_custom_integrator(bool p_enable) {
  576. if (custom_integrator == p_enable)
  577. return;
  578. custom_integrator = p_enable;
  579. Physics2DServer::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
  580. }
  581. bool RigidBody2D::is_using_custom_integrator() {
  582. return custom_integrator;
  583. }
  584. void RigidBody2D::set_sleeping(bool p_sleeping) {
  585. sleeping = p_sleeping;
  586. Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_SLEEPING, sleeping);
  587. }
  588. void RigidBody2D::set_can_sleep(bool p_active) {
  589. can_sleep = p_active;
  590. Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_CAN_SLEEP, p_active);
  591. }
  592. bool RigidBody2D::is_able_to_sleep() const {
  593. return can_sleep;
  594. }
  595. bool RigidBody2D::is_sleeping() const {
  596. return sleeping;
  597. }
  598. void RigidBody2D::set_max_contacts_reported(int p_amount) {
  599. max_contacts_reported = p_amount;
  600. Physics2DServer::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount);
  601. }
  602. int RigidBody2D::get_max_contacts_reported() const {
  603. return max_contacts_reported;
  604. }
  605. void RigidBody2D::apply_central_impulse(const Vector2 &p_impulse) {
  606. Physics2DServer::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
  607. }
  608. void RigidBody2D::apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse) {
  609. Physics2DServer::get_singleton()->body_apply_impulse(get_rid(), p_offset, p_impulse);
  610. }
  611. void RigidBody2D::apply_torque_impulse(float p_torque) {
  612. Physics2DServer::get_singleton()->body_apply_torque_impulse(get_rid(), p_torque);
  613. }
  614. void RigidBody2D::set_applied_force(const Vector2 &p_force) {
  615. Physics2DServer::get_singleton()->body_set_applied_force(get_rid(), p_force);
  616. };
  617. Vector2 RigidBody2D::get_applied_force() const {
  618. return Physics2DServer::get_singleton()->body_get_applied_force(get_rid());
  619. };
  620. void RigidBody2D::set_applied_torque(const float p_torque) {
  621. Physics2DServer::get_singleton()->body_set_applied_torque(get_rid(), p_torque);
  622. };
  623. float RigidBody2D::get_applied_torque() const {
  624. return Physics2DServer::get_singleton()->body_get_applied_torque(get_rid());
  625. };
  626. void RigidBody2D::add_central_force(const Vector2 &p_force) {
  627. Physics2DServer::get_singleton()->body_add_central_force(get_rid(), p_force);
  628. }
  629. void RigidBody2D::add_force(const Vector2 &p_offset, const Vector2 &p_force) {
  630. Physics2DServer::get_singleton()->body_add_force(get_rid(), p_offset, p_force);
  631. }
  632. void RigidBody2D::add_torque(const float p_torque) {
  633. Physics2DServer::get_singleton()->body_add_torque(get_rid(), p_torque);
  634. }
  635. void RigidBody2D::set_continuous_collision_detection_mode(CCDMode p_mode) {
  636. ccd_mode = p_mode;
  637. Physics2DServer::get_singleton()->body_set_continuous_collision_detection_mode(get_rid(), Physics2DServer::CCDMode(p_mode));
  638. }
  639. RigidBody2D::CCDMode RigidBody2D::get_continuous_collision_detection_mode() const {
  640. return ccd_mode;
  641. }
  642. Array RigidBody2D::get_colliding_bodies() const {
  643. ERR_FAIL_COND_V(!contact_monitor, Array());
  644. Array ret;
  645. ret.resize(contact_monitor->body_map.size());
  646. int idx = 0;
  647. for (const Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  648. Object *obj = ObjectDB::get_instance(E->key());
  649. if (!obj) {
  650. ret.resize(ret.size() - 1); //ops
  651. } else {
  652. ret[idx++] = obj;
  653. }
  654. }
  655. return ret;
  656. }
  657. void RigidBody2D::set_contact_monitor(bool p_enabled) {
  658. if (p_enabled == is_contact_monitor_enabled())
  659. return;
  660. if (!p_enabled) {
  661. if (contact_monitor->locked) {
  662. ERR_EXPLAIN("Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\",false) instead");
  663. }
  664. ERR_FAIL_COND(contact_monitor->locked);
  665. for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  666. //clean up mess
  667. Object *obj = ObjectDB::get_instance(E->key());
  668. Node *node = Object::cast_to<Node>(obj);
  669. if (node) {
  670. node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree);
  671. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
  672. }
  673. }
  674. memdelete(contact_monitor);
  675. contact_monitor = NULL;
  676. } else {
  677. contact_monitor = memnew(ContactMonitor);
  678. contact_monitor->locked = false;
  679. }
  680. }
  681. bool RigidBody2D::is_contact_monitor_enabled() const {
  682. return contact_monitor != NULL;
  683. }
  684. void RigidBody2D::_notification(int p_what) {
  685. #ifdef TOOLS_ENABLED
  686. if (p_what == NOTIFICATION_ENTER_TREE) {
  687. if (Engine::get_singleton()->is_editor_hint()) {
  688. set_notify_local_transform(true); //used for warnings and only in editor
  689. }
  690. }
  691. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  692. if (Engine::get_singleton()->is_editor_hint()) {
  693. update_configuration_warning();
  694. }
  695. }
  696. #endif
  697. }
  698. String RigidBody2D::get_configuration_warning() const {
  699. Transform2D t = get_transform();
  700. String warning = CollisionObject2D::get_configuration_warning();
  701. if ((get_mode() == MODE_RIGID || get_mode() == MODE_CHARACTER) && (ABS(t.elements[0].length() - 1.0) > 0.05 || ABS(t.elements[1].length() - 1.0) > 0.05)) {
  702. if (warning != String()) {
  703. warning += "\n";
  704. }
  705. warning += TTR("Size changes to RigidBody2D (in character or rigid modes) will be overridden by the physics engine when running.\nChange the size in children collision shapes instead.");
  706. }
  707. return warning;
  708. }
  709. void RigidBody2D::_bind_methods() {
  710. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &RigidBody2D::set_mode);
  711. ClassDB::bind_method(D_METHOD("get_mode"), &RigidBody2D::get_mode);
  712. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &RigidBody2D::set_mass);
  713. ClassDB::bind_method(D_METHOD("get_mass"), &RigidBody2D::get_mass);
  714. ClassDB::bind_method(D_METHOD("get_inertia"), &RigidBody2D::get_inertia);
  715. ClassDB::bind_method(D_METHOD("set_inertia", "inertia"), &RigidBody2D::set_inertia);
  716. ClassDB::bind_method(D_METHOD("set_weight", "weight"), &RigidBody2D::set_weight);
  717. ClassDB::bind_method(D_METHOD("get_weight"), &RigidBody2D::get_weight);
  718. #ifndef DISABLE_DEPRECATED
  719. ClassDB::bind_method(D_METHOD("set_friction", "friction"), &RigidBody2D::set_friction);
  720. ClassDB::bind_method(D_METHOD("get_friction"), &RigidBody2D::get_friction);
  721. ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &RigidBody2D::set_bounce);
  722. ClassDB::bind_method(D_METHOD("get_bounce"), &RigidBody2D::get_bounce);
  723. #endif // DISABLE_DEPRECATED
  724. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &RigidBody2D::set_physics_material_override);
  725. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &RigidBody2D::get_physics_material_override);
  726. ClassDB::bind_method(D_METHOD("_reload_physics_characteristics"), &RigidBody2D::_reload_physics_characteristics);
  727. ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &RigidBody2D::set_gravity_scale);
  728. ClassDB::bind_method(D_METHOD("get_gravity_scale"), &RigidBody2D::get_gravity_scale);
  729. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &RigidBody2D::set_linear_damp);
  730. ClassDB::bind_method(D_METHOD("get_linear_damp"), &RigidBody2D::get_linear_damp);
  731. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &RigidBody2D::set_angular_damp);
  732. ClassDB::bind_method(D_METHOD("get_angular_damp"), &RigidBody2D::get_angular_damp);
  733. ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &RigidBody2D::set_linear_velocity);
  734. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &RigidBody2D::get_linear_velocity);
  735. ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &RigidBody2D::set_angular_velocity);
  736. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &RigidBody2D::get_angular_velocity);
  737. ClassDB::bind_method(D_METHOD("set_max_contacts_reported", "amount"), &RigidBody2D::set_max_contacts_reported);
  738. ClassDB::bind_method(D_METHOD("get_max_contacts_reported"), &RigidBody2D::get_max_contacts_reported);
  739. ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &RigidBody2D::set_use_custom_integrator);
  740. ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &RigidBody2D::is_using_custom_integrator);
  741. ClassDB::bind_method(D_METHOD("set_contact_monitor", "enabled"), &RigidBody2D::set_contact_monitor);
  742. ClassDB::bind_method(D_METHOD("is_contact_monitor_enabled"), &RigidBody2D::is_contact_monitor_enabled);
  743. ClassDB::bind_method(D_METHOD("set_continuous_collision_detection_mode", "mode"), &RigidBody2D::set_continuous_collision_detection_mode);
  744. ClassDB::bind_method(D_METHOD("get_continuous_collision_detection_mode"), &RigidBody2D::get_continuous_collision_detection_mode);
  745. ClassDB::bind_method(D_METHOD("set_axis_velocity", "axis_velocity"), &RigidBody2D::set_axis_velocity);
  746. ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &RigidBody2D::apply_central_impulse);
  747. ClassDB::bind_method(D_METHOD("apply_impulse", "offset", "impulse"), &RigidBody2D::apply_impulse);
  748. ClassDB::bind_method(D_METHOD("apply_torque_impulse", "torque"), &RigidBody2D::apply_torque_impulse);
  749. ClassDB::bind_method(D_METHOD("set_applied_force", "force"), &RigidBody2D::set_applied_force);
  750. ClassDB::bind_method(D_METHOD("get_applied_force"), &RigidBody2D::get_applied_force);
  751. ClassDB::bind_method(D_METHOD("set_applied_torque", "torque"), &RigidBody2D::set_applied_torque);
  752. ClassDB::bind_method(D_METHOD("get_applied_torque"), &RigidBody2D::get_applied_torque);
  753. ClassDB::bind_method(D_METHOD("add_central_force", "force"), &RigidBody2D::add_central_force);
  754. ClassDB::bind_method(D_METHOD("add_force", "offset", "force"), &RigidBody2D::add_force);
  755. ClassDB::bind_method(D_METHOD("add_torque", "torque"), &RigidBody2D::add_torque);
  756. ClassDB::bind_method(D_METHOD("set_sleeping", "sleeping"), &RigidBody2D::set_sleeping);
  757. ClassDB::bind_method(D_METHOD("is_sleeping"), &RigidBody2D::is_sleeping);
  758. ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &RigidBody2D::set_can_sleep);
  759. ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &RigidBody2D::is_able_to_sleep);
  760. ClassDB::bind_method(D_METHOD("test_motion", "motion", "infinite_inertia", "margin", "result"), &RigidBody2D::_test_motion, DEFVAL(true), DEFVAL(0.08), DEFVAL(Variant()));
  761. ClassDB::bind_method(D_METHOD("_direct_state_changed"), &RigidBody2D::_direct_state_changed);
  762. ClassDB::bind_method(D_METHOD("_body_enter_tree"), &RigidBody2D::_body_enter_tree);
  763. ClassDB::bind_method(D_METHOD("_body_exit_tree"), &RigidBody2D::_body_exit_tree);
  764. ClassDB::bind_method(D_METHOD("get_colliding_bodies"), &RigidBody2D::get_colliding_bodies);
  765. BIND_VMETHOD(MethodInfo("_integrate_forces", PropertyInfo(Variant::OBJECT, "state", PROPERTY_HINT_RESOURCE_TYPE, "Physics2DDirectBodyState")));
  766. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Rigid,Static,Character,Kinematic"), "set_mode", "get_mode");
  767. ADD_PROPERTY(PropertyInfo(Variant::REAL, "mass", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01"), "set_mass", "get_mass");
  768. ADD_PROPERTY(PropertyInfo(Variant::REAL, "inertia", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01", 0), "set_inertia", "get_inertia");
  769. ADD_PROPERTY(PropertyInfo(Variant::REAL, "weight", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01", PROPERTY_USAGE_EDITOR), "set_weight", "get_weight");
  770. #ifndef DISABLE_DEPRECATED
  771. ADD_PROPERTY(PropertyInfo(Variant::REAL, "friction", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_friction", "get_friction");
  772. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_bounce", "get_bounce");
  773. #endif // DISABLE_DEPRECATED
  774. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  775. ADD_PROPERTY(PropertyInfo(Variant::REAL, "gravity_scale", PROPERTY_HINT_RANGE, "-128,128,0.01"), "set_gravity_scale", "get_gravity_scale");
  776. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
  777. ADD_PROPERTY(PropertyInfo(Variant::INT, "continuous_cd", PROPERTY_HINT_ENUM, "Disabled,Cast Ray,Cast Shape"), "set_continuous_collision_detection_mode", "get_continuous_collision_detection_mode");
  778. ADD_PROPERTY(PropertyInfo(Variant::INT, "contacts_reported"), "set_max_contacts_reported", "get_max_contacts_reported");
  779. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor"), "set_contact_monitor", "is_contact_monitor_enabled");
  780. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleeping", "is_sleeping");
  781. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
  782. ADD_GROUP("Linear", "linear_");
  783. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
  784. ADD_PROPERTY(PropertyInfo(Variant::REAL, "linear_damp", PROPERTY_HINT_RANGE, "-1,128,0.01"), "set_linear_damp", "get_linear_damp");
  785. ADD_GROUP("Angular", "angular_");
  786. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
  787. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_damp", PROPERTY_HINT_RANGE, "-1,128,0.01"), "set_angular_damp", "get_angular_damp");
  788. ADD_GROUP("Applied Forces", "applied_");
  789. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "applied_force"), "set_applied_force", "get_applied_force");
  790. ADD_PROPERTY(PropertyInfo(Variant::REAL, "applied_torque"), "set_applied_torque", "get_applied_torque");
  791. 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")));
  792. 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")));
  793. ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  794. ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  795. ADD_SIGNAL(MethodInfo("sleeping_state_changed"));
  796. BIND_ENUM_CONSTANT(MODE_RIGID);
  797. BIND_ENUM_CONSTANT(MODE_STATIC);
  798. BIND_ENUM_CONSTANT(MODE_CHARACTER);
  799. BIND_ENUM_CONSTANT(MODE_KINEMATIC);
  800. BIND_ENUM_CONSTANT(CCD_MODE_DISABLED);
  801. BIND_ENUM_CONSTANT(CCD_MODE_CAST_RAY);
  802. BIND_ENUM_CONSTANT(CCD_MODE_CAST_SHAPE);
  803. }
  804. RigidBody2D::RigidBody2D() :
  805. PhysicsBody2D(Physics2DServer::BODY_MODE_RIGID) {
  806. mode = MODE_RIGID;
  807. mass = 1;
  808. gravity_scale = 1;
  809. linear_damp = -1;
  810. angular_damp = -1;
  811. max_contacts_reported = 0;
  812. state = NULL;
  813. angular_velocity = 0;
  814. sleeping = false;
  815. ccd_mode = CCD_MODE_DISABLED;
  816. custom_integrator = false;
  817. contact_monitor = NULL;
  818. can_sleep = true;
  819. Physics2DServer::get_singleton()->body_set_force_integration_callback(get_rid(), this, "_direct_state_changed");
  820. }
  821. RigidBody2D::~RigidBody2D() {
  822. if (contact_monitor)
  823. memdelete(contact_monitor);
  824. }
  825. void RigidBody2D::_reload_physics_characteristics() {
  826. if (physics_material_override.is_null()) {
  827. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_BOUNCE, 0);
  828. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_FRICTION, 1);
  829. } else {
  830. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  831. Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  832. }
  833. }
  834. //////////////////////////
  835. Ref<KinematicCollision2D> KinematicBody2D::_move(const Vector2 &p_motion, bool p_infinite_inertia, bool p_exclude_raycast_shapes, bool p_test_only) {
  836. Collision col;
  837. if (move_and_collide(p_motion, p_infinite_inertia, col, p_exclude_raycast_shapes, p_test_only)) {
  838. if (motion_cache.is_null()) {
  839. motion_cache.instance();
  840. motion_cache->owner = this;
  841. }
  842. motion_cache->collision = col;
  843. return motion_cache;
  844. }
  845. return Ref<KinematicCollision2D>();
  846. }
  847. bool KinematicBody2D::separate_raycast_shapes(bool p_infinite_inertia, Collision &r_collision) {
  848. Physics2DServer::SeparationResult sep_res[8]; //max 8 rays
  849. Transform2D gt = get_global_transform();
  850. Vector2 recover;
  851. int hits = Physics2DServer::get_singleton()->body_test_ray_separation(get_rid(), gt, p_infinite_inertia, recover, sep_res, 8, margin);
  852. int deepest = -1;
  853. float deepest_depth;
  854. for (int i = 0; i < hits; i++) {
  855. if (deepest == -1 || sep_res[i].collision_depth > deepest_depth) {
  856. deepest = i;
  857. deepest_depth = sep_res[i].collision_depth;
  858. }
  859. }
  860. gt.elements[2] += recover;
  861. set_global_transform(gt);
  862. if (deepest != -1) {
  863. r_collision.collider = sep_res[deepest].collider_id;
  864. r_collision.collider_metadata = sep_res[deepest].collider_metadata;
  865. r_collision.collider_shape = sep_res[deepest].collider_shape;
  866. r_collision.collider_vel = sep_res[deepest].collider_velocity;
  867. r_collision.collision = sep_res[deepest].collision_point;
  868. r_collision.normal = sep_res[deepest].collision_normal;
  869. r_collision.local_shape = sep_res[deepest].collision_local_shape;
  870. r_collision.travel = recover;
  871. r_collision.remainder = Vector2();
  872. return true;
  873. } else {
  874. return false;
  875. }
  876. }
  877. bool KinematicBody2D::move_and_collide(const Vector2 &p_motion, bool p_infinite_inertia, Collision &r_collision, bool p_exclude_raycast_shapes, bool p_test_only) {
  878. if (sync_to_physics) {
  879. ERR_PRINT("Functions move_and_slide and move_and_collide do not work together with 'sync to physics' option. Please read the documentation.");
  880. }
  881. Transform2D gt = get_global_transform();
  882. Physics2DServer::MotionResult result;
  883. bool colliding = Physics2DServer::get_singleton()->body_test_motion(get_rid(), gt, p_motion, p_infinite_inertia, margin, &result, p_exclude_raycast_shapes);
  884. if (colliding) {
  885. r_collision.collider_metadata = result.collider_metadata;
  886. r_collision.collider_shape = result.collider_shape;
  887. r_collision.collider_vel = result.collider_velocity;
  888. r_collision.collision = result.collision_point;
  889. r_collision.normal = result.collision_normal;
  890. r_collision.collider = result.collider_id;
  891. r_collision.collider_rid = result.collider;
  892. r_collision.travel = result.motion;
  893. r_collision.remainder = result.remainder;
  894. r_collision.local_shape = result.collision_local_shape;
  895. }
  896. if (!p_test_only) {
  897. gt.elements[2] += result.motion;
  898. set_global_transform(gt);
  899. }
  900. return colliding;
  901. }
  902. //so, if you pass 45 as limit, avoid numerical precision erros when angle is 45.
  903. #define FLOOR_ANGLE_THRESHOLD 0.01
  904. Vector2 KinematicBody2D::move_and_slide(const Vector2 &p_linear_velocity, const Vector2 &p_floor_direction, bool p_stop_on_slope, int p_max_slides, float p_floor_max_angle, bool p_infinite_inertia) {
  905. Vector2 floor_motion = floor_velocity;
  906. if (on_floor && on_floor_body.is_valid()) {
  907. //this approach makes sure there is less delay between the actual body velocity and the one we saved
  908. Physics2DDirectBodyState *bs = Physics2DServer::get_singleton()->body_get_direct_state(on_floor_body);
  909. if (bs) {
  910. floor_motion = bs->get_linear_velocity();
  911. }
  912. }
  913. // Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky
  914. Vector2 motion = (floor_motion + p_linear_velocity) * (Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time());
  915. Vector2 lv = p_linear_velocity;
  916. on_floor = false;
  917. on_floor_body = RID();
  918. on_ceiling = false;
  919. on_wall = false;
  920. colliders.clear();
  921. floor_velocity = Vector2();
  922. Vector2 lv_n = p_linear_velocity.normalized();
  923. while (p_max_slides) {
  924. Collision collision;
  925. bool found_collision = false;
  926. for (int i = 0; i < 2; i++) {
  927. bool collided;
  928. if (i == 0) { //collide
  929. collided = move_and_collide(motion, p_infinite_inertia, collision);
  930. if (!collided) {
  931. motion = Vector2(); //clear because no collision happened and motion completed
  932. }
  933. } else { //separate raycasts (if any)
  934. collided = separate_raycast_shapes(p_infinite_inertia, collision);
  935. if (collided) {
  936. collision.remainder = motion; //keep
  937. collision.travel = Vector2();
  938. }
  939. }
  940. if (collided) {
  941. found_collision = true;
  942. }
  943. if (collided) {
  944. colliders.push_back(collision);
  945. motion = collision.remainder;
  946. if (p_floor_direction == Vector2()) {
  947. //all is a wall
  948. on_wall = true;
  949. } else {
  950. if (collision.normal.dot(p_floor_direction) >= Math::cos(p_floor_max_angle + FLOOR_ANGLE_THRESHOLD)) { //floor
  951. on_floor = true;
  952. on_floor_body = collision.collider_rid;
  953. floor_velocity = collision.collider_vel;
  954. if (p_stop_on_slope) {
  955. if ((lv_n + p_floor_direction).length() < 0.01 && collision.travel.length() < 1) {
  956. Transform2D gt = get_global_transform();
  957. gt.elements[2] -= collision.travel.project(p_floor_direction.tangent());
  958. set_global_transform(gt);
  959. return Vector2();
  960. }
  961. }
  962. } else if (collision.normal.dot(-p_floor_direction) >= Math::cos(p_floor_max_angle + FLOOR_ANGLE_THRESHOLD)) { //ceiling
  963. on_ceiling = true;
  964. } else {
  965. on_wall = true;
  966. }
  967. }
  968. Vector2 n = collision.normal;
  969. motion = motion.slide(n);
  970. lv = lv.slide(n);
  971. }
  972. }
  973. if (!found_collision) {
  974. break;
  975. }
  976. p_max_slides--;
  977. if (motion == Vector2())
  978. break;
  979. }
  980. return lv;
  981. }
  982. Vector2 KinematicBody2D::move_and_slide_with_snap(const Vector2 &p_linear_velocity, const Vector2 &p_snap, const Vector2 &p_floor_direction, bool p_stop_on_slope, int p_max_slides, float p_floor_max_angle, bool p_infinite_inertia) {
  983. bool was_on_floor = on_floor;
  984. Vector2 ret = move_and_slide(p_linear_velocity, p_floor_direction, p_stop_on_slope, p_max_slides, p_floor_max_angle, p_infinite_inertia);
  985. if (!was_on_floor || p_snap == Vector2()) {
  986. return ret;
  987. }
  988. Collision col;
  989. Transform2D gt = get_global_transform();
  990. if (move_and_collide(p_snap, p_infinite_inertia, col, false, true)) {
  991. bool apply = true;
  992. if (p_floor_direction != Vector2()) {
  993. if (Math::acos(p_floor_direction.normalized().dot(col.normal)) < p_floor_max_angle) {
  994. on_floor = true;
  995. on_floor_body = col.collider_rid;
  996. floor_velocity = col.collider_vel;
  997. if (p_stop_on_slope) {
  998. // move and collide may stray the object a bit because of pre un-stucking,
  999. // so only ensure that motion happens on floor direction in this case.
  1000. col.travel = p_floor_direction * p_floor_direction.dot(col.travel);
  1001. }
  1002. } else {
  1003. apply = false;
  1004. }
  1005. }
  1006. if (apply) {
  1007. gt.elements[2] += col.travel;
  1008. set_global_transform(gt);
  1009. }
  1010. }
  1011. return ret;
  1012. }
  1013. bool KinematicBody2D::is_on_floor() const {
  1014. return on_floor;
  1015. }
  1016. bool KinematicBody2D::is_on_wall() const {
  1017. return on_wall;
  1018. }
  1019. bool KinematicBody2D::is_on_ceiling() const {
  1020. return on_ceiling;
  1021. }
  1022. Vector2 KinematicBody2D::get_floor_velocity() const {
  1023. return floor_velocity;
  1024. }
  1025. bool KinematicBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia) {
  1026. ERR_FAIL_COND_V(!is_inside_tree(), false);
  1027. return Physics2DServer::get_singleton()->body_test_motion(get_rid(), p_from, p_motion, p_infinite_inertia, margin);
  1028. }
  1029. void KinematicBody2D::set_safe_margin(float p_margin) {
  1030. margin = p_margin;
  1031. }
  1032. float KinematicBody2D::get_safe_margin() const {
  1033. return margin;
  1034. }
  1035. int KinematicBody2D::get_slide_count() const {
  1036. return colliders.size();
  1037. }
  1038. KinematicBody2D::Collision KinematicBody2D::get_slide_collision(int p_bounce) const {
  1039. ERR_FAIL_INDEX_V(p_bounce, colliders.size(), Collision());
  1040. return colliders[p_bounce];
  1041. }
  1042. Ref<KinematicCollision2D> KinematicBody2D::_get_slide_collision(int p_bounce) {
  1043. ERR_FAIL_INDEX_V(p_bounce, colliders.size(), Ref<KinematicCollision2D>());
  1044. if (p_bounce >= slide_colliders.size()) {
  1045. slide_colliders.resize(p_bounce + 1);
  1046. }
  1047. if (slide_colliders[p_bounce].is_null()) {
  1048. slide_colliders.write[p_bounce].instance();
  1049. slide_colliders.write[p_bounce]->owner = this;
  1050. }
  1051. slide_colliders.write[p_bounce]->collision = colliders[p_bounce];
  1052. return slide_colliders[p_bounce];
  1053. }
  1054. void KinematicBody2D::set_sync_to_physics(bool p_enable) {
  1055. if (sync_to_physics == p_enable) {
  1056. return;
  1057. }
  1058. sync_to_physics = p_enable;
  1059. if (Engine::get_singleton()->is_editor_hint())
  1060. return;
  1061. if (p_enable) {
  1062. Physics2DServer::get_singleton()->body_set_force_integration_callback(get_rid(), this, "_direct_state_changed");
  1063. set_only_update_transform_changes(true);
  1064. set_notify_local_transform(true);
  1065. } else {
  1066. Physics2DServer::get_singleton()->body_set_force_integration_callback(get_rid(), NULL, "");
  1067. set_only_update_transform_changes(false);
  1068. set_notify_local_transform(false);
  1069. }
  1070. }
  1071. bool KinematicBody2D::is_sync_to_physics_enabled() const {
  1072. return sync_to_physics;
  1073. }
  1074. void KinematicBody2D::_direct_state_changed(Object *p_state) {
  1075. if (!sync_to_physics)
  1076. return;
  1077. Physics2DDirectBodyState *state = Object::cast_to<Physics2DDirectBodyState>(p_state);
  1078. last_valid_transform = state->get_transform();
  1079. set_notify_local_transform(false);
  1080. set_global_transform(last_valid_transform);
  1081. set_notify_local_transform(true);
  1082. }
  1083. void KinematicBody2D::_notification(int p_what) {
  1084. if (p_what == NOTIFICATION_ENTER_TREE) {
  1085. last_valid_transform = get_global_transform();
  1086. }
  1087. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  1088. //used by sync to physics, send the new transform to the physics
  1089. Transform2D new_transform = get_global_transform();
  1090. Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_TRANSFORM, new_transform);
  1091. //but then revert changes
  1092. set_notify_local_transform(false);
  1093. set_global_transform(last_valid_transform);
  1094. set_notify_local_transform(true);
  1095. }
  1096. }
  1097. void KinematicBody2D::_bind_methods() {
  1098. ClassDB::bind_method(D_METHOD("move_and_collide", "rel_vec", "infinite_inertia", "exclude_raycast_shapes", "test_only"), &KinematicBody2D::_move, DEFVAL(true), DEFVAL(true), DEFVAL(false));
  1099. ClassDB::bind_method(D_METHOD("move_and_slide", "linear_velocity", "floor_normal", "stop_on_slope", "max_slides", "floor_max_angle", "infinite_inertia"), &KinematicBody2D::move_and_slide, DEFVAL(Vector2(0, 0)), DEFVAL(false), DEFVAL(4), DEFVAL(Math::deg2rad((float)45)), DEFVAL(true));
  1100. 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"), &KinematicBody2D::move_and_slide_with_snap, DEFVAL(Vector2(0, 0)), DEFVAL(false), DEFVAL(4), DEFVAL(Math::deg2rad((float)45)), DEFVAL(true));
  1101. ClassDB::bind_method(D_METHOD("test_move", "from", "rel_vec", "infinite_inertia"), &KinematicBody2D::test_move, DEFVAL(true));
  1102. ClassDB::bind_method(D_METHOD("is_on_floor"), &KinematicBody2D::is_on_floor);
  1103. ClassDB::bind_method(D_METHOD("is_on_ceiling"), &KinematicBody2D::is_on_ceiling);
  1104. ClassDB::bind_method(D_METHOD("is_on_wall"), &KinematicBody2D::is_on_wall);
  1105. ClassDB::bind_method(D_METHOD("get_floor_velocity"), &KinematicBody2D::get_floor_velocity);
  1106. ClassDB::bind_method(D_METHOD("set_safe_margin", "pixels"), &KinematicBody2D::set_safe_margin);
  1107. ClassDB::bind_method(D_METHOD("get_safe_margin"), &KinematicBody2D::get_safe_margin);
  1108. ClassDB::bind_method(D_METHOD("get_slide_count"), &KinematicBody2D::get_slide_count);
  1109. ClassDB::bind_method(D_METHOD("get_slide_collision", "slide_idx"), &KinematicBody2D::_get_slide_collision);
  1110. ClassDB::bind_method(D_METHOD("set_sync_to_physics", "enable"), &KinematicBody2D::set_sync_to_physics);
  1111. ClassDB::bind_method(D_METHOD("is_sync_to_physics_enabled"), &KinematicBody2D::is_sync_to_physics_enabled);
  1112. ClassDB::bind_method(D_METHOD("_direct_state_changed"), &KinematicBody2D::_direct_state_changed);
  1113. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision/safe_margin", PROPERTY_HINT_RANGE, "0.001,256,0.001"), "set_safe_margin", "get_safe_margin");
  1114. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "motion/sync_to_physics"), "set_sync_to_physics", "is_sync_to_physics_enabled");
  1115. }
  1116. KinematicBody2D::KinematicBody2D() :
  1117. PhysicsBody2D(Physics2DServer::BODY_MODE_KINEMATIC) {
  1118. margin = 0.08;
  1119. on_floor = false;
  1120. on_ceiling = false;
  1121. on_wall = false;
  1122. sync_to_physics = false;
  1123. }
  1124. KinematicBody2D::~KinematicBody2D() {
  1125. if (motion_cache.is_valid()) {
  1126. motion_cache->owner = NULL;
  1127. }
  1128. for (int i = 0; i < slide_colliders.size(); i++) {
  1129. if (slide_colliders[i].is_valid()) {
  1130. slide_colliders.write[i]->owner = NULL;
  1131. }
  1132. }
  1133. }
  1134. ////////////////////////
  1135. Vector2 KinematicCollision2D::get_position() const {
  1136. return collision.collision;
  1137. }
  1138. Vector2 KinematicCollision2D::get_normal() const {
  1139. return collision.normal;
  1140. }
  1141. Vector2 KinematicCollision2D::get_travel() const {
  1142. return collision.travel;
  1143. }
  1144. Vector2 KinematicCollision2D::get_remainder() const {
  1145. return collision.remainder;
  1146. }
  1147. Object *KinematicCollision2D::get_local_shape() const {
  1148. ERR_FAIL_COND_V(!owner, NULL);
  1149. uint32_t ownerid = owner->shape_find_owner(collision.local_shape);
  1150. return owner->shape_owner_get_owner(ownerid);
  1151. }
  1152. Object *KinematicCollision2D::get_collider() const {
  1153. if (collision.collider) {
  1154. return ObjectDB::get_instance(collision.collider);
  1155. }
  1156. return NULL;
  1157. }
  1158. ObjectID KinematicCollision2D::get_collider_id() const {
  1159. return collision.collider;
  1160. }
  1161. Object *KinematicCollision2D::get_collider_shape() const {
  1162. Object *collider = get_collider();
  1163. if (collider) {
  1164. CollisionObject2D *obj2d = Object::cast_to<CollisionObject2D>(collider);
  1165. if (obj2d) {
  1166. uint32_t ownerid = obj2d->shape_find_owner(collision.collider_shape);
  1167. return obj2d->shape_owner_get_owner(ownerid);
  1168. }
  1169. }
  1170. return NULL;
  1171. }
  1172. int KinematicCollision2D::get_collider_shape_index() const {
  1173. return collision.collider_shape;
  1174. }
  1175. Vector2 KinematicCollision2D::get_collider_velocity() const {
  1176. return collision.collider_vel;
  1177. }
  1178. Variant KinematicCollision2D::get_collider_metadata() const {
  1179. return Variant();
  1180. }
  1181. void KinematicCollision2D::_bind_methods() {
  1182. ClassDB::bind_method(D_METHOD("get_position"), &KinematicCollision2D::get_position);
  1183. ClassDB::bind_method(D_METHOD("get_normal"), &KinematicCollision2D::get_normal);
  1184. ClassDB::bind_method(D_METHOD("get_travel"), &KinematicCollision2D::get_travel);
  1185. ClassDB::bind_method(D_METHOD("get_remainder"), &KinematicCollision2D::get_remainder);
  1186. ClassDB::bind_method(D_METHOD("get_local_shape"), &KinematicCollision2D::get_local_shape);
  1187. ClassDB::bind_method(D_METHOD("get_collider"), &KinematicCollision2D::get_collider);
  1188. ClassDB::bind_method(D_METHOD("get_collider_id"), &KinematicCollision2D::get_collider_id);
  1189. ClassDB::bind_method(D_METHOD("get_collider_shape"), &KinematicCollision2D::get_collider_shape);
  1190. ClassDB::bind_method(D_METHOD("get_collider_shape_index"), &KinematicCollision2D::get_collider_shape_index);
  1191. ClassDB::bind_method(D_METHOD("get_collider_velocity"), &KinematicCollision2D::get_collider_velocity);
  1192. ClassDB::bind_method(D_METHOD("get_collider_metadata"), &KinematicCollision2D::get_collider_metadata);
  1193. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "", "get_position");
  1194. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "normal"), "", "get_normal");
  1195. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "travel"), "", "get_travel");
  1196. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "remainder"), "", "get_remainder");
  1197. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "local_shape"), "", "get_local_shape");
  1198. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider"), "", "get_collider");
  1199. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_id"), "", "get_collider_id");
  1200. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider_shape"), "", "get_collider_shape");
  1201. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_shape_index"), "", "get_collider_shape_index");
  1202. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "collider_velocity"), "", "get_collider_velocity");
  1203. ADD_PROPERTY(PropertyInfo(Variant::NIL, "collider_metadata", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "", "get_collider_metadata");
  1204. }
  1205. KinematicCollision2D::KinematicCollision2D() {
  1206. collision.collider = 0;
  1207. collision.collider_shape = 0;
  1208. collision.local_shape = 0;
  1209. owner = NULL;
  1210. }