physics_body_2d.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  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. Transform2D gt = get_global_transform();
  879. Physics2DServer::MotionResult result;
  880. bool colliding = Physics2DServer::get_singleton()->body_test_motion(get_rid(), gt, p_motion, p_infinite_inertia, margin, &result, p_exclude_raycast_shapes);
  881. if (colliding) {
  882. r_collision.collider_metadata = result.collider_metadata;
  883. r_collision.collider_shape = result.collider_shape;
  884. r_collision.collider_vel = result.collider_velocity;
  885. r_collision.collision = result.collision_point;
  886. r_collision.normal = result.collision_normal;
  887. r_collision.collider = result.collider_id;
  888. r_collision.collider_rid = result.collider;
  889. r_collision.travel = result.motion;
  890. r_collision.remainder = result.remainder;
  891. r_collision.local_shape = result.collision_local_shape;
  892. }
  893. if (!p_test_only) {
  894. gt.elements[2] += result.motion;
  895. set_global_transform(gt);
  896. }
  897. return colliding;
  898. }
  899. //so, if you pass 45 as limit, avoid numerical precision erros when angle is 45.
  900. #define FLOOR_ANGLE_THRESHOLD 0.01
  901. 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) {
  902. Vector2 floor_motion = floor_velocity;
  903. if (on_floor && on_floor_body.is_valid()) {
  904. //this approach makes sure there is less delay between the actual body velocity and the one we saved
  905. Physics2DDirectBodyState *bs = Physics2DServer::get_singleton()->body_get_direct_state(on_floor_body);
  906. if (bs) {
  907. floor_motion = bs->get_linear_velocity();
  908. }
  909. }
  910. // Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky
  911. Vector2 motion = (floor_motion + p_linear_velocity) * (Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time());
  912. Vector2 lv = p_linear_velocity;
  913. on_floor = false;
  914. on_floor_body = RID();
  915. on_ceiling = false;
  916. on_wall = false;
  917. colliders.clear();
  918. floor_velocity = Vector2();
  919. Vector2 lv_n = p_linear_velocity.normalized();
  920. while (p_max_slides) {
  921. Collision collision;
  922. bool found_collision = false;
  923. for (int i = 0; i < 2; i++) {
  924. bool collided;
  925. if (i == 0) { //collide
  926. collided = move_and_collide(motion, p_infinite_inertia, collision);
  927. if (!collided) {
  928. motion = Vector2(); //clear because no collision happened and motion completed
  929. }
  930. } else { //separate raycasts (if any)
  931. collided = separate_raycast_shapes(p_infinite_inertia, collision);
  932. if (collided) {
  933. collision.remainder = motion; //keep
  934. collision.travel = Vector2();
  935. }
  936. }
  937. if (collided) {
  938. found_collision = true;
  939. }
  940. if (collided) {
  941. colliders.push_back(collision);
  942. motion = collision.remainder;
  943. if (p_floor_direction == Vector2()) {
  944. //all is a wall
  945. on_wall = true;
  946. } else {
  947. if (collision.normal.dot(p_floor_direction) >= Math::cos(p_floor_max_angle + FLOOR_ANGLE_THRESHOLD)) { //floor
  948. on_floor = true;
  949. on_floor_body = collision.collider_rid;
  950. floor_velocity = collision.collider_vel;
  951. if (p_stop_on_slope) {
  952. if (Vector2() == lv_n + p_floor_direction && collision.travel.length() < 1) {
  953. Transform2D gt = get_global_transform();
  954. gt.elements[2] -= collision.travel.project(p_floor_direction.tangent());
  955. set_global_transform(gt);
  956. return Vector2();
  957. }
  958. }
  959. } else if (collision.normal.dot(-p_floor_direction) >= Math::cos(p_floor_max_angle + FLOOR_ANGLE_THRESHOLD)) { //ceiling
  960. on_ceiling = true;
  961. } else {
  962. on_wall = true;
  963. }
  964. }
  965. Vector2 n = collision.normal;
  966. motion = motion.slide(n);
  967. lv = lv.slide(n);
  968. }
  969. if (p_stop_on_slope)
  970. break;
  971. }
  972. if (!found_collision) {
  973. break;
  974. }
  975. p_max_slides--;
  976. if (motion == Vector2())
  977. break;
  978. }
  979. return lv;
  980. }
  981. 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) {
  982. bool was_on_floor = on_floor;
  983. 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);
  984. if (!was_on_floor || p_snap == Vector2()) {
  985. return ret;
  986. }
  987. Collision col;
  988. Transform2D gt = get_global_transform();
  989. if (move_and_collide(p_snap, p_infinite_inertia, col, false, true)) {
  990. gt.elements[2] += col.travel;
  991. if (p_floor_direction != Vector2() && Math::acos(p_floor_direction.normalized().dot(col.normal)) < p_floor_max_angle) {
  992. on_floor = true;
  993. on_floor_body = col.collider_rid;
  994. floor_velocity = col.collider_vel;
  995. }
  996. set_global_transform(gt);
  997. }
  998. return ret;
  999. }
  1000. bool KinematicBody2D::is_on_floor() const {
  1001. return on_floor;
  1002. }
  1003. bool KinematicBody2D::is_on_wall() const {
  1004. return on_wall;
  1005. }
  1006. bool KinematicBody2D::is_on_ceiling() const {
  1007. return on_ceiling;
  1008. }
  1009. Vector2 KinematicBody2D::get_floor_velocity() const {
  1010. return floor_velocity;
  1011. }
  1012. bool KinematicBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia) {
  1013. ERR_FAIL_COND_V(!is_inside_tree(), false);
  1014. return Physics2DServer::get_singleton()->body_test_motion(get_rid(), p_from, p_motion, p_infinite_inertia, margin);
  1015. }
  1016. void KinematicBody2D::set_safe_margin(float p_margin) {
  1017. margin = p_margin;
  1018. }
  1019. float KinematicBody2D::get_safe_margin() const {
  1020. return margin;
  1021. }
  1022. int KinematicBody2D::get_slide_count() const {
  1023. return colliders.size();
  1024. }
  1025. KinematicBody2D::Collision KinematicBody2D::get_slide_collision(int p_bounce) const {
  1026. ERR_FAIL_INDEX_V(p_bounce, colliders.size(), Collision());
  1027. return colliders[p_bounce];
  1028. }
  1029. Ref<KinematicCollision2D> KinematicBody2D::_get_slide_collision(int p_bounce) {
  1030. ERR_FAIL_INDEX_V(p_bounce, colliders.size(), Ref<KinematicCollision2D>());
  1031. if (p_bounce >= slide_colliders.size()) {
  1032. slide_colliders.resize(p_bounce + 1);
  1033. }
  1034. if (slide_colliders[p_bounce].is_null()) {
  1035. slide_colliders.write[p_bounce].instance();
  1036. slide_colliders.write[p_bounce]->owner = this;
  1037. }
  1038. slide_colliders.write[p_bounce]->collision = colliders[p_bounce];
  1039. return slide_colliders[p_bounce];
  1040. }
  1041. void KinematicBody2D::set_sync_to_physics(bool p_enable) {
  1042. if (sync_to_physics == p_enable) {
  1043. return;
  1044. }
  1045. sync_to_physics = p_enable;
  1046. if (Engine::get_singleton()->is_editor_hint())
  1047. return;
  1048. if (p_enable) {
  1049. Physics2DServer::get_singleton()->body_set_force_integration_callback(get_rid(), this, "_direct_state_changed");
  1050. set_only_update_transform_changes(true);
  1051. set_notify_local_transform(true);
  1052. } else {
  1053. Physics2DServer::get_singleton()->body_set_force_integration_callback(get_rid(), NULL, "");
  1054. set_only_update_transform_changes(false);
  1055. set_notify_local_transform(false);
  1056. }
  1057. }
  1058. bool KinematicBody2D::is_sync_to_physics_enabled() const {
  1059. return sync_to_physics;
  1060. }
  1061. void KinematicBody2D::_direct_state_changed(Object *p_state) {
  1062. if (!sync_to_physics)
  1063. return;
  1064. Physics2DDirectBodyState *state = Object::cast_to<Physics2DDirectBodyState>(p_state);
  1065. last_valid_transform = state->get_transform();
  1066. set_notify_local_transform(false);
  1067. set_global_transform(last_valid_transform);
  1068. set_notify_local_transform(true);
  1069. }
  1070. void KinematicBody2D::_notification(int p_what) {
  1071. if (p_what == NOTIFICATION_ENTER_TREE) {
  1072. last_valid_transform = get_global_transform();
  1073. }
  1074. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  1075. //used by sync to physics, send the new transform to the physics
  1076. Transform2D new_transform = get_global_transform();
  1077. Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_TRANSFORM, new_transform);
  1078. //but then revert changes
  1079. set_notify_local_transform(false);
  1080. set_global_transform(last_valid_transform);
  1081. set_notify_local_transform(true);
  1082. }
  1083. }
  1084. void KinematicBody2D::_bind_methods() {
  1085. 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));
  1086. 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));
  1087. 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));
  1088. ClassDB::bind_method(D_METHOD("test_move", "from", "rel_vec", "infinite_inertia"), &KinematicBody2D::test_move, DEFVAL(true));
  1089. ClassDB::bind_method(D_METHOD("is_on_floor"), &KinematicBody2D::is_on_floor);
  1090. ClassDB::bind_method(D_METHOD("is_on_ceiling"), &KinematicBody2D::is_on_ceiling);
  1091. ClassDB::bind_method(D_METHOD("is_on_wall"), &KinematicBody2D::is_on_wall);
  1092. ClassDB::bind_method(D_METHOD("get_floor_velocity"), &KinematicBody2D::get_floor_velocity);
  1093. ClassDB::bind_method(D_METHOD("set_safe_margin", "pixels"), &KinematicBody2D::set_safe_margin);
  1094. ClassDB::bind_method(D_METHOD("get_safe_margin"), &KinematicBody2D::get_safe_margin);
  1095. ClassDB::bind_method(D_METHOD("get_slide_count"), &KinematicBody2D::get_slide_count);
  1096. ClassDB::bind_method(D_METHOD("get_slide_collision", "slide_idx"), &KinematicBody2D::_get_slide_collision);
  1097. ClassDB::bind_method(D_METHOD("set_sync_to_physics", "enable"), &KinematicBody2D::set_sync_to_physics);
  1098. ClassDB::bind_method(D_METHOD("is_sync_to_physics_enabled"), &KinematicBody2D::is_sync_to_physics_enabled);
  1099. ClassDB::bind_method(D_METHOD("_direct_state_changed"), &KinematicBody2D::_direct_state_changed);
  1100. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision/safe_margin", PROPERTY_HINT_RANGE, "0.001,256,0.001"), "set_safe_margin", "get_safe_margin");
  1101. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "motion/sync_to_physics"), "set_sync_to_physics", "is_sync_to_physics_enabled");
  1102. }
  1103. KinematicBody2D::KinematicBody2D() :
  1104. PhysicsBody2D(Physics2DServer::BODY_MODE_KINEMATIC) {
  1105. margin = 0.08;
  1106. on_floor = false;
  1107. on_ceiling = false;
  1108. on_wall = false;
  1109. sync_to_physics = false;
  1110. }
  1111. KinematicBody2D::~KinematicBody2D() {
  1112. if (motion_cache.is_valid()) {
  1113. motion_cache->owner = NULL;
  1114. }
  1115. for (int i = 0; i < slide_colliders.size(); i++) {
  1116. if (slide_colliders[i].is_valid()) {
  1117. slide_colliders.write[i]->owner = NULL;
  1118. }
  1119. }
  1120. }
  1121. ////////////////////////
  1122. Vector2 KinematicCollision2D::get_position() const {
  1123. return collision.collision;
  1124. }
  1125. Vector2 KinematicCollision2D::get_normal() const {
  1126. return collision.normal;
  1127. }
  1128. Vector2 KinematicCollision2D::get_travel() const {
  1129. return collision.travel;
  1130. }
  1131. Vector2 KinematicCollision2D::get_remainder() const {
  1132. return collision.remainder;
  1133. }
  1134. Object *KinematicCollision2D::get_local_shape() const {
  1135. ERR_FAIL_COND_V(!owner, NULL);
  1136. uint32_t ownerid = owner->shape_find_owner(collision.local_shape);
  1137. return owner->shape_owner_get_owner(ownerid);
  1138. }
  1139. Object *KinematicCollision2D::get_collider() const {
  1140. if (collision.collider) {
  1141. return ObjectDB::get_instance(collision.collider);
  1142. }
  1143. return NULL;
  1144. }
  1145. ObjectID KinematicCollision2D::get_collider_id() const {
  1146. return collision.collider;
  1147. }
  1148. Object *KinematicCollision2D::get_collider_shape() const {
  1149. Object *collider = get_collider();
  1150. if (collider) {
  1151. CollisionObject2D *obj2d = Object::cast_to<CollisionObject2D>(collider);
  1152. if (obj2d) {
  1153. uint32_t ownerid = obj2d->shape_find_owner(collision.collider_shape);
  1154. return obj2d->shape_owner_get_owner(ownerid);
  1155. }
  1156. }
  1157. return NULL;
  1158. }
  1159. int KinematicCollision2D::get_collider_shape_index() const {
  1160. return collision.collider_shape;
  1161. }
  1162. Vector2 KinematicCollision2D::get_collider_velocity() const {
  1163. return collision.collider_vel;
  1164. }
  1165. Variant KinematicCollision2D::get_collider_metadata() const {
  1166. return Variant();
  1167. }
  1168. void KinematicCollision2D::_bind_methods() {
  1169. ClassDB::bind_method(D_METHOD("get_position"), &KinematicCollision2D::get_position);
  1170. ClassDB::bind_method(D_METHOD("get_normal"), &KinematicCollision2D::get_normal);
  1171. ClassDB::bind_method(D_METHOD("get_travel"), &KinematicCollision2D::get_travel);
  1172. ClassDB::bind_method(D_METHOD("get_remainder"), &KinematicCollision2D::get_remainder);
  1173. ClassDB::bind_method(D_METHOD("get_local_shape"), &KinematicCollision2D::get_local_shape);
  1174. ClassDB::bind_method(D_METHOD("get_collider"), &KinematicCollision2D::get_collider);
  1175. ClassDB::bind_method(D_METHOD("get_collider_id"), &KinematicCollision2D::get_collider_id);
  1176. ClassDB::bind_method(D_METHOD("get_collider_shape"), &KinematicCollision2D::get_collider_shape);
  1177. ClassDB::bind_method(D_METHOD("get_collider_shape_index"), &KinematicCollision2D::get_collider_shape_index);
  1178. ClassDB::bind_method(D_METHOD("get_collider_velocity"), &KinematicCollision2D::get_collider_velocity);
  1179. ClassDB::bind_method(D_METHOD("get_collider_metadata"), &KinematicCollision2D::get_collider_metadata);
  1180. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "", "get_position");
  1181. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "normal"), "", "get_normal");
  1182. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "travel"), "", "get_travel");
  1183. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "remainder"), "", "get_remainder");
  1184. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "local_shape"), "", "get_local_shape");
  1185. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider"), "", "get_collider");
  1186. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_id"), "", "get_collider_id");
  1187. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider_shape"), "", "get_collider_shape");
  1188. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_shape_index"), "", "get_collider_shape_index");
  1189. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "collider_velocity"), "", "get_collider_velocity");
  1190. ADD_PROPERTY(PropertyInfo(Variant::NIL, "collider_metadata", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "", "get_collider_metadata");
  1191. }
  1192. KinematicCollision2D::KinematicCollision2D() {
  1193. collision.collider = 0;
  1194. collision.collider_shape = 0;
  1195. collision.local_shape = 0;
  1196. owner = NULL;
  1197. }