|
@@ -32,8 +32,8 @@
|
|
|
|
|
|
#include "core/method_bind_ext.gen.inc"
|
|
#include "core/method_bind_ext.gen.inc"
|
|
#include "engine.h"
|
|
#include "engine.h"
|
|
|
|
+#include "math_funcs.h"
|
|
#include "scene/scene_string_names.h"
|
|
#include "scene/scene_string_names.h"
|
|
-
|
|
|
|
void PhysicsBody2D::_notification(int p_what) {
|
|
void PhysicsBody2D::_notification(int p_what) {
|
|
|
|
|
|
/*
|
|
/*
|
|
@@ -971,11 +971,11 @@ RigidBody2D::~RigidBody2D() {
|
|
|
|
|
|
//////////////////////////
|
|
//////////////////////////
|
|
|
|
|
|
-Ref<KinematicCollision2D> KinematicBody2D::_move(const Vector2 &p_motion, bool p_infinite_inertia, bool p_exclude_raycast_shapes, bool p_only_move_if_collided, float p_custom_margin) {
|
|
|
|
|
|
+Ref<KinematicCollision2D> KinematicBody2D::_move(const Vector2 &p_motion, bool p_infinite_inertia, bool p_exclude_raycast_shapes) {
|
|
|
|
|
|
Collision col;
|
|
Collision col;
|
|
|
|
|
|
- if (move_and_collide(p_motion, p_infinite_inertia, col, p_exclude_raycast_shapes, p_only_move_if_collided, p_custom_margin)) {
|
|
|
|
|
|
+ if (move_and_collide(p_motion, p_infinite_inertia, col, p_exclude_raycast_shapes)) {
|
|
if (motion_cache.is_null()) {
|
|
if (motion_cache.is_null()) {
|
|
motion_cache.instance();
|
|
motion_cache.instance();
|
|
motion_cache->owner = this;
|
|
motion_cache->owner = this;
|
|
@@ -1026,11 +1026,11 @@ bool KinematicBody2D::separate_raycast_shapes(bool p_infinite_inertia, Collision
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-bool KinematicBody2D::move_and_collide(const Vector2 &p_motion, bool p_infinite_inertia, Collision &r_collision, bool p_exclude_raycast_shapes, bool p_only_move_if_collided, float p_custom_margin) {
|
|
|
|
|
|
+bool KinematicBody2D::move_and_collide(const Vector2 &p_motion, bool p_infinite_inertia, Collision &r_collision, bool p_exclude_raycast_shapes) {
|
|
|
|
|
|
Transform2D gt = get_global_transform();
|
|
Transform2D gt = get_global_transform();
|
|
Physics2DServer::MotionResult result;
|
|
Physics2DServer::MotionResult result;
|
|
- bool colliding = Physics2DServer::get_singleton()->body_test_motion(get_rid(), gt, p_motion, p_infinite_inertia, p_custom_margin >= 0 ? p_custom_margin : margin, &result, p_exclude_raycast_shapes);
|
|
|
|
|
|
+ bool colliding = Physics2DServer::get_singleton()->body_test_motion(get_rid(), gt, p_motion, p_infinite_inertia, margin, &result, p_exclude_raycast_shapes);
|
|
|
|
|
|
if (colliding) {
|
|
if (colliding) {
|
|
r_collision.collider_metadata = result.collider_metadata;
|
|
r_collision.collider_metadata = result.collider_metadata;
|
|
@@ -1044,15 +1044,13 @@ bool KinematicBody2D::move_and_collide(const Vector2 &p_motion, bool p_infinite_
|
|
r_collision.local_shape = result.collision_local_shape;
|
|
r_collision.local_shape = result.collision_local_shape;
|
|
}
|
|
}
|
|
|
|
|
|
- if (!p_only_move_if_collided || colliding) {
|
|
|
|
- gt.elements[2] += result.motion;
|
|
|
|
- set_global_transform(gt);
|
|
|
|
- }
|
|
|
|
|
|
+ gt.elements[2] += result.motion;
|
|
|
|
+ set_global_transform(gt);
|
|
|
|
|
|
return colliding;
|
|
return colliding;
|
|
}
|
|
}
|
|
|
|
|
|
-Vector2 KinematicBody2D::move_and_slide(const Vector2 &p_linear_velocity, const Vector2 &p_floor_direction, bool p_infinite_inertia, float p_slope_stop_min_velocity, int p_max_slides, float p_floor_max_angle, const Vector2 &p_snap) {
|
|
|
|
|
|
+Vector2 KinematicBody2D::move_and_slide(const Vector2 &p_linear_velocity, const Vector2 &p_floor_direction, bool p_infinite_inertia, float p_slope_stop_min_velocity, int p_max_slides, float p_floor_max_angle) {
|
|
|
|
|
|
Vector2 motion = (floor_velocity + p_linear_velocity) * get_physics_process_delta_time();
|
|
Vector2 motion = (floor_velocity + p_linear_velocity) * get_physics_process_delta_time();
|
|
Vector2 lv = p_linear_velocity;
|
|
Vector2 lv = p_linear_velocity;
|
|
@@ -1132,20 +1130,23 @@ Vector2 KinematicBody2D::move_and_slide(const Vector2 &p_linear_velocity, const
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
- if (p_snap != Vector2() && (on_floor || snapped)) {
|
|
|
|
|
|
+ return lv;
|
|
|
|
+}
|
|
|
|
|
|
- Collision collision;
|
|
|
|
- if (move_and_collide(p_snap, p_infinite_inertia, collision, false, true, 0)) {
|
|
|
|
- snapped = true;
|
|
|
|
- } else {
|
|
|
|
- snapped = false;
|
|
|
|
- }
|
|
|
|
|
|
+bool KinematicBody2D::snap_to_floor(const Vector2 &p_direction, float p_floor_max_angle) {
|
|
|
|
|
|
- } else {
|
|
|
|
- snapped = false;
|
|
|
|
|
|
+ Transform2D gt = get_global_transform();
|
|
|
|
+ Physics2DServer::MotionResult result;
|
|
|
|
+ bool colliding = Physics2DServer::get_singleton()->body_test_motion(get_rid(), gt, p_direction, false, margin, &result, false);
|
|
|
|
+ if (colliding) {
|
|
|
|
+ gt.elements[2] += result.motion;
|
|
|
|
+ if (Math::acos(p_direction.normalized().dot(-result.collision_normal)) < p_floor_max_angle) {
|
|
|
|
+ on_floor = true;
|
|
|
|
+ }
|
|
|
|
+ set_global_transform(gt);
|
|
}
|
|
}
|
|
|
|
|
|
- return lv;
|
|
|
|
|
|
+ return colliding;
|
|
}
|
|
}
|
|
|
|
|
|
bool KinematicBody2D::is_on_floor() const {
|
|
bool KinematicBody2D::is_on_floor() const {
|
|
@@ -1211,8 +1212,9 @@ Ref<KinematicCollision2D> KinematicBody2D::_get_slide_collision(int p_bounce) {
|
|
|
|
|
|
void KinematicBody2D::_bind_methods() {
|
|
void KinematicBody2D::_bind_methods() {
|
|
|
|
|
|
- ClassDB::bind_method(D_METHOD("move_and_collide", "rel_vec", "infinite_inertia", "exclude_raycast_shapes", "only_move_if_collided", "custom_margin"), &KinematicBody2D::_move, DEFVAL(true), DEFVAL(true), DEFVAL(false), DEFVAL(-1.0));
|
|
|
|
- ClassDB::bind_method(D_METHOD("move_and_slide", "linear_velocity", "floor_normal", "infinite_inertia", "slope_stop_min_velocity", "max_bounces", "floor_max_angle", "snap"), &KinematicBody2D::move_and_slide, DEFVAL(Vector2(0, 0)), DEFVAL(true), DEFVAL(5), DEFVAL(4), DEFVAL(Math::deg2rad((float)45)), DEFVAL(Vector2()));
|
|
|
|
|
|
+ ClassDB::bind_method(D_METHOD("move_and_collide", "rel_vec", "infinite_inertia", "exclude_raycast_shapes"), &KinematicBody2D::_move, DEFVAL(true), DEFVAL(true));
|
|
|
|
+ ClassDB::bind_method(D_METHOD("move_and_slide", "linear_velocity", "floor_normal", "infinite_inertia", "slope_stop_min_velocity", "max_bounces", "floor_max_angle"), &KinematicBody2D::move_and_slide, DEFVAL(Vector2(0, 0)), DEFVAL(true), DEFVAL(5), DEFVAL(4), DEFVAL(Math::deg2rad((float)45)));
|
|
|
|
+ ClassDB::bind_method(D_METHOD("snap_to_floor", "motion", "max_floor_angle"), &KinematicBody2D::snap_to_floor, DEFVAL(Math::deg2rad(45.0)));
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("test_move", "from", "rel_vec", "infinite_inertia"), &KinematicBody2D::test_move);
|
|
ClassDB::bind_method(D_METHOD("test_move", "from", "rel_vec", "infinite_inertia"), &KinematicBody2D::test_move);
|
|
|
|
|
|
@@ -1238,7 +1240,6 @@ KinematicBody2D::KinematicBody2D() :
|
|
on_floor = false;
|
|
on_floor = false;
|
|
on_ceiling = false;
|
|
on_ceiling = false;
|
|
on_wall = false;
|
|
on_wall = false;
|
|
- snapped = false;
|
|
|
|
}
|
|
}
|
|
KinematicBody2D::~KinematicBody2D() {
|
|
KinematicBody2D::~KinematicBody2D() {
|
|
if (motion_cache.is_valid()) {
|
|
if (motion_cache.is_valid()) {
|