Path: blob/master/modules/godot_physics_3d/godot_body_pair_3d.cpp
11352 views
/**************************************************************************/1/* godot_body_pair_3d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "godot_body_pair_3d.h"3132#include "godot_collision_solver_3d.h"33#include "godot_space_3d.h"3435#define MIN_VELOCITY 0.000136#define MAX_BIAS_ROTATION (Math::PI / 8)3738void GodotBodyPair3D::_contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata) {39GodotBodyPair3D *pair = static_cast<GodotBodyPair3D *>(p_userdata);40pair->contact_added_callback(p_point_A, p_index_A, p_point_B, p_index_B, normal);41}4243void GodotBodyPair3D::contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal) {44Vector3 local_A = A->get_inv_transform().basis.xform(p_point_A);45Vector3 local_B = B->get_inv_transform().basis.xform(p_point_B - offset_B);4647int new_index = contact_count;4849ERR_FAIL_COND(new_index >= (MAX_CONTACTS + 1));5051Contact contact;52contact.index_A = p_index_A;53contact.index_B = p_index_B;54contact.local_A = local_A;55contact.local_B = local_B;56contact.normal = (p_point_A - p_point_B).normalized();57contact.used = true;5859// Attempt to determine if the contact will be reused.60real_t contact_recycle_radius = space->get_contact_recycle_radius();6162for (int i = 0; i < contact_count; i++) {63Contact &c = contacts[i];64if (c.local_A.distance_squared_to(local_A) < (contact_recycle_radius * contact_recycle_radius) &&65c.local_B.distance_squared_to(local_B) < (contact_recycle_radius * contact_recycle_radius)) {66contact.acc_normal_impulse = c.acc_normal_impulse;67contact.acc_bias_impulse = c.acc_bias_impulse;68contact.acc_bias_impulse_center_of_mass = c.acc_bias_impulse_center_of_mass;69contact.acc_tangent_impulse = c.acc_tangent_impulse;70c = contact;71return;72}73}7475// Figure out if the contact amount must be reduced to fit the new contact.76if (new_index == MAX_CONTACTS) {77// Remove the contact with the minimum depth.7879const Basis &basis_A = A->get_transform().basis;80const Basis &basis_B = B->get_transform().basis;8182int least_deep = -1;83real_t min_depth;8485// Start with depth for new contact.86{87Vector3 global_A = basis_A.xform(contact.local_A);88Vector3 global_B = basis_B.xform(contact.local_B) + offset_B;8990Vector3 axis = global_A - global_B;91min_depth = axis.dot(contact.normal);92}9394for (int i = 0; i < contact_count; i++) {95const Contact &c = contacts[i];96Vector3 global_A = basis_A.xform(c.local_A);97Vector3 global_B = basis_B.xform(c.local_B) + offset_B;9899Vector3 axis = global_A - global_B;100real_t depth = axis.dot(c.normal);101102if (depth < min_depth) {103min_depth = depth;104least_deep = i;105}106}107108if (least_deep > -1) {109// Replace the least deep contact by the new one.110contacts[least_deep] = contact;111}112113return;114}115116contacts[new_index] = contact;117contact_count++;118}119120void GodotBodyPair3D::validate_contacts() {121// Make sure to erase contacts that are no longer valid.122real_t max_separation = space->get_contact_max_separation();123real_t max_separation2 = max_separation * max_separation;124125const Basis &basis_A = A->get_transform().basis;126const Basis &basis_B = B->get_transform().basis;127128for (int i = 0; i < contact_count; i++) {129Contact &c = contacts[i];130131bool erase = false;132if (!c.used) {133// Was left behind in previous frame.134erase = true;135} else {136c.used = false;137138Vector3 global_A = basis_A.xform(c.local_A);139Vector3 global_B = basis_B.xform(c.local_B) + offset_B;140Vector3 axis = global_A - global_B;141real_t depth = axis.dot(c.normal);142143if (depth < -max_separation || (global_B + c.normal * depth - global_A).length_squared() > max_separation2) {144erase = true;145}146}147148if (erase) {149// Contact no longer needed, remove.150if ((i + 1) < contact_count) {151// Swap with the last one.152SWAP(contacts[i], contacts[contact_count - 1]);153}154155i--;156contact_count--;157}158}159}160161// `_test_ccd` prevents tunneling by slowing down a high velocity body that is about to collide so162// that next frame it will be at an appropriate location to collide (i.e. slight overlap).163// WARNING: The way velocity is adjusted down to cause a collision means the momentum will be164// weaker than it should for a bounce!165// Process: Only proceed if body A's motion is high relative to its size.166// Cast forward along motion vector to see if A is going to enter/pass B's collider next frame, only proceed if it does.167// Adjust the velocity of A down so that it will just slightly intersect the collider instead of blowing right past it.168bool GodotBodyPair3D::_test_ccd(real_t p_step, GodotBody3D *p_A, int p_shape_A, const Transform3D &p_xform_A, GodotBody3D *p_B, int p_shape_B, const Transform3D &p_xform_B) {169GodotShape3D *shape_A_ptr = p_A->get_shape(p_shape_A);170171Vector3 motion = p_A->get_linear_velocity() * p_step;172real_t mlen = motion.length();173if (mlen < CMP_EPSILON) {174return false;175}176177Vector3 mnormal = motion / mlen;178179real_t min = 0.0, max = 0.0;180shape_A_ptr->project_range(mnormal, p_xform_A, min, max);181182// Did it move enough in this direction to even attempt raycast?183// Let's say it should move more than 1/3 the size of the object in that axis.184bool fast_object = mlen > (max - min) * 0.3;185if (!fast_object) {186return false; // moving slow enough that there's no chance of tunneling.187}188189// A is moving fast enough that tunneling might occur. See if it's really about to collide.190191// Roughly predict body B's position in the next frame (ignoring collisions).192Transform3D predicted_xform_B = p_xform_B.translated(p_B->get_linear_velocity() * p_step);193194// Support points are the farthest forward points on A in the direction of the motion vector.195// i.e. the candidate points of which one should hit B first if any collision does occur.196static const int max_supports = 16;197Vector3 supports_A[max_supports];198int support_count_A;199GodotShape3D::FeatureType support_type_A;200// Convert mnormal into body A's local xform because get_supports requires (and returns) local coordinates.201shape_A_ptr->get_supports(p_xform_A.basis.xform_inv(mnormal).normalized(), max_supports, supports_A, support_count_A, support_type_A);202203// Cast a segment from each support point of A in the motion direction.204int segment_support_idx = -1;205float segment_hit_length = FLT_MAX;206Vector3 segment_hit_local;207for (int i = 0; i < support_count_A; i++) {208supports_A[i] = p_xform_A.xform(supports_A[i]);209210Vector3 from = supports_A[i];211Vector3 to = from + motion;212213Transform3D from_inv = predicted_xform_B.affine_inverse();214215// Back up 10% of the per-frame motion behind the support point and use that as the beginning of our cast.216// At high speeds, this may mean we're actually casting from well behind the body instead of inside it, which is odd.217// But it still works out.218Vector3 local_from = from_inv.xform(from - motion * 0.1);219Vector3 local_to = from_inv.xform(to);220221Vector3 rpos, rnorm;222int fi = -1;223if (p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm, fi, true)) {224float hit_length = local_from.distance_to(rpos);225if (hit_length < segment_hit_length) {226segment_support_idx = i;227segment_hit_length = hit_length;228segment_hit_local = rpos;229}230}231}232233if (segment_support_idx == -1) {234// There was no hit. Since the segment is the length of per-frame motion, this means the bodies will not235// actually collide yet on next frame. We'll probably check again next frame once they're closer.236return false;237}238239Vector3 hitpos = predicted_xform_B.xform(segment_hit_local);240241real_t newlen = hitpos.distance_to(supports_A[segment_support_idx]);242// Adding 1% of body length to the distance between collision and support point243// should cause body A's support point to arrive just within B's collider next frame.244newlen += (max - min) * 0.01;245// FIXME: This doesn't always work well when colliding with a triangle face of a trimesh shape.246247p_A->set_linear_velocity((mnormal * newlen) / p_step);248249return true;250}251252real_t combine_bounce(GodotBody3D *A, GodotBody3D *B) {253return CLAMP(A->get_bounce() + B->get_bounce(), 0, 1);254}255256real_t combine_friction(GodotBody3D *A, GodotBody3D *B) {257return Math::abs(MIN(A->get_friction(), B->get_friction()));258}259260bool GodotBodyPair3D::setup(real_t p_step) {261check_ccd = false;262263if (!A->interacts_with(B) || A->has_exception(B->get_self()) || B->has_exception(A->get_self())) {264collided = false;265return false;266}267268collide_A = (A->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC) && A->collides_with(B);269collide_B = (B->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC) && B->collides_with(A);270271report_contacts_only = false;272if (!collide_A && !collide_B) {273if ((A->get_max_contacts_reported() > 0) || (B->get_max_contacts_reported() > 0)) {274report_contacts_only = true;275} else {276collided = false;277return false;278}279}280281offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();282283validate_contacts();284285const Vector3 &offset_A = A->get_transform().get_origin();286Transform3D xform_Au = Transform3D(A->get_transform().basis, Vector3());287Transform3D xform_A = xform_Au * A->get_shape_transform(shape_A);288289Transform3D xform_Bu = B->get_transform();290xform_Bu.origin -= offset_A;291Transform3D xform_B = xform_Bu * B->get_shape_transform(shape_B);292293GodotShape3D *shape_A_ptr = A->get_shape(shape_A);294GodotShape3D *shape_B_ptr = B->get_shape(shape_B);295296collided = GodotCollisionSolver3D::solve_static(shape_A_ptr, xform_A, shape_B_ptr, xform_B, _contact_added_callback, this, &sep_axis);297298if (!collided) {299if (A->is_continuous_collision_detection_enabled() && collide_A) {300check_ccd = true;301return true;302}303304if (B->is_continuous_collision_detection_enabled() && collide_B) {305check_ccd = true;306return true;307}308309return false;310}311312return true;313}314315bool GodotBodyPair3D::pre_solve(real_t p_step) {316if (!collided) {317if (check_ccd) {318const Vector3 &offset_A = A->get_transform().get_origin();319Transform3D xform_Au = Transform3D(A->get_transform().basis, Vector3());320Transform3D xform_A = xform_Au * A->get_shape_transform(shape_A);321322Transform3D xform_Bu = B->get_transform();323xform_Bu.origin -= offset_A;324Transform3D xform_B = xform_Bu * B->get_shape_transform(shape_B);325326if (A->is_continuous_collision_detection_enabled() && collide_A) {327_test_ccd(p_step, A, shape_A, xform_A, B, shape_B, xform_B);328}329330if (B->is_continuous_collision_detection_enabled() && collide_B) {331_test_ccd(p_step, B, shape_B, xform_B, A, shape_A, xform_A);332}333}334335return false;336}337338real_t max_penetration = space->get_contact_max_allowed_penetration();339340real_t bias = 0.8;341342GodotShape3D *shape_A_ptr = A->get_shape(shape_A);343GodotShape3D *shape_B_ptr = B->get_shape(shape_B);344345if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {346if (shape_A_ptr->get_custom_bias() == 0) {347bias = shape_B_ptr->get_custom_bias();348} else if (shape_B_ptr->get_custom_bias() == 0) {349bias = shape_A_ptr->get_custom_bias();350} else {351bias = (shape_B_ptr->get_custom_bias() + shape_A_ptr->get_custom_bias()) * 0.5;352}353}354355real_t inv_dt = 1.0 / p_step;356357bool do_process = false;358359const Vector3 &offset_A = A->get_transform().get_origin();360361const Basis &basis_A = A->get_transform().basis;362const Basis &basis_B = B->get_transform().basis;363364Basis zero_basis;365zero_basis.set_zero();366367const Basis &inv_inertia_tensor_A = collide_A ? A->get_inv_inertia_tensor() : zero_basis;368const Basis &inv_inertia_tensor_B = collide_B ? B->get_inv_inertia_tensor() : zero_basis;369370real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;371real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;372373for (int i = 0; i < contact_count; i++) {374Contact &c = contacts[i];375c.active = false;376377Vector3 global_A = basis_A.xform(c.local_A);378Vector3 global_B = basis_B.xform(c.local_B) + offset_B;379380Vector3 axis = global_A - global_B;381real_t depth = axis.dot(c.normal);382383if (depth <= 0.0) {384continue;385}386387#ifdef DEBUG_ENABLED388if (space->is_debugging_contacts()) {389space->add_debug_contact(global_A + offset_A);390space->add_debug_contact(global_B + offset_A);391}392#endif393394c.rA = global_A - A->get_center_of_mass();395c.rB = global_B - B->get_center_of_mass() - offset_B;396397// Precompute normal mass, tangent mass, and bias.398Vector3 inertia_A = inv_inertia_tensor_A.xform(c.rA.cross(c.normal));399Vector3 inertia_B = inv_inertia_tensor_B.xform(c.rB.cross(c.normal));400real_t kNormal = inv_mass_A + inv_mass_B;401kNormal += c.normal.dot(inertia_A.cross(c.rA)) + c.normal.dot(inertia_B.cross(c.rB));402c.mass_normal = 1.0f / kNormal;403404c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);405c.depth = depth;406407Vector3 j_vec = c.normal * c.acc_normal_impulse + c.acc_tangent_impulse;408409c.acc_impulse -= j_vec;410411// contact query reporting...412413if (A->can_report_contacts() || B->can_report_contacts()) {414Vector3 crB = B->get_angular_velocity().cross(c.rB) + B->get_linear_velocity();415Vector3 crA = A->get_angular_velocity().cross(c.rA) + A->get_linear_velocity();416417if (A->can_report_contacts()) {418A->add_contact(global_A + offset_A, -c.normal, depth, shape_A, crA, global_B + offset_A, shape_B, B->get_instance_id(), B->get_self(), crB, c.acc_impulse);419}420421if (B->can_report_contacts()) {422B->add_contact(global_B + offset_A, c.normal, depth, shape_B, crB, global_A + offset_A, shape_A, A->get_instance_id(), A->get_self(), crA, -c.acc_impulse);423}424}425426if (report_contacts_only) {427collided = false;428continue;429}430431c.active = true;432do_process = true;433434if (collide_A) {435A->apply_impulse(-j_vec, c.rA + A->get_center_of_mass());436}437if (collide_B) {438B->apply_impulse(j_vec, c.rB + B->get_center_of_mass());439}440441c.bounce = combine_bounce(A, B);442if (c.bounce) {443Vector3 crA = A->get_prev_angular_velocity().cross(c.rA);444Vector3 crB = B->get_prev_angular_velocity().cross(c.rB);445Vector3 dv = B->get_prev_linear_velocity() + crB - A->get_prev_linear_velocity() - crA;446c.bounce = c.bounce * dv.dot(c.normal);447}448}449450return do_process;451}452453void GodotBodyPair3D::solve(real_t p_step) {454if (!collided) {455return;456}457458const real_t max_bias_av = MAX_BIAS_ROTATION / p_step;459460Basis zero_basis;461zero_basis.set_zero();462463const Basis &inv_inertia_tensor_A = collide_A ? A->get_inv_inertia_tensor() : zero_basis;464const Basis &inv_inertia_tensor_B = collide_B ? B->get_inv_inertia_tensor() : zero_basis;465466real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;467real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;468469for (int i = 0; i < contact_count; i++) {470Contact &c = contacts[i];471if (!c.active) {472continue;473}474475c.active = false; //try to deactivate, will activate itself if still needed476477//bias impulse478479Vector3 crbA = A->get_biased_angular_velocity().cross(c.rA);480Vector3 crbB = B->get_biased_angular_velocity().cross(c.rB);481Vector3 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;482483real_t vbn = dbv.dot(c.normal);484485if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {486real_t jbn = (-vbn + c.bias) * c.mass_normal;487real_t jbnOld = c.acc_bias_impulse;488c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);489490Vector3 jb = c.normal * (c.acc_bias_impulse - jbnOld);491492if (collide_A) {493A->apply_bias_impulse(-jb, c.rA + A->get_center_of_mass(), max_bias_av);494}495if (collide_B) {496B->apply_bias_impulse(jb, c.rB + B->get_center_of_mass(), max_bias_av);497}498499crbA = A->get_biased_angular_velocity().cross(c.rA);500crbB = B->get_biased_angular_velocity().cross(c.rB);501dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;502503vbn = dbv.dot(c.normal);504505if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {506real_t jbn_com = (-vbn + c.bias) / (inv_mass_A + inv_mass_B);507real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;508c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);509510Vector3 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);511512if (collide_A) {513A->apply_bias_impulse(-jb_com, A->get_center_of_mass(), 0.0f);514}515if (collide_B) {516B->apply_bias_impulse(jb_com, B->get_center_of_mass(), 0.0f);517}518}519520c.active = true;521}522523Vector3 crA = A->get_angular_velocity().cross(c.rA);524Vector3 crB = B->get_angular_velocity().cross(c.rB);525Vector3 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;526527//normal impulse528real_t vn = dv.dot(c.normal);529530if (Math::abs(vn) > MIN_VELOCITY) {531real_t jn = -(c.bounce + vn) * c.mass_normal;532real_t jnOld = c.acc_normal_impulse;533c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);534535Vector3 j = c.normal * (c.acc_normal_impulse - jnOld);536537if (collide_A) {538A->apply_impulse(-j, c.rA + A->get_center_of_mass());539}540if (collide_B) {541B->apply_impulse(j, c.rB + B->get_center_of_mass());542}543c.acc_impulse -= j;544545c.active = true;546}547548//friction impulse549550real_t friction = combine_friction(A, B);551552Vector3 lvA = A->get_linear_velocity() + A->get_angular_velocity().cross(c.rA);553Vector3 lvB = B->get_linear_velocity() + B->get_angular_velocity().cross(c.rB);554555Vector3 dtv = lvB - lvA;556real_t tn = c.normal.dot(dtv);557558// tangential velocity559Vector3 tv = dtv - c.normal * tn;560real_t tvl = tv.length();561562if (tvl > MIN_VELOCITY) {563tv /= tvl;564565Vector3 temp1 = inv_inertia_tensor_A.xform(c.rA.cross(tv));566Vector3 temp2 = inv_inertia_tensor_B.xform(c.rB.cross(tv));567568real_t t = -tvl / (inv_mass_A + inv_mass_B + tv.dot(temp1.cross(c.rA) + temp2.cross(c.rB)));569570Vector3 jt = t * tv;571572Vector3 jtOld = c.acc_tangent_impulse;573c.acc_tangent_impulse += jt;574575real_t fi_len = c.acc_tangent_impulse.length();576real_t jtMax = c.acc_normal_impulse * friction;577578if (fi_len > CMP_EPSILON && fi_len > jtMax) {579c.acc_tangent_impulse *= jtMax / fi_len;580}581582jt = c.acc_tangent_impulse - jtOld;583584if (collide_A) {585A->apply_impulse(-jt, c.rA + A->get_center_of_mass());586}587if (collide_B) {588B->apply_impulse(jt, c.rB + B->get_center_of_mass());589}590c.acc_impulse -= jt;591592c.active = true;593}594}595}596597GodotBodyPair3D::GodotBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotBody3D *p_B, int p_shape_B) :598GodotBodyContact3D(_arr, 2) {599A = p_A;600B = p_B;601shape_A = p_shape_A;602shape_B = p_shape_B;603space = A->get_space();604A->add_constraint(this, 0);605B->add_constraint(this, 1);606}607608GodotBodyPair3D::~GodotBodyPair3D() {609A->remove_constraint(this);610B->remove_constraint(this);611}612613void GodotBodySoftBodyPair3D::_contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata) {614GodotBodySoftBodyPair3D *pair = static_cast<GodotBodySoftBodyPair3D *>(p_userdata);615pair->contact_added_callback(p_point_A, p_index_A, p_point_B, p_index_B, normal);616}617618void GodotBodySoftBodyPair3D::contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal) {619Vector3 local_A = body->get_inv_transform().xform(p_point_A);620Vector3 local_B = p_point_B - soft_body->get_node_position(p_index_B);621622Contact contact;623contact.index_A = p_index_A;624contact.index_B = p_index_B;625contact.local_A = local_A;626contact.local_B = local_B;627contact.normal = (normal.dot((p_point_A - p_point_B)) < 0 ? -normal : normal);628contact.used = true;629630// Attempt to determine if the contact will be reused.631real_t contact_recycle_radius = space->get_contact_recycle_radius();632633uint32_t contact_count = contacts.size();634for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {635Contact &c = contacts[contact_index];636if (c.index_B == p_index_B) {637if (c.local_A.distance_squared_to(local_A) < (contact_recycle_radius * contact_recycle_radius) &&638c.local_B.distance_squared_to(local_B) < (contact_recycle_radius * contact_recycle_radius)) {639contact.acc_normal_impulse = c.acc_normal_impulse;640contact.acc_bias_impulse = c.acc_bias_impulse;641contact.acc_bias_impulse_center_of_mass = c.acc_bias_impulse_center_of_mass;642contact.acc_tangent_impulse = c.acc_tangent_impulse;643}644c = contact;645return;646}647}648649contacts.push_back(contact);650}651652void GodotBodySoftBodyPair3D::validate_contacts() {653// Make sure to erase contacts that are no longer valid.654real_t max_separation = space->get_contact_max_separation();655real_t max_separation2 = max_separation * max_separation;656657const Transform3D &transform_A = body->get_transform();658659uint32_t contact_count = contacts.size();660for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {661Contact &c = contacts[contact_index];662663bool erase = false;664if (!c.used) {665// Was left behind in previous frame.666erase = true;667} else {668c.used = false;669670Vector3 global_A = transform_A.xform(c.local_A);671Vector3 global_B = soft_body->get_node_position(c.index_B) + c.local_B;672Vector3 axis = global_A - global_B;673real_t depth = axis.dot(c.normal);674675if (depth < -max_separation || (global_B + c.normal * depth - global_A).length_squared() > max_separation2) {676erase = true;677}678}679680if (erase) {681// Contact no longer needed, remove.682if ((contact_index + 1) < contact_count) {683// Swap with the last one.684SWAP(c, contacts[contact_count - 1]);685}686687contact_index--;688contact_count--;689}690}691692contacts.resize(contact_count);693}694695bool GodotBodySoftBodyPair3D::setup(real_t p_step) {696if (!body->interacts_with(soft_body) || body->has_exception(soft_body->get_self()) || soft_body->has_exception(body->get_self())) {697collided = false;698return false;699}700701body_collides = (body->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC) && body->collides_with(soft_body);702soft_body_collides = soft_body->collides_with(body);703704if (!body_collides && !soft_body_collides) {705if (body->get_max_contacts_reported() > 0) {706report_contacts_only = true;707} else {708collided = false;709return false;710}711}712713const Transform3D &xform_Au = body->get_transform();714Transform3D xform_A = xform_Au * body->get_shape_transform(body_shape);715716Transform3D xform_Bu = soft_body->get_transform();717Transform3D xform_B = xform_Bu * soft_body->get_shape_transform(0);718719validate_contacts();720721GodotShape3D *shape_A_ptr = body->get_shape(body_shape);722GodotShape3D *shape_B_ptr = soft_body->get_shape(0);723724collided = GodotCollisionSolver3D::solve_static(shape_A_ptr, xform_A, shape_B_ptr, xform_B, _contact_added_callback, this, &sep_axis);725726return collided;727}728729bool GodotBodySoftBodyPair3D::pre_solve(real_t p_step) {730if (!collided) {731return false;732}733734real_t max_penetration = space->get_contact_max_allowed_penetration();735736real_t bias = space->get_contact_bias();737738GodotShape3D *shape_A_ptr = body->get_shape(body_shape);739740if (shape_A_ptr->get_custom_bias()) {741bias = shape_A_ptr->get_custom_bias();742}743744real_t inv_dt = 1.0 / p_step;745746bool do_process = false;747748const Transform3D &transform_A = body->get_transform();749750Basis zero_basis;751zero_basis.set_zero();752753const Basis &body_inv_inertia_tensor = body_collides ? body->get_inv_inertia_tensor() : zero_basis;754755real_t body_inv_mass = body_collides ? body->get_inv_mass() : 0.0;756757uint32_t contact_count = contacts.size();758for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {759Contact &c = contacts[contact_index];760c.active = false;761762real_t node_inv_mass = soft_body_collides ? soft_body->get_node_inv_mass(c.index_B) : 0.0;763if ((node_inv_mass == 0.0) && (body_inv_mass == 0.0)) {764continue;765}766767Vector3 global_A = transform_A.xform(c.local_A);768Vector3 global_B = soft_body->get_node_position(c.index_B) + c.local_B;769Vector3 axis = global_A - global_B;770real_t depth = axis.dot(c.normal);771772if (depth <= 0.0) {773continue;774}775776#ifdef DEBUG_ENABLED777if (space->is_debugging_contacts()) {778space->add_debug_contact(global_A);779space->add_debug_contact(global_B);780}781#endif782783c.rA = global_A - transform_A.origin - body->get_center_of_mass();784c.rB = global_B;785786// Precompute normal mass, tangent mass, and bias.787Vector3 inertia_A = body_inv_inertia_tensor.xform(c.rA.cross(c.normal));788real_t kNormal = body_inv_mass + node_inv_mass;789kNormal += c.normal.dot(inertia_A.cross(c.rA));790c.mass_normal = 1.0f / kNormal;791792c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);793c.depth = depth;794795Vector3 j_vec = c.normal * c.acc_normal_impulse + c.acc_tangent_impulse;796if (body_collides) {797body->apply_impulse(-j_vec, c.rA + body->get_center_of_mass());798}799if (soft_body_collides) {800soft_body->apply_node_impulse(c.index_B, j_vec);801}802c.acc_impulse -= j_vec;803804if (body->can_report_contacts()) {805Vector3 crA = body->get_angular_velocity().cross(c.rA) + body->get_linear_velocity();806Vector3 crB = soft_body->get_node_velocity(c.index_B);807body->add_contact(global_A, -c.normal, depth, body_shape, crA, global_B, 0, soft_body->get_instance_id(), soft_body->get_self(), crB, c.acc_impulse);808}809if (report_contacts_only) {810collided = false;811continue;812}813814c.active = true;815do_process = true;816817if (body_collides) {818body->set_active(true);819}820821c.bounce = body->get_bounce();822823if (c.bounce) {824Vector3 crA = body->get_angular_velocity().cross(c.rA);825Vector3 dv = soft_body->get_node_velocity(c.index_B) - body->get_linear_velocity() - crA;826827// Normal impulse.828c.bounce = c.bounce * dv.dot(c.normal);829}830}831832return do_process;833}834835void GodotBodySoftBodyPair3D::solve(real_t p_step) {836if (!collided) {837return;838}839840const real_t max_bias_av = MAX_BIAS_ROTATION / p_step;841842Basis zero_basis;843zero_basis.set_zero();844845const Basis &body_inv_inertia_tensor = body_collides ? body->get_inv_inertia_tensor() : zero_basis;846847real_t body_inv_mass = body_collides ? body->get_inv_mass() : 0.0;848849uint32_t contact_count = contacts.size();850for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {851Contact &c = contacts[contact_index];852if (!c.active) {853continue;854}855856c.active = false;857858real_t node_inv_mass = soft_body_collides ? soft_body->get_node_inv_mass(c.index_B) : 0.0;859860// Bias impulse.861Vector3 crbA = body->get_biased_angular_velocity().cross(c.rA);862Vector3 dbv = soft_body->get_node_biased_velocity(c.index_B) - body->get_biased_linear_velocity() - crbA;863864real_t vbn = dbv.dot(c.normal);865866if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {867real_t jbn = (-vbn + c.bias) * c.mass_normal;868real_t jbnOld = c.acc_bias_impulse;869c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);870871Vector3 jb = c.normal * (c.acc_bias_impulse - jbnOld);872873if (body_collides) {874body->apply_bias_impulse(-jb, c.rA + body->get_center_of_mass(), max_bias_av);875}876if (soft_body_collides) {877soft_body->apply_node_bias_impulse(c.index_B, jb);878}879880crbA = body->get_biased_angular_velocity().cross(c.rA);881dbv = soft_body->get_node_biased_velocity(c.index_B) - body->get_biased_linear_velocity() - crbA;882883vbn = dbv.dot(c.normal);884885if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {886real_t jbn_com = (-vbn + c.bias) / (body_inv_mass + node_inv_mass);887real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;888c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);889890Vector3 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);891892if (body_collides) {893body->apply_bias_impulse(-jb_com, body->get_center_of_mass(), 0.0f);894}895if (soft_body_collides) {896soft_body->apply_node_bias_impulse(c.index_B, jb_com);897}898}899900c.active = true;901}902903Vector3 crA = body->get_angular_velocity().cross(c.rA);904Vector3 dv = soft_body->get_node_velocity(c.index_B) - body->get_linear_velocity() - crA;905906// Normal impulse.907real_t vn = dv.dot(c.normal);908909if (Math::abs(vn) > MIN_VELOCITY) {910real_t jn = -(c.bounce + vn) * c.mass_normal;911real_t jnOld = c.acc_normal_impulse;912c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);913914Vector3 j = c.normal * (c.acc_normal_impulse - jnOld);915916if (body_collides) {917body->apply_impulse(-j, c.rA + body->get_center_of_mass());918}919if (soft_body_collides) {920soft_body->apply_node_impulse(c.index_B, j);921}922c.acc_impulse -= j;923924c.active = true;925}926927// Friction impulse.928real_t friction = body->get_friction();929930Vector3 lvA = body->get_linear_velocity() + body->get_angular_velocity().cross(c.rA);931Vector3 lvB = soft_body->get_node_velocity(c.index_B);932Vector3 dtv = lvB - lvA;933934real_t tn = c.normal.dot(dtv);935936// Tangential velocity.937Vector3 tv = dtv - c.normal * tn;938real_t tvl = tv.length();939940if (tvl > MIN_VELOCITY) {941tv /= tvl;942943Vector3 temp1 = body_inv_inertia_tensor.xform(c.rA.cross(tv));944945real_t t = -tvl / (body_inv_mass + node_inv_mass + tv.dot(temp1.cross(c.rA)));946947Vector3 jt = t * tv;948949Vector3 jtOld = c.acc_tangent_impulse;950c.acc_tangent_impulse += jt;951952real_t fi_len = c.acc_tangent_impulse.length();953real_t jtMax = c.acc_normal_impulse * friction;954955if (fi_len > CMP_EPSILON && fi_len > jtMax) {956c.acc_tangent_impulse *= jtMax / fi_len;957}958959jt = c.acc_tangent_impulse - jtOld;960961if (body_collides) {962body->apply_impulse(-jt, c.rA + body->get_center_of_mass());963}964if (soft_body_collides) {965soft_body->apply_node_impulse(c.index_B, jt);966}967c.acc_impulse -= jt;968969c.active = true;970}971}972}973974GodotBodySoftBodyPair3D::GodotBodySoftBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotSoftBody3D *p_B) :975GodotBodyContact3D(&body, 1) {976body = p_A;977soft_body = p_B;978body_shape = p_shape_A;979space = p_A->get_space();980body->add_constraint(this, 0);981soft_body->add_constraint(this);982}983984GodotBodySoftBodyPair3D::~GodotBodySoftBodyPair3D() {985body->remove_constraint(this);986soft_body->remove_constraint(this);987}988989990