Remove infinite inertia and ray shapes from CharacterBody
Infinite inertia: Not needed anymore, since it's now possible to set one-directional collision layers in order for characters to ignore rigid bodies, while rigid bodies still collide with characters. Ray shapes: They were introduced as a work around to allow constant speed on slopes, which is now possible with the new property in CharacterBody instead.
This commit is contained in:
@@ -528,188 +528,7 @@ int Space2DSW::_cull_aabb_for_body(Body2DSW *p_body, const Rect2 &p_aabb) {
|
||||
return amount;
|
||||
}
|
||||
|
||||
int Space2DSW::test_body_ray_separation(Body2DSW *p_body, const Transform2D &p_transform, bool p_infinite_inertia, Vector2 &r_recover_motion, PhysicsServer2D::SeparationResult *r_results, int p_result_max, real_t p_margin) {
|
||||
Rect2 body_aabb;
|
||||
|
||||
bool shapes_found = false;
|
||||
|
||||
for (int i = 0; i < p_body->get_shape_count(); i++) {
|
||||
if (p_body->is_shape_disabled(i)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (p_body->get_shape(i)->get_type() != PhysicsServer2D::SHAPE_RAY) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!shapes_found) {
|
||||
body_aabb = p_body->get_shape_aabb(i);
|
||||
shapes_found = true;
|
||||
} else {
|
||||
body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
|
||||
}
|
||||
}
|
||||
|
||||
if (!shapes_found) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Undo the currently transform the physics server is aware of and apply the provided one
|
||||
body_aabb = p_transform.xform(p_body->get_inv_transform().xform(body_aabb));
|
||||
body_aabb = body_aabb.grow(p_margin);
|
||||
|
||||
Transform2D body_transform = p_transform;
|
||||
|
||||
for (int i = 0; i < p_result_max; i++) {
|
||||
//reset results
|
||||
r_results[i].collision_depth = 0;
|
||||
}
|
||||
|
||||
int rays_found = 0;
|
||||
|
||||
{
|
||||
// raycast AND separate
|
||||
|
||||
const int max_results = 32;
|
||||
int recover_attempts = 4;
|
||||
Vector2 sr[max_results * 2];
|
||||
PhysicsServer2DSW::CollCbkData cbk;
|
||||
cbk.max = max_results;
|
||||
PhysicsServer2DSW::CollCbkData *cbkptr = &cbk;
|
||||
CollisionSolver2DSW::CallbackResult cbkres = PhysicsServer2DSW::_shape_col_cbk;
|
||||
|
||||
do {
|
||||
Vector2 recover_motion;
|
||||
|
||||
bool collided = false;
|
||||
|
||||
int amount = _cull_aabb_for_body(p_body, body_aabb);
|
||||
|
||||
for (int j = 0; j < p_body->get_shape_count(); j++) {
|
||||
if (p_body->is_shape_disabled(j)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Shape2DSW *body_shape = p_body->get_shape(j);
|
||||
|
||||
if (body_shape->get_type() != PhysicsServer2D::SHAPE_RAY) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Transform2D body_shape_xform = body_transform * p_body->get_shape_transform(j);
|
||||
|
||||
for (int i = 0; i < amount; i++) {
|
||||
const CollisionObject2DSW *col_obj = intersection_query_results[i];
|
||||
int shape_idx = intersection_query_subindex_results[i];
|
||||
|
||||
cbk.amount = 0;
|
||||
cbk.passed = 0;
|
||||
cbk.ptr = sr;
|
||||
cbk.invalid_by_dir = 0;
|
||||
|
||||
if (CollisionObject2DSW::TYPE_BODY == col_obj->get_type()) {
|
||||
const Body2DSW *b = static_cast<const Body2DSW *>(col_obj);
|
||||
if (p_infinite_inertia && PhysicsServer2D::BODY_MODE_STATIC != b->get_mode() && PhysicsServer2D::BODY_MODE_KINEMATIC != b->get_mode()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
|
||||
|
||||
/*
|
||||
* There is no point in supporting one way collisions with ray shapes, as they will always collide in the desired
|
||||
* direction. Use a short ray shape if you want to achieve a similar effect.
|
||||
*
|
||||
if (col_obj->is_shape_set_as_one_way_collision(shape_idx)) {
|
||||
cbk.valid_dir = col_obj_shape_xform.get_axis(1).normalized();
|
||||
cbk.valid_depth = p_margin; //only valid depth is the collision margin
|
||||
cbk.invalid_by_dir = 0;
|
||||
|
||||
} else {
|
||||
*/
|
||||
|
||||
cbk.valid_dir = Vector2();
|
||||
cbk.valid_depth = 0;
|
||||
cbk.invalid_by_dir = 0;
|
||||
|
||||
/*
|
||||
}
|
||||
*/
|
||||
|
||||
Shape2DSW *against_shape = col_obj->get_shape(shape_idx);
|
||||
if (CollisionSolver2DSW::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), cbkres, cbkptr, nullptr, p_margin)) {
|
||||
if (cbk.amount > 0) {
|
||||
collided = true;
|
||||
}
|
||||
|
||||
int ray_index = -1; //reuse shape
|
||||
for (int k = 0; k < rays_found; k++) {
|
||||
if (r_results[ray_index].collision_local_shape == j) {
|
||||
ray_index = k;
|
||||
}
|
||||
}
|
||||
|
||||
if (ray_index == -1 && rays_found < p_result_max) {
|
||||
ray_index = rays_found;
|
||||
rays_found++;
|
||||
}
|
||||
|
||||
if (ray_index != -1) {
|
||||
PhysicsServer2D::SeparationResult &result = r_results[ray_index];
|
||||
|
||||
for (int k = 0; k < cbk.amount; k++) {
|
||||
Vector2 a = sr[k * 2 + 0];
|
||||
Vector2 b = sr[k * 2 + 1];
|
||||
|
||||
recover_motion += (b - a) / cbk.amount;
|
||||
|
||||
real_t depth = a.distance_to(b);
|
||||
if (depth > result.collision_depth) {
|
||||
result.collision_depth = depth;
|
||||
result.collision_point = b;
|
||||
result.collision_normal = (b - a).normalized();
|
||||
result.collision_local_shape = j;
|
||||
result.collider_shape = shape_idx;
|
||||
result.collider = col_obj->get_self();
|
||||
result.collider_id = col_obj->get_instance_id();
|
||||
result.collider_metadata = col_obj->get_shape_metadata(shape_idx);
|
||||
if (col_obj->get_type() == CollisionObject2DSW::TYPE_BODY) {
|
||||
Body2DSW *body = (Body2DSW *)col_obj;
|
||||
|
||||
Vector2 rel_vec = b - body->get_transform().get_origin();
|
||||
result.collider_velocity = Vector2(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!collided || recover_motion == Vector2()) {
|
||||
break;
|
||||
}
|
||||
|
||||
body_transform.elements[2] += recover_motion;
|
||||
body_aabb.position += recover_motion;
|
||||
|
||||
recover_attempts--;
|
||||
} while (recover_attempts);
|
||||
}
|
||||
|
||||
//optimize results (remove non colliding)
|
||||
for (int i = 0; i < rays_found; i++) {
|
||||
if (r_results[i].collision_depth == 0) {
|
||||
rays_found--;
|
||||
SWAP(r_results[i], r_results[rays_found]);
|
||||
}
|
||||
}
|
||||
|
||||
r_recover_motion = body_transform.elements[2] - p_transform.elements[2];
|
||||
return rays_found;
|
||||
}
|
||||
|
||||
bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, real_t p_margin, PhysicsServer2D::MotionResult *r_result, bool p_exclude_raycast_shapes, const Set<RID> &p_exclude) {
|
||||
bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, const Vector2 &p_motion, real_t p_margin, PhysicsServer2D::MotionResult *r_result, const Set<RID> &p_exclude) {
|
||||
//give me back regular physics engine logic
|
||||
//this is madness
|
||||
//and most people using this function will think
|
||||
@@ -730,10 +549,6 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co
|
||||
continue;
|
||||
}
|
||||
|
||||
if (p_exclude_raycast_shapes && p_body->get_shape(i)->get_type() == PhysicsServer2D::SHAPE_RAY) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!shapes_found) {
|
||||
body_aabb = p_body->get_shape_aabb(i);
|
||||
shapes_found = true;
|
||||
@@ -794,24 +609,15 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co
|
||||
}
|
||||
|
||||
Shape2DSW *body_shape = p_body->get_shape(j);
|
||||
if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer2D::SHAPE_RAY) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Transform2D body_shape_xform = body_transform * p_body->get_shape_transform(j);
|
||||
|
||||
for (int i = 0; i < amount; i++) {
|
||||
const CollisionObject2DSW *col_obj = intersection_query_results[i];
|
||||
if (p_exclude.has(col_obj->get_self())) {
|
||||
continue;
|
||||
}
|
||||
int shape_idx = intersection_query_subindex_results[i];
|
||||
|
||||
if (CollisionObject2DSW::TYPE_BODY == col_obj->get_type()) {
|
||||
const Body2DSW *b = static_cast<const Body2DSW *>(col_obj);
|
||||
if (p_infinite_inertia && PhysicsServer2D::BODY_MODE_STATIC != b->get_mode() && PhysicsServer2D::BODY_MODE_KINEMATIC != b->get_mode()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
int shape_idx = intersection_query_subindex_results[i];
|
||||
|
||||
Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
|
||||
|
||||
@@ -920,10 +726,6 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co
|
||||
}
|
||||
|
||||
Shape2DSW *body_shape = p_body->get_shape(body_shape_idx);
|
||||
if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer2D::SHAPE_RAY) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Transform2D body_shape_xform = body_transform * p_body->get_shape_transform(body_shape_idx);
|
||||
|
||||
bool stuck = false;
|
||||
@@ -939,13 +741,6 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co
|
||||
int col_shape_idx = intersection_query_subindex_results[i];
|
||||
Shape2DSW *against_shape = col_obj->get_shape(col_shape_idx);
|
||||
|
||||
if (CollisionObject2DSW::TYPE_BODY == col_obj->get_type()) {
|
||||
const Body2DSW *b = static_cast<const Body2DSW *>(col_obj);
|
||||
if (p_infinite_inertia && PhysicsServer2D::BODY_MODE_STATIC != b->get_mode() && PhysicsServer2D::BODY_MODE_KINEMATIC != b->get_mode()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
bool excluded = false;
|
||||
|
||||
for (int k = 0; k < excluded_shape_pair_count; k++) {
|
||||
@@ -1082,10 +877,6 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co
|
||||
Transform2D body_shape_xform = ugt * p_body->get_shape_transform(j);
|
||||
Shape2DSW *body_shape = p_body->get_shape(j);
|
||||
|
||||
if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer2D::SHAPE_RAY) {
|
||||
continue;
|
||||
}
|
||||
|
||||
body_aabb.position += p_motion * unsafe;
|
||||
|
||||
int amount = _cull_aabb_for_body(p_body, body_aabb);
|
||||
@@ -1095,14 +886,8 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, co
|
||||
if (p_exclude.has(col_obj->get_self())) {
|
||||
continue;
|
||||
}
|
||||
int shape_idx = intersection_query_subindex_results[i];
|
||||
|
||||
if (CollisionObject2DSW::TYPE_BODY == col_obj->get_type()) {
|
||||
const Body2DSW *b = static_cast<const Body2DSW *>(col_obj);
|
||||
if (p_infinite_inertia && PhysicsServer2D::BODY_MODE_STATIC != b->get_mode() && PhysicsServer2D::BODY_MODE_KINEMATIC != b->get_mode()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
int shape_idx = intersection_query_subindex_results[i];
|
||||
|
||||
Shape2DSW *against_shape = col_obj->get_shape(shape_idx);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user