Path: blob/master/modules/godot_physics_3d/godot_body_3d.cpp
10277 views
/**************************************************************************/1/* godot_body_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_3d.h"3132#include "godot_area_3d.h"33#include "godot_body_direct_state_3d.h"34#include "godot_constraint_3d.h"35#include "godot_space_3d.h"3637void GodotBody3D::_mass_properties_changed() {38if (get_space() && !mass_properties_update_list.in_list()) {39get_space()->body_add_to_mass_properties_update_list(&mass_properties_update_list);40}41}4243void GodotBody3D::_update_transform_dependent() {44center_of_mass = get_transform().basis.xform(center_of_mass_local);45principal_inertia_axes = get_transform().basis * principal_inertia_axes_local;4647// Update inertia tensor.48Basis tb = principal_inertia_axes;49Basis tbt = tb.transposed();50Basis diag;51diag.scale(_inv_inertia);52_inv_inertia_tensor = tb * diag * tbt;53}5455void GodotBody3D::update_mass_properties() {56// Update shapes and motions.5758switch (mode) {59case PhysicsServer3D::BODY_MODE_RIGID: {60real_t total_area = 0;61for (int i = 0; i < get_shape_count(); i++) {62if (is_shape_disabled(i)) {63continue;64}6566total_area += get_shape_area(i);67}6869if (calculate_center_of_mass) {70// We have to recompute the center of mass.71center_of_mass_local.zero();7273if (total_area != 0.0) {74for (int i = 0; i < get_shape_count(); i++) {75if (is_shape_disabled(i)) {76continue;77}7879real_t area = get_shape_area(i);8081real_t mass_new = area * mass / total_area;8283// NOTE: we assume that the shape origin is also its center of mass.84center_of_mass_local += mass_new * get_shape_transform(i).origin;85}8687center_of_mass_local /= mass;88}89}9091if (calculate_inertia) {92// Recompute the inertia tensor.93Basis inertia_tensor;94inertia_tensor.set_zero();95bool inertia_set = false;9697for (int i = 0; i < get_shape_count(); i++) {98if (is_shape_disabled(i)) {99continue;100}101102real_t area = get_shape_area(i);103if (area == 0.0) {104continue;105}106107inertia_set = true;108109const GodotShape3D *shape = get_shape(i);110111real_t mass_new = area * mass / total_area;112113Basis shape_inertia_tensor = Basis::from_scale(shape->get_moment_of_inertia(mass_new));114Transform3D shape_transform = get_shape_transform(i);115Basis shape_basis = shape_transform.basis.orthonormalized();116117// NOTE: we don't take the scale of collision shapes into account when computing the inertia tensor!118shape_inertia_tensor = shape_basis * shape_inertia_tensor * shape_basis.transposed();119120Vector3 shape_origin = shape_transform.origin - center_of_mass_local;121inertia_tensor += shape_inertia_tensor + (Basis() * shape_origin.dot(shape_origin) - shape_origin.outer(shape_origin)) * mass_new;122}123124// Set the inertia to a valid value when there are no valid shapes.125if (!inertia_set) {126inertia_tensor = Basis();127}128129// Handle partial custom inertia.130if (inertia.x > 0.0) {131inertia_tensor[0][0] = inertia.x;132}133if (inertia.y > 0.0) {134inertia_tensor[1][1] = inertia.y;135}136if (inertia.z > 0.0) {137inertia_tensor[2][2] = inertia.z;138}139140// Compute the principal axes of inertia.141principal_inertia_axes_local = inertia_tensor.diagonalize().transposed();142_inv_inertia = inertia_tensor.get_main_diagonal().inverse();143}144145if (mass) {146_inv_mass = 1.0 / mass;147} else {148_inv_mass = 0;149}150151} break;152case PhysicsServer3D::BODY_MODE_KINEMATIC:153case PhysicsServer3D::BODY_MODE_STATIC: {154_inv_inertia = Vector3();155_inv_mass = 0;156} break;157case PhysicsServer3D::BODY_MODE_RIGID_LINEAR: {158_inv_inertia_tensor.set_zero();159_inv_mass = 1.0 / mass;160161} break;162}163164_update_transform_dependent();165}166167void GodotBody3D::reset_mass_properties() {168calculate_inertia = true;169calculate_center_of_mass = true;170_mass_properties_changed();171}172173void GodotBody3D::set_active(bool p_active) {174if (active == p_active) {175return;176}177178active = p_active;179180if (active) {181if (mode == PhysicsServer3D::BODY_MODE_STATIC) {182// Static bodies can't be active.183active = false;184} else if (get_space()) {185get_space()->body_add_to_active_list(&active_list);186}187} else if (get_space()) {188get_space()->body_remove_from_active_list(&active_list);189}190}191192void GodotBody3D::set_param(PhysicsServer3D::BodyParameter p_param, const Variant &p_value) {193switch (p_param) {194case PhysicsServer3D::BODY_PARAM_BOUNCE: {195bounce = p_value;196} break;197case PhysicsServer3D::BODY_PARAM_FRICTION: {198friction = p_value;199} break;200case PhysicsServer3D::BODY_PARAM_MASS: {201real_t mass_value = p_value;202ERR_FAIL_COND(mass_value <= 0);203mass = mass_value;204if (mode >= PhysicsServer3D::BODY_MODE_RIGID) {205_mass_properties_changed();206}207} break;208case PhysicsServer3D::BODY_PARAM_INERTIA: {209inertia = p_value;210if ((inertia.x <= 0.0) || (inertia.y <= 0.0) || (inertia.z <= 0.0)) {211calculate_inertia = true;212if (mode == PhysicsServer3D::BODY_MODE_RIGID) {213_mass_properties_changed();214}215} else {216calculate_inertia = false;217if (mode == PhysicsServer3D::BODY_MODE_RIGID) {218principal_inertia_axes_local = Basis();219_inv_inertia = inertia.inverse();220_update_transform_dependent();221}222}223} break;224case PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS: {225calculate_center_of_mass = false;226center_of_mass_local = p_value;227_update_transform_dependent();228} break;229case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: {230if (Math::is_zero_approx(gravity_scale)) {231wakeup();232}233gravity_scale = p_value;234} break;235case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE: {236int mode_value = p_value;237linear_damp_mode = (PhysicsServer3D::BodyDampMode)mode_value;238} break;239case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE: {240int mode_value = p_value;241angular_damp_mode = (PhysicsServer3D::BodyDampMode)mode_value;242} break;243case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: {244linear_damp = p_value;245} break;246case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: {247angular_damp = p_value;248} break;249default: {250}251}252}253254Variant GodotBody3D::get_param(PhysicsServer3D::BodyParameter p_param) const {255switch (p_param) {256case PhysicsServer3D::BODY_PARAM_BOUNCE: {257return bounce;258} break;259case PhysicsServer3D::BODY_PARAM_FRICTION: {260return friction;261} break;262case PhysicsServer3D::BODY_PARAM_MASS: {263return mass;264} break;265case PhysicsServer3D::BODY_PARAM_INERTIA: {266if (mode == PhysicsServer3D::BODY_MODE_RIGID) {267return _inv_inertia.inverse();268} else {269return Vector3();270}271} break;272case PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS: {273return center_of_mass_local;274} break;275case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: {276return gravity_scale;277} break;278case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE: {279return linear_damp_mode;280}281case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE: {282return angular_damp_mode;283}284case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: {285return linear_damp;286} break;287case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: {288return angular_damp;289} break;290291default: {292}293}294295return 0;296}297298void GodotBody3D::set_mode(PhysicsServer3D::BodyMode p_mode) {299PhysicsServer3D::BodyMode prev = mode;300mode = p_mode;301302switch (p_mode) {303case PhysicsServer3D::BODY_MODE_STATIC:304case PhysicsServer3D::BODY_MODE_KINEMATIC: {305_set_inv_transform(get_transform().affine_inverse());306_inv_mass = 0;307_inv_inertia = Vector3();308_set_static(p_mode == PhysicsServer3D::BODY_MODE_STATIC);309set_active(p_mode == PhysicsServer3D::BODY_MODE_KINEMATIC && contacts.size());310linear_velocity = Vector3();311angular_velocity = Vector3();312if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC && prev != mode) {313first_time_kinematic = true;314}315_update_transform_dependent();316317} break;318case PhysicsServer3D::BODY_MODE_RIGID: {319_inv_mass = mass > 0 ? (1.0 / mass) : 0;320if (!calculate_inertia) {321principal_inertia_axes_local = Basis();322_inv_inertia = inertia.inverse();323_update_transform_dependent();324}325_mass_properties_changed();326_set_static(false);327set_active(true);328329} break;330case PhysicsServer3D::BODY_MODE_RIGID_LINEAR: {331_inv_mass = mass > 0 ? (1.0 / mass) : 0;332_inv_inertia = Vector3();333angular_velocity = Vector3();334_update_transform_dependent();335_set_static(false);336set_active(true);337}338}339}340341PhysicsServer3D::BodyMode GodotBody3D::get_mode() const {342return mode;343}344345void GodotBody3D::_shapes_changed() {346_mass_properties_changed();347wakeup();348wakeup_neighbours();349}350351void GodotBody3D::set_state(PhysicsServer3D::BodyState p_state, const Variant &p_variant) {352switch (p_state) {353case PhysicsServer3D::BODY_STATE_TRANSFORM: {354if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {355new_transform = p_variant;356//wakeup_neighbours();357set_active(true);358if (first_time_kinematic) {359_set_transform(p_variant);360_set_inv_transform(get_transform().affine_inverse());361first_time_kinematic = false;362}363364} else if (mode == PhysicsServer3D::BODY_MODE_STATIC) {365_set_transform(p_variant);366_set_inv_transform(get_transform().affine_inverse());367wakeup_neighbours();368} else {369Transform3D t = p_variant;370t.orthonormalize();371new_transform = get_transform(); //used as old to compute motion372if (new_transform == t) {373break;374}375_set_transform(t);376_set_inv_transform(get_transform().inverse());377_update_transform_dependent();378}379wakeup();380381} break;382case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: {383linear_velocity = p_variant;384constant_linear_velocity = linear_velocity;385wakeup();386} break;387case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: {388angular_velocity = p_variant;389constant_angular_velocity = angular_velocity;390wakeup();391392} break;393case PhysicsServer3D::BODY_STATE_SLEEPING: {394if (mode == PhysicsServer3D::BODY_MODE_STATIC || mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {395break;396}397bool do_sleep = p_variant;398if (do_sleep) {399linear_velocity = Vector3();400//biased_linear_velocity=Vector3();401angular_velocity = Vector3();402//biased_angular_velocity=Vector3();403set_active(false);404} else {405set_active(true);406}407} break;408case PhysicsServer3D::BODY_STATE_CAN_SLEEP: {409can_sleep = p_variant;410if (mode >= PhysicsServer3D::BODY_MODE_RIGID && !active && !can_sleep) {411set_active(true);412}413414} break;415}416}417418Variant GodotBody3D::get_state(PhysicsServer3D::BodyState p_state) const {419switch (p_state) {420case PhysicsServer3D::BODY_STATE_TRANSFORM: {421return get_transform();422} break;423case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: {424return linear_velocity;425} break;426case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: {427return angular_velocity;428} break;429case PhysicsServer3D::BODY_STATE_SLEEPING: {430return !is_active();431} break;432case PhysicsServer3D::BODY_STATE_CAN_SLEEP: {433return can_sleep;434} break;435}436437return Variant();438}439440void GodotBody3D::set_space(GodotSpace3D *p_space) {441if (get_space()) {442if (mass_properties_update_list.in_list()) {443get_space()->body_remove_from_mass_properties_update_list(&mass_properties_update_list);444}445if (active_list.in_list()) {446get_space()->body_remove_from_active_list(&active_list);447}448if (direct_state_query_list.in_list()) {449get_space()->body_remove_from_state_query_list(&direct_state_query_list);450}451}452453_set_space(p_space);454455if (get_space()) {456_mass_properties_changed();457458if (active && !active_list.in_list()) {459get_space()->body_add_to_active_list(&active_list);460}461}462}463464void GodotBody3D::set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool lock) {465if (lock) {466locked_axis |= p_axis;467} else {468locked_axis &= ~p_axis;469}470}471472bool GodotBody3D::is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const {473return locked_axis & p_axis;474}475476void GodotBody3D::integrate_forces(real_t p_step) {477if (mode == PhysicsServer3D::BODY_MODE_STATIC) {478return;479}480481ERR_FAIL_NULL(get_space());482483int ac = areas.size();484485bool gravity_done = false;486bool linear_damp_done = false;487bool angular_damp_done = false;488489bool stopped = false;490491gravity = Vector3(0, 0, 0);492493total_linear_damp = 0.0;494total_angular_damp = 0.0;495496// Combine gravity and damping from overlapping areas in priority order.497if (ac) {498areas.sort();499const AreaCMP *aa = &areas[0];500for (int i = ac - 1; i >= 0 && !stopped; i--) {501if (!gravity_done) {502PhysicsServer3D::AreaSpaceOverrideMode area_gravity_mode = (PhysicsServer3D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE);503if (area_gravity_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {504Vector3 area_gravity;505aa[i].area->compute_gravity(get_transform().get_origin(), area_gravity);506switch (area_gravity_mode) {507case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE:508case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {509gravity += area_gravity;510gravity_done = area_gravity_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;511} break;512case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE:513case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {514gravity = area_gravity;515gravity_done = area_gravity_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE;516} break;517default: {518}519}520}521}522if (!linear_damp_done) {523PhysicsServer3D::AreaSpaceOverrideMode area_linear_damp_mode = (PhysicsServer3D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer3D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE);524if (area_linear_damp_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {525real_t area_linear_damp = aa[i].area->get_linear_damp();526switch (area_linear_damp_mode) {527case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE:528case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {529total_linear_damp += area_linear_damp;530linear_damp_done = area_linear_damp_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;531} break;532case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE:533case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {534total_linear_damp = area_linear_damp;535linear_damp_done = area_linear_damp_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE;536} break;537default: {538}539}540}541}542if (!angular_damp_done) {543PhysicsServer3D::AreaSpaceOverrideMode area_angular_damp_mode = (PhysicsServer3D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE);544if (area_angular_damp_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {545real_t area_angular_damp = aa[i].area->get_angular_damp();546switch (area_angular_damp_mode) {547case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE:548case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {549total_angular_damp += area_angular_damp;550angular_damp_done = area_angular_damp_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;551} break;552case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE:553case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {554total_angular_damp = area_angular_damp;555angular_damp_done = area_angular_damp_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE;556} break;557default: {558}559}560}561}562stopped = gravity_done && linear_damp_done && angular_damp_done;563}564}565566// Add default gravity and damping from space area.567if (!stopped) {568GodotArea3D *default_area = get_space()->get_default_area();569ERR_FAIL_NULL(default_area);570571if (!gravity_done) {572Vector3 default_gravity;573default_area->compute_gravity(get_transform().get_origin(), default_gravity);574gravity += default_gravity;575}576577if (!linear_damp_done) {578total_linear_damp += default_area->get_linear_damp();579}580581if (!angular_damp_done) {582total_angular_damp += default_area->get_angular_damp();583}584}585586// Override linear damping with body's value.587switch (linear_damp_mode) {588case PhysicsServer3D::BODY_DAMP_MODE_COMBINE: {589total_linear_damp += linear_damp;590} break;591case PhysicsServer3D::BODY_DAMP_MODE_REPLACE: {592total_linear_damp = linear_damp;593} break;594}595596// Override angular damping with body's value.597switch (angular_damp_mode) {598case PhysicsServer3D::BODY_DAMP_MODE_COMBINE: {599total_angular_damp += angular_damp;600} break;601case PhysicsServer3D::BODY_DAMP_MODE_REPLACE: {602total_angular_damp = angular_damp;603} break;604}605606gravity *= gravity_scale;607608prev_linear_velocity = linear_velocity;609prev_angular_velocity = angular_velocity;610611Vector3 motion;612bool do_motion = false;613614if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {615//compute motion, angular and etc. velocities from prev transform616motion = new_transform.origin - get_transform().origin;617do_motion = true;618linear_velocity = constant_linear_velocity + motion / p_step;619620//compute a FAKE angular velocity, not so easy621Basis rot = new_transform.basis.orthonormalized() * get_transform().basis.orthonormalized().transposed();622Vector3 axis;623real_t angle;624625rot.get_axis_angle(axis, angle);626axis.normalize();627angular_velocity = constant_angular_velocity + axis * (angle / p_step);628} else {629if (!omit_force_integration) {630//overridden by direct state query631632Vector3 force = gravity * mass + applied_force + constant_force;633Vector3 torque = applied_torque + constant_torque;634635real_t damp = 1.0 - p_step * total_linear_damp;636637if (damp < 0) { // reached zero in the given time638damp = 0;639}640641real_t angular_damp_new = 1.0 - p_step * total_angular_damp;642643if (angular_damp_new < 0) { // reached zero in the given time644angular_damp_new = 0;645}646647linear_velocity *= damp;648angular_velocity *= angular_damp_new;649650linear_velocity += _inv_mass * force * p_step;651angular_velocity += _inv_inertia_tensor.xform(torque) * p_step;652}653654if (continuous_cd) {655motion = linear_velocity * p_step;656do_motion = true;657}658}659660applied_force = Vector3();661applied_torque = Vector3();662663biased_angular_velocity = Vector3();664biased_linear_velocity = Vector3();665666if (do_motion) { //shapes temporarily extend for raycast667_update_shapes_with_motion(motion);668}669670contact_count = 0;671}672673void GodotBody3D::integrate_velocities(real_t p_step) {674if (mode == PhysicsServer3D::BODY_MODE_STATIC) {675return;676}677678ERR_FAIL_NULL(get_space());679680if (fi_callback_data || body_state_callback.is_valid()) {681get_space()->body_add_to_state_query_list(&direct_state_query_list);682}683684//apply axis lock linear685for (int i = 0; i < 3; i++) {686if (is_axis_locked((PhysicsServer3D::BodyAxis)(1 << i))) {687linear_velocity[i] = 0;688biased_linear_velocity[i] = 0;689new_transform.origin[i] = get_transform().origin[i];690}691}692//apply axis lock angular693for (int i = 0; i < 3; i++) {694if (is_axis_locked((PhysicsServer3D::BodyAxis)(1 << (i + 3)))) {695angular_velocity[i] = 0;696biased_angular_velocity[i] = 0;697}698}699700if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {701_set_transform(new_transform, false);702_set_inv_transform(new_transform.affine_inverse());703if (contacts.is_empty() && linear_velocity == Vector3() && angular_velocity == Vector3()) {704set_active(false); //stopped moving, deactivate705}706707return;708}709710Vector3 total_angular_velocity = angular_velocity + biased_angular_velocity;711712real_t ang_vel = total_angular_velocity.length();713Transform3D transform_new = get_transform();714715if (!Math::is_zero_approx(ang_vel)) {716Vector3 ang_vel_axis = total_angular_velocity / ang_vel;717Basis rot(ang_vel_axis, ang_vel * p_step);718Basis identity3(1, 0, 0, 0, 1, 0, 0, 0, 1);719transform_new.origin += ((identity3 - rot) * transform_new.basis).xform(center_of_mass_local);720transform_new.basis = rot * transform_new.basis;721transform_new.orthonormalize();722}723724Vector3 total_linear_velocity = linear_velocity + biased_linear_velocity;725/*for(int i=0;i<3;i++) {726if (axis_lock&(1<<i)) {727transform_new.origin[i]=0.0;728}729}*/730731transform_new.origin += total_linear_velocity * p_step;732733_set_transform(transform_new);734_set_inv_transform(get_transform().inverse());735736_update_transform_dependent();737}738739void GodotBody3D::wakeup_neighbours() {740for (const KeyValue<GodotConstraint3D *, int> &E : constraint_map) {741const GodotConstraint3D *c = E.key;742GodotBody3D **n = c->get_body_ptr();743int bc = c->get_body_count();744745for (int i = 0; i < bc; i++) {746if (i == E.value) {747continue;748}749GodotBody3D *b = n[i];750if (b->mode < PhysicsServer3D::BODY_MODE_RIGID) {751continue;752}753754if (!b->is_active()) {755b->set_active(true);756}757}758}759}760761void GodotBody3D::call_queries() {762Variant direct_state_variant = get_direct_state();763764if (fi_callback_data) {765if (!fi_callback_data->callable.is_valid()) {766set_force_integration_callback(Callable());767} else {768const Variant *vp[2] = { &direct_state_variant, &fi_callback_data->udata };769770Callable::CallError ce;771int argc = (fi_callback_data->udata.get_type() == Variant::NIL) ? 1 : 2;772Variant rv;773fi_callback_data->callable.callp(vp, argc, rv, ce);774}775}776777if (body_state_callback.is_valid()) {778body_state_callback.call(direct_state_variant);779}780}781782bool GodotBody3D::sleep_test(real_t p_step) {783if (mode == PhysicsServer3D::BODY_MODE_STATIC || mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {784return true;785} else if (!can_sleep) {786return false;787}788789ERR_FAIL_NULL_V(get_space(), true);790791if (Math::abs(angular_velocity.length()) < get_space()->get_body_angular_velocity_sleep_threshold() && Math::abs(linear_velocity.length_squared()) < get_space()->get_body_linear_velocity_sleep_threshold() * get_space()->get_body_linear_velocity_sleep_threshold()) {792still_time += p_step;793794return still_time > get_space()->get_body_time_to_sleep();795} else {796still_time = 0; //maybe this should be set to 0 on set_active?797return false;798}799}800801void GodotBody3D::set_state_sync_callback(const Callable &p_callable) {802body_state_callback = p_callable;803}804805void GodotBody3D::set_force_integration_callback(const Callable &p_callable, const Variant &p_udata) {806if (p_callable.is_valid()) {807if (!fi_callback_data) {808fi_callback_data = memnew(ForceIntegrationCallbackData);809}810fi_callback_data->callable = p_callable;811fi_callback_data->udata = p_udata;812} else if (fi_callback_data) {813memdelete(fi_callback_data);814fi_callback_data = nullptr;815}816}817818GodotPhysicsDirectBodyState3D *GodotBody3D::get_direct_state() {819if (!direct_state) {820direct_state = memnew(GodotPhysicsDirectBodyState3D);821direct_state->body = this;822}823return direct_state;824}825826GodotBody3D::GodotBody3D() :827GodotCollisionObject3D(TYPE_BODY),828active_list(this),829mass_properties_update_list(this),830direct_state_query_list(this) {831_set_static(false);832}833834GodotBody3D::~GodotBody3D() {835if (fi_callback_data) {836memdelete(fi_callback_data);837}838if (direct_state) {839memdelete(direct_state);840}841}842843844