character_body_2d.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /**************************************************************************/
  2. /* character_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 "character_body_2d.h"
  31. // So, if you pass 45 as limit, avoid numerical precision errors when angle is 45.
  32. #define FLOOR_ANGLE_THRESHOLD 0.01
  33. bool CharacterBody2D::move_and_slide() {
  34. // Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky.
  35. double delta = Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time();
  36. Vector2 current_platform_velocity = platform_velocity;
  37. Transform2D gt = get_global_transform();
  38. previous_position = gt.columns[2];
  39. if ((on_floor || on_wall) && platform_rid.is_valid()) {
  40. bool excluded = false;
  41. if (on_floor) {
  42. excluded = (platform_floor_layers & platform_layer) == 0;
  43. } else if (on_wall) {
  44. excluded = (platform_wall_layers & platform_layer) == 0;
  45. }
  46. if (!excluded) {
  47. //this approach makes sure there is less delay between the actual body velocity and the one we saved
  48. PhysicsDirectBodyState2D *bs = PhysicsServer2D::get_singleton()->body_get_direct_state(platform_rid);
  49. if (bs) {
  50. Vector2 local_position = gt.columns[2] - bs->get_transform().columns[2];
  51. current_platform_velocity = bs->get_velocity_at_local_position(local_position);
  52. } else {
  53. // Body is removed or destroyed, invalidate floor.
  54. current_platform_velocity = Vector2();
  55. platform_rid = RID();
  56. }
  57. } else {
  58. current_platform_velocity = Vector2();
  59. }
  60. }
  61. motion_results.clear();
  62. last_motion = Vector2();
  63. bool was_on_floor = on_floor;
  64. on_floor = false;
  65. on_ceiling = false;
  66. on_wall = false;
  67. if (!current_platform_velocity.is_zero_approx()) {
  68. PhysicsServer2D::MotionParameters parameters(get_global_transform(), current_platform_velocity * delta, margin);
  69. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  70. parameters.exclude_bodies.insert(platform_rid);
  71. if (platform_object_id.is_valid()) {
  72. parameters.exclude_objects.insert(platform_object_id);
  73. }
  74. PhysicsServer2D::MotionResult floor_result;
  75. if (move_and_collide(parameters, floor_result, false, false)) {
  76. motion_results.push_back(floor_result);
  77. _set_collision_direction(floor_result);
  78. }
  79. }
  80. if (motion_mode == MOTION_MODE_GROUNDED) {
  81. _move_and_slide_grounded(delta, was_on_floor);
  82. } else {
  83. _move_and_slide_floating(delta);
  84. }
  85. // Compute real velocity.
  86. real_velocity = get_position_delta() / delta;
  87. if (platform_on_leave != PLATFORM_ON_LEAVE_DO_NOTHING) {
  88. // Add last platform velocity when just left a moving platform.
  89. if (!on_floor && !on_wall) {
  90. if (platform_on_leave == PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY && current_platform_velocity.dot(up_direction) < 0) {
  91. current_platform_velocity = current_platform_velocity.slide(up_direction);
  92. }
  93. velocity += current_platform_velocity;
  94. }
  95. }
  96. return motion_results.size() > 0;
  97. }
  98. void CharacterBody2D::_move_and_slide_grounded(double p_delta, bool p_was_on_floor) {
  99. Vector2 motion = velocity * p_delta;
  100. Vector2 motion_slide_up = motion.slide(up_direction);
  101. Vector2 prev_floor_normal = floor_normal;
  102. platform_rid = RID();
  103. platform_object_id = ObjectID();
  104. floor_normal = Vector2();
  105. platform_velocity = Vector2();
  106. // No sliding on first attempt to keep floor motion stable when possible,
  107. // When stop on slope is enabled or when there is no up direction.
  108. bool sliding_enabled = !floor_stop_on_slope;
  109. // Constant speed can be applied only the first time sliding is enabled.
  110. bool can_apply_constant_speed = sliding_enabled;
  111. // If the platform's ceiling push down the body.
  112. bool apply_ceiling_velocity = false;
  113. bool first_slide = true;
  114. bool vel_dir_facing_up = velocity.dot(up_direction) > 0;
  115. Vector2 last_travel;
  116. for (int iteration = 0; iteration < max_slides; ++iteration) {
  117. PhysicsServer2D::MotionParameters parameters(get_global_transform(), motion, margin);
  118. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  119. Vector2 prev_position = parameters.from.columns[2];
  120. PhysicsServer2D::MotionResult result;
  121. bool collided = move_and_collide(parameters, result, false, !sliding_enabled);
  122. last_motion = result.travel;
  123. if (collided) {
  124. motion_results.push_back(result);
  125. _set_collision_direction(result);
  126. // If we hit a ceiling platform, we set the vertical velocity to at least the platform one.
  127. if (on_ceiling && result.collider_velocity != Vector2() && result.collider_velocity.dot(up_direction) < 0) {
  128. // If ceiling sliding is on, only apply when the ceiling is flat or when the motion is upward.
  129. if (!slide_on_ceiling || motion.dot(up_direction) < 0 || (result.collision_normal + up_direction).length() < 0.01) {
  130. apply_ceiling_velocity = true;
  131. Vector2 ceiling_vertical_velocity = up_direction * up_direction.dot(result.collider_velocity);
  132. Vector2 motion_vertical_velocity = up_direction * up_direction.dot(velocity);
  133. if (motion_vertical_velocity.dot(up_direction) > 0 || ceiling_vertical_velocity.length_squared() > motion_vertical_velocity.length_squared()) {
  134. velocity = ceiling_vertical_velocity + velocity.slide(up_direction);
  135. }
  136. }
  137. }
  138. if (on_floor && floor_stop_on_slope && (velocity.normalized() + up_direction).length() < 0.01) {
  139. Transform2D gt = get_global_transform();
  140. if (result.travel.length() <= margin + CMP_EPSILON) {
  141. gt.columns[2] -= result.travel;
  142. }
  143. set_global_transform(gt);
  144. velocity = Vector2();
  145. last_motion = Vector2();
  146. motion = Vector2();
  147. break;
  148. }
  149. if (result.remainder.is_zero_approx()) {
  150. motion = Vector2();
  151. break;
  152. }
  153. // Move on floor only checks.
  154. if (floor_block_on_wall && on_wall && motion_slide_up.dot(result.collision_normal) <= 0) {
  155. // Avoid to move forward on a wall if floor_block_on_wall is true.
  156. if (p_was_on_floor && !on_floor && !vel_dir_facing_up) {
  157. // If the movement is large the body can be prevented from reaching the walls.
  158. if (result.travel.length() <= margin + CMP_EPSILON) {
  159. // Cancels the motion.
  160. Transform2D gt = get_global_transform();
  161. gt.columns[2] -= result.travel;
  162. set_global_transform(gt);
  163. }
  164. // Determines if you are on the ground.
  165. _snap_on_floor(true, false, true);
  166. velocity = Vector2();
  167. last_motion = Vector2();
  168. motion = Vector2();
  169. break;
  170. }
  171. // Prevents the body from being able to climb a slope when it moves forward against the wall.
  172. else if (!on_floor) {
  173. motion = up_direction * up_direction.dot(result.remainder);
  174. motion = motion.slide(result.collision_normal);
  175. } else {
  176. motion = result.remainder;
  177. }
  178. }
  179. // Constant Speed when the slope is upward.
  180. else if (floor_constant_speed && is_on_floor_only() && can_apply_constant_speed && p_was_on_floor && motion.dot(result.collision_normal) < 0) {
  181. can_apply_constant_speed = false;
  182. Vector2 motion_slide_norm = result.remainder.slide(result.collision_normal).normalized();
  183. motion = motion_slide_norm * (motion_slide_up.length() - result.travel.slide(up_direction).length() - last_travel.slide(up_direction).length());
  184. }
  185. // Regular sliding, the last part of the test handle the case when you don't want to slide on the ceiling.
  186. else if ((sliding_enabled || !on_floor) && (!on_ceiling || slide_on_ceiling || !vel_dir_facing_up) && !apply_ceiling_velocity) {
  187. Vector2 slide_motion = result.remainder.slide(result.collision_normal);
  188. if (slide_motion.dot(velocity) > 0.0) {
  189. motion = slide_motion;
  190. } else {
  191. motion = Vector2();
  192. }
  193. if (slide_on_ceiling && on_ceiling) {
  194. // Apply slide only in the direction of the input motion, otherwise just stop to avoid jittering when moving against a wall.
  195. if (vel_dir_facing_up) {
  196. velocity = velocity.slide(result.collision_normal);
  197. } else {
  198. // Avoid acceleration in slope when falling.
  199. velocity = up_direction * up_direction.dot(velocity);
  200. }
  201. }
  202. }
  203. // No sliding on first attempt to keep floor motion stable when possible.
  204. else {
  205. motion = result.remainder;
  206. if (on_ceiling && !slide_on_ceiling && vel_dir_facing_up) {
  207. velocity = velocity.slide(up_direction);
  208. motion = motion.slide(up_direction);
  209. }
  210. }
  211. last_travel = result.travel;
  212. }
  213. // When you move forward in a downward slope you don’t collide because you will be in the air.
  214. // This test ensures that constant speed is applied, only if the player is still on the ground after the snap is applied.
  215. else if (floor_constant_speed && first_slide && _on_floor_if_snapped(p_was_on_floor, vel_dir_facing_up)) {
  216. can_apply_constant_speed = false;
  217. sliding_enabled = true;
  218. Transform2D gt = get_global_transform();
  219. gt.columns[2] = prev_position;
  220. set_global_transform(gt);
  221. Vector2 motion_slide_norm = motion.slide(prev_floor_normal).normalized();
  222. motion = motion_slide_norm * (motion_slide_up.length());
  223. collided = true;
  224. }
  225. can_apply_constant_speed = !can_apply_constant_speed && !sliding_enabled;
  226. sliding_enabled = true;
  227. first_slide = false;
  228. if (!collided || motion.is_zero_approx()) {
  229. break;
  230. }
  231. }
  232. _snap_on_floor(p_was_on_floor, vel_dir_facing_up);
  233. // Scales the horizontal velocity according to the wall slope.
  234. if (is_on_wall_only() && motion_slide_up.dot(motion_results.get(0).collision_normal) < 0) {
  235. Vector2 slide_motion = velocity.slide(motion_results.get(0).collision_normal);
  236. if (motion_slide_up.dot(slide_motion) < 0) {
  237. velocity = up_direction * up_direction.dot(velocity);
  238. } else {
  239. // Keeps the vertical motion from velocity and add the horizontal motion of the projection.
  240. velocity = up_direction * up_direction.dot(velocity) + slide_motion.slide(up_direction);
  241. }
  242. }
  243. // Reset the gravity accumulation when touching the ground.
  244. if (on_floor && !vel_dir_facing_up) {
  245. velocity = velocity.slide(up_direction);
  246. }
  247. }
  248. void CharacterBody2D::_move_and_slide_floating(double p_delta) {
  249. Vector2 motion = velocity * p_delta;
  250. platform_rid = RID();
  251. platform_object_id = ObjectID();
  252. floor_normal = Vector2();
  253. platform_velocity = Vector2();
  254. bool first_slide = true;
  255. for (int iteration = 0; iteration < max_slides; ++iteration) {
  256. PhysicsServer2D::MotionParameters parameters(get_global_transform(), motion, margin);
  257. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  258. PhysicsServer2D::MotionResult result;
  259. bool collided = move_and_collide(parameters, result, false, false);
  260. last_motion = result.travel;
  261. if (collided) {
  262. motion_results.push_back(result);
  263. _set_collision_direction(result);
  264. if (result.remainder.is_zero_approx()) {
  265. motion = Vector2();
  266. break;
  267. }
  268. if (wall_min_slide_angle != 0 && result.get_angle(-velocity.normalized()) < wall_min_slide_angle + FLOOR_ANGLE_THRESHOLD) {
  269. motion = Vector2();
  270. } else if (first_slide) {
  271. Vector2 motion_slide_norm = result.remainder.slide(result.collision_normal).normalized();
  272. motion = motion_slide_norm * (motion.length() - result.travel.length());
  273. } else {
  274. motion = result.remainder.slide(result.collision_normal);
  275. }
  276. if (motion.dot(velocity) <= 0.0) {
  277. motion = Vector2();
  278. }
  279. }
  280. if (!collided || motion.is_zero_approx()) {
  281. break;
  282. }
  283. first_slide = false;
  284. }
  285. }
  286. void CharacterBody2D::apply_floor_snap() {
  287. _apply_floor_snap();
  288. }
  289. // Method that avoids the p_wall_as_floor parameter for the public method.
  290. void CharacterBody2D::_apply_floor_snap(bool p_wall_as_floor) {
  291. if (on_floor) {
  292. return;
  293. }
  294. // Snap by at least collision margin to keep floor state consistent.
  295. real_t length = MAX(floor_snap_length, margin);
  296. PhysicsServer2D::MotionParameters parameters(get_global_transform(), -up_direction * length, margin);
  297. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  298. parameters.collide_separation_ray = true;
  299. PhysicsServer2D::MotionResult result;
  300. if (move_and_collide(parameters, result, true, false)) {
  301. if ((result.get_angle(up_direction) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) ||
  302. (p_wall_as_floor && result.get_angle(-up_direction) > floor_max_angle + FLOOR_ANGLE_THRESHOLD)) {
  303. on_floor = true;
  304. floor_normal = result.collision_normal;
  305. _set_platform_data(result);
  306. // Ensure that we only move the body along the up axis, because
  307. // move_and_collide may stray the object a bit when getting it unstuck.
  308. // Canceling this motion should not affect move_and_slide, as previous
  309. // calls to move_and_collide already took care of freeing the body.
  310. if (result.travel.length() > margin) {
  311. result.travel = up_direction * up_direction.dot(result.travel);
  312. } else {
  313. result.travel = Vector2();
  314. }
  315. parameters.from.columns[2] += result.travel;
  316. set_global_transform(parameters.from);
  317. }
  318. }
  319. }
  320. void CharacterBody2D::_snap_on_floor(bool p_was_on_floor, bool p_vel_dir_facing_up, bool p_wall_as_floor) {
  321. if (on_floor || !p_was_on_floor || p_vel_dir_facing_up) {
  322. return;
  323. }
  324. _apply_floor_snap(p_wall_as_floor);
  325. }
  326. bool CharacterBody2D::_on_floor_if_snapped(bool p_was_on_floor, bool p_vel_dir_facing_up) {
  327. if (up_direction == Vector2() || on_floor || !p_was_on_floor || p_vel_dir_facing_up) {
  328. return false;
  329. }
  330. // Snap by at least collision margin to keep floor state consistent.
  331. real_t length = MAX(floor_snap_length, margin);
  332. PhysicsServer2D::MotionParameters parameters(get_global_transform(), -up_direction * length, margin);
  333. parameters.recovery_as_collision = true; // Also report collisions generated only from recovery.
  334. parameters.collide_separation_ray = true;
  335. PhysicsServer2D::MotionResult result;
  336. if (move_and_collide(parameters, result, true, false)) {
  337. if (result.get_angle(up_direction) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) {
  338. return true;
  339. }
  340. }
  341. return false;
  342. }
  343. void CharacterBody2D::_set_collision_direction(const PhysicsServer2D::MotionResult &p_result) {
  344. if (motion_mode == MOTION_MODE_GROUNDED && p_result.get_angle(up_direction) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { //floor
  345. on_floor = true;
  346. floor_normal = p_result.collision_normal;
  347. _set_platform_data(p_result);
  348. } else if (motion_mode == MOTION_MODE_GROUNDED && p_result.get_angle(-up_direction) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { //ceiling
  349. on_ceiling = true;
  350. } else {
  351. on_wall = true;
  352. wall_normal = p_result.collision_normal;
  353. // Don't apply wall velocity when the collider is a CharacterBody2D.
  354. if (ObjectDB::get_instance<CharacterBody2D>(p_result.collider_id) == nullptr) {
  355. _set_platform_data(p_result);
  356. }
  357. }
  358. }
  359. void CharacterBody2D::_set_platform_data(const PhysicsServer2D::MotionResult &p_result) {
  360. platform_rid = p_result.collider;
  361. platform_object_id = p_result.collider_id;
  362. platform_velocity = p_result.collider_velocity;
  363. platform_layer = PhysicsServer2D::get_singleton()->body_get_collision_layer(platform_rid);
  364. }
  365. const Vector2 &CharacterBody2D::get_velocity() const {
  366. return velocity;
  367. }
  368. void CharacterBody2D::set_velocity(const Vector2 &p_velocity) {
  369. velocity = p_velocity;
  370. }
  371. bool CharacterBody2D::is_on_floor() const {
  372. return on_floor;
  373. }
  374. bool CharacterBody2D::is_on_floor_only() const {
  375. return on_floor && !on_wall && !on_ceiling;
  376. }
  377. bool CharacterBody2D::is_on_wall() const {
  378. return on_wall;
  379. }
  380. bool CharacterBody2D::is_on_wall_only() const {
  381. return on_wall && !on_floor && !on_ceiling;
  382. }
  383. bool CharacterBody2D::is_on_ceiling() const {
  384. return on_ceiling;
  385. }
  386. bool CharacterBody2D::is_on_ceiling_only() const {
  387. return on_ceiling && !on_floor && !on_wall;
  388. }
  389. const Vector2 &CharacterBody2D::get_floor_normal() const {
  390. return floor_normal;
  391. }
  392. const Vector2 &CharacterBody2D::get_wall_normal() const {
  393. return wall_normal;
  394. }
  395. const Vector2 &CharacterBody2D::get_last_motion() const {
  396. return last_motion;
  397. }
  398. Vector2 CharacterBody2D::get_position_delta() const {
  399. return get_global_transform().columns[2] - previous_position;
  400. }
  401. const Vector2 &CharacterBody2D::get_real_velocity() const {
  402. return real_velocity;
  403. }
  404. real_t CharacterBody2D::get_floor_angle(const Vector2 &p_up_direction) const {
  405. ERR_FAIL_COND_V(p_up_direction == Vector2(), 0);
  406. return Math::acos(floor_normal.dot(p_up_direction));
  407. }
  408. const Vector2 &CharacterBody2D::get_platform_velocity() const {
  409. return platform_velocity;
  410. }
  411. int CharacterBody2D::get_slide_collision_count() const {
  412. return motion_results.size();
  413. }
  414. PhysicsServer2D::MotionResult CharacterBody2D::get_slide_collision(int p_bounce) const {
  415. ERR_FAIL_INDEX_V(p_bounce, motion_results.size(), PhysicsServer2D::MotionResult());
  416. return motion_results[p_bounce];
  417. }
  418. Ref<KinematicCollision2D> CharacterBody2D::_get_slide_collision(int p_bounce) {
  419. ERR_FAIL_INDEX_V(p_bounce, motion_results.size(), Ref<KinematicCollision2D>());
  420. if (p_bounce >= slide_colliders.size()) {
  421. slide_colliders.resize(p_bounce + 1);
  422. }
  423. // Create a new instance when the cached reference is invalid or still in use in script.
  424. if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->get_reference_count() > 1) {
  425. slide_colliders.write[p_bounce].instantiate();
  426. slide_colliders.write[p_bounce]->owner_id = get_instance_id();
  427. }
  428. slide_colliders.write[p_bounce]->result = motion_results[p_bounce];
  429. return slide_colliders[p_bounce];
  430. }
  431. Ref<KinematicCollision2D> CharacterBody2D::_get_last_slide_collision() {
  432. if (motion_results.is_empty()) {
  433. return Ref<KinematicCollision2D>();
  434. }
  435. return _get_slide_collision(motion_results.size() - 1);
  436. }
  437. void CharacterBody2D::set_safe_margin(real_t p_margin) {
  438. margin = p_margin;
  439. }
  440. real_t CharacterBody2D::get_safe_margin() const {
  441. return margin;
  442. }
  443. bool CharacterBody2D::is_floor_stop_on_slope_enabled() const {
  444. return floor_stop_on_slope;
  445. }
  446. void CharacterBody2D::set_floor_stop_on_slope_enabled(bool p_enabled) {
  447. floor_stop_on_slope = p_enabled;
  448. }
  449. bool CharacterBody2D::is_floor_constant_speed_enabled() const {
  450. return floor_constant_speed;
  451. }
  452. void CharacterBody2D::set_floor_constant_speed_enabled(bool p_enabled) {
  453. floor_constant_speed = p_enabled;
  454. }
  455. bool CharacterBody2D::is_floor_block_on_wall_enabled() const {
  456. return floor_block_on_wall;
  457. }
  458. void CharacterBody2D::set_floor_block_on_wall_enabled(bool p_enabled) {
  459. floor_block_on_wall = p_enabled;
  460. }
  461. bool CharacterBody2D::is_slide_on_ceiling_enabled() const {
  462. return slide_on_ceiling;
  463. }
  464. void CharacterBody2D::set_slide_on_ceiling_enabled(bool p_enabled) {
  465. slide_on_ceiling = p_enabled;
  466. }
  467. uint32_t CharacterBody2D::get_platform_floor_layers() const {
  468. return platform_floor_layers;
  469. }
  470. void CharacterBody2D::set_platform_floor_layers(uint32_t p_exclude_layers) {
  471. platform_floor_layers = p_exclude_layers;
  472. }
  473. uint32_t CharacterBody2D::get_platform_wall_layers() const {
  474. return platform_wall_layers;
  475. }
  476. void CharacterBody2D::set_platform_wall_layers(uint32_t p_exclude_layers) {
  477. platform_wall_layers = p_exclude_layers;
  478. }
  479. void CharacterBody2D::set_motion_mode(MotionMode p_mode) {
  480. motion_mode = p_mode;
  481. }
  482. CharacterBody2D::MotionMode CharacterBody2D::get_motion_mode() const {
  483. return motion_mode;
  484. }
  485. void CharacterBody2D::set_platform_on_leave(PlatformOnLeave p_on_leave_apply_velocity) {
  486. platform_on_leave = p_on_leave_apply_velocity;
  487. }
  488. CharacterBody2D::PlatformOnLeave CharacterBody2D::get_platform_on_leave() const {
  489. return platform_on_leave;
  490. }
  491. int CharacterBody2D::get_max_slides() const {
  492. return max_slides;
  493. }
  494. void CharacterBody2D::set_max_slides(int p_max_slides) {
  495. ERR_FAIL_COND(p_max_slides < 1);
  496. max_slides = p_max_slides;
  497. }
  498. real_t CharacterBody2D::get_floor_max_angle() const {
  499. return floor_max_angle;
  500. }
  501. void CharacterBody2D::set_floor_max_angle(real_t p_radians) {
  502. floor_max_angle = p_radians;
  503. }
  504. real_t CharacterBody2D::get_floor_snap_length() {
  505. return floor_snap_length;
  506. }
  507. void CharacterBody2D::set_floor_snap_length(real_t p_floor_snap_length) {
  508. ERR_FAIL_COND(p_floor_snap_length < 0);
  509. floor_snap_length = p_floor_snap_length;
  510. }
  511. real_t CharacterBody2D::get_wall_min_slide_angle() const {
  512. return wall_min_slide_angle;
  513. }
  514. void CharacterBody2D::set_wall_min_slide_angle(real_t p_radians) {
  515. wall_min_slide_angle = p_radians;
  516. }
  517. const Vector2 &CharacterBody2D::get_up_direction() const {
  518. return up_direction;
  519. }
  520. void CharacterBody2D::set_up_direction(const Vector2 &p_up_direction) {
  521. ERR_FAIL_COND_MSG(p_up_direction == Vector2(), "up_direction can't be equal to Vector2.ZERO, consider using Floating motion mode instead.");
  522. up_direction = p_up_direction.normalized();
  523. }
  524. void CharacterBody2D::_notification(int p_what) {
  525. switch (p_what) {
  526. case NOTIFICATION_ENTER_TREE: {
  527. // Reset move_and_slide() data.
  528. on_floor = false;
  529. platform_rid = RID();
  530. platform_object_id = ObjectID();
  531. on_ceiling = false;
  532. on_wall = false;
  533. motion_results.clear();
  534. platform_velocity = Vector2();
  535. } break;
  536. }
  537. }
  538. void CharacterBody2D::_validate_property(PropertyInfo &p_property) const {
  539. if (motion_mode == MOTION_MODE_FLOATING) {
  540. if (p_property.name.begins_with("floor_") || p_property.name == "up_direction" || p_property.name == "slide_on_ceiling") {
  541. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  542. }
  543. } else {
  544. if (p_property.name == "wall_min_slide_angle") {
  545. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  546. }
  547. }
  548. }
  549. void CharacterBody2D::_bind_methods() {
  550. ClassDB::bind_method(D_METHOD("move_and_slide"), &CharacterBody2D::move_and_slide);
  551. ClassDB::bind_method(D_METHOD("apply_floor_snap"), &CharacterBody2D::apply_floor_snap);
  552. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &CharacterBody2D::set_velocity);
  553. ClassDB::bind_method(D_METHOD("get_velocity"), &CharacterBody2D::get_velocity);
  554. ClassDB::bind_method(D_METHOD("set_safe_margin", "margin"), &CharacterBody2D::set_safe_margin);
  555. ClassDB::bind_method(D_METHOD("get_safe_margin"), &CharacterBody2D::get_safe_margin);
  556. ClassDB::bind_method(D_METHOD("is_floor_stop_on_slope_enabled"), &CharacterBody2D::is_floor_stop_on_slope_enabled);
  557. ClassDB::bind_method(D_METHOD("set_floor_stop_on_slope_enabled", "enabled"), &CharacterBody2D::set_floor_stop_on_slope_enabled);
  558. ClassDB::bind_method(D_METHOD("set_floor_constant_speed_enabled", "enabled"), &CharacterBody2D::set_floor_constant_speed_enabled);
  559. ClassDB::bind_method(D_METHOD("is_floor_constant_speed_enabled"), &CharacterBody2D::is_floor_constant_speed_enabled);
  560. ClassDB::bind_method(D_METHOD("set_floor_block_on_wall_enabled", "enabled"), &CharacterBody2D::set_floor_block_on_wall_enabled);
  561. ClassDB::bind_method(D_METHOD("is_floor_block_on_wall_enabled"), &CharacterBody2D::is_floor_block_on_wall_enabled);
  562. ClassDB::bind_method(D_METHOD("set_slide_on_ceiling_enabled", "enabled"), &CharacterBody2D::set_slide_on_ceiling_enabled);
  563. ClassDB::bind_method(D_METHOD("is_slide_on_ceiling_enabled"), &CharacterBody2D::is_slide_on_ceiling_enabled);
  564. ClassDB::bind_method(D_METHOD("set_platform_floor_layers", "exclude_layer"), &CharacterBody2D::set_platform_floor_layers);
  565. ClassDB::bind_method(D_METHOD("get_platform_floor_layers"), &CharacterBody2D::get_platform_floor_layers);
  566. ClassDB::bind_method(D_METHOD("set_platform_wall_layers", "exclude_layer"), &CharacterBody2D::set_platform_wall_layers);
  567. ClassDB::bind_method(D_METHOD("get_platform_wall_layers"), &CharacterBody2D::get_platform_wall_layers);
  568. ClassDB::bind_method(D_METHOD("get_max_slides"), &CharacterBody2D::get_max_slides);
  569. ClassDB::bind_method(D_METHOD("set_max_slides", "max_slides"), &CharacterBody2D::set_max_slides);
  570. ClassDB::bind_method(D_METHOD("get_floor_max_angle"), &CharacterBody2D::get_floor_max_angle);
  571. ClassDB::bind_method(D_METHOD("set_floor_max_angle", "radians"), &CharacterBody2D::set_floor_max_angle);
  572. ClassDB::bind_method(D_METHOD("get_floor_snap_length"), &CharacterBody2D::get_floor_snap_length);
  573. ClassDB::bind_method(D_METHOD("set_floor_snap_length", "floor_snap_length"), &CharacterBody2D::set_floor_snap_length);
  574. ClassDB::bind_method(D_METHOD("get_wall_min_slide_angle"), &CharacterBody2D::get_wall_min_slide_angle);
  575. ClassDB::bind_method(D_METHOD("set_wall_min_slide_angle", "radians"), &CharacterBody2D::set_wall_min_slide_angle);
  576. ClassDB::bind_method(D_METHOD("get_up_direction"), &CharacterBody2D::get_up_direction);
  577. ClassDB::bind_method(D_METHOD("set_up_direction", "up_direction"), &CharacterBody2D::set_up_direction);
  578. ClassDB::bind_method(D_METHOD("set_motion_mode", "mode"), &CharacterBody2D::set_motion_mode);
  579. ClassDB::bind_method(D_METHOD("get_motion_mode"), &CharacterBody2D::get_motion_mode);
  580. ClassDB::bind_method(D_METHOD("set_platform_on_leave", "on_leave_apply_velocity"), &CharacterBody2D::set_platform_on_leave);
  581. ClassDB::bind_method(D_METHOD("get_platform_on_leave"), &CharacterBody2D::get_platform_on_leave);
  582. ClassDB::bind_method(D_METHOD("is_on_floor"), &CharacterBody2D::is_on_floor);
  583. ClassDB::bind_method(D_METHOD("is_on_floor_only"), &CharacterBody2D::is_on_floor_only);
  584. ClassDB::bind_method(D_METHOD("is_on_ceiling"), &CharacterBody2D::is_on_ceiling);
  585. ClassDB::bind_method(D_METHOD("is_on_ceiling_only"), &CharacterBody2D::is_on_ceiling_only);
  586. ClassDB::bind_method(D_METHOD("is_on_wall"), &CharacterBody2D::is_on_wall);
  587. ClassDB::bind_method(D_METHOD("is_on_wall_only"), &CharacterBody2D::is_on_wall_only);
  588. ClassDB::bind_method(D_METHOD("get_floor_normal"), &CharacterBody2D::get_floor_normal);
  589. ClassDB::bind_method(D_METHOD("get_wall_normal"), &CharacterBody2D::get_wall_normal);
  590. ClassDB::bind_method(D_METHOD("get_last_motion"), &CharacterBody2D::get_last_motion);
  591. ClassDB::bind_method(D_METHOD("get_position_delta"), &CharacterBody2D::get_position_delta);
  592. ClassDB::bind_method(D_METHOD("get_real_velocity"), &CharacterBody2D::get_real_velocity);
  593. ClassDB::bind_method(D_METHOD("get_floor_angle", "up_direction"), &CharacterBody2D::get_floor_angle, DEFVAL(Vector2(0.0, -1.0)));
  594. ClassDB::bind_method(D_METHOD("get_platform_velocity"), &CharacterBody2D::get_platform_velocity);
  595. ClassDB::bind_method(D_METHOD("get_slide_collision_count"), &CharacterBody2D::get_slide_collision_count);
  596. ClassDB::bind_method(D_METHOD("get_slide_collision", "slide_idx"), &CharacterBody2D::_get_slide_collision);
  597. ClassDB::bind_method(D_METHOD("get_last_slide_collision"), &CharacterBody2D::_get_last_slide_collision);
  598. 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");
  599. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "up_direction"), "set_up_direction", "get_up_direction");
  600. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "suffix:px/s", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
  601. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "slide_on_ceiling"), "set_slide_on_ceiling_enabled", "is_slide_on_ceiling_enabled");
  602. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_slides", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_max_slides", "get_max_slides");
  603. 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");
  604. ADD_GROUP("Floor", "floor_");
  605. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_stop_on_slope"), "set_floor_stop_on_slope_enabled", "is_floor_stop_on_slope_enabled");
  606. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_constant_speed"), "set_floor_constant_speed_enabled", "is_floor_constant_speed_enabled");
  607. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "floor_block_on_wall"), "set_floor_block_on_wall_enabled", "is_floor_block_on_wall_enabled");
  608. 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");
  609. 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");
  610. ADD_GROUP("Moving Platform", "platform_");
  611. 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");
  612. ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_floor_layers", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_platform_floor_layers", "get_platform_floor_layers");
  613. ADD_PROPERTY(PropertyInfo(Variant::INT, "platform_wall_layers", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_platform_wall_layers", "get_platform_wall_layers");
  614. ADD_GROUP("Collision", "");
  615. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "safe_margin", PROPERTY_HINT_RANGE, "0.001,256,0.001,suffix:px"), "set_safe_margin", "get_safe_margin");
  616. BIND_ENUM_CONSTANT(MOTION_MODE_GROUNDED);
  617. BIND_ENUM_CONSTANT(MOTION_MODE_FLOATING);
  618. BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_ADD_VELOCITY);
  619. BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY);
  620. BIND_ENUM_CONSTANT(PLATFORM_ON_LEAVE_DO_NOTHING);
  621. }
  622. CharacterBody2D::CharacterBody2D() :
  623. PhysicsBody2D(PhysicsServer2D::BODY_MODE_KINEMATIC) {
  624. }