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