2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-03-28 13:19:05 +01:00
|
|
|
/* body_3d_sw.cpp */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
#include "body_3d_sw.h"
|
|
|
|
#include "area_3d_sw.h"
|
|
|
|
#include "space_3d_sw.h"
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::_update_inertia() {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
if (get_space() && !inertia_update_list.in_list())
|
|
|
|
get_space()->body_add_to_inertia_update_list(&inertia_update_list);
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::_update_transform_dependant() {
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
center_of_mass = get_transform().basis.xform(center_of_mass_local);
|
|
|
|
principal_inertia_axes = get_transform().basis * principal_inertia_axes_local;
|
|
|
|
|
|
|
|
// update inertia tensor
|
2017-01-11 04:52:51 +01:00
|
|
|
Basis tb = principal_inertia_axes;
|
|
|
|
Basis tbt = tb.transposed();
|
2017-09-16 18:50:46 +02:00
|
|
|
Basis diag;
|
|
|
|
diag.scale(_inv_inertia);
|
|
|
|
_inv_inertia_tensor = tb * diag * tbt;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::update_inertias() {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
//update shapes and motions
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (mode) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_MODE_RIGID: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
//update tensor for all shapes, not the best way but should be somehow OK. (inspired from bullet)
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t total_area = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
total_area += get_shape_area(i);
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
// We have to recompute the center of mass
|
|
|
|
center_of_mass_local.zero();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
|
|
|
real_t area = get_shape_area(i);
|
2016-12-31 15:39:25 +01:00
|
|
|
|
2017-02-14 00:25:05 +01:00
|
|
|
real_t mass = area * this->mass / total_area;
|
2016-12-31 15:39:25 +01:00
|
|
|
|
|
|
|
// NOTE: we assume that the shape origin is also its center of mass
|
|
|
|
center_of_mass_local += mass * get_shape_transform(i).origin;
|
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
center_of_mass_local /= mass;
|
|
|
|
|
|
|
|
// Recompute the inertia tensor
|
2017-01-11 04:52:51 +01:00
|
|
|
Basis inertia_tensor;
|
2016-12-31 15:39:25 +01:00
|
|
|
inertia_tensor.set_zero();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2018-12-07 22:09:39 +01:00
|
|
|
if (is_shape_disabled(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
const Shape3DSW *shape = get_shape(i);
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t area = get_shape_area(i);
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-02-14 00:25:05 +01:00
|
|
|
real_t mass = area * this->mass / total_area;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Basis shape_inertia_tensor = shape->get_moment_of_inertia(mass).to_diagonal_matrix();
|
|
|
|
Transform shape_transform = get_shape_transform(i);
|
2017-01-11 04:52:51 +01:00
|
|
|
Basis shape_basis = shape_transform.basis.orthonormalized();
|
2016-12-31 15:39:25 +01:00
|
|
|
|
|
|
|
// NOTE: we don't take the scale of collision shapes into account when computing the inertia tensor!
|
|
|
|
shape_inertia_tensor = shape_basis * shape_inertia_tensor * shape_basis.transposed();
|
|
|
|
|
|
|
|
Vector3 shape_origin = shape_transform.origin - center_of_mass_local;
|
2017-03-05 16:44:50 +01:00
|
|
|
inertia_tensor += shape_inertia_tensor + (Basis() * shape_origin.dot(shape_origin) - shape_origin.outer(shape_origin)) * mass;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
// Compute the principal axes of inertia
|
|
|
|
principal_inertia_axes_local = inertia_tensor.diagonalize().transposed();
|
|
|
|
_inv_inertia = inertia_tensor.get_main_diagonal().inverse();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
if (mass)
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = 1.0 / mass;
|
2014-02-19 15:57:14 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_MODE_KINEMATIC:
|
|
|
|
case PhysicsServer3D::BODY_MODE_STATIC: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
_inv_inertia_tensor.set_zero();
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_MODE_CHARACTER: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
_inv_inertia_tensor.set_zero();
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = 1.0 / mass;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
2016-12-31 15:39:25 +01:00
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
//_update_shapes();
|
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
_update_transform_dependant();
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::set_active(bool p_active) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (active == p_active)
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
active = p_active;
|
2014-02-19 15:57:14 +01:00
|
|
|
if (!p_active) {
|
|
|
|
if (get_space())
|
|
|
|
get_space()->body_remove_from_active_list(&active_list);
|
|
|
|
} else {
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_STATIC)
|
2014-02-19 15:57:14 +01:00
|
|
|
return; //static bodies can't become active
|
|
|
|
if (get_space())
|
|
|
|
get_space()->body_add_to_active_list(&active_list);
|
|
|
|
|
|
|
|
//still_time=0;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
/*
|
2014-02-19 15:57:14 +01:00
|
|
|
if (!space)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(int i=0;i<get_shape_count();i++) {
|
|
|
|
Shape &s=shapes[i];
|
|
|
|
if (s.bpid>0) {
|
|
|
|
get_space()->get_broadphase()->set_active(s.bpid,active);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::set_param(PhysicsServer3D::BodyParameter p_param, real_t p_value) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_param) {
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_BOUNCE: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bounce = p_value;
|
2014-02-19 15:57:14 +01:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_FRICTION: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
friction = p_value;
|
2014-02-19 15:57:14 +01:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_MASS: {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(p_value <= 0);
|
|
|
|
mass = p_value;
|
2014-02-19 15:57:14 +01:00
|
|
|
_update_inertia();
|
|
|
|
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: {
|
2017-03-05 16:44:50 +01:00
|
|
|
gravity_scale = p_value;
|
2015-08-30 23:57:17 +02:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: {
|
2015-08-30 23:57:17 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_damp = p_value;
|
2015-08-30 23:57:17 +02:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: {
|
2015-08-30 23:57:17 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
angular_damp = p_value;
|
2015-08-30 23:57:17 +02:00
|
|
|
} break;
|
2019-04-09 17:08:36 +02:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
real_t Body3DSW::get_param(PhysicsServer3D::BodyParameter p_param) const {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_param) {
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_BOUNCE: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
return bounce;
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_FRICTION: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
return friction;
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_MASS: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return mass;
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: {
|
2015-08-30 23:57:17 +02:00
|
|
|
return gravity_scale;
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: {
|
2015-08-30 23:57:17 +02:00
|
|
|
|
|
|
|
return linear_damp;
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: {
|
2015-08-30 23:57:17 +02:00
|
|
|
|
|
|
|
return angular_damp;
|
|
|
|
} break;
|
|
|
|
|
2019-04-09 17:08:36 +02:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::set_mode(PhysicsServer3D::BodyMode p_mode) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::BodyMode prev = mode;
|
2017-03-05 16:44:50 +01:00
|
|
|
mode = p_mode;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_mode) {
|
2014-02-19 15:57:14 +01:00
|
|
|
//CLEAR UP EVERYTHING IN CASE IT NOT WORKS!
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_MODE_STATIC:
|
|
|
|
case PhysicsServer3D::BODY_MODE_KINEMATIC: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
_set_inv_transform(get_transform().affine_inverse());
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = 0;
|
2020-03-27 19:21:27 +01:00
|
|
|
_set_static(p_mode == PhysicsServer3D::BODY_MODE_STATIC);
|
|
|
|
//set_active(p_mode==PhysicsServer3D::BODY_MODE_KINEMATIC);
|
|
|
|
set_active(p_mode == PhysicsServer3D::BODY_MODE_KINEMATIC && contacts.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_velocity = Vector3();
|
|
|
|
angular_velocity = Vector3();
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC && prev != mode) {
|
2017-03-05 16:44:50 +01:00
|
|
|
first_time_kinematic = true;
|
2014-10-14 06:01:25 +02:00
|
|
|
}
|
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_MODE_RIGID: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
_set_static(false);
|
2019-10-14 16:19:15 +02:00
|
|
|
set_active(true);
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_MODE_CHARACTER: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
_set_static(false);
|
2019-10-14 16:19:15 +02:00
|
|
|
set_active(true);
|
2019-08-15 13:34:47 +02:00
|
|
|
angular_velocity = Vector3();
|
2014-02-19 15:57:14 +01:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_update_inertia();
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
if (get_space())
|
|
|
|
_update_queries();
|
|
|
|
*/
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::BodyMode Body3DSW::get_mode() const {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::_shapes_changed() {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
_update_inertia();
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::set_state(PhysicsServer3D::BodyState p_state, const Variant &p_variant) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_state) {
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_STATE_TRANSFORM: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_transform = p_variant;
|
2014-09-03 04:13:40 +02:00
|
|
|
//wakeup_neighbours();
|
|
|
|
set_active(true);
|
2014-10-14 06:01:25 +02:00
|
|
|
if (first_time_kinematic) {
|
|
|
|
_set_transform(p_variant);
|
|
|
|
_set_inv_transform(get_transform().affine_inverse());
|
2017-03-05 16:44:50 +01:00
|
|
|
first_time_kinematic = false;
|
2014-10-14 06:01:25 +02:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
} else if (mode == PhysicsServer3D::BODY_MODE_STATIC) {
|
2014-02-19 15:57:14 +01:00
|
|
|
_set_transform(p_variant);
|
|
|
|
_set_inv_transform(get_transform().affine_inverse());
|
|
|
|
wakeup_neighbours();
|
|
|
|
} else {
|
|
|
|
Transform t = p_variant;
|
|
|
|
t.orthonormalize();
|
2017-03-05 16:44:50 +01:00
|
|
|
new_transform = get_transform(); //used as old to compute motion
|
|
|
|
if (new_transform == t)
|
2015-05-16 22:54:36 +02:00
|
|
|
break;
|
2014-02-19 15:57:14 +01:00
|
|
|
_set_transform(t);
|
|
|
|
_set_inv_transform(get_transform().inverse());
|
|
|
|
}
|
2015-04-26 21:20:00 +02:00
|
|
|
wakeup();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode==PhysicsServer3D::BODY_MODE_STATIC)
|
2017-01-14 12:26:56 +01:00
|
|
|
break;
|
|
|
|
*/
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_velocity = p_variant;
|
2015-04-26 21:20:00 +02:00
|
|
|
wakeup();
|
2014-02-19 15:57:14 +01:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: {
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode!=PhysicsServer3D::BODY_MODE_RIGID)
|
2017-01-14 12:26:56 +01:00
|
|
|
break;
|
|
|
|
*/
|
2017-03-05 16:44:50 +01:00
|
|
|
angular_velocity = p_variant;
|
2015-04-26 21:20:00 +02:00
|
|
|
wakeup();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_STATE_SLEEPING: {
|
2014-02-19 15:57:14 +01:00
|
|
|
//?
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_STATIC || mode == PhysicsServer3D::BODY_MODE_KINEMATIC)
|
2014-02-19 15:57:14 +01:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool do_sleep = p_variant;
|
2014-02-19 15:57:14 +01:00
|
|
|
if (do_sleep) {
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_velocity = Vector3();
|
2014-02-19 15:57:14 +01:00
|
|
|
//biased_linear_velocity=Vector3();
|
2017-03-05 16:44:50 +01:00
|
|
|
angular_velocity = Vector3();
|
2014-02-19 15:57:14 +01:00
|
|
|
//biased_angular_velocity=Vector3();
|
|
|
|
set_active(false);
|
|
|
|
} else {
|
2019-06-20 16:59:48 +02:00
|
|
|
set_active(true);
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_STATE_CAN_SLEEP: {
|
2017-03-05 16:44:50 +01:00
|
|
|
can_sleep = p_variant;
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_RIGID && !active && !can_sleep)
|
2014-02-19 15:57:14 +01:00
|
|
|
set_active(true);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
Variant Body3DSW::get_state(PhysicsServer3D::BodyState p_state) const {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_state) {
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_STATE_TRANSFORM: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return get_transform();
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return linear_velocity;
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return angular_velocity;
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_STATE_SLEEPING: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return !is_active();
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::BODY_STATE_CAN_SLEEP: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return can_sleep;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::set_space(Space3DSW *p_space) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
if (get_space()) {
|
|
|
|
|
|
|
|
if (inertia_update_list.in_list())
|
|
|
|
get_space()->body_remove_from_inertia_update_list(&inertia_update_list);
|
|
|
|
if (active_list.in_list())
|
|
|
|
get_space()->body_remove_from_active_list(&active_list);
|
|
|
|
if (direct_state_query_list.in_list())
|
|
|
|
get_space()->body_remove_from_state_query_list(&direct_state_query_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
_set_space(p_space);
|
|
|
|
|
|
|
|
if (get_space()) {
|
|
|
|
|
|
|
|
_update_inertia();
|
|
|
|
if (active)
|
|
|
|
get_space()->body_add_to_active_list(&active_list);
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
_update_queries();
|
|
|
|
if (is_active()) {
|
|
|
|
active=false;
|
|
|
|
set_active(true);
|
|
|
|
}
|
|
|
|
*/
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
first_integration = true;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::_compute_area_gravity_and_dampenings(const Area3DSW *p_area) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
if (p_area->is_gravity_point()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_area->get_gravity_distance_scale() > 0) {
|
2015-03-19 15:10:07 +01:00
|
|
|
Vector3 v = p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin();
|
2017-03-05 16:44:50 +01:00
|
|
|
gravity += v.normalized() * (p_area->get_gravity() / Math::pow(v.length() * p_area->get_gravity_distance_scale() + 1, 2));
|
2015-03-19 15:10:07 +01:00
|
|
|
} else {
|
|
|
|
gravity += (p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin()).normalized() * p_area->get_gravity();
|
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
} else {
|
2015-03-20 04:38:12 +01:00
|
|
|
gravity += p_area->get_gravity_vector() * p_area->get_gravity();
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
2015-12-08 18:44:29 +01:00
|
|
|
|
|
|
|
area_linear_damp += p_area->get_linear_damp();
|
|
|
|
area_angular_damp += p_area->get_angular_damp();
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool lock) {
|
2017-12-10 17:21:14 +01:00
|
|
|
if (lock) {
|
|
|
|
locked_axis |= p_axis;
|
|
|
|
} else {
|
|
|
|
locked_axis &= ~p_axis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
bool Body3DSW::is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const {
|
2017-12-10 17:21:14 +01:00
|
|
|
return locked_axis & p_axis;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::integrate_forces(real_t p_step) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_STATIC)
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
Area3DSW *def_area = get_space()->get_default_area();
|
2015-12-14 02:56:11 +01:00
|
|
|
// AreaSW *damp_area = def_area;
|
2015-08-30 23:57:17 +02:00
|
|
|
|
2015-03-20 04:38:12 +01:00
|
|
|
ERR_FAIL_COND(!def_area);
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
int ac = areas.size();
|
2015-12-14 02:56:11 +01:00
|
|
|
bool stopped = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
gravity = Vector3(0, 0, 0);
|
2015-12-08 18:44:29 +01:00
|
|
|
area_linear_damp = 0;
|
|
|
|
area_angular_damp = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
if (ac) {
|
2015-03-24 05:05:56 +01:00
|
|
|
areas.sort();
|
2014-02-19 15:57:14 +01:00
|
|
|
const AreaCMP *aa = &areas[0];
|
2015-12-14 02:56:11 +01:00
|
|
|
// damp_area = aa[ac-1].area;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = ac - 1; i >= 0 && !stopped; i--) {
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::AreaSpaceOverrideMode mode = aa[i].area->get_space_override_mode();
|
2015-12-14 02:56:11 +01:00
|
|
|
switch (mode) {
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE:
|
|
|
|
case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
|
2015-12-14 02:56:11 +01:00
|
|
|
_compute_area_gravity_and_dampenings(aa[i].area);
|
2020-03-27 19:21:27 +01:00
|
|
|
stopped = mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
|
2015-12-14 02:56:11 +01:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE:
|
|
|
|
case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
|
2017-03-05 16:44:50 +01:00
|
|
|
gravity = Vector3(0, 0, 0);
|
2015-12-14 02:56:11 +01:00
|
|
|
area_angular_damp = 0;
|
|
|
|
area_linear_damp = 0;
|
|
|
|
_compute_area_gravity_and_dampenings(aa[i].area);
|
2020-03-27 19:21:27 +01:00
|
|
|
stopped = mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE;
|
2015-12-14 02:56:11 +01:00
|
|
|
} break;
|
2019-04-09 17:08:36 +02:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-20 04:38:12 +01:00
|
|
|
}
|
2015-08-30 23:57:17 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!stopped) {
|
2015-12-08 18:44:29 +01:00
|
|
|
_compute_area_gravity_and_dampenings(def_area);
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
gravity *= gravity_scale;
|
2015-08-30 23:57:17 +02:00
|
|
|
|
2015-12-08 18:44:29 +01:00
|
|
|
// If less than 0, override dampenings with that of the Body
|
2017-03-05 16:44:50 +01:00
|
|
|
if (angular_damp >= 0)
|
|
|
|
area_angular_damp = angular_damp;
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
else
|
|
|
|
area_angular_damp=damp_area->get_angular_damp();
|
|
|
|
*/
|
2015-08-30 23:57:17 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (linear_damp >= 0)
|
|
|
|
area_linear_damp = linear_damp;
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
else
|
|
|
|
area_linear_damp=damp_area->get_linear_damp();
|
|
|
|
*/
|
2015-08-30 23:57:17 +02:00
|
|
|
|
2014-09-03 04:13:40 +02:00
|
|
|
Vector3 motion;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool do_motion = false;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2014-09-03 04:13:40 +02:00
|
|
|
//compute motion, angular and etc. velocities from prev transform
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_velocity = (new_transform.origin - get_transform().origin) / p_step;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2014-09-03 04:13:40 +02:00
|
|
|
//compute a FAKE angular velocity, not so easy
|
2017-03-05 16:44:50 +01:00
|
|
|
Basis rot = new_transform.basis.orthonormalized().transposed() * get_transform().basis.orthonormalized();
|
2014-09-03 04:13:40 +02:00
|
|
|
Vector3 axis;
|
2017-02-14 00:25:05 +01:00
|
|
|
real_t angle;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-04-06 00:47:13 +02:00
|
|
|
rot.get_axis_angle(axis, angle);
|
2014-09-03 04:13:40 +02:00
|
|
|
axis.normalize();
|
2017-03-05 16:44:50 +01:00
|
|
|
angular_velocity = axis.normalized() * (angle / p_step);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
motion = new_transform.origin - get_transform().origin;
|
2017-03-05 16:44:50 +01:00
|
|
|
do_motion = true;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
} else {
|
2016-01-02 17:28:18 +01:00
|
|
|
if (!omit_force_integration && !first_integration) {
|
2017-03-24 21:45:31 +01:00
|
|
|
//overridden by direct state query
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 force = gravity * mass;
|
|
|
|
force += applied_force;
|
|
|
|
Vector3 torque = applied_torque;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2015-08-30 23:57:17 +02:00
|
|
|
real_t damp = 1.0 - p_step * area_linear_damp;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (damp < 0) // reached zero in the given time
|
|
|
|
damp = 0;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2015-08-30 23:57:17 +02:00
|
|
|
real_t angular_damp = 1.0 - p_step * area_angular_damp;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (angular_damp < 0) // reached zero in the given time
|
|
|
|
angular_damp = 0;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_velocity *= damp;
|
|
|
|
angular_velocity *= angular_damp;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_velocity += _inv_mass * force * p_step;
|
|
|
|
angular_velocity += _inv_inertia_tensor.xform(torque) * p_step;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (continuous_cd) {
|
2017-03-05 16:44:50 +01:00
|
|
|
motion = linear_velocity * p_step;
|
|
|
|
do_motion = true;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
applied_force = Vector3();
|
|
|
|
applied_torque = Vector3();
|
|
|
|
first_integration = false;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
//motion=linear_velocity*p_step;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
biased_angular_velocity = Vector3();
|
|
|
|
biased_linear_velocity = Vector3();
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (do_motion) { //shapes temporarily extend for raycast
|
2014-09-03 04:13:40 +02:00
|
|
|
_update_shapes_with_motion(motion);
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
def_area = nullptr; // clear the area, so it is set in the next frame
|
2017-03-05 16:44:50 +01:00
|
|
|
contact_count = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::integrate_velocities(real_t p_step) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_STATIC)
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
|
|
|
|
2014-09-03 04:13:40 +02:00
|
|
|
if (fi_callback)
|
|
|
|
get_space()->body_add_to_state_query_list(&direct_state_query_list);
|
|
|
|
|
2017-12-10 17:21:14 +01:00
|
|
|
//apply axis lock linear
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2020-03-27 19:21:27 +01:00
|
|
|
if (is_axis_locked((PhysicsServer3D::BodyAxis)(1 << i))) {
|
2017-12-10 17:21:14 +01:00
|
|
|
linear_velocity[i] = 0;
|
|
|
|
biased_linear_velocity[i] = 0;
|
|
|
|
new_transform.origin[i] = get_transform().origin[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//apply axis lock angular
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2020-03-27 19:21:27 +01:00
|
|
|
if (is_axis_locked((PhysicsServer3D::BodyAxis)(1 << (i + 3)))) {
|
2017-12-10 17:21:14 +01:00
|
|
|
angular_velocity[i] = 0;
|
|
|
|
biased_angular_velocity[i] = 0;
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
|
2017-11-08 22:35:47 +01:00
|
|
|
|
|
|
|
_set_transform(new_transform, false);
|
|
|
|
_set_inv_transform(new_transform.affine_inverse());
|
|
|
|
if (contacts.size() == 0 && linear_velocity == Vector3() && angular_velocity == Vector3())
|
|
|
|
set_active(false); //stopped moving, deactivate
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 total_angular_velocity = angular_velocity + biased_angular_velocity;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-02-14 00:25:05 +01:00
|
|
|
real_t ang_vel = total_angular_velocity.length();
|
2014-02-19 15:57:14 +01:00
|
|
|
Transform transform = get_transform();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (ang_vel != 0.0) {
|
2014-02-19 15:57:14 +01:00
|
|
|
Vector3 ang_vel_axis = total_angular_velocity / ang_vel;
|
2017-03-05 16:44:50 +01:00
|
|
|
Basis rot(ang_vel_axis, ang_vel * p_step);
|
2017-01-11 04:52:51 +01:00
|
|
|
Basis identity3(1, 0, 0, 0, 1, 0, 0, 0, 1);
|
2016-12-31 15:39:25 +01:00
|
|
|
transform.origin += ((identity3 - rot) * transform.basis).xform(center_of_mass_local);
|
2014-02-19 15:57:14 +01:00
|
|
|
transform.basis = rot * transform.basis;
|
|
|
|
transform.orthonormalize();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 total_linear_velocity = linear_velocity + biased_linear_velocity;
|
2014-05-14 06:22:15 +02:00
|
|
|
/*for(int i=0;i<3;i++) {
|
|
|
|
if (axis_lock&(1<<i)) {
|
|
|
|
transform.origin[i]=0.0;
|
|
|
|
}
|
|
|
|
}*/
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
transform.origin += total_linear_velocity * p_step;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
_set_transform(transform);
|
|
|
|
_set_inv_transform(get_transform().inverse());
|
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
_update_transform_dependant();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
if (fi_callback) {
|
|
|
|
get_space()->body_add_to_state_query_list(&direct_state_query_list);
|
|
|
|
*/
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2014-09-03 04:13:40 +02:00
|
|
|
/*
|
2014-02-19 15:57:14 +01:00
|
|
|
void BodySW::simulate_motion(const Transform& p_xform,real_t p_step) {
|
|
|
|
|
|
|
|
Transform inv_xform = p_xform.affine_inverse();
|
|
|
|
if (!get_space()) {
|
|
|
|
_set_transform(p_xform);
|
|
|
|
_set_inv_transform(inv_xform);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//compute a FAKE linear velocity - this is easy
|
|
|
|
|
|
|
|
linear_velocity=(p_xform.origin - get_transform().origin)/p_step;
|
|
|
|
|
|
|
|
//compute a FAKE angular velocity, not so easy
|
2019-02-09 06:24:02 +01:00
|
|
|
Basis rot=get_transform().basis.orthonormalized().transposed() * p_xform.basis.orthonormalized();
|
2014-02-19 15:57:14 +01:00
|
|
|
Vector3 axis;
|
2017-02-14 00:25:05 +01:00
|
|
|
real_t angle;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-04-06 00:47:13 +02:00
|
|
|
rot.get_axis_angle(axis,angle);
|
2014-02-19 15:57:14 +01:00
|
|
|
axis.normalize();
|
|
|
|
angular_velocity=axis.normalized() * (angle/p_step);
|
|
|
|
linear_velocity = (p_xform.origin - get_transform().origin)/p_step;
|
|
|
|
|
|
|
|
if (!direct_state_query_list.in_list())// - callalways, so lv and av are cleared && (state_query || direct_state_query))
|
|
|
|
get_space()->body_add_to_state_query_list(&direct_state_query_list);
|
|
|
|
simulated_motion=true;
|
|
|
|
_set_transform(p_xform);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
*/
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::wakeup_neighbours() {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
for (Map<Constraint3DSW *, int>::Element *E = constraint_map.front(); E; E = E->next()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
const Constraint3DSW *c = E->key();
|
|
|
|
Body3DSW **n = c->get_body_ptr();
|
2017-03-05 16:44:50 +01:00
|
|
|
int bc = c->get_body_count();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < bc; i++) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (i == E->get())
|
2014-02-19 15:57:14 +01:00
|
|
|
continue;
|
2020-03-27 19:21:27 +01:00
|
|
|
Body3DSW *b = n[i];
|
|
|
|
if (b->mode != PhysicsServer3D::BODY_MODE_RIGID)
|
2014-02-19 15:57:14 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!b->is_active())
|
|
|
|
b->set_active(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::call_queries() {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
if (fi_callback) {
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsDirectBodyState3DSW *dbs = PhysicsDirectBodyState3DSW::singleton;
|
2017-03-05 16:44:50 +01:00
|
|
|
dbs->body = this;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Variant v = dbs;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
Object *obj = ObjectDB::get_instance(fi_callback->id);
|
|
|
|
if (!obj) {
|
|
|
|
|
2020-02-12 18:24:06 +01:00
|
|
|
set_force_integration_callback(ObjectID(), StringName());
|
2014-02-19 15:57:14 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
const Variant *vp[2] = { &v, &fi_callback->udata };
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
Callable::CallError ce;
|
2017-03-05 16:44:50 +01:00
|
|
|
int argc = (fi_callback->udata.get_type() == Variant::NIL) ? 1 : 2;
|
|
|
|
obj->call(fi_callback->method, vp, argc, ce);
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
bool Body3DSW::sleep_test(real_t p_step) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_STATIC || mode == PhysicsServer3D::BODY_MODE_KINEMATIC)
|
2014-02-19 15:57:14 +01:00
|
|
|
return true; //
|
2020-03-27 19:21:27 +01:00
|
|
|
else if (mode == PhysicsServer3D::BODY_MODE_CHARACTER)
|
2014-02-19 15:57:14 +01:00
|
|
|
return !active; // characters don't sleep unless asked to sleep
|
|
|
|
else if (!can_sleep)
|
|
|
|
return false;
|
|
|
|
|
2017-07-08 17:12:18 +02:00
|
|
|
if (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()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
still_time += p_step;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
return still_time > get_space()->get_body_time_to_sleep();
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
still_time = 0; //maybe this should be set to 0 on set_active?
|
2014-02-19 15:57:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
if (fi_callback) {
|
|
|
|
|
|
|
|
memdelete(fi_callback);
|
2020-04-02 01:20:12 +02:00
|
|
|
fi_callback = nullptr;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 18:24:06 +01:00
|
|
|
if (p_id.is_valid()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
fi_callback = memnew(ForceIntegrationCallback);
|
|
|
|
fi_callback->id = p_id;
|
|
|
|
fi_callback->method = p_method;
|
|
|
|
fi_callback->udata = p_udata;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::set_kinematic_margin(real_t p_margin) {
|
2017-11-07 15:22:09 +01:00
|
|
|
kinematic_safe_margin = p_margin;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
Body3DSW::Body3DSW() :
|
|
|
|
CollisionObject3DSW(TYPE_BODY),
|
2018-09-28 17:17:38 +02:00
|
|
|
locked_axis(0),
|
2017-12-06 21:36:34 +01:00
|
|
|
active_list(this),
|
|
|
|
inertia_update_list(this),
|
2018-09-28 17:17:38 +02:00
|
|
|
direct_state_query_list(this) {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
mode = PhysicsServer3D::BODY_MODE_RIGID;
|
2017-03-05 16:44:50 +01:00
|
|
|
active = true;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
mass = 1;
|
2017-11-07 15:22:09 +01:00
|
|
|
kinematic_safe_margin = 0.01;
|
2017-01-14 12:26:56 +01:00
|
|
|
//_inv_inertia=Transform();
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = 1;
|
|
|
|
bounce = 0;
|
|
|
|
friction = 1;
|
|
|
|
omit_force_integration = false;
|
2017-01-14 12:26:56 +01:00
|
|
|
//applied_torque=0;
|
2017-03-05 16:44:50 +01:00
|
|
|
island_step = 0;
|
2020-04-02 01:20:12 +02:00
|
|
|
island_next = nullptr;
|
|
|
|
island_list_next = nullptr;
|
2017-03-05 16:44:50 +01:00
|
|
|
first_time_kinematic = false;
|
|
|
|
first_integration = false;
|
2014-02-19 15:57:14 +01:00
|
|
|
_set_static(false);
|
2015-08-30 23:57:17 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
contact_count = 0;
|
|
|
|
gravity_scale = 1.0;
|
2017-07-25 04:20:32 +02:00
|
|
|
linear_damp = -1;
|
|
|
|
angular_damp = -1;
|
2017-03-05 16:44:50 +01:00
|
|
|
area_angular_damp = 0;
|
|
|
|
area_linear_damp = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
still_time = 0;
|
|
|
|
continuous_cd = false;
|
2019-10-12 08:02:03 +02:00
|
|
|
can_sleep = true;
|
2020-04-02 01:20:12 +02:00
|
|
|
fi_callback = nullptr;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
Body3DSW::~Body3DSW() {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
if (fi_callback)
|
|
|
|
memdelete(fi_callback);
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
PhysicsDirectBodyState3DSW *PhysicsDirectBodyState3DSW::singleton = nullptr;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsDirectSpaceState3D *PhysicsDirectBodyState3DSW::get_space_state() {
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
return body->get_space()->get_direct_state();
|
|
|
|
}
|