physics_body_2d.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. /**************************************************************************/
  2. /* physics_body_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "physics_body_2d.h"
  31. #include "scene/scene_string_names.h"
  32. void PhysicsBody2D::_bind_methods() {
  33. ClassDB::bind_method(D_METHOD("move_and_collide", "motion", "test_only", "safe_margin", "recovery_as_collision"), &PhysicsBody2D::_move, DEFVAL(false), DEFVAL(0.08), DEFVAL(false));
  34. ClassDB::bind_method(D_METHOD("test_move", "from", "motion", "collision", "safe_margin", "recovery_as_collision"), &PhysicsBody2D::test_move, DEFVAL(Variant()), DEFVAL(0.08), DEFVAL(false));
  35. ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody2D::get_collision_exceptions);
  36. ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody2D::add_collision_exception_with);
  37. ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody2D::remove_collision_exception_with);
  38. }
  39. PhysicsBody2D::PhysicsBody2D(PhysicsServer2D::BodyMode p_mode) :
  40. CollisionObject2D(PhysicsServer2D::get_singleton()->body_create(), false) {
  41. set_body_mode(p_mode);
  42. set_pickable(false);
  43. }
  44. PhysicsBody2D::~PhysicsBody2D() {
  45. if (motion_cache.is_valid()) {
  46. motion_cache->owner = nullptr;
  47. }
  48. }
  49. Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_test_only, real_t p_margin, bool p_recovery_as_collision) {
  50. PhysicsServer2D::MotionParameters parameters(get_global_transform(), p_motion, p_margin);
  51. parameters.recovery_as_collision = p_recovery_as_collision;
  52. PhysicsServer2D::MotionResult result;
  53. if (move_and_collide(parameters, result, p_test_only)) {
  54. // Create a new instance when the cached reference is invalid or still in use in script.
  55. if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) {
  56. motion_cache.instantiate();
  57. motion_cache->owner = this;
  58. }
  59. motion_cache->result = result;
  60. return motion_cache;
  61. }
  62. return Ref<KinematicCollision2D>();
  63. }
  64. bool PhysicsBody2D::move_and_collide(const PhysicsServer2D::MotionParameters &p_parameters, PhysicsServer2D::MotionResult &r_result, bool p_test_only, bool p_cancel_sliding) {
  65. if (is_only_update_transform_changes_enabled()) {
  66. ERR_PRINT("Move functions do not work together with 'sync to physics' option. See the documentation for details.");
  67. }
  68. bool colliding = PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), p_parameters, &r_result);
  69. // Restore direction of motion to be along original motion,
  70. // in order to avoid sliding due to recovery,
  71. // but only if collision depth is low enough to avoid tunneling.
  72. if (p_cancel_sliding) {
  73. real_t motion_length = p_parameters.motion.length();
  74. real_t precision = 0.001;
  75. if (colliding) {
  76. // Can't just use margin as a threshold because collision depth is calculated on unsafe motion,
  77. // so even in normal resting cases the depth can be a bit more than the margin.
  78. precision += motion_length * (r_result.collision_unsafe_fraction - r_result.collision_safe_fraction);
  79. if (r_result.collision_depth > p_parameters.margin + precision) {
  80. p_cancel_sliding = false;
  81. }
  82. }
  83. if (p_cancel_sliding) {
  84. // When motion is null, recovery is the resulting motion.
  85. Vector2 motion_normal;
  86. if (motion_length > CMP_EPSILON) {
  87. motion_normal = p_parameters.motion / motion_length;
  88. }
  89. // Check depth of recovery.
  90. real_t projected_length = r_result.travel.dot(motion_normal);
  91. Vector2 recovery = r_result.travel - motion_normal * projected_length;
  92. real_t recovery_length = recovery.length();
  93. // Fixes cases where canceling slide causes the motion to go too deep into the ground,
  94. // because we're only taking rest information into account and not general recovery.
  95. if (recovery_length < p_parameters.margin + precision) {
  96. // Apply adjustment to motion.
  97. r_result.travel = motion_normal * projected_length;
  98. r_result.remainder = p_parameters.motion - r_result.travel;
  99. }
  100. }
  101. }
  102. if (!p_test_only) {
  103. Transform2D gt = p_parameters.from;
  104. gt.columns[2] += r_result.travel;
  105. set_global_transform(gt);
  106. }
  107. return colliding;
  108. }
  109. bool PhysicsBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion, const Ref<KinematicCollision2D> &r_collision, real_t p_margin, bool p_recovery_as_collision) {
  110. ERR_FAIL_COND_V(!is_inside_tree(), false);
  111. PhysicsServer2D::MotionResult *r = nullptr;
  112. PhysicsServer2D::MotionResult temp_result;
  113. if (r_collision.is_valid()) {
  114. // Needs const_cast because method bindings don't support non-const Ref.
  115. r = const_cast<PhysicsServer2D::MotionResult *>(&r_collision->result);
  116. } else {
  117. r = &temp_result;
  118. }
  119. PhysicsServer2D::MotionParameters parameters(p_from, p_motion, p_margin);
  120. parameters.recovery_as_collision = p_recovery_as_collision;
  121. return PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), parameters, r);
  122. }
  123. TypedArray<PhysicsBody2D> PhysicsBody2D::get_collision_exceptions() {
  124. List<RID> exceptions;
  125. PhysicsServer2D::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);
  126. Array ret;
  127. for (const RID &body : exceptions) {
  128. ObjectID instance_id = PhysicsServer2D::get_singleton()->body_get_object_instance_id(body);
  129. Object *obj = ObjectDB::get_instance(instance_id);
  130. PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(obj);
  131. ret.append(physics_body);
  132. }
  133. return ret;
  134. }
  135. void PhysicsBody2D::add_collision_exception_with(Node *p_node) {
  136. ERR_FAIL_NULL(p_node);
  137. PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);
  138. ERR_FAIL_NULL_MSG(physics_body, "Collision exception only works between two nodes that inherit from PhysicsBody2D.");
  139. PhysicsServer2D::get_singleton()->body_add_collision_exception(get_rid(), physics_body->get_rid());
  140. }
  141. void PhysicsBody2D::remove_collision_exception_with(Node *p_node) {
  142. ERR_FAIL_NULL(p_node);
  143. PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);
  144. ERR_FAIL_NULL_MSG(physics_body, "Collision exception only works between two nodes that inherit from PhysicsBody2D.");
  145. PhysicsServer2D::get_singleton()->body_remove_collision_exception(get_rid(), physics_body->get_rid());
  146. }
  147. void StaticBody2D::set_constant_linear_velocity(const Vector2 &p_vel) {
  148. constant_linear_velocity = p_vel;
  149. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, constant_linear_velocity);
  150. }
  151. void StaticBody2D::set_constant_angular_velocity(real_t p_vel) {
  152. constant_angular_velocity = p_vel;
  153. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, constant_angular_velocity);
  154. }
  155. Vector2 StaticBody2D::get_constant_linear_velocity() const {
  156. return constant_linear_velocity;
  157. }
  158. real_t StaticBody2D::get_constant_angular_velocity() const {
  159. return constant_angular_velocity;
  160. }
  161. void StaticBody2D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  162. if (physics_material_override.is_valid()) {
  163. physics_material_override->disconnect_changed(callable_mp(this, &StaticBody2D::_reload_physics_characteristics));
  164. }
  165. physics_material_override = p_physics_material_override;
  166. if (physics_material_override.is_valid()) {
  167. physics_material_override->connect_changed(callable_mp(this, &StaticBody2D::_reload_physics_characteristics));
  168. }
  169. _reload_physics_characteristics();
  170. }
  171. Ref<PhysicsMaterial> StaticBody2D::get_physics_material_override() const {
  172. return physics_material_override;
  173. }
  174. void StaticBody2D::_bind_methods() {
  175. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "vel"), &StaticBody2D::set_constant_linear_velocity);
  176. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "vel"), &StaticBody2D::set_constant_angular_velocity);
  177. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity"), &StaticBody2D::get_constant_linear_velocity);
  178. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity"), &StaticBody2D::get_constant_angular_velocity);
  179. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &StaticBody2D::set_physics_material_override);
  180. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &StaticBody2D::get_physics_material_override);
  181. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  182. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "constant_linear_velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_constant_linear_velocity", "get_constant_linear_velocity");
  183. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "constant_angular_velocity", PROPERTY_HINT_NONE, U"radians_as_degrees,suffix:\u00B0/s"), "set_constant_angular_velocity", "get_constant_angular_velocity");
  184. }
  185. StaticBody2D::StaticBody2D(PhysicsServer2D::BodyMode p_mode) :
  186. PhysicsBody2D(p_mode) {
  187. }
  188. void StaticBody2D::_reload_physics_characteristics() {
  189. if (physics_material_override.is_null()) {
  190. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_BOUNCE, 0);
  191. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_FRICTION, 1);
  192. } else {
  193. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  194. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  195. }
  196. }
  197. void AnimatableBody2D::set_sync_to_physics(bool p_enable) {
  198. if (sync_to_physics == p_enable) {
  199. return;
  200. }
  201. sync_to_physics = p_enable;
  202. _update_kinematic_motion();
  203. }
  204. bool AnimatableBody2D::is_sync_to_physics_enabled() const {
  205. return sync_to_physics;
  206. }
  207. void AnimatableBody2D::_update_kinematic_motion() {
  208. #ifdef TOOLS_ENABLED
  209. if (Engine::get_singleton()->is_editor_hint()) {
  210. return;
  211. }
  212. #endif
  213. if (sync_to_physics) {
  214. PhysicsServer2D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &AnimatableBody2D::_body_state_changed));
  215. set_only_update_transform_changes(true);
  216. set_notify_local_transform(true);
  217. } else {
  218. PhysicsServer2D::get_singleton()->body_set_state_sync_callback(get_rid(), Callable());
  219. set_only_update_transform_changes(false);
  220. set_notify_local_transform(false);
  221. }
  222. }
  223. void AnimatableBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state) {
  224. if (!sync_to_physics) {
  225. return;
  226. }
  227. last_valid_transform = p_state->get_transform();
  228. set_notify_local_transform(false);
  229. set_global_transform(last_valid_transform);
  230. set_notify_local_transform(true);
  231. }
  232. void AnimatableBody2D::_notification(int p_what) {
  233. switch (p_what) {
  234. case NOTIFICATION_ENTER_TREE: {
  235. last_valid_transform = get_global_transform();
  236. _update_kinematic_motion();
  237. } break;
  238. case NOTIFICATION_EXIT_TREE: {
  239. set_only_update_transform_changes(false);
  240. set_notify_local_transform(false);
  241. } break;
  242. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  243. // Used by sync to physics, send the new transform to the physics...
  244. Transform2D new_transform = get_global_transform();
  245. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_TRANSFORM, new_transform);
  246. // ... but then revert changes.
  247. set_notify_local_transform(false);
  248. set_global_transform(last_valid_transform);
  249. set_notify_local_transform(true);
  250. } break;
  251. }
  252. }
  253. void AnimatableBody2D::_bind_methods() {
  254. ClassDB::bind_method(D_METHOD("set_sync_to_physics", "enable"), &AnimatableBody2D::set_sync_to_physics);
  255. ClassDB::bind_method(D_METHOD("is_sync_to_physics_enabled"), &AnimatableBody2D::is_sync_to_physics_enabled);
  256. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync_to_physics"), "set_sync_to_physics", "is_sync_to_physics_enabled");
  257. }
  258. AnimatableBody2D::AnimatableBody2D() :
  259. StaticBody2D(PhysicsServer2D::BODY_MODE_KINEMATIC) {
  260. }
  261. void RigidBody2D::_body_enter_tree(ObjectID p_id) {
  262. Object *obj = ObjectDB::get_instance(p_id);
  263. Node *node = Object::cast_to<Node>(obj);
  264. ERR_FAIL_NULL(node);
  265. ERR_FAIL_NULL(contact_monitor);
  266. HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(p_id);
  267. ERR_FAIL_COND(!E);
  268. ERR_FAIL_COND(E->value.in_scene);
  269. contact_monitor->locked = true;
  270. E->value.in_scene = true;
  271. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  272. for (int i = 0; i < E->value.shapes.size(); i++) {
  273. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].local_shape);
  274. }
  275. contact_monitor->locked = false;
  276. }
  277. void RigidBody2D::_body_exit_tree(ObjectID p_id) {
  278. Object *obj = ObjectDB::get_instance(p_id);
  279. Node *node = Object::cast_to<Node>(obj);
  280. ERR_FAIL_NULL(node);
  281. ERR_FAIL_NULL(contact_monitor);
  282. HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(p_id);
  283. ERR_FAIL_COND(!E);
  284. ERR_FAIL_COND(!E->value.in_scene);
  285. E->value.in_scene = false;
  286. contact_monitor->locked = true;
  287. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  288. for (int i = 0; i < E->value.shapes.size(); i++) {
  289. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].local_shape);
  290. }
  291. contact_monitor->locked = false;
  292. }
  293. void RigidBody2D::_body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape) {
  294. bool body_in = p_status == 1;
  295. ObjectID objid = p_instance;
  296. Object *obj = ObjectDB::get_instance(objid);
  297. Node *node = Object::cast_to<Node>(obj);
  298. ERR_FAIL_NULL(contact_monitor);
  299. HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(objid);
  300. ERR_FAIL_COND(!body_in && !E);
  301. if (body_in) {
  302. if (!E) {
  303. E = contact_monitor->body_map.insert(objid, BodyState());
  304. E->value.rid = p_body;
  305. //E->value.rc=0;
  306. E->value.in_scene = node && node->is_inside_tree();
  307. if (node) {
  308. node->connect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &RigidBody2D::_body_enter_tree).bind(objid));
  309. node->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &RigidBody2D::_body_exit_tree).bind(objid));
  310. if (E->value.in_scene) {
  311. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  312. }
  313. }
  314. //E->value.rc++;
  315. }
  316. if (node) {
  317. E->value.shapes.insert(ShapePair(p_body_shape, p_local_shape));
  318. }
  319. if (E->value.in_scene) {
  320. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, p_body, node, p_body_shape, p_local_shape);
  321. }
  322. } else {
  323. //E->value.rc--;
  324. if (node) {
  325. E->value.shapes.erase(ShapePair(p_body_shape, p_local_shape));
  326. }
  327. bool in_scene = E->value.in_scene;
  328. if (E->value.shapes.is_empty()) {
  329. if (node) {
  330. node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &RigidBody2D::_body_enter_tree));
  331. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &RigidBody2D::_body_exit_tree));
  332. if (in_scene) {
  333. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  334. }
  335. }
  336. contact_monitor->body_map.remove(E);
  337. }
  338. if (node && in_scene) {
  339. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, p_body, node, p_body_shape, p_local_shape);
  340. }
  341. }
  342. }
  343. struct _RigidBody2DInOut {
  344. RID rid;
  345. ObjectID id;
  346. int shape = 0;
  347. int local_shape = 0;
  348. };
  349. void RigidBody2D::_sync_body_state(PhysicsDirectBodyState2D *p_state) {
  350. if (!freeze || freeze_mode != FREEZE_MODE_KINEMATIC) {
  351. set_block_transform_notify(true);
  352. set_global_transform(p_state->get_transform());
  353. set_block_transform_notify(false);
  354. }
  355. linear_velocity = p_state->get_linear_velocity();
  356. angular_velocity = p_state->get_angular_velocity();
  357. if (sleeping != p_state->is_sleeping()) {
  358. sleeping = p_state->is_sleeping();
  359. emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed);
  360. }
  361. }
  362. void RigidBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state) {
  363. lock_callback();
  364. if (GDVIRTUAL_IS_OVERRIDDEN(_integrate_forces)) {
  365. _sync_body_state(p_state);
  366. Transform2D old_transform = get_global_transform();
  367. GDVIRTUAL_CALL(_integrate_forces, p_state);
  368. Transform2D new_transform = get_global_transform();
  369. if (new_transform != old_transform) {
  370. // Update the physics server with the new transform, to prevent it from being overwritten at the sync below.
  371. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_TRANSFORM, new_transform);
  372. }
  373. }
  374. _sync_body_state(p_state);
  375. if (contact_monitor) {
  376. contact_monitor->locked = true;
  377. //untag all
  378. int rc = 0;
  379. for (KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
  380. for (int i = 0; i < E.value.shapes.size(); i++) {
  381. E.value.shapes[i].tagged = false;
  382. rc++;
  383. }
  384. }
  385. _RigidBody2DInOut *toadd = (_RigidBody2DInOut *)alloca(p_state->get_contact_count() * sizeof(_RigidBody2DInOut));
  386. int toadd_count = 0; //state->get_contact_count();
  387. RigidBody2D_RemoveAction *toremove = (RigidBody2D_RemoveAction *)alloca(rc * sizeof(RigidBody2D_RemoveAction));
  388. int toremove_count = 0;
  389. //put the ones to add
  390. for (int i = 0; i < p_state->get_contact_count(); i++) {
  391. RID col_rid = p_state->get_contact_collider(i);
  392. ObjectID col_obj = p_state->get_contact_collider_id(i);
  393. int local_shape = p_state->get_contact_local_shape(i);
  394. int col_shape = p_state->get_contact_collider_shape(i);
  395. HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(col_obj);
  396. if (!E) {
  397. toadd[toadd_count].rid = col_rid;
  398. toadd[toadd_count].local_shape = local_shape;
  399. toadd[toadd_count].id = col_obj;
  400. toadd[toadd_count].shape = col_shape;
  401. toadd_count++;
  402. continue;
  403. }
  404. ShapePair sp(col_shape, local_shape);
  405. int idx = E->value.shapes.find(sp);
  406. if (idx == -1) {
  407. toadd[toadd_count].rid = col_rid;
  408. toadd[toadd_count].local_shape = local_shape;
  409. toadd[toadd_count].id = col_obj;
  410. toadd[toadd_count].shape = col_shape;
  411. toadd_count++;
  412. continue;
  413. }
  414. E->value.shapes[idx].tagged = true;
  415. }
  416. //put the ones to remove
  417. for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
  418. for (int i = 0; i < E.value.shapes.size(); i++) {
  419. if (!E.value.shapes[i].tagged) {
  420. toremove[toremove_count].rid = E.value.rid;
  421. toremove[toremove_count].body_id = E.key;
  422. toremove[toremove_count].pair = E.value.shapes[i];
  423. toremove_count++;
  424. }
  425. }
  426. }
  427. //process removals
  428. for (int i = 0; i < toremove_count; i++) {
  429. _body_inout(0, toremove[i].rid, toremove[i].body_id, toremove[i].pair.body_shape, toremove[i].pair.local_shape);
  430. }
  431. //process additions
  432. for (int i = 0; i < toadd_count; i++) {
  433. _body_inout(1, toadd[i].rid, toadd[i].id, toadd[i].shape, toadd[i].local_shape);
  434. }
  435. contact_monitor->locked = false;
  436. }
  437. unlock_callback();
  438. }
  439. void RigidBody2D::_apply_body_mode() {
  440. if (freeze) {
  441. switch (freeze_mode) {
  442. case FREEZE_MODE_STATIC: {
  443. set_body_mode(PhysicsServer2D::BODY_MODE_STATIC);
  444. } break;
  445. case FREEZE_MODE_KINEMATIC: {
  446. set_body_mode(PhysicsServer2D::BODY_MODE_KINEMATIC);
  447. } break;
  448. }
  449. } else if (lock_rotation) {
  450. set_body_mode(PhysicsServer2D::BODY_MODE_RIGID_LINEAR);
  451. } else {
  452. set_body_mode(PhysicsServer2D::BODY_MODE_RIGID);
  453. }
  454. }
  455. void RigidBody2D::set_lock_rotation_enabled(bool p_lock_rotation) {
  456. if (p_lock_rotation == lock_rotation) {
  457. return;
  458. }
  459. lock_rotation = p_lock_rotation;
  460. _apply_body_mode();
  461. }
  462. bool RigidBody2D::is_lock_rotation_enabled() const {
  463. return lock_rotation;
  464. }
  465. void RigidBody2D::set_freeze_enabled(bool p_freeze) {
  466. if (p_freeze == freeze) {
  467. return;
  468. }
  469. freeze = p_freeze;
  470. _apply_body_mode();
  471. }
  472. bool RigidBody2D::is_freeze_enabled() const {
  473. return freeze;
  474. }
  475. void RigidBody2D::set_freeze_mode(FreezeMode p_freeze_mode) {
  476. if (p_freeze_mode == freeze_mode) {
  477. return;
  478. }
  479. freeze_mode = p_freeze_mode;
  480. _apply_body_mode();
  481. }
  482. RigidBody2D::FreezeMode RigidBody2D::get_freeze_mode() const {
  483. return freeze_mode;
  484. }
  485. void RigidBody2D::set_mass(real_t p_mass) {
  486. ERR_FAIL_COND(p_mass <= 0);
  487. mass = p_mass;
  488. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_MASS, mass);
  489. }
  490. real_t RigidBody2D::get_mass() const {
  491. return mass;
  492. }
  493. void RigidBody2D::set_inertia(real_t p_inertia) {
  494. ERR_FAIL_COND(p_inertia < 0);
  495. inertia = p_inertia;
  496. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_INERTIA, inertia);
  497. }
  498. real_t RigidBody2D::get_inertia() const {
  499. return inertia;
  500. }
  501. void RigidBody2D::set_center_of_mass_mode(CenterOfMassMode p_mode) {
  502. if (center_of_mass_mode == p_mode) {
  503. return;
  504. }
  505. center_of_mass_mode = p_mode;
  506. switch (center_of_mass_mode) {
  507. case CENTER_OF_MASS_MODE_AUTO: {
  508. center_of_mass = Vector2();
  509. PhysicsServer2D::get_singleton()->body_reset_mass_properties(get_rid());
  510. if (inertia != 0.0) {
  511. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_INERTIA, inertia);
  512. }
  513. } break;
  514. case CENTER_OF_MASS_MODE_CUSTOM: {
  515. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS, center_of_mass);
  516. } break;
  517. }
  518. }
  519. RigidBody2D::CenterOfMassMode RigidBody2D::get_center_of_mass_mode() const {
  520. return center_of_mass_mode;
  521. }
  522. void RigidBody2D::set_center_of_mass(const Vector2 &p_center_of_mass) {
  523. if (center_of_mass == p_center_of_mass) {
  524. return;
  525. }
  526. ERR_FAIL_COND(center_of_mass_mode != CENTER_OF_MASS_MODE_CUSTOM);
  527. center_of_mass = p_center_of_mass;
  528. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS, center_of_mass);
  529. }
  530. const Vector2 &RigidBody2D::get_center_of_mass() const {
  531. return center_of_mass;
  532. }
  533. void RigidBody2D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  534. if (physics_material_override.is_valid()) {
  535. physics_material_override->disconnect_changed(callable_mp(this, &RigidBody2D::_reload_physics_characteristics));
  536. }
  537. physics_material_override = p_physics_material_override;
  538. if (physics_material_override.is_valid()) {
  539. physics_material_override->connect_changed(callable_mp(this, &RigidBody2D::_reload_physics_characteristics));
  540. }
  541. _reload_physics_characteristics();
  542. }
  543. Ref<PhysicsMaterial> RigidBody2D::get_physics_material_override() const {
  544. return physics_material_override;
  545. }
  546. void RigidBody2D::set_gravity_scale(real_t p_gravity_scale) {
  547. gravity_scale = p_gravity_scale;
  548. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
  549. }
  550. real_t RigidBody2D::get_gravity_scale() const {
  551. return gravity_scale;
  552. }
  553. void RigidBody2D::set_linear_damp_mode(DampMode p_mode) {
  554. linear_damp_mode = p_mode;
  555. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE, linear_damp_mode);
  556. }
  557. RigidBody2D::DampMode RigidBody2D::get_linear_damp_mode() const {
  558. return linear_damp_mode;
  559. }
  560. void RigidBody2D::set_angular_damp_mode(DampMode p_mode) {
  561. angular_damp_mode = p_mode;
  562. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE, angular_damp_mode);
  563. }
  564. RigidBody2D::DampMode RigidBody2D::get_angular_damp_mode() const {
  565. return angular_damp_mode;
  566. }
  567. void RigidBody2D::set_linear_damp(real_t p_linear_damp) {
  568. ERR_FAIL_COND(p_linear_damp < -1);
  569. linear_damp = p_linear_damp;
  570. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_LINEAR_DAMP, linear_damp);
  571. }
  572. real_t RigidBody2D::get_linear_damp() const {
  573. return linear_damp;
  574. }
  575. void RigidBody2D::set_angular_damp(real_t p_angular_damp) {
  576. ERR_FAIL_COND(p_angular_damp < -1);
  577. angular_damp = p_angular_damp;
  578. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP, angular_damp);
  579. }
  580. real_t RigidBody2D::get_angular_damp() const {
  581. return angular_damp;
  582. }
  583. void RigidBody2D::set_axis_velocity(const Vector2 &p_axis) {
  584. Vector2 axis = p_axis.normalized();
  585. linear_velocity -= axis * axis.dot(linear_velocity);
  586. linear_velocity += p_axis;
  587. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  588. }
  589. void RigidBody2D::set_linear_velocity(const Vector2 &p_velocity) {
  590. linear_velocity = p_velocity;
  591. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  592. }
  593. Vector2 RigidBody2D::get_linear_velocity() const {
  594. return linear_velocity;
  595. }
  596. void RigidBody2D::set_angular_velocity(real_t p_velocity) {
  597. angular_velocity = p_velocity;
  598. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
  599. }
  600. real_t RigidBody2D::get_angular_velocity() const {
  601. return angular_velocity;
  602. }
  603. void RigidBody2D::set_use_custom_integrator(bool p_enable) {
  604. if (custom_integrator == p_enable) {
  605. return;
  606. }
  607. custom_integrator = p_enable;
  608. PhysicsServer2D::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
  609. }
  610. bool RigidBody2D::is_using_custom_integrator() {
  611. return custom_integrator;
  612. }
  613. void RigidBody2D::set_sleeping(bool p_sleeping) {
  614. sleeping = p_sleeping;
  615. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_SLEEPING, sleeping);
  616. }
  617. void RigidBody2D::set_can_sleep(bool p_active) {
  618. can_sleep = p_active;
  619. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_CAN_SLEEP, p_active);
  620. }
  621. bool RigidBody2D::is_able_to_sleep() const {
  622. return can_sleep;
  623. }
  624. bool RigidBody2D::is_sleeping() const {
  625. return sleeping;
  626. }
  627. void RigidBody2D::set_max_contacts_reported(int p_amount) {
  628. max_contacts_reported = p_amount;
  629. PhysicsServer2D::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount);
  630. }
  631. int RigidBody2D::get_max_contacts_reported() const {
  632. return max_contacts_reported;
  633. }
  634. int RigidBody2D::get_contact_count() const {
  635. PhysicsDirectBodyState2D *bs = PhysicsServer2D::get_singleton()->body_get_direct_state(get_rid());
  636. ERR_FAIL_NULL_V(bs, 0);
  637. return bs->get_contact_count();
  638. }
  639. void RigidBody2D::apply_central_impulse(const Vector2 &p_impulse) {
  640. PhysicsServer2D::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
  641. }
  642. void RigidBody2D::apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position) {
  643. PhysicsServer2D::get_singleton()->body_apply_impulse(get_rid(), p_impulse, p_position);
  644. }
  645. void RigidBody2D::apply_torque_impulse(real_t p_torque) {
  646. PhysicsServer2D::get_singleton()->body_apply_torque_impulse(get_rid(), p_torque);
  647. }
  648. void RigidBody2D::apply_central_force(const Vector2 &p_force) {
  649. PhysicsServer2D::get_singleton()->body_apply_central_force(get_rid(), p_force);
  650. }
  651. void RigidBody2D::apply_force(const Vector2 &p_force, const Vector2 &p_position) {
  652. PhysicsServer2D::get_singleton()->body_apply_force(get_rid(), p_force, p_position);
  653. }
  654. void RigidBody2D::apply_torque(real_t p_torque) {
  655. PhysicsServer2D::get_singleton()->body_apply_torque(get_rid(), p_torque);
  656. }
  657. void RigidBody2D::add_constant_central_force(const Vector2 &p_force) {
  658. PhysicsServer2D::get_singleton()->body_add_constant_central_force(get_rid(), p_force);
  659. }
  660. void RigidBody2D::add_constant_force(const Vector2 &p_force, const Vector2 &p_position) {
  661. PhysicsServer2D::get_singleton()->body_add_constant_force(get_rid(), p_force, p_position);
  662. }
  663. void RigidBody2D::add_constant_torque(const real_t p_torque) {
  664. PhysicsServer2D::get_singleton()->body_add_constant_torque(get_rid(), p_torque);
  665. }
  666. void RigidBody2D::set_constant_force(const Vector2 &p_force) {
  667. PhysicsServer2D::get_singleton()->body_set_constant_force(get_rid(), p_force);
  668. }
  669. Vector2 RigidBody2D::get_constant_force() const {
  670. return PhysicsServer2D::get_singleton()->body_get_constant_force(get_rid());
  671. }
  672. void RigidBody2D::set_constant_torque(real_t p_torque) {
  673. PhysicsServer2D::get_singleton()->body_set_constant_torque(get_rid(), p_torque);
  674. }
  675. real_t RigidBody2D::get_constant_torque() const {
  676. return PhysicsServer2D::get_singleton()->body_get_constant_torque(get_rid());
  677. }
  678. void RigidBody2D::set_continuous_collision_detection_mode(CCDMode p_mode) {
  679. ccd_mode = p_mode;
  680. PhysicsServer2D::get_singleton()->body_set_continuous_collision_detection_mode(get_rid(), PhysicsServer2D::CCDMode(p_mode));
  681. }
  682. RigidBody2D::CCDMode RigidBody2D::get_continuous_collision_detection_mode() const {
  683. return ccd_mode;
  684. }
  685. TypedArray<Node2D> RigidBody2D::get_colliding_bodies() const {
  686. ERR_FAIL_NULL_V(contact_monitor, TypedArray<Node2D>());
  687. TypedArray<Node2D> ret;
  688. ret.resize(contact_monitor->body_map.size());
  689. int idx = 0;
  690. for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
  691. Object *obj = ObjectDB::get_instance(E.key);
  692. if (!obj) {
  693. ret.resize(ret.size() - 1); //ops
  694. } else {
  695. ret[idx++] = obj;
  696. }
  697. }
  698. return ret;
  699. }
  700. void RigidBody2D::set_contact_monitor(bool p_enabled) {
  701. if (p_enabled == is_contact_monitor_enabled()) {
  702. return;
  703. }
  704. if (!p_enabled) {
  705. ERR_FAIL_COND_MSG(contact_monitor->locked, "Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\", false) instead.");
  706. for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
  707. //clean up mess
  708. Object *obj = ObjectDB::get_instance(E.key);
  709. Node *node = Object::cast_to<Node>(obj);
  710. if (node) {
  711. node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &RigidBody2D::_body_enter_tree));
  712. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &RigidBody2D::_body_exit_tree));
  713. }
  714. }
  715. memdelete(contact_monitor);
  716. contact_monitor = nullptr;
  717. } else {
  718. contact_monitor = memnew(ContactMonitor);
  719. contact_monitor->locked = false;
  720. }
  721. }
  722. bool RigidBody2D::is_contact_monitor_enabled() const {
  723. return contact_monitor != nullptr;
  724. }
  725. void RigidBody2D::_notification(int p_what) {
  726. #ifdef TOOLS_ENABLED
  727. switch (p_what) {
  728. case NOTIFICATION_ENTER_TREE: {
  729. if (Engine::get_singleton()->is_editor_hint()) {
  730. set_notify_local_transform(true); // Used for warnings and only in editor.
  731. }
  732. } break;
  733. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  734. update_configuration_warnings();
  735. } break;
  736. }
  737. #endif
  738. }
  739. PackedStringArray RigidBody2D::get_configuration_warnings() const {
  740. Transform2D t = get_transform();
  741. PackedStringArray warnings = CollisionObject2D::get_configuration_warnings();
  742. if (ABS(t.columns[0].length() - 1.0) > 0.05 || ABS(t.columns[1].length() - 1.0) > 0.05) {
  743. warnings.push_back(RTR("Size changes to RigidBody2D will be overridden by the physics engine when running.\nChange the size in children collision shapes instead."));
  744. }
  745. return warnings;
  746. }
  747. void RigidBody2D::_bind_methods() {
  748. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &RigidBody2D::set_mass);
  749. ClassDB::bind_method(D_METHOD("get_mass"), &RigidBody2D::get_mass);
  750. ClassDB::bind_method(D_METHOD("get_inertia"), &RigidBody2D::get_inertia);
  751. ClassDB::bind_method(D_METHOD("set_inertia", "inertia"), &RigidBody2D::set_inertia);
  752. ClassDB::bind_method(D_METHOD("set_center_of_mass_mode", "mode"), &RigidBody2D::set_center_of_mass_mode);
  753. ClassDB::bind_method(D_METHOD("get_center_of_mass_mode"), &RigidBody2D::get_center_of_mass_mode);
  754. ClassDB::bind_method(D_METHOD("set_center_of_mass", "center_of_mass"), &RigidBody2D::set_center_of_mass);
  755. ClassDB::bind_method(D_METHOD("get_center_of_mass"), &RigidBody2D::get_center_of_mass);
  756. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &RigidBody2D::set_physics_material_override);
  757. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &RigidBody2D::get_physics_material_override);
  758. ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &RigidBody2D::set_gravity_scale);
  759. ClassDB::bind_method(D_METHOD("get_gravity_scale"), &RigidBody2D::get_gravity_scale);
  760. ClassDB::bind_method(D_METHOD("set_linear_damp_mode", "linear_damp_mode"), &RigidBody2D::set_linear_damp_mode);
  761. ClassDB::bind_method(D_METHOD("get_linear_damp_mode"), &RigidBody2D::get_linear_damp_mode);
  762. ClassDB::bind_method(D_METHOD("set_angular_damp_mode", "angular_damp_mode"), &RigidBody2D::set_angular_damp_mode);
  763. ClassDB::bind_method(D_METHOD("get_angular_damp_mode"), &RigidBody2D::get_angular_damp_mode);
  764. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &RigidBody2D::set_linear_damp);
  765. ClassDB::bind_method(D_METHOD("get_linear_damp"), &RigidBody2D::get_linear_damp);
  766. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &RigidBody2D::set_angular_damp);
  767. ClassDB::bind_method(D_METHOD("get_angular_damp"), &RigidBody2D::get_angular_damp);
  768. ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &RigidBody2D::set_linear_velocity);
  769. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &RigidBody2D::get_linear_velocity);
  770. ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &RigidBody2D::set_angular_velocity);
  771. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &RigidBody2D::get_angular_velocity);
  772. ClassDB::bind_method(D_METHOD("set_max_contacts_reported", "amount"), &RigidBody2D::set_max_contacts_reported);
  773. ClassDB::bind_method(D_METHOD("get_max_contacts_reported"), &RigidBody2D::get_max_contacts_reported);
  774. ClassDB::bind_method(D_METHOD("get_contact_count"), &RigidBody2D::get_contact_count);
  775. ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &RigidBody2D::set_use_custom_integrator);
  776. ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &RigidBody2D::is_using_custom_integrator);
  777. ClassDB::bind_method(D_METHOD("set_contact_monitor", "enabled"), &RigidBody2D::set_contact_monitor);
  778. ClassDB::bind_method(D_METHOD("is_contact_monitor_enabled"), &RigidBody2D::is_contact_monitor_enabled);
  779. ClassDB::bind_method(D_METHOD("set_continuous_collision_detection_mode", "mode"), &RigidBody2D::set_continuous_collision_detection_mode);
  780. ClassDB::bind_method(D_METHOD("get_continuous_collision_detection_mode"), &RigidBody2D::get_continuous_collision_detection_mode);
  781. ClassDB::bind_method(D_METHOD("set_axis_velocity", "axis_velocity"), &RigidBody2D::set_axis_velocity);
  782. ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &RigidBody2D::apply_central_impulse, Vector2());
  783. ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &RigidBody2D::apply_impulse, Vector2());
  784. ClassDB::bind_method(D_METHOD("apply_torque_impulse", "torque"), &RigidBody2D::apply_torque_impulse);
  785. ClassDB::bind_method(D_METHOD("apply_central_force", "force"), &RigidBody2D::apply_central_force);
  786. ClassDB::bind_method(D_METHOD("apply_force", "force", "position"), &RigidBody2D::apply_force, Vector2());
  787. ClassDB::bind_method(D_METHOD("apply_torque", "torque"), &RigidBody2D::apply_torque);
  788. ClassDB::bind_method(D_METHOD("add_constant_central_force", "force"), &RigidBody2D::add_constant_central_force);
  789. ClassDB::bind_method(D_METHOD("add_constant_force", "force", "position"), &RigidBody2D::add_constant_force, Vector2());
  790. ClassDB::bind_method(D_METHOD("add_constant_torque", "torque"), &RigidBody2D::add_constant_torque);
  791. ClassDB::bind_method(D_METHOD("set_constant_force", "force"), &RigidBody2D::set_constant_force);
  792. ClassDB::bind_method(D_METHOD("get_constant_force"), &RigidBody2D::get_constant_force);
  793. ClassDB::bind_method(D_METHOD("set_constant_torque", "torque"), &RigidBody2D::set_constant_torque);
  794. ClassDB::bind_method(D_METHOD("get_constant_torque"), &RigidBody2D::get_constant_torque);
  795. ClassDB::bind_method(D_METHOD("set_sleeping", "sleeping"), &RigidBody2D::set_sleeping);
  796. ClassDB::bind_method(D_METHOD("is_sleeping"), &RigidBody2D::is_sleeping);
  797. ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &RigidBody2D::set_can_sleep);
  798. ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &RigidBody2D::is_able_to_sleep);
  799. ClassDB::bind_method(D_METHOD("set_lock_rotation_enabled", "lock_rotation"), &RigidBody2D::set_lock_rotation_enabled);
  800. ClassDB::bind_method(D_METHOD("is_lock_rotation_enabled"), &RigidBody2D::is_lock_rotation_enabled);
  801. ClassDB::bind_method(D_METHOD("set_freeze_enabled", "freeze_mode"), &RigidBody2D::set_freeze_enabled);
  802. ClassDB::bind_method(D_METHOD("is_freeze_enabled"), &RigidBody2D::is_freeze_enabled);
  803. ClassDB::bind_method(D_METHOD("set_freeze_mode", "freeze_mode"), &RigidBody2D::set_freeze_mode);
  804. ClassDB::bind_method(D_METHOD("get_freeze_mode"), &RigidBody2D::get_freeze_mode);
  805. ClassDB::bind_method(D_METHOD("get_colliding_bodies"), &RigidBody2D::get_colliding_bodies);
  806. GDVIRTUAL_BIND(_integrate_forces, "state");
  807. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass", PROPERTY_HINT_RANGE, "0.01,1000,0.01,or_greater,exp,suffix:kg"), "set_mass", "get_mass");
  808. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  809. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_scale", PROPERTY_HINT_RANGE, "-8,8,0.001,or_less,or_greater"), "set_gravity_scale", "get_gravity_scale");
  810. ADD_GROUP("Mass Distribution", "");
  811. ADD_PROPERTY(PropertyInfo(Variant::INT, "center_of_mass_mode", PROPERTY_HINT_ENUM, "Auto,Custom", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_center_of_mass_mode", "get_center_of_mass_mode");
  812. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "center_of_mass", PROPERTY_HINT_RANGE, "-10,10,0.01,or_less,or_greater,suffix:px"), "set_center_of_mass", "get_center_of_mass");
  813. ADD_LINKED_PROPERTY("center_of_mass_mode", "center_of_mass");
  814. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inertia", PROPERTY_HINT_RANGE, U"0,1000,0.01,or_greater,exp,suffix:kg\u22C5px\u00B2"), "set_inertia", "get_inertia");
  815. ADD_GROUP("Deactivation", "");
  816. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleeping", "is_sleeping");
  817. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
  818. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "lock_rotation"), "set_lock_rotation_enabled", "is_lock_rotation_enabled");
  819. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "freeze"), "set_freeze_enabled", "is_freeze_enabled");
  820. ADD_PROPERTY(PropertyInfo(Variant::INT, "freeze_mode", PROPERTY_HINT_ENUM, "Static,Kinematic"), "set_freeze_mode", "get_freeze_mode");
  821. ADD_GROUP("Solver", "");
  822. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
  823. 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");
  824. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_contacts_reported", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), "set_max_contacts_reported", "get_max_contacts_reported");
  825. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor"), "set_contact_monitor", "is_contact_monitor_enabled");
  826. ADD_GROUP("Linear", "linear_");
  827. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "linear_velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_linear_velocity", "get_linear_velocity");
  828. ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_linear_damp_mode", "get_linear_damp_mode");
  829. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
  830. ADD_GROUP("Angular", "angular_");
  831. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_velocity", PROPERTY_HINT_NONE, U"radians_as_degrees,suffix:\u00B0/s"), "set_angular_velocity", "get_angular_velocity");
  832. ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_angular_damp_mode", "get_angular_damp_mode");
  833. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
  834. ADD_GROUP("Constant Forces", "constant_");
  835. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "constant_force", PROPERTY_HINT_NONE, U"suffix:kg\u22C5px/s\u00B2"), "set_constant_force", "get_constant_force");
  836. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "constant_torque", PROPERTY_HINT_NONE, U"suffix:kg\u22C5px\u00B2/s\u00B2/rad"), "set_constant_torque", "get_constant_torque");
  837. ADD_SIGNAL(MethodInfo("body_shape_entered", PropertyInfo(Variant::RID, "body_rid"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::INT, "body_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  838. ADD_SIGNAL(MethodInfo("body_shape_exited", PropertyInfo(Variant::RID, "body_rid"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::INT, "body_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  839. ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  840. ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  841. ADD_SIGNAL(MethodInfo("sleeping_state_changed"));
  842. BIND_ENUM_CONSTANT(FREEZE_MODE_STATIC);
  843. BIND_ENUM_CONSTANT(FREEZE_MODE_KINEMATIC);
  844. BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_AUTO);
  845. BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_CUSTOM);
  846. BIND_ENUM_CONSTANT(DAMP_MODE_COMBINE);
  847. BIND_ENUM_CONSTANT(DAMP_MODE_REPLACE);
  848. BIND_ENUM_CONSTANT(CCD_MODE_DISABLED);
  849. BIND_ENUM_CONSTANT(CCD_MODE_CAST_RAY);
  850. BIND_ENUM_CONSTANT(CCD_MODE_CAST_SHAPE);
  851. }
  852. void RigidBody2D::_validate_property(PropertyInfo &p_property) const {
  853. if (center_of_mass_mode != CENTER_OF_MASS_MODE_CUSTOM) {
  854. if (p_property.name == "center_of_mass") {
  855. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  856. }
  857. }
  858. }
  859. RigidBody2D::RigidBody2D() :
  860. PhysicsBody2D(PhysicsServer2D::BODY_MODE_RIGID) {
  861. PhysicsServer2D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &RigidBody2D::_body_state_changed));
  862. }
  863. RigidBody2D::~RigidBody2D() {
  864. if (contact_monitor) {
  865. memdelete(contact_monitor);
  866. }
  867. }
  868. void RigidBody2D::_reload_physics_characteristics() {
  869. if (physics_material_override.is_null()) {
  870. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_BOUNCE, 0);
  871. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_FRICTION, 1);
  872. } else {
  873. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  874. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  875. }
  876. }
  877. //////////////////////////
  878. // So, if you pass 45 as limit, avoid numerical precision errors when angle is 45.
  879. #define FLOOR_ANGLE_THRESHOLD 0.01
  880. bool CharacterBody2D::move_and_slide() {
  881. // Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky.
  882. double delta = Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time();
  883. Vector2 current_platform_velocity = platform_velocity;
  884. Transform2D gt = get_global_transform();
  885. previous_position = gt.columns[2];
  886. if ((on_floor || on_wall) && platform_rid.is_valid()) {
  887. bool excluded = false;
  888. if (on_floor) {
  889. excluded = (platform_floor_layers & platform_layer) == 0;
  890. } else if (on_wall) {
  891. excluded = (platform_wall_layers & platform_layer) == 0;
  892. }
  893. if (!excluded) {
  894. //this approach makes sure there is less delay between the actual body velocity and the one we saved
  895. PhysicsDirectBodyState2D *bs = PhysicsServer2D::get_singleton()->body_get_direct_state(platform_rid);
  896. if (bs) {
  897. Vector2 local_position = gt.columns[2] - bs->get_transform().columns[2];
  898. current_platform_velocity = bs->get_velocity_at_local_position(local_position);
  899. } else {
  900. // Body is removed or destroyed, invalidate floor.
  901. current_platform_velocity = Vector2();
  902. platform_rid = RID();
  903. }
  904. } else {
  905. current_platform_velocity = Vector2();
  906. }
  907. }
  908. motion_results.clear();
  909. last_motion = Vector2();
  910. bool was_on_floor = on_floor;
  911. on_floor = false;
  912. on_ceiling = false;
  913. on_wall = false;
  914. if (!current_platform_velocity.is_zero_approx()) {
  915. PhysicsServer2D::MotionParameters parameters(get_global_transform(), current_platform_velocity * delta, margin);
  916. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  917. parameters.exclude_bodies.insert(platform_rid);
  918. if (platform_object_id.is_valid()) {
  919. parameters.exclude_objects.insert(platform_object_id);
  920. }
  921. PhysicsServer2D::MotionResult floor_result;
  922. if (move_and_collide(parameters, floor_result, false, false)) {
  923. motion_results.push_back(floor_result);
  924. _set_collision_direction(floor_result);
  925. }
  926. }
  927. if (motion_mode == MOTION_MODE_GROUNDED) {
  928. _move_and_slide_grounded(delta, was_on_floor);
  929. } else {
  930. _move_and_slide_floating(delta);
  931. }
  932. // Compute real velocity.
  933. real_velocity = get_position_delta() / delta;
  934. if (platform_on_leave != PLATFORM_ON_LEAVE_DO_NOTHING) {
  935. // Add last platform velocity when just left a moving platform.
  936. if (!on_floor && !on_wall) {
  937. if (platform_on_leave == PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY && current_platform_velocity.dot(up_direction) < 0) {
  938. current_platform_velocity = current_platform_velocity.slide(up_direction);
  939. }
  940. velocity += current_platform_velocity;
  941. }
  942. }
  943. return motion_results.size() > 0;
  944. }
  945. void CharacterBody2D::_move_and_slide_grounded(double p_delta, bool p_was_on_floor) {
  946. Vector2 motion = velocity * p_delta;
  947. Vector2 motion_slide_up = motion.slide(up_direction);
  948. Vector2 prev_floor_normal = floor_normal;
  949. platform_rid = RID();
  950. platform_object_id = ObjectID();
  951. floor_normal = Vector2();
  952. platform_velocity = Vector2();
  953. // No sliding on first attempt to keep floor motion stable when possible,
  954. // When stop on slope is enabled or when there is no up direction.
  955. bool sliding_enabled = !floor_stop_on_slope;
  956. // Constant speed can be applied only the first time sliding is enabled.
  957. bool can_apply_constant_speed = sliding_enabled;
  958. // If the platform's ceiling push down the body.
  959. bool apply_ceiling_velocity = false;
  960. bool first_slide = true;
  961. bool vel_dir_facing_up = velocity.dot(up_direction) > 0;
  962. Vector2 last_travel;
  963. for (int iteration = 0; iteration < max_slides; ++iteration) {
  964. PhysicsServer2D::MotionParameters parameters(get_global_transform(), motion, margin);
  965. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  966. Vector2 prev_position = parameters.from.columns[2];
  967. PhysicsServer2D::MotionResult result;
  968. bool collided = move_and_collide(parameters, result, false, !sliding_enabled);
  969. last_motion = result.travel;
  970. if (collided) {
  971. motion_results.push_back(result);
  972. _set_collision_direction(result);
  973. // If we hit a ceiling platform, we set the vertical velocity to at least the platform one.
  974. if (on_ceiling && result.collider_velocity != Vector2() && result.collider_velocity.dot(up_direction) < 0) {
  975. // If ceiling sliding is on, only apply when the ceiling is flat or when the motion is upward.
  976. if (!slide_on_ceiling || motion.dot(up_direction) < 0 || (result.collision_normal + up_direction).length() < 0.01) {
  977. apply_ceiling_velocity = true;
  978. Vector2 ceiling_vertical_velocity = up_direction * up_direction.dot(result.collider_velocity);
  979. Vector2 motion_vertical_velocity = up_direction * up_direction.dot(velocity);
  980. if (motion_vertical_velocity.dot(up_direction) > 0 || ceiling_vertical_velocity.length_squared() > motion_vertical_velocity.length_squared()) {
  981. velocity = ceiling_vertical_velocity + velocity.slide(up_direction);
  982. }
  983. }
  984. }
  985. if (on_floor && floor_stop_on_slope && (velocity.normalized() + up_direction).length() < 0.01) {
  986. Transform2D gt = get_global_transform();
  987. if (result.travel.length() <= margin + CMP_EPSILON) {
  988. gt.columns[2] -= result.travel;
  989. }
  990. set_global_transform(gt);
  991. velocity = Vector2();
  992. last_motion = Vector2();
  993. motion = Vector2();
  994. break;
  995. }
  996. if (result.remainder.is_zero_approx()) {
  997. motion = Vector2();
  998. break;
  999. }
  1000. // Move on floor only checks.
  1001. if (floor_block_on_wall && on_wall && motion_slide_up.dot(result.collision_normal) <= 0) {
  1002. // Avoid to move forward on a wall if floor_block_on_wall is true.
  1003. if (p_was_on_floor && !on_floor && !vel_dir_facing_up) {
  1004. // If the movement is large the body can be prevented from reaching the walls.
  1005. if (result.travel.length() <= margin + CMP_EPSILON) {
  1006. // Cancels the motion.
  1007. Transform2D gt = get_global_transform();
  1008. gt.columns[2] -= result.travel;
  1009. set_global_transform(gt);
  1010. }
  1011. // Determines if you are on the ground.
  1012. _snap_on_floor(true, false, true);
  1013. velocity = Vector2();
  1014. last_motion = Vector2();
  1015. motion = Vector2();
  1016. break;
  1017. }
  1018. // Prevents the body from being able to climb a slope when it moves forward against the wall.
  1019. else if (!on_floor) {
  1020. motion = up_direction * up_direction.dot(result.remainder);
  1021. motion = motion.slide(result.collision_normal);
  1022. } else {
  1023. motion = result.remainder;
  1024. }
  1025. }
  1026. // Constant Speed when the slope is upward.
  1027. else if (floor_constant_speed && is_on_floor_only() && can_apply_constant_speed && p_was_on_floor && motion.dot(result.collision_normal) < 0) {
  1028. can_apply_constant_speed = false;
  1029. Vector2 motion_slide_norm = result.remainder.slide(result.collision_normal).normalized();
  1030. motion = motion_slide_norm * (motion_slide_up.length() - result.travel.slide(up_direction).length() - last_travel.slide(up_direction).length());
  1031. }
  1032. // Regular sliding, the last part of the test handle the case when you don't want to slide on the ceiling.
  1033. else if ((sliding_enabled || !on_floor) && (!on_ceiling || slide_on_ceiling || !vel_dir_facing_up) && !apply_ceiling_velocity) {
  1034. Vector2 slide_motion = result.remainder.slide(result.collision_normal);
  1035. if (slide_motion.dot(velocity) > 0.0) {
  1036. motion = slide_motion;
  1037. } else {
  1038. motion = Vector2();
  1039. }
  1040. if (slide_on_ceiling && on_ceiling) {
  1041. // Apply slide only in the direction of the input motion, otherwise just stop to avoid jittering when moving against a wall.
  1042. if (vel_dir_facing_up) {
  1043. velocity = velocity.slide(result.collision_normal);
  1044. } else {
  1045. // Avoid acceleration in slope when falling.
  1046. velocity = up_direction * up_direction.dot(velocity);
  1047. }
  1048. }
  1049. }
  1050. // No sliding on first attempt to keep floor motion stable when possible.
  1051. else {
  1052. motion = result.remainder;
  1053. if (on_ceiling && !slide_on_ceiling && vel_dir_facing_up) {
  1054. velocity = velocity.slide(up_direction);
  1055. motion = motion.slide(up_direction);
  1056. }
  1057. }
  1058. last_travel = result.travel;
  1059. }
  1060. // When you move forward in a downward slope you don’t collide because you will be in the air.
  1061. // This test ensures that constant speed is applied, only if the player is still on the ground after the snap is applied.
  1062. else if (floor_constant_speed && first_slide && _on_floor_if_snapped(p_was_on_floor, vel_dir_facing_up)) {
  1063. can_apply_constant_speed = false;
  1064. sliding_enabled = true;
  1065. Transform2D gt = get_global_transform();
  1066. gt.columns[2] = prev_position;
  1067. set_global_transform(gt);
  1068. Vector2 motion_slide_norm = motion.slide(prev_floor_normal).normalized();
  1069. motion = motion_slide_norm * (motion_slide_up.length());
  1070. collided = true;
  1071. }
  1072. can_apply_constant_speed = !can_apply_constant_speed && !sliding_enabled;
  1073. sliding_enabled = true;
  1074. first_slide = false;
  1075. if (!collided || motion.is_zero_approx()) {
  1076. break;
  1077. }
  1078. }
  1079. _snap_on_floor(p_was_on_floor, vel_dir_facing_up);
  1080. // Scales the horizontal velocity according to the wall slope.
  1081. if (is_on_wall_only() && motion_slide_up.dot(motion_results.get(0).collision_normal) < 0) {
  1082. Vector2 slide_motion = velocity.slide(motion_results.get(0).collision_normal);
  1083. if (motion_slide_up.dot(slide_motion) < 0) {
  1084. velocity = up_direction * up_direction.dot(velocity);
  1085. } else {
  1086. // Keeps the vertical motion from velocity and add the horizontal motion of the projection.
  1087. velocity = up_direction * up_direction.dot(velocity) + slide_motion.slide(up_direction);
  1088. }
  1089. }
  1090. // Reset the gravity accumulation when touching the ground.
  1091. if (on_floor && !vel_dir_facing_up) {
  1092. velocity = velocity.slide(up_direction);
  1093. }
  1094. }
  1095. void CharacterBody2D::_move_and_slide_floating(double p_delta) {
  1096. Vector2 motion = velocity * p_delta;
  1097. platform_rid = RID();
  1098. platform_object_id = ObjectID();
  1099. floor_normal = Vector2();
  1100. platform_velocity = Vector2();
  1101. bool first_slide = true;
  1102. for (int iteration = 0; iteration < max_slides; ++iteration) {
  1103. PhysicsServer2D::MotionParameters parameters(get_global_transform(), motion, margin);
  1104. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  1105. PhysicsServer2D::MotionResult result;
  1106. bool collided = move_and_collide(parameters, result, false, false);
  1107. last_motion = result.travel;
  1108. if (collided) {
  1109. motion_results.push_back(result);
  1110. _set_collision_direction(result);
  1111. if (result.remainder.is_zero_approx()) {
  1112. motion = Vector2();
  1113. break;
  1114. }
  1115. if (wall_min_slide_angle != 0 && result.get_angle(-velocity.normalized()) < wall_min_slide_angle + FLOOR_ANGLE_THRESHOLD) {
  1116. motion = Vector2();
  1117. } else if (first_slide) {
  1118. Vector2 motion_slide_norm = result.remainder.slide(result.collision_normal).normalized();
  1119. motion = motion_slide_norm * (motion.length() - result.travel.length());
  1120. } else {
  1121. motion = result.remainder.slide(result.collision_normal);
  1122. }
  1123. if (motion.dot(velocity) <= 0.0) {
  1124. motion = Vector2();
  1125. }
  1126. }
  1127. if (!collided || motion.is_zero_approx()) {
  1128. break;
  1129. }
  1130. first_slide = false;
  1131. }
  1132. }
  1133. void CharacterBody2D::apply_floor_snap() {
  1134. _apply_floor_snap();
  1135. }
  1136. // Method that avoids the p_wall_as_floor parameter for the public method.
  1137. void CharacterBody2D::_apply_floor_snap(bool p_wall_as_floor) {
  1138. if (on_floor) {
  1139. return;
  1140. }
  1141. // Snap by at least collision margin to keep floor state consistent.
  1142. real_t length = MAX(floor_snap_length, margin);
  1143. PhysicsServer2D::MotionParameters parameters(get_global_transform(), -up_direction * length, margin);
  1144. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  1145. parameters.collide_separation_ray = true;
  1146. PhysicsServer2D::MotionResult result;
  1147. if (move_and_collide(parameters, result, true, false)) {
  1148. if ((result.get_angle(up_direction) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) ||
  1149. (p_wall_as_floor && result.get_angle(-up_direction) > floor_max_angle + FLOOR_ANGLE_THRESHOLD)) {
  1150. on_floor = true;
  1151. floor_normal = result.collision_normal;
  1152. _set_platform_data(result);
  1153. if (floor_stop_on_slope) {
  1154. // move and collide may stray the object a bit because of pre un-stucking,
  1155. // so only ensure that motion happens on floor direction in this case.
  1156. if (result.travel.length() > margin) {
  1157. result.travel = up_direction * up_direction.dot(result.travel);
  1158. } else {
  1159. result.travel = Vector2();
  1160. }
  1161. }
  1162. parameters.from.columns[2] += result.travel;
  1163. set_global_transform(parameters.from);
  1164. }
  1165. }
  1166. }
  1167. void CharacterBody2D::_snap_on_floor(bool p_was_on_floor, bool p_vel_dir_facing_up, bool p_wall_as_floor) {
  1168. if (on_floor || !p_was_on_floor || p_vel_dir_facing_up) {
  1169. return;
  1170. }
  1171. _apply_floor_snap(p_wall_as_floor);
  1172. }
  1173. bool CharacterBody2D::_on_floor_if_snapped(bool p_was_on_floor, bool p_vel_dir_facing_up) {
  1174. if (up_direction == Vector2() || on_floor || !p_was_on_floor || p_vel_dir_facing_up) {
  1175. return false;
  1176. }
  1177. // Snap by at least collision margin to keep floor state consistent.
  1178. real_t length = MAX(floor_snap_length, margin);
  1179. PhysicsServer2D::MotionParameters parameters(get_global_transform(), -up_direction * length, margin);
  1180. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  1181. parameters.collide_separation_ray = true;
  1182. PhysicsServer2D::MotionResult result;
  1183. if (move_and_collide(parameters, result, true, false)) {
  1184. if (result.get_angle(up_direction) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) {
  1185. return true;
  1186. }
  1187. }
  1188. return false;
  1189. }
  1190. void CharacterBody2D::_set_collision_direction(const PhysicsServer2D::MotionResult &p_result) {
  1191. if (motion_mode == MOTION_MODE_GROUNDED && p_result.get_angle(up_direction) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { //floor
  1192. on_floor = true;
  1193. floor_normal = p_result.collision_normal;
  1194. _set_platform_data(p_result);
  1195. } else if (motion_mode == MOTION_MODE_GROUNDED && p_result.get_angle(-up_direction) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { //ceiling
  1196. on_ceiling = true;
  1197. } else {
  1198. on_wall = true;
  1199. wall_normal = p_result.collision_normal;
  1200. // Don't apply wall velocity when the collider is a CharacterBody2D.
  1201. if (Object::cast_to<CharacterBody2D>(ObjectDB::get_instance(p_result.collider_id)) == nullptr) {
  1202. _set_platform_data(p_result);
  1203. }
  1204. }
  1205. }
  1206. void CharacterBody2D::_set_platform_data(const PhysicsServer2D::MotionResult &p_result) {
  1207. platform_rid = p_result.collider;
  1208. platform_object_id = p_result.collider_id;
  1209. platform_velocity = p_result.collider_velocity;
  1210. platform_layer = PhysicsServer2D::get_singleton()->body_get_collision_layer(platform_rid);
  1211. }
  1212. const Vector2 &CharacterBody2D::get_velocity() const {
  1213. return velocity;
  1214. }
  1215. void CharacterBody2D::set_velocity(const Vector2 &p_velocity) {
  1216. velocity = p_velocity;
  1217. }
  1218. bool CharacterBody2D::is_on_floor() const {
  1219. return on_floor;
  1220. }
  1221. bool CharacterBody2D::is_on_floor_only() const {
  1222. return on_floor && !on_wall && !on_ceiling;
  1223. }
  1224. bool CharacterBody2D::is_on_wall() const {
  1225. return on_wall;
  1226. }
  1227. bool CharacterBody2D::is_on_wall_only() const {
  1228. return on_wall && !on_floor && !on_ceiling;
  1229. }
  1230. bool CharacterBody2D::is_on_ceiling() const {
  1231. return on_ceiling;
  1232. }
  1233. bool CharacterBody2D::is_on_ceiling_only() const {
  1234. return on_ceiling && !on_floor && !on_wall;
  1235. }
  1236. const Vector2 &CharacterBody2D::get_floor_normal() const {
  1237. return floor_normal;
  1238. }
  1239. const Vector2 &CharacterBody2D::get_wall_normal() const {
  1240. return wall_normal;
  1241. }
  1242. const Vector2 &CharacterBody2D::get_last_motion() const {
  1243. return last_motion;
  1244. }
  1245. Vector2 CharacterBody2D::get_position_delta() const {
  1246. return get_global_transform().columns[2] - previous_position;
  1247. }
  1248. const Vector2 &CharacterBody2D::get_real_velocity() const {
  1249. return real_velocity;
  1250. }
  1251. real_t CharacterBody2D::get_floor_angle(const Vector2 &p_up_direction) const {
  1252. ERR_FAIL_COND_V(p_up_direction == Vector2(), 0);
  1253. return Math::acos(floor_normal.dot(p_up_direction));
  1254. }
  1255. const Vector2 &CharacterBody2D::get_platform_velocity() const {
  1256. return platform_velocity;
  1257. }
  1258. int CharacterBody2D::get_slide_collision_count() const {
  1259. return motion_results.size();
  1260. }
  1261. PhysicsServer2D::MotionResult CharacterBody2D::get_slide_collision(int p_bounce) const {
  1262. ERR_FAIL_INDEX_V(p_bounce, motion_results.size(), PhysicsServer2D::MotionResult());
  1263. return motion_results[p_bounce];
  1264. }
  1265. Ref<KinematicCollision2D> CharacterBody2D::_get_slide_collision(int p_bounce) {
  1266. ERR_FAIL_INDEX_V(p_bounce, motion_results.size(), Ref<KinematicCollision2D>());
  1267. if (p_bounce >= slide_colliders.size()) {
  1268. slide_colliders.resize(p_bounce + 1);
  1269. }
  1270. // Create a new instance when the cached reference is invalid or still in use in script.
  1271. if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->get_reference_count() > 1) {
  1272. slide_colliders.write[p_bounce].instantiate();
  1273. slide_colliders.write[p_bounce]->owner = this;
  1274. }
  1275. slide_colliders.write[p_bounce]->result = motion_results[p_bounce];
  1276. return slide_colliders[p_bounce];
  1277. }
  1278. Ref<KinematicCollision2D> CharacterBody2D::_get_last_slide_collision() {
  1279. if (motion_results.size() == 0) {
  1280. return Ref<KinematicCollision2D>();
  1281. }
  1282. return _get_slide_collision(motion_results.size() - 1);
  1283. }
  1284. void CharacterBody2D::set_safe_margin(real_t p_margin) {
  1285. margin = p_margin;
  1286. }
  1287. real_t CharacterBody2D::get_safe_margin() const {
  1288. return margin;
  1289. }
  1290. bool CharacterBody2D::is_floor_stop_on_slope_enabled() const {
  1291. return floor_stop_on_slope;
  1292. }
  1293. void CharacterBody2D::set_floor_stop_on_slope_enabled(bool p_enabled) {
  1294. floor_stop_on_slope = p_enabled;
  1295. }
  1296. bool CharacterBody2D::is_floor_constant_speed_enabled() const {
  1297. return floor_constant_speed;
  1298. }
  1299. void CharacterBody2D::set_floor_constant_speed_enabled(bool p_enabled) {
  1300. floor_constant_speed = p_enabled;
  1301. }
  1302. bool CharacterBody2D::is_floor_block_on_wall_enabled() const {
  1303. return floor_block_on_wall;
  1304. }
  1305. void CharacterBody2D::set_floor_block_on_wall_enabled(bool p_enabled) {
  1306. floor_block_on_wall = p_enabled;
  1307. }
  1308. bool CharacterBody2D::is_slide_on_ceiling_enabled() const {
  1309. return slide_on_ceiling;
  1310. }
  1311. void CharacterBody2D::set_slide_on_ceiling_enabled(bool p_enabled) {
  1312. slide_on_ceiling = p_enabled;
  1313. }
  1314. uint32_t CharacterBody2D::get_platform_floor_layers() const {
  1315. return platform_floor_layers;
  1316. }
  1317. void CharacterBody2D::set_platform_floor_layers(uint32_t p_exclude_layers) {
  1318. platform_floor_layers = p_exclude_layers;
  1319. }
  1320. uint32_t CharacterBody2D::get_platform_wall_layers() const {
  1321. return platform_wall_layers;
  1322. }
  1323. void CharacterBody2D::set_platform_wall_layers(uint32_t p_exclude_layers) {
  1324. platform_wall_layers = p_exclude_layers;
  1325. }
  1326. void CharacterBody2D::set_motion_mode(MotionMode p_mode) {
  1327. motion_mode = p_mode;
  1328. }
  1329. CharacterBody2D::MotionMode CharacterBody2D::get_motion_mode() const {
  1330. return motion_mode;
  1331. }
  1332. void CharacterBody2D::set_platform_on_leave(PlatformOnLeave p_on_leave_apply_velocity) {
  1333. platform_on_leave = p_on_leave_apply_velocity;
  1334. }
  1335. CharacterBody2D::PlatformOnLeave CharacterBody2D::get_platform_on_leave() const {
  1336. return platform_on_leave;
  1337. }
  1338. int CharacterBody2D::get_max_slides() const {
  1339. return max_slides;
  1340. }
  1341. void CharacterBody2D::set_max_slides(int p_max_slides) {
  1342. ERR_FAIL_COND(p_max_slides < 1);
  1343. max_slides = p_max_slides;
  1344. }
  1345. real_t CharacterBody2D::get_floor_max_angle() const {
  1346. return floor_max_angle;
  1347. }
  1348. void CharacterBody2D::set_floor_max_angle(real_t p_radians) {
  1349. floor_max_angle = p_radians;
  1350. }
  1351. real_t CharacterBody2D::get_floor_snap_length() {
  1352. return floor_snap_length;
  1353. }
  1354. void CharacterBody2D::set_floor_snap_length(real_t p_floor_snap_length) {
  1355. ERR_FAIL_COND(p_floor_snap_length < 0);
  1356. floor_snap_length = p_floor_snap_length;
  1357. }
  1358. real_t CharacterBody2D::get_wall_min_slide_angle() const {
  1359. return wall_min_slide_angle;
  1360. }
  1361. void CharacterBody2D::set_wall_min_slide_angle(real_t p_radians) {
  1362. wall_min_slide_angle = p_radians;
  1363. }
  1364. const Vector2 &CharacterBody2D::get_up_direction() const {
  1365. return up_direction;
  1366. }
  1367. void CharacterBody2D::set_up_direction(const Vector2 &p_up_direction) {
  1368. ERR_FAIL_COND_MSG(p_up_direction == Vector2(), "up_direction can't be equal to Vector2.ZERO, consider using Floating motion mode instead.");
  1369. up_direction = p_up_direction.normalized();
  1370. }
  1371. void CharacterBody2D::_notification(int p_what) {
  1372. switch (p_what) {
  1373. case NOTIFICATION_ENTER_TREE: {
  1374. // Reset move_and_slide() data.
  1375. on_floor = false;
  1376. platform_rid = RID();
  1377. platform_object_id = ObjectID();
  1378. on_ceiling = false;
  1379. on_wall = false;
  1380. motion_results.clear();
  1381. platform_velocity = Vector2();
  1382. } break;
  1383. }
  1384. }
  1385. void CharacterBody2D::_bind_methods() {
  1386. ClassDB::bind_method(D_METHOD("move_and_slide"), &CharacterBody2D::move_and_slide);
  1387. ClassDB::bind_method(D_METHOD("apply_floor_snap"), &CharacterBody2D::apply_floor_snap);
  1388. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &CharacterBody2D::set_velocity);
  1389. ClassDB::bind_method(D_METHOD("get_velocity"), &CharacterBody2D::get_velocity);
  1390. ClassDB::bind_method(D_METHOD("set_safe_margin", "margin"), &CharacterBody2D::set_safe_margin);
  1391. ClassDB::bind_method(D_METHOD("get_safe_margin"), &CharacterBody2D::get_safe_margin);
  1392. ClassDB::bind_method(D_METHOD("is_floor_stop_on_slope_enabled"), &CharacterBody2D::is_floor_stop_on_slope_enabled);
  1393. ClassDB::bind_method(D_METHOD("set_floor_stop_on_slope_enabled", "enabled"), &CharacterBody2D::set_floor_stop_on_slope_enabled);
  1394. ClassDB::bind_method(D_METHOD("set_floor_constant_speed_enabled", "enabled"), &CharacterBody2D::set_floor_constant_speed_enabled);
  1395. ClassDB::bind_method(D_METHOD("is_floor_constant_speed_enabled"), &CharacterBody2D::is_floor_constant_speed_enabled);
  1396. ClassDB::bind_method(D_METHOD("set_floor_block_on_wall_enabled", "enabled"), &CharacterBody2D::set_floor_block_on_wall_enabled);
  1397. ClassDB::bind_method(D_METHOD("is_floor_block_on_wall_enabled"), &CharacterBody2D::is_floor_block_on_wall_enabled);
  1398. ClassDB::bind_method(D_METHOD("set_slide_on_ceiling_enabled", "enabled"), &CharacterBody2D::set_slide_on_ceiling_enabled);
  1399. ClassDB::bind_method(D_METHOD("is_slide_on_ceiling_enabled"), &CharacterBody2D::is_slide_on_ceiling_enabled);
  1400. ClassDB::bind_method(D_METHOD("set_platform_floor_layers", "exclude_layer"), &CharacterBody2D::set_platform_floor_layers);
  1401. ClassDB::bind_method(D_METHOD("get_platform_floor_layers"), &CharacterBody2D::get_platform_floor_layers);
  1402. ClassDB::bind_method(D_METHOD("set_platform_wall_layers", "exclude_layer"), &CharacterBody2D::set_platform_wall_layers);
  1403. ClassDB::bind_method(D_METHOD("get_platform_wall_layers"), &CharacterBody2D::get_platform_wall_layers);
  1404. ClassDB::bind_method(D_METHOD("get_max_slides"), &CharacterBody2D::get_max_slides);
  1405. ClassDB::bind_method(D_METHOD("set_max_slides", "max_slides"), &CharacterBody2D::set_max_slides);
  1406. ClassDB::bind_method(D_METHOD("get_floor_max_angle"), &CharacterBody2D::get_floor_max_angle);
  1407. ClassDB::bind_method(D_METHOD("set_floor_max_angle", "radians"), &CharacterBody2D::set_floor_max_angle);
  1408. ClassDB::bind_method(D_METHOD("get_floor_snap_length"), &CharacterBody2D::get_floor_snap_length);
  1409. ClassDB::bind_method(D_METHOD("set_floor_snap_length", "floor_snap_length"), &CharacterBody2D::set_floor_snap_length);
  1410. ClassDB::bind_method(D_METHOD("get_wall_min_slide_angle"), &CharacterBody2D::get_wall_min_slide_angle);
  1411. ClassDB::bind_method(D_METHOD("set_wall_min_slide_angle", "radians"), &CharacterBody2D::set_wall_min_slide_angle);
  1412. ClassDB::bind_method(D_METHOD("get_up_direction"), &CharacterBody2D::get_up_direction);
  1413. ClassDB::bind_method(D_METHOD("set_up_direction", "up_direction"), &CharacterBody2D::set_up_direction);
  1414. ClassDB::bind_method(D_METHOD("set_motion_mode", "mode"), &CharacterBody2D::set_motion_mode);
  1415. ClassDB::bind_method(D_METHOD("get_motion_mode"), &CharacterBody2D::get_motion_mode);
  1416. ClassDB::bind_method(D_METHOD("set_platform_on_leave", "on_leave_apply_velocity"), &CharacterBody2D::set_platform_on_leave);
  1417. ClassDB::bind_method(D_METHOD("get_platform_on_leave"), &CharacterBody2D::get_platform_on_leave);
  1418. ClassDB::bind_method(D_METHOD("is_on_floor"), &CharacterBody2D::is_on_floor);
  1419. ClassDB::bind_method(D_METHOD("is_on_floor_only"), &CharacterBody2D::is_on_floor_only);
  1420. ClassDB::bind_method(D_METHOD("is_on_ceiling"), &CharacterBody2D::is_on_ceiling);
  1421. ClassDB::bind_method(D_METHOD("is_on_ceiling_only"), &CharacterBody2D::is_on_ceiling_only);
  1422. ClassDB::bind_method(D_METHOD("is_on_wall"), &CharacterBody2D::is_on_wall);
  1423. ClassDB::bind_method(D_METHOD("is_on_wall_only"), &CharacterBody2D::is_on_wall_only);
  1424. ClassDB::bind_method(D_METHOD("get_floor_normal"), &CharacterBody2D::get_floor_normal);
  1425. ClassDB::bind_method(D_METHOD("get_wall_normal"), &CharacterBody2D::get_wall_normal);
  1426. ClassDB::bind_method(D_METHOD("get_last_motion"), &CharacterBody2D::get_last_motion);
  1427. ClassDB::bind_method(D_METHOD("get_position_delta"), &CharacterBody2D::get_position_delta);
  1428. ClassDB::bind_method(D_METHOD("get_real_velocity"), &CharacterBody2D::get_real_velocity);
  1429. ClassDB::bind_method(D_METHOD("get_floor_angle", "up_direction"), &CharacterBody2D::get_floor_angle, DEFVAL(Vector2(0.0, -1.0)));
  1430. ClassDB::bind_method(D_METHOD("get_platform_velocity"), &CharacterBody2D::get_platform_velocity);
  1431. ClassDB::bind_method(D_METHOD("get_slide_collision_count"), &CharacterBody2D::get_slide_collision_count);
  1432. ClassDB::bind_method(D_METHOD("get_slide_collision", "slide_idx"), &CharacterBody2D::_get_slide_collision);
  1433. ClassDB::bind_method(D_METHOD("get_last_slide_collision"), &CharacterBody2D::_get_last_slide_collision);
  1434. ADD_PROPERTY(PropertyInfo(Variant::INT, "motion_mode", PROPERTY_HINT_ENUM, "Grounded,Floating", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_motion_mode", "get_motion_mode");
  1435. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "up_direction"), "set_up_direction", "get_up_direction");
  1436. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "suffix:px/s", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
  1437. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "slide_on_ceiling"), "set_slide_on_ceiling_enabled", "is_slide_on_ceiling_enabled");
  1438. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_slides", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_max_slides", "get_max_slides");
  1439. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wall_min_slide_angle", PROPERTY_HINT_RANGE, "0,180,0.1,radians_as_degrees", PROPERTY_USAGE_DEFAULT), "set_wall_min_slide_angle", "get_wall_min_slide_angle");
  1440. ADD_GROUP("Floor", "floor_");
  1441. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_stop_on_slope"), "set_floor_stop_on_slope_enabled", "is_floor_stop_on_slope_enabled");
  1442. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_constant_speed"), "set_floor_constant_speed_enabled", "is_floor_constant_speed_enabled");
  1443. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_block_on_wall"), "set_floor_block_on_wall_enabled", "is_floor_block_on_wall_enabled");
  1444. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "floor_max_angle", PROPERTY_HINT_RANGE, "0,180,0.1,radians_as_degrees"), "set_floor_max_angle", "get_floor_max_angle");
  1445. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "floor_snap_length", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater,suffix:px"), "set_floor_snap_length", "get_floor_snap_length");
  1446. ADD_GROUP("Moving Platform", "platform_");
  1447. ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_on_leave", PROPERTY_HINT_ENUM, "Add Velocity,Add Upward Velocity,Do Nothing", PROPERTY_USAGE_DEFAULT), "set_platform_on_leave", "get_platform_on_leave");
  1448. ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_floor_layers", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_platform_floor_layers", "get_platform_floor_layers");
  1449. ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_wall_layers", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_platform_wall_layers", "get_platform_wall_layers");
  1450. ADD_GROUP("Collision", "");
  1451. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "safe_margin", PROPERTY_HINT_RANGE, "0.001,256,0.001,suffix:px"), "set_safe_margin", "get_safe_margin");
  1452. BIND_ENUM_CONSTANT(MOTION_MODE_GROUNDED);
  1453. BIND_ENUM_CONSTANT(MOTION_MODE_FLOATING);
  1454. BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_ADD_VELOCITY);
  1455. BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY);
  1456. BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_DO_NOTHING);
  1457. }
  1458. void CharacterBody2D::_validate_property(PropertyInfo &p_property) const {
  1459. if (motion_mode == MOTION_MODE_FLOATING) {
  1460. if (p_property.name.begins_with("floor_") || p_property.name == "up_direction" || p_property.name == "slide_on_ceiling") {
  1461. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1462. }
  1463. } else {
  1464. if (p_property.name == "wall_min_slide_angle") {
  1465. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1466. }
  1467. }
  1468. }
  1469. CharacterBody2D::CharacterBody2D() :
  1470. PhysicsBody2D(PhysicsServer2D::BODY_MODE_KINEMATIC) {
  1471. }
  1472. CharacterBody2D::~CharacterBody2D() {
  1473. for (int i = 0; i < slide_colliders.size(); i++) {
  1474. if (slide_colliders[i].is_valid()) {
  1475. slide_colliders.write[i]->owner = nullptr;
  1476. }
  1477. }
  1478. }
  1479. ////////////////////////
  1480. Vector2 KinematicCollision2D::get_position() const {
  1481. return result.collision_point;
  1482. }
  1483. Vector2 KinematicCollision2D::get_normal() const {
  1484. return result.collision_normal;
  1485. }
  1486. Vector2 KinematicCollision2D::get_travel() const {
  1487. return result.travel;
  1488. }
  1489. Vector2 KinematicCollision2D::get_remainder() const {
  1490. return result.remainder;
  1491. }
  1492. real_t KinematicCollision2D::get_angle(const Vector2 &p_up_direction) const {
  1493. ERR_FAIL_COND_V(p_up_direction == Vector2(), 0);
  1494. return result.get_angle(p_up_direction);
  1495. }
  1496. real_t KinematicCollision2D::get_depth() const {
  1497. return result.collision_depth;
  1498. }
  1499. Object *KinematicCollision2D::get_local_shape() const {
  1500. if (!owner) {
  1501. return nullptr;
  1502. }
  1503. uint32_t ownerid = owner->shape_find_owner(result.collision_local_shape);
  1504. return owner->shape_owner_get_owner(ownerid);
  1505. }
  1506. Object *KinematicCollision2D::get_collider() const {
  1507. if (result.collider_id.is_valid()) {
  1508. return ObjectDB::get_instance(result.collider_id);
  1509. }
  1510. return nullptr;
  1511. }
  1512. ObjectID KinematicCollision2D::get_collider_id() const {
  1513. return result.collider_id;
  1514. }
  1515. RID KinematicCollision2D::get_collider_rid() const {
  1516. return result.collider;
  1517. }
  1518. Object *KinematicCollision2D::get_collider_shape() const {
  1519. Object *collider = get_collider();
  1520. if (collider) {
  1521. CollisionObject2D *obj2d = Object::cast_to<CollisionObject2D>(collider);
  1522. if (obj2d) {
  1523. uint32_t ownerid = obj2d->shape_find_owner(result.collider_shape);
  1524. return obj2d->shape_owner_get_owner(ownerid);
  1525. }
  1526. }
  1527. return nullptr;
  1528. }
  1529. int KinematicCollision2D::get_collider_shape_index() const {
  1530. return result.collider_shape;
  1531. }
  1532. Vector2 KinematicCollision2D::get_collider_velocity() const {
  1533. return result.collider_velocity;
  1534. }
  1535. void KinematicCollision2D::_bind_methods() {
  1536. ClassDB::bind_method(D_METHOD("get_position"), &KinematicCollision2D::get_position);
  1537. ClassDB::bind_method(D_METHOD("get_normal"), &KinematicCollision2D::get_normal);
  1538. ClassDB::bind_method(D_METHOD("get_travel"), &KinematicCollision2D::get_travel);
  1539. ClassDB::bind_method(D_METHOD("get_remainder"), &KinematicCollision2D::get_remainder);
  1540. ClassDB::bind_method(D_METHOD("get_angle", "up_direction"), &KinematicCollision2D::get_angle, DEFVAL(Vector2(0.0, -1.0)));
  1541. ClassDB::bind_method(D_METHOD("get_depth"), &KinematicCollision2D::get_depth);
  1542. ClassDB::bind_method(D_METHOD("get_local_shape"), &KinematicCollision2D::get_local_shape);
  1543. ClassDB::bind_method(D_METHOD("get_collider"), &KinematicCollision2D::get_collider);
  1544. ClassDB::bind_method(D_METHOD("get_collider_id"), &KinematicCollision2D::get_collider_id);
  1545. ClassDB::bind_method(D_METHOD("get_collider_rid"), &KinematicCollision2D::get_collider_rid);
  1546. ClassDB::bind_method(D_METHOD("get_collider_shape"), &KinematicCollision2D::get_collider_shape);
  1547. ClassDB::bind_method(D_METHOD("get_collider_shape_index"), &KinematicCollision2D::get_collider_shape_index);
  1548. ClassDB::bind_method(D_METHOD("get_collider_velocity"), &KinematicCollision2D::get_collider_velocity);
  1549. }