2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2021-10-19 03:24:30 +08:00
|
|
|
/* godot_body_3d.cpp */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2021-01-02 03:13:46 +08:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 09:10:30 +08: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 07:50:27 +08:00
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
#include "godot_body_3d.h"
|
2021-06-05 07:09:41 +08:00
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
#include "godot_area_3d.h"
|
|
|
|
#include "godot_body_direct_state_3d.h"
|
|
|
|
#include "godot_space_3d.h"
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::_mass_properties_changed() {
|
2021-06-11 08:37:19 +08:00
|
|
|
if (get_space() && !mass_properties_update_list.in_list() && (calculate_inertia || calculate_center_of_mass)) {
|
|
|
|
get_space()->body_add_to_mass_properties_update_list(&mass_properties_update_list);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-20 07:16:00 +08:00
|
|
|
void GodotBody3D::_update_transform_dependent() {
|
2016-12-31 22:39:25 +08:00
|
|
|
center_of_mass = get_transform().basis.xform(center_of_mass_local);
|
|
|
|
principal_inertia_axes = get_transform().basis * principal_inertia_axes_local;
|
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
// Update inertia tensor.
|
2017-01-11 11:52:51 +08:00
|
|
|
Basis tb = principal_inertia_axes;
|
|
|
|
Basis tbt = tb.transposed();
|
2017-09-17 00:50:46 +08:00
|
|
|
Basis diag;
|
|
|
|
diag.scale(_inv_inertia);
|
|
|
|
_inv_inertia_tensor = tb * diag * tbt;
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::update_mass_properties() {
|
2021-03-09 05:11:55 +08:00
|
|
|
// Update shapes and motions.
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
switch (mode) {
|
2021-05-20 09:15:07 +08:00
|
|
|
case PhysicsServer3D::BODY_MODE_DYNAMIC: {
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t total_area = 0;
|
|
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
2021-06-11 08:37:19 +08:00
|
|
|
if (is_shape_disabled(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
total_area += get_shape_area(i);
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
if (calculate_center_of_mass) {
|
|
|
|
// We have to recompute the center of mass.
|
|
|
|
center_of_mass_local.zero();
|
2016-12-31 22:39:25 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
if (total_area != 0.0) {
|
|
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
|
|
|
if (is_shape_disabled(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-31 22:39:25 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
real_t area = get_shape_area(i);
|
2016-12-31 22:39:25 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
real_t mass = area * this->mass / total_area;
|
|
|
|
|
|
|
|
// 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 22:57:14 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
center_of_mass_local /= mass;
|
|
|
|
}
|
2021-05-29 20:11:59 +08:00
|
|
|
}
|
2016-12-31 22:39:25 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
if (calculate_inertia) {
|
|
|
|
// Recompute the inertia tensor.
|
|
|
|
Basis inertia_tensor;
|
|
|
|
inertia_tensor.set_zero();
|
|
|
|
bool inertia_set = false;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
|
|
|
if (is_shape_disabled(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-08 05:09:39 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
real_t area = get_shape_area(i);
|
|
|
|
if (area == 0.0) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-29 20:11:59 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
inertia_set = true;
|
2021-03-09 05:11:55 +08:00
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
const GodotShape3D *shape = get_shape(i);
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
real_t mass = area * this->mass / total_area;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2021-09-07 04:39:01 +08:00
|
|
|
Basis shape_inertia_tensor = Basis::from_scale(shape->get_moment_of_inertia(mass));
|
2021-06-11 08:37:19 +08:00
|
|
|
Transform3D shape_transform = get_shape_transform(i);
|
|
|
|
Basis shape_basis = shape_transform.basis.orthonormalized();
|
2016-12-31 22:39:25 +08:00
|
|
|
|
2021-06-11 08:37:19 +08: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();
|
2016-12-31 22:39:25 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
Vector3 shape_origin = shape_transform.origin - center_of_mass_local;
|
|
|
|
inertia_tensor += shape_inertia_tensor + (Basis() * shape_origin.dot(shape_origin) - shape_origin.outer(shape_origin)) * mass;
|
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
// Set the inertia to a valid value when there are no valid shapes.
|
|
|
|
if (!inertia_set) {
|
2021-09-07 04:39:01 +08:00
|
|
|
inertia_tensor = Basis();
|
2021-06-11 08:37:19 +08:00
|
|
|
}
|
2021-03-09 05:11:55 +08:00
|
|
|
|
2021-06-11 08:37:19 +08:00
|
|
|
// Handle partial custom inertia.
|
|
|
|
if (inertia.x > 0.0) {
|
|
|
|
inertia_tensor[0][0] = inertia.x;
|
|
|
|
}
|
|
|
|
if (inertia.y > 0.0) {
|
|
|
|
inertia_tensor[1][1] = inertia.y;
|
|
|
|
}
|
|
|
|
if (inertia.z > 0.0) {
|
|
|
|
inertia_tensor[2][2] = inertia.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 22:57:14 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (mass) {
|
2017-03-05 23:44:50 +08:00
|
|
|
_inv_mass = 1.0 / mass;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2017-03-05 23:44:50 +08:00
|
|
|
_inv_mass = 0;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_MODE_KINEMATIC:
|
|
|
|
case PhysicsServer3D::BODY_MODE_STATIC: {
|
2021-06-11 08:37:19 +08:00
|
|
|
_inv_inertia = Vector3();
|
2017-03-05 23:44:50 +08:00
|
|
|
_inv_mass = 0;
|
2014-02-19 22:57:14 +08:00
|
|
|
} break;
|
2021-09-17 03:37:24 +08:00
|
|
|
case PhysicsServer3D::BODY_MODE_DYNAMIC_LINEAR: {
|
2016-12-31 22:39:25 +08:00
|
|
|
_inv_inertia_tensor.set_zero();
|
2017-03-05 23:44:50 +08:00
|
|
|
_inv_mass = 1.0 / mass;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
2016-12-31 22:39:25 +08:00
|
|
|
|
2021-10-20 07:16:00 +08:00
|
|
|
_update_transform_dependent();
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::reset_mass_properties() {
|
2021-06-11 08:37:19 +08:00
|
|
|
calculate_inertia = true;
|
|
|
|
calculate_center_of_mass = true;
|
|
|
|
_mass_properties_changed();
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::set_active(bool p_active) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (active == p_active) {
|
2014-02-19 22:57:14 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
active = p_active;
|
2021-04-20 09:38:11 +08:00
|
|
|
|
|
|
|
if (active) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_STATIC) {
|
2021-04-20 09:38:11 +08:00
|
|
|
// Static bodies can't be active.
|
|
|
|
active = false;
|
|
|
|
} else if (get_space()) {
|
2014-02-19 22:57:14 +08:00
|
|
|
get_space()->body_add_to_active_list(&active_list);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2021-04-20 09:38:11 +08:00
|
|
|
} else if (get_space()) {
|
|
|
|
get_space()->body_remove_from_active_list(&active_list);
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::set_param(PhysicsServer3D::BodyParameter p_param, const Variant &p_value) {
|
2017-03-05 23:44:50 +08:00
|
|
|
switch (p_param) {
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_BOUNCE: {
|
2017-03-05 23:44:50 +08:00
|
|
|
bounce = p_value;
|
2014-02-19 22:57:14 +08:00
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_FRICTION: {
|
2017-03-05 23:44:50 +08:00
|
|
|
friction = p_value;
|
2014-02-19 22:57:14 +08:00
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_MASS: {
|
2021-06-11 08:37:19 +08:00
|
|
|
real_t mass_value = p_value;
|
|
|
|
ERR_FAIL_COND(mass_value <= 0);
|
|
|
|
mass = mass_value;
|
|
|
|
if (mode >= PhysicsServer3D::BODY_MODE_DYNAMIC) {
|
|
|
|
_mass_properties_changed();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case PhysicsServer3D::BODY_PARAM_INERTIA: {
|
|
|
|
inertia = p_value;
|
|
|
|
if ((inertia.x <= 0.0) || (inertia.y <= 0.0) || (inertia.z <= 0.0)) {
|
|
|
|
calculate_inertia = true;
|
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_DYNAMIC) {
|
|
|
|
_mass_properties_changed();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
calculate_inertia = false;
|
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_DYNAMIC) {
|
2021-09-07 04:39:01 +08:00
|
|
|
principal_inertia_axes_local = Basis();
|
2021-06-11 08:37:19 +08:00
|
|
|
_inv_inertia = inertia.inverse();
|
2021-10-20 07:16:00 +08:00
|
|
|
_update_transform_dependent();
|
2021-06-11 08:37:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS: {
|
|
|
|
calculate_center_of_mass = false;
|
|
|
|
center_of_mass_local = p_value;
|
2021-10-20 07:16:00 +08:00
|
|
|
_update_transform_dependent();
|
2014-02-19 22:57:14 +08:00
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: {
|
2017-03-05 23:44:50 +08:00
|
|
|
gravity_scale = p_value;
|
2015-08-31 05:57:17 +08:00
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: {
|
2017-03-05 23:44:50 +08:00
|
|
|
linear_damp = p_value;
|
2015-08-31 05:57:17 +08:00
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: {
|
2017-03-05 23:44:50 +08:00
|
|
|
angular_damp = p_value;
|
2015-08-31 05:57:17 +08:00
|
|
|
} break;
|
2019-04-09 23:08:36 +08:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
Variant GodotBody3D::get_param(PhysicsServer3D::BodyParameter p_param) const {
|
2017-03-05 23:44:50 +08:00
|
|
|
switch (p_param) {
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_BOUNCE: {
|
2014-02-19 22:57:14 +08:00
|
|
|
return bounce;
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_FRICTION: {
|
2014-02-19 22:57:14 +08:00
|
|
|
return friction;
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_MASS: {
|
2014-02-19 22:57:14 +08:00
|
|
|
return mass;
|
|
|
|
} break;
|
2021-06-11 08:37:19 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_INERTIA: {
|
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_DYNAMIC) {
|
|
|
|
return _inv_inertia.inverse();
|
|
|
|
} else {
|
|
|
|
return Vector3();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS: {
|
|
|
|
return center_of_mass;
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: {
|
2015-08-31 05:57:17 +08:00
|
|
|
return gravity_scale;
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: {
|
2015-08-31 05:57:17 +08:00
|
|
|
return linear_damp;
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: {
|
2015-08-31 05:57:17 +08:00
|
|
|
return angular_damp;
|
|
|
|
} break;
|
|
|
|
|
2019-04-09 23:08:36 +08:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::set_mode(PhysicsServer3D::BodyMode p_mode) {
|
2020-03-28 02:21:27 +08:00
|
|
|
PhysicsServer3D::BodyMode prev = mode;
|
2017-03-05 23:44:50 +08:00
|
|
|
mode = p_mode;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
switch (p_mode) {
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_MODE_STATIC:
|
|
|
|
case PhysicsServer3D::BODY_MODE_KINEMATIC: {
|
2014-02-19 22:57:14 +08:00
|
|
|
_set_inv_transform(get_transform().affine_inverse());
|
2017-03-05 23:44:50 +08:00
|
|
|
_inv_mass = 0;
|
2021-06-11 08:37:19 +08:00
|
|
|
_inv_inertia = Vector3();
|
2020-03-28 02:21:27 +08:00
|
|
|
_set_static(p_mode == PhysicsServer3D::BODY_MODE_STATIC);
|
|
|
|
set_active(p_mode == PhysicsServer3D::BODY_MODE_KINEMATIC && contacts.size());
|
2017-03-05 23:44:50 +08:00
|
|
|
linear_velocity = Vector3();
|
|
|
|
angular_velocity = Vector3();
|
2020-03-28 02:21:27 +08:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC && prev != mode) {
|
2017-03-05 23:44:50 +08:00
|
|
|
first_time_kinematic = true;
|
2014-10-14 12:01:25 +08:00
|
|
|
}
|
2021-10-20 07:16:00 +08:00
|
|
|
_update_transform_dependent();
|
2014-10-14 12:01:25 +08:00
|
|
|
|
2014-02-19 22:57:14 +08:00
|
|
|
} break;
|
2021-05-20 09:15:07 +08:00
|
|
|
case PhysicsServer3D::BODY_MODE_DYNAMIC: {
|
2017-03-05 23:44:50 +08:00
|
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
2021-06-11 08:37:19 +08:00
|
|
|
if (!calculate_inertia) {
|
2021-09-07 04:39:01 +08:00
|
|
|
principal_inertia_axes_local = Basis();
|
2021-06-11 08:37:19 +08:00
|
|
|
_inv_inertia = inertia.inverse();
|
2021-10-20 07:16:00 +08:00
|
|
|
_update_transform_dependent();
|
2021-06-11 08:37:19 +08:00
|
|
|
}
|
|
|
|
_mass_properties_changed();
|
2014-02-19 22:57:14 +08:00
|
|
|
_set_static(false);
|
2019-10-14 22:19:15 +08:00
|
|
|
set_active(true);
|
2014-02-19 22:57:14 +08:00
|
|
|
|
|
|
|
} break;
|
2021-09-17 03:37:24 +08:00
|
|
|
case PhysicsServer3D::BODY_MODE_DYNAMIC_LINEAR: {
|
2017-03-05 23:44:50 +08:00
|
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
2021-06-11 08:37:19 +08:00
|
|
|
_inv_inertia = Vector3();
|
|
|
|
angular_velocity = Vector3();
|
2021-10-20 07:16:00 +08:00
|
|
|
_update_transform_dependent();
|
2014-02-19 22:57:14 +08:00
|
|
|
_set_static(false);
|
2019-10-14 22:19:15 +08:00
|
|
|
set_active(true);
|
2021-06-11 08:37:19 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 20:29:06 +08:00
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
PhysicsServer3D::BodyMode GodotBody3D::get_mode() const {
|
2014-02-19 22:57:14 +08:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::_shapes_changed() {
|
2021-06-11 08:37:19 +08:00
|
|
|
_mass_properties_changed();
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::set_state(PhysicsServer3D::BodyState p_state, const Variant &p_variant) {
|
2017-03-05 23:44:50 +08:00
|
|
|
switch (p_state) {
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_STATE_TRANSFORM: {
|
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
|
2017-03-05 23:44:50 +08:00
|
|
|
new_transform = p_variant;
|
2014-09-03 10:13:40 +08:00
|
|
|
//wakeup_neighbours();
|
|
|
|
set_active(true);
|
2014-10-14 12:01:25 +08:00
|
|
|
if (first_time_kinematic) {
|
|
|
|
_set_transform(p_variant);
|
|
|
|
_set_inv_transform(get_transform().affine_inverse());
|
2017-03-05 23:44:50 +08:00
|
|
|
first_time_kinematic = false;
|
2014-10-14 12:01:25 +08:00
|
|
|
}
|
2014-09-03 10:13:40 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
} else if (mode == PhysicsServer3D::BODY_MODE_STATIC) {
|
2014-02-19 22:57:14 +08:00
|
|
|
_set_transform(p_variant);
|
|
|
|
_set_inv_transform(get_transform().affine_inverse());
|
|
|
|
wakeup_neighbours();
|
|
|
|
} else {
|
2020-10-17 13:08:21 +08:00
|
|
|
Transform3D t = p_variant;
|
2014-02-19 22:57:14 +08:00
|
|
|
t.orthonormalize();
|
2017-03-05 23:44:50 +08:00
|
|
|
new_transform = get_transform(); //used as old to compute motion
|
2020-05-14 22:41:43 +08:00
|
|
|
if (new_transform == t) {
|
2015-05-17 04:54:36 +08:00
|
|
|
break;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
_set_transform(t);
|
|
|
|
_set_inv_transform(get_transform().inverse());
|
2021-10-20 07:16:00 +08:00
|
|
|
_update_transform_dependent();
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
2015-04-27 03:20:00 +08:00
|
|
|
wakeup();
|
2014-02-19 22:57:14 +08:00
|
|
|
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: {
|
2017-03-05 23:44:50 +08:00
|
|
|
linear_velocity = p_variant;
|
2021-08-31 02:48:43 +08:00
|
|
|
constant_linear_velocity = linear_velocity;
|
2015-04-27 03:20:00 +08:00
|
|
|
wakeup();
|
2014-02-19 22:57:14 +08:00
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: {
|
2017-03-05 23:44:50 +08:00
|
|
|
angular_velocity = p_variant;
|
2021-08-31 02:48:43 +08:00
|
|
|
constant_angular_velocity = angular_velocity;
|
2015-04-27 03:20:00 +08:00
|
|
|
wakeup();
|
2014-02-19 22:57:14 +08:00
|
|
|
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_STATE_SLEEPING: {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_STATIC || mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
|
2014-02-19 22:57:14 +08:00
|
|
|
break;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
bool do_sleep = p_variant;
|
2014-02-19 22:57:14 +08:00
|
|
|
if (do_sleep) {
|
2017-03-05 23:44:50 +08:00
|
|
|
linear_velocity = Vector3();
|
2014-02-19 22:57:14 +08:00
|
|
|
//biased_linear_velocity=Vector3();
|
2017-03-05 23:44:50 +08:00
|
|
|
angular_velocity = Vector3();
|
2014-02-19 22:57:14 +08:00
|
|
|
//biased_angular_velocity=Vector3();
|
|
|
|
set_active(false);
|
|
|
|
} else {
|
2019-06-20 22:59:48 +08:00
|
|
|
set_active(true);
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_STATE_CAN_SLEEP: {
|
2017-03-05 23:44:50 +08:00
|
|
|
can_sleep = p_variant;
|
2021-06-11 08:37:19 +08:00
|
|
|
if (mode >= PhysicsServer3D::BODY_MODE_DYNAMIC && !active && !can_sleep) {
|
2014-02-19 22:57:14 +08:00
|
|
|
set_active(true);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 20:29:06 +08:00
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
Variant GodotBody3D::get_state(PhysicsServer3D::BodyState p_state) const {
|
2017-03-05 23:44:50 +08:00
|
|
|
switch (p_state) {
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_STATE_TRANSFORM: {
|
2014-02-19 22:57:14 +08:00
|
|
|
return get_transform();
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: {
|
2014-02-19 22:57:14 +08:00
|
|
|
return linear_velocity;
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: {
|
2014-02-19 22:57:14 +08:00
|
|
|
return angular_velocity;
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_STATE_SLEEPING: {
|
2014-02-19 22:57:14 +08:00
|
|
|
return !is_active();
|
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::BODY_STATE_CAN_SLEEP: {
|
2014-02-19 22:57:14 +08:00
|
|
|
return can_sleep;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::set_space(GodotSpace3D *p_space) {
|
2014-02-19 22:57:14 +08:00
|
|
|
if (get_space()) {
|
2021-06-11 08:37:19 +08:00
|
|
|
if (mass_properties_update_list.in_list()) {
|
|
|
|
get_space()->body_remove_from_mass_properties_update_list(&mass_properties_update_list);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (active_list.in_list()) {
|
2014-02-19 22:57:14 +08:00
|
|
|
get_space()->body_remove_from_active_list(&active_list);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (direct_state_query_list.in_list()) {
|
2014-02-19 22:57:14 +08:00
|
|
|
get_space()->body_remove_from_state_query_list(&direct_state_query_list);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_set_space(p_space);
|
|
|
|
|
|
|
|
if (get_space()) {
|
2021-06-11 08:37:19 +08:00
|
|
|
_mass_properties_changed();
|
2020-05-14 22:41:43 +08:00
|
|
|
if (active) {
|
2014-02-19 22:57:14 +08:00
|
|
|
get_space()->body_add_to_active_list(&active_list);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::_compute_area_gravity_and_damping(const GodotArea3D *p_area) {
|
2021-08-25 00:30:31 +08:00
|
|
|
Vector3 area_gravity;
|
|
|
|
p_area->compute_gravity(get_transform().get_origin(), area_gravity);
|
|
|
|
gravity += area_gravity;
|
2015-12-09 01:44:29 +08:00
|
|
|
|
|
|
|
area_linear_damp += p_area->get_linear_damp();
|
|
|
|
area_angular_damp += p_area->get_angular_damp();
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool lock) {
|
2017-12-11 00:21:14 +08:00
|
|
|
if (lock) {
|
|
|
|
locked_axis |= p_axis;
|
|
|
|
} else {
|
|
|
|
locked_axis &= ~p_axis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
bool GodotBody3D::is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const {
|
2017-12-11 00:21:14 +08:00
|
|
|
return locked_axis & p_axis;
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::integrate_forces(real_t p_step) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_STATIC) {
|
2014-02-19 22:57:14 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
GodotArea3D *def_area = get_space()->get_default_area();
|
2015-08-31 05:57:17 +08:00
|
|
|
|
2015-03-20 11:38:12 +08:00
|
|
|
ERR_FAIL_COND(!def_area);
|
2014-02-19 22:57:14 +08:00
|
|
|
|
|
|
|
int ac = areas.size();
|
2015-12-14 09:56:11 +08:00
|
|
|
bool stopped = false;
|
2017-03-05 23:44:50 +08:00
|
|
|
gravity = Vector3(0, 0, 0);
|
2015-12-09 01:44:29 +08:00
|
|
|
area_linear_damp = 0;
|
|
|
|
area_angular_damp = 0;
|
2014-02-19 22:57:14 +08:00
|
|
|
if (ac) {
|
2015-03-24 12:05:56 +08:00
|
|
|
areas.sort();
|
2014-02-19 22:57:14 +08:00
|
|
|
const AreaCMP *aa = &areas[0];
|
2015-12-14 09:56:11 +08:00
|
|
|
// damp_area = aa[ac-1].area;
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = ac - 1; i >= 0 && !stopped; i--) {
|
2020-03-28 02:21:27 +08:00
|
|
|
PhysicsServer3D::AreaSpaceOverrideMode mode = aa[i].area->get_space_override_mode();
|
2015-12-14 09:56:11 +08:00
|
|
|
switch (mode) {
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE:
|
|
|
|
case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
|
2021-08-25 00:30:31 +08:00
|
|
|
_compute_area_gravity_and_damping(aa[i].area);
|
2020-03-28 02:21:27 +08:00
|
|
|
stopped = mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
|
2015-12-14 09:56:11 +08:00
|
|
|
} break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE:
|
|
|
|
case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
|
2017-03-05 23:44:50 +08:00
|
|
|
gravity = Vector3(0, 0, 0);
|
2015-12-14 09:56:11 +08:00
|
|
|
area_angular_damp = 0;
|
|
|
|
area_linear_damp = 0;
|
2021-08-25 00:30:31 +08:00
|
|
|
_compute_area_gravity_and_damping(aa[i].area);
|
2020-03-28 02:21:27 +08:00
|
|
|
stopped = mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE;
|
2015-12-14 09:56:11 +08:00
|
|
|
} break;
|
2019-04-09 23:08:36 +08:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
}
|
2015-03-20 11:38:12 +08:00
|
|
|
}
|
2015-08-31 05:57:17 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (!stopped) {
|
2021-08-25 00:30:31 +08:00
|
|
|
_compute_area_gravity_and_damping(def_area);
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
gravity *= gravity_scale;
|
2015-08-31 05:57:17 +08:00
|
|
|
|
2015-12-09 01:44:29 +08:00
|
|
|
// If less than 0, override dampenings with that of the Body
|
2020-05-14 22:41:43 +08:00
|
|
|
if (angular_damp >= 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
area_angular_damp = angular_damp;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-01-14 19:26:56 +08:00
|
|
|
/*
|
|
|
|
else
|
|
|
|
area_angular_damp=damp_area->get_angular_damp();
|
|
|
|
*/
|
2015-08-31 05:57:17 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (linear_damp >= 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
area_linear_damp = linear_damp;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-01-14 19:26:56 +08:00
|
|
|
/*
|
|
|
|
else
|
|
|
|
area_linear_damp=damp_area->get_linear_damp();
|
|
|
|
*/
|
2015-08-31 05:57:17 +08:00
|
|
|
|
2014-09-03 10:13:40 +08:00
|
|
|
Vector3 motion;
|
2017-03-05 23:44:50 +08:00
|
|
|
bool do_motion = false;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
|
2014-09-03 10:13:40 +08:00
|
|
|
//compute motion, angular and etc. velocities from prev transform
|
2021-03-18 21:14:19 +08:00
|
|
|
motion = new_transform.origin - get_transform().origin;
|
|
|
|
do_motion = true;
|
2021-08-31 02:48:43 +08:00
|
|
|
linear_velocity = constant_linear_velocity + motion / p_step;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2014-09-03 10:13:40 +08:00
|
|
|
//compute a FAKE angular velocity, not so easy
|
2021-03-18 21:14:19 +08:00
|
|
|
Basis rot = new_transform.basis.orthonormalized() * get_transform().basis.orthonormalized().transposed();
|
2014-09-03 10:13:40 +08:00
|
|
|
Vector3 axis;
|
2017-02-14 07:25:05 +08:00
|
|
|
real_t angle;
|
2014-09-03 10:13:40 +08:00
|
|
|
|
2017-04-06 06:47:13 +08:00
|
|
|
rot.get_axis_angle(axis, angle);
|
2014-09-03 10:13:40 +08:00
|
|
|
axis.normalize();
|
2021-08-31 02:48:43 +08:00
|
|
|
angular_velocity = constant_angular_velocity + axis * (angle / p_step);
|
2014-09-03 10:13:40 +08:00
|
|
|
} else {
|
2021-06-11 08:37:19 +08:00
|
|
|
if (!omit_force_integration) {
|
2017-03-25 04:45:31 +08:00
|
|
|
//overridden by direct state query
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 force = gravity * mass;
|
|
|
|
force += applied_force;
|
|
|
|
Vector3 torque = applied_torque;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2015-08-31 05:57:17 +08:00
|
|
|
real_t damp = 1.0 - p_step * area_linear_damp;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (damp < 0) { // reached zero in the given time
|
2017-03-05 23:44:50 +08:00
|
|
|
damp = 0;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-09-03 10:13:40 +08:00
|
|
|
|
2015-08-31 05:57:17 +08:00
|
|
|
real_t angular_damp = 1.0 - p_step * area_angular_damp;
|
2014-09-03 10:13:40 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (angular_damp < 0) { // reached zero in the given time
|
2017-03-05 23:44:50 +08:00
|
|
|
angular_damp = 0;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-09-03 10:13:40 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
linear_velocity *= damp;
|
|
|
|
angular_velocity *= angular_damp;
|
2014-09-03 10:13:40 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
linear_velocity += _inv_mass * force * p_step;
|
|
|
|
angular_velocity += _inv_inertia_tensor.xform(torque) * p_step;
|
2014-09-03 10:13:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (continuous_cd) {
|
2017-03-05 23:44:50 +08:00
|
|
|
motion = linear_velocity * p_step;
|
|
|
|
do_motion = true;
|
2014-09-03 10:13:40 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
applied_force = Vector3();
|
|
|
|
applied_torque = Vector3();
|
2014-02-19 22:57:14 +08:00
|
|
|
|
|
|
|
//motion=linear_velocity*p_step;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
biased_angular_velocity = Vector3();
|
|
|
|
biased_linear_velocity = Vector3();
|
2014-09-03 10:13:40 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (do_motion) { //shapes temporarily extend for raycast
|
2014-09-03 10:13:40 +08:00
|
|
|
_update_shapes_with_motion(motion);
|
|
|
|
}
|
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
def_area = nullptr; // clear the area, so it is set in the next frame
|
2017-03-05 23:44:50 +08:00
|
|
|
contact_count = 0;
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::integrate_velocities(real_t p_step) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_STATIC) {
|
2014-02-19 22:57:14 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2021-06-05 07:09:41 +08:00
|
|
|
if (fi_callback_data || body_state_callback) {
|
2014-09-03 10:13:40 +08:00
|
|
|
get_space()->body_add_to_state_query_list(&direct_state_query_list);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-09-03 10:13:40 +08:00
|
|
|
|
2017-12-11 00:21:14 +08:00
|
|
|
//apply axis lock linear
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2020-03-28 02:21:27 +08:00
|
|
|
if (is_axis_locked((PhysicsServer3D::BodyAxis)(1 << i))) {
|
2017-12-11 00:21:14 +08: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-28 02:21:27 +08:00
|
|
|
if (is_axis_locked((PhysicsServer3D::BodyAxis)(1 << (i + 3)))) {
|
2017-12-11 00:21:14 +08:00
|
|
|
angular_velocity[i] = 0;
|
|
|
|
biased_angular_velocity[i] = 0;
|
2014-05-14 12:22:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
|
2017-11-09 05:35:47 +08:00
|
|
|
_set_transform(new_transform, false);
|
|
|
|
_set_inv_transform(new_transform.affine_inverse());
|
2020-05-14 22:41:43 +08:00
|
|
|
if (contacts.size() == 0 && linear_velocity == Vector3() && angular_velocity == Vector3()) {
|
2017-11-09 05:35:47 +08:00
|
|
|
set_active(false); //stopped moving, deactivate
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-11-09 05:35:47 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 total_angular_velocity = angular_velocity + biased_angular_velocity;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2017-02-14 07:25:05 +08:00
|
|
|
real_t ang_vel = total_angular_velocity.length();
|
2020-10-17 13:08:21 +08:00
|
|
|
Transform3D transform = get_transform();
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2021-08-17 02:05:11 +08:00
|
|
|
if (!Math::is_zero_approx(ang_vel)) {
|
2014-02-19 22:57:14 +08:00
|
|
|
Vector3 ang_vel_axis = total_angular_velocity / ang_vel;
|
2017-03-05 23:44:50 +08:00
|
|
|
Basis rot(ang_vel_axis, ang_vel * p_step);
|
2017-01-11 11:52:51 +08:00
|
|
|
Basis identity3(1, 0, 0, 0, 1, 0, 0, 0, 1);
|
2016-12-31 22:39:25 +08:00
|
|
|
transform.origin += ((identity3 - rot) * transform.basis).xform(center_of_mass_local);
|
2014-02-19 22:57:14 +08:00
|
|
|
transform.basis = rot * transform.basis;
|
|
|
|
transform.orthonormalize();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 total_linear_velocity = linear_velocity + biased_linear_velocity;
|
2014-05-14 12:22:15 +08:00
|
|
|
/*for(int i=0;i<3;i++) {
|
|
|
|
if (axis_lock&(1<<i)) {
|
|
|
|
transform.origin[i]=0.0;
|
|
|
|
}
|
|
|
|
}*/
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
transform.origin += total_linear_velocity * p_step;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
|
|
|
_set_transform(transform);
|
|
|
|
_set_inv_transform(get_transform().inverse());
|
|
|
|
|
2021-10-20 07:16:00 +08:00
|
|
|
_update_transform_dependent();
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::wakeup_neighbours() {
|
|
|
|
for (const KeyValue<GodotConstraint3D *, int> &E : constraint_map) {
|
|
|
|
const GodotConstraint3D *c = E.key;
|
|
|
|
GodotBody3D **n = c->get_body_ptr();
|
2017-03-05 23:44:50 +08:00
|
|
|
int bc = c->get_body_count();
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < bc; i++) {
|
2021-08-10 04:13:42 +08:00
|
|
|
if (i == E.value) {
|
2014-02-19 22:57:14 +08:00
|
|
|
continue;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2021-10-19 03:24:30 +08:00
|
|
|
GodotBody3D *b = n[i];
|
2021-06-11 08:37:19 +08:00
|
|
|
if (b->mode < PhysicsServer3D::BODY_MODE_DYNAMIC) {
|
2014-02-19 22:57:14 +08:00
|
|
|
continue;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!b->is_active()) {
|
2014-02-19 22:57:14 +08:00
|
|
|
b->set_active(true);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::call_queries() {
|
2021-06-05 07:09:41 +08:00
|
|
|
if (fi_callback_data) {
|
|
|
|
if (!fi_callback_data->callable.get_object()) {
|
2021-03-30 14:22:23 +08:00
|
|
|
set_force_integration_callback(Callable());
|
2014-02-19 22:57:14 +08:00
|
|
|
} else {
|
2021-06-05 07:09:41 +08:00
|
|
|
Variant direct_state_variant = get_direct_state();
|
|
|
|
const Variant *vp[2] = { &direct_state_variant, &fi_callback_data->udata };
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2020-02-20 03:27:19 +08:00
|
|
|
Callable::CallError ce;
|
2021-06-05 07:09:41 +08:00
|
|
|
int argc = (fi_callback_data->udata.get_type() == Variant::NIL) ? 1 : 2;
|
2021-03-30 14:22:23 +08:00
|
|
|
Variant rv;
|
2021-06-05 07:09:41 +08:00
|
|
|
fi_callback_data->callable.call(vp, argc, rv, ce);
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-05 07:09:41 +08:00
|
|
|
|
|
|
|
if (body_state_callback_instance) {
|
|
|
|
(body_state_callback)(body_state_callback_instance, get_direct_state());
|
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
bool GodotBody3D::sleep_test(real_t p_step) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_STATIC || mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
|
2021-05-20 09:15:07 +08:00
|
|
|
return true;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (!can_sleep) {
|
2014-02-19 22:57:14 +08:00
|
|
|
return false;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2017-07-08 23:12:18 +08: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()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
still_time += p_step;
|
2014-02-19 22:57:14 +08:00
|
|
|
|
|
|
|
return still_time > get_space()->get_body_time_to_sleep();
|
|
|
|
} else {
|
2017-03-05 23:44:50 +08:00
|
|
|
still_time = 0; //maybe this should be set to 0 on set_active?
|
2014-02-19 22:57:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::set_state_sync_callback(void *p_instance, PhysicsServer3D::BodyStateCallback p_callback) {
|
2021-06-05 07:09:41 +08:00
|
|
|
body_state_callback_instance = p_instance;
|
|
|
|
body_state_callback = p_callback;
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
void GodotBody3D::set_force_integration_callback(const Callable &p_callable, const Variant &p_udata) {
|
2021-06-05 07:09:41 +08:00
|
|
|
if (p_callable.get_object()) {
|
|
|
|
if (!fi_callback_data) {
|
|
|
|
fi_callback_data = memnew(ForceIntegrationCallbackData);
|
|
|
|
}
|
|
|
|
fi_callback_data->callable = p_callable;
|
|
|
|
fi_callback_data->udata = p_udata;
|
|
|
|
} else if (fi_callback_data) {
|
|
|
|
memdelete(fi_callback_data);
|
|
|
|
fi_callback_data = nullptr;
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
2021-06-05 07:09:41 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
GodotPhysicsDirectBodyState3D *GodotBody3D::get_direct_state() {
|
2021-06-05 07:09:41 +08:00
|
|
|
if (!direct_state) {
|
2021-10-19 03:24:30 +08:00
|
|
|
direct_state = memnew(GodotPhysicsDirectBodyState3D);
|
2021-06-05 07:09:41 +08:00
|
|
|
direct_state->body = this;
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
2021-06-05 07:09:41 +08:00
|
|
|
return direct_state;
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
GodotBody3D::GodotBody3D() :
|
|
|
|
GodotCollisionObject3D(TYPE_BODY),
|
2017-12-07 04:36:34 +08:00
|
|
|
active_list(this),
|
2021-06-11 08:37:19 +08:00
|
|
|
mass_properties_update_list(this),
|
2018-09-28 23:17:38 +08:00
|
|
|
direct_state_query_list(this) {
|
2014-02-19 22:57:14 +08:00
|
|
|
_set_static(false);
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:24:30 +08:00
|
|
|
GodotBody3D::~GodotBody3D() {
|
2021-06-05 07:09:41 +08:00
|
|
|
if (fi_callback_data) {
|
|
|
|
memdelete(fi_callback_data);
|
|
|
|
}
|
|
|
|
if (direct_state) {
|
|
|
|
memdelete(direct_state);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-19 22:57:14 +08:00
|
|
|
}
|