2014-02-10 01:10:30 +00:00
|
|
|
/**************************************************************************/
|
2021-10-18 19:24:30 +00:00
|
|
|
/* godot_body_2d.cpp */
|
2014-02-10 01:10:30 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-04 23:50:27 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
#include "godot_body_2d.h"
|
2021-06-04 23:09:41 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
#include "godot_area_2d.h"
|
|
|
|
#include "godot_body_direct_state_2d.h"
|
|
|
|
#include "godot_space_2d.h"
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::_mass_properties_changed() {
|
2023-06-27 15:01:16 +00:00
|
|
|
if (get_space() && !mass_properties_update_list.in_list()) {
|
2021-06-11 00:37:19 +00:00
|
|
|
get_space()->body_add_to_mass_properties_update_list(&mass_properties_update_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::update_mass_properties() {
|
2014-02-19 14:57:14 +00:00
|
|
|
//update shapes and motions
|
|
|
|
|
|
|
|
switch (mode) {
|
2022-08-25 17:35:52 +00:00
|
|
|
case PhysicsServer2D::BODY_MODE_RIGID: {
|
2017-02-13 23:25:05 +00:00
|
|
|
real_t total_area = 0;
|
2014-02-19 14:57:14 +00:00
|
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
2021-06-11 00:37:19 +00:00
|
|
|
if (is_shape_disabled(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
total_area += get_shape_aabb(i).get_area();
|
|
|
|
}
|
|
|
|
|
2021-06-11 00:37:19 +00:00
|
|
|
if (calculate_center_of_mass) {
|
|
|
|
// We have to recompute the center of mass.
|
2021-10-22 17:57:31 +00:00
|
|
|
center_of_mass_local = Vector2();
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2021-06-11 00:37:19 +00:00
|
|
|
if (total_area != 0.0) {
|
|
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
|
|
|
if (is_shape_disabled(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t area = get_shape_aabb(i).get_area();
|
|
|
|
|
2022-09-29 09:53:28 +00:00
|
|
|
real_t mass_new = area * mass / total_area;
|
2021-06-11 00:37:19 +00:00
|
|
|
|
|
|
|
// NOTE: we assume that the shape origin is also its center of mass.
|
2022-09-29 09:53:28 +00:00
|
|
|
center_of_mass_local += mass_new * get_shape_transform(i).get_origin();
|
2021-06-11 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 17:57:31 +00:00
|
|
|
center_of_mass_local /= mass;
|
2018-12-07 21:09:39 +00:00
|
|
|
}
|
2021-06-11 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (calculate_inertia) {
|
|
|
|
inertia = 0;
|
2018-12-07 21:09:39 +00:00
|
|
|
|
2021-06-11 00:37:19 +00:00
|
|
|
for (int i = 0; i < get_shape_count(); i++) {
|
|
|
|
if (is_shape_disabled(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
const GodotShape2D *shape = get_shape(i);
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2021-06-11 00:37:19 +00:00
|
|
|
real_t area = get_shape_aabb(i).get_area();
|
|
|
|
if (area == 0.0) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2022-09-29 09:53:28 +00:00
|
|
|
real_t mass_new = area * mass / total_area;
|
2021-06-11 00:37:19 +00:00
|
|
|
|
|
|
|
Transform2D mtx = get_shape_transform(i);
|
|
|
|
Vector2 scale = mtx.get_scale();
|
2021-10-22 17:57:31 +00:00
|
|
|
Vector2 shape_origin = mtx.get_origin() - center_of_mass_local;
|
2022-09-29 09:53:28 +00:00
|
|
|
inertia += shape->get_moment_of_inertia(mass_new, scale) + mass_new * shape_origin.length_squared();
|
2021-06-11 00:37:19 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 00:37:19 +00:00
|
|
|
_inv_inertia = inertia > 0.0 ? (1.0 / inertia) : 0.0;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (mass) {
|
2014-02-19 14:57:14 +00:00
|
|
|
_inv_mass = 1.0 / mass;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2014-02-19 14:57:14 +00:00
|
|
|
_inv_mass = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_MODE_KINEMATIC:
|
|
|
|
case PhysicsServer2D::BODY_MODE_STATIC: {
|
2014-02-19 14:57:14 +00:00
|
|
|
_inv_inertia = 0;
|
|
|
|
_inv_mass = 0;
|
|
|
|
} break;
|
2022-08-25 17:35:52 +00:00
|
|
|
case PhysicsServer2D::BODY_MODE_RIGID_LINEAR: {
|
2014-02-19 14:57:14 +00:00
|
|
|
_inv_inertia = 0;
|
|
|
|
_inv_mass = 1.0 / mass;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
2021-10-19 23:16:00 +00:00
|
|
|
|
|
|
|
_update_transform_dependent();
|
2021-06-11 00:37:19 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::reset_mass_properties() {
|
2021-06-11 00:37:19 +00:00
|
|
|
calculate_inertia = true;
|
|
|
|
calculate_center_of_mass = true;
|
|
|
|
_mass_properties_changed();
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::set_active(bool p_active) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (active == p_active) {
|
2014-02-19 14:57:14 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
active = p_active;
|
2021-04-20 01:38:11 +00:00
|
|
|
|
|
|
|
if (active) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
|
2021-04-20 01:38:11 +00:00
|
|
|
// Static bodies can't be active.
|
|
|
|
active = false;
|
|
|
|
} else if (get_space()) {
|
2014-02-19 14:57:14 +00:00
|
|
|
get_space()->body_add_to_active_list(&active_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-04-20 01:38:11 +00:00
|
|
|
} else if (get_space()) {
|
|
|
|
get_space()->body_remove_from_active_list(&active_list);
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::set_param(PhysicsServer2D::BodyParameter p_param, const Variant &p_value) {
|
2014-02-19 14:57:14 +00:00
|
|
|
switch (p_param) {
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_BOUNCE: {
|
2014-02-19 14:57:14 +00:00
|
|
|
bounce = p_value;
|
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_FRICTION: {
|
2014-02-19 14:57:14 +00:00
|
|
|
friction = p_value;
|
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_MASS: {
|
2021-06-11 00:37:19 +00:00
|
|
|
real_t mass_value = p_value;
|
|
|
|
ERR_FAIL_COND(mass_value <= 0);
|
|
|
|
mass = mass_value;
|
2022-08-25 17:35:52 +00:00
|
|
|
if (mode >= PhysicsServer2D::BODY_MODE_RIGID) {
|
2021-06-11 00:37:19 +00:00
|
|
|
_mass_properties_changed();
|
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_INERTIA: {
|
2021-06-11 00:37:19 +00:00
|
|
|
real_t inertia_value = p_value;
|
|
|
|
if (inertia_value <= 0.0) {
|
|
|
|
calculate_inertia = true;
|
2022-08-25 17:35:52 +00:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_RIGID) {
|
2021-06-11 00:37:19 +00:00
|
|
|
_mass_properties_changed();
|
|
|
|
}
|
2016-04-26 12:15:15 +00:00
|
|
|
} else {
|
2021-06-11 00:37:19 +00:00
|
|
|
calculate_inertia = false;
|
|
|
|
inertia = inertia_value;
|
2022-08-25 17:35:52 +00:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_RIGID) {
|
2021-06-11 00:37:19 +00:00
|
|
|
_inv_inertia = 1.0 / inertia;
|
|
|
|
}
|
2016-04-26 12:15:15 +00:00
|
|
|
}
|
|
|
|
} break;
|
2021-06-11 00:37:19 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS: {
|
|
|
|
calculate_center_of_mass = false;
|
2021-10-19 23:16:00 +00:00
|
|
|
center_of_mass_local = p_value;
|
|
|
|
_update_transform_dependent();
|
2021-06-11 00:37:19 +00:00
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
|
2021-11-20 16:25:27 +00:00
|
|
|
if (Math::is_zero_approx(gravity_scale)) {
|
|
|
|
wakeup();
|
|
|
|
}
|
2015-01-05 21:37:12 +00:00
|
|
|
gravity_scale = p_value;
|
|
|
|
} break;
|
2020-04-14 17:59:53 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE: {
|
|
|
|
int mode_value = p_value;
|
|
|
|
linear_damp_mode = (PhysicsServer2D::BodyDampMode)mode_value;
|
|
|
|
} break;
|
|
|
|
case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE: {
|
|
|
|
int mode_value = p_value;
|
|
|
|
angular_damp_mode = (PhysicsServer2D::BodyDampMode)mode_value;
|
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
|
2015-01-05 21:37:12 +00:00
|
|
|
linear_damp = p_value;
|
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
|
2015-01-05 21:37:12 +00:00
|
|
|
angular_damp = p_value;
|
|
|
|
} break;
|
2019-04-09 15:08:36 +00:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Variant GodotBody2D::get_param(PhysicsServer2D::BodyParameter p_param) const {
|
2014-02-19 14:57:14 +00:00
|
|
|
switch (p_param) {
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_BOUNCE: {
|
2014-02-19 14:57:14 +00:00
|
|
|
return bounce;
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_FRICTION: {
|
2014-02-19 14:57:14 +00:00
|
|
|
return friction;
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_MASS: {
|
2014-02-19 14:57:14 +00:00
|
|
|
return mass;
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_INERTIA: {
|
2019-08-15 11:34:47 +00:00
|
|
|
return inertia;
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2021-06-11 00:37:19 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS: {
|
2021-10-22 18:56:00 +00:00
|
|
|
return center_of_mass_local;
|
2021-06-11 00:37:19 +00:00
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
|
2015-01-05 21:37:12 +00:00
|
|
|
return gravity_scale;
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2020-04-14 17:59:53 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE: {
|
|
|
|
return linear_damp_mode;
|
|
|
|
}
|
|
|
|
case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE: {
|
|
|
|
return angular_damp_mode;
|
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
|
2015-01-05 21:37:12 +00:00
|
|
|
return linear_damp;
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
|
2015-01-05 21:37:12 +00:00
|
|
|
return angular_damp;
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2019-04-09 15:08:36 +00:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::set_mode(PhysicsServer2D::BodyMode p_mode) {
|
2020-03-27 18:21:27 +00:00
|
|
|
PhysicsServer2D::BodyMode prev = mode;
|
2014-02-19 14:57:14 +00:00
|
|
|
mode = p_mode;
|
|
|
|
|
|
|
|
switch (p_mode) {
|
|
|
|
//CLEAR UP EVERYTHING IN CASE IT NOT WORKS!
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_MODE_STATIC:
|
|
|
|
case PhysicsServer2D::BODY_MODE_KINEMATIC: {
|
2014-02-19 14:57:14 +00:00
|
|
|
_set_inv_transform(get_transform().affine_inverse());
|
|
|
|
_inv_mass = 0;
|
2019-08-15 11:34:47 +00:00
|
|
|
_inv_inertia = 0;
|
2020-03-27 18:21:27 +00:00
|
|
|
_set_static(p_mode == PhysicsServer2D::BODY_MODE_STATIC);
|
|
|
|
set_active(p_mode == PhysicsServer2D::BODY_MODE_KINEMATIC && contacts.size());
|
2014-02-19 14:57:14 +00:00
|
|
|
linear_velocity = Vector2();
|
|
|
|
angular_velocity = 0;
|
2020-03-27 18:21:27 +00:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC && prev != mode) {
|
2014-10-14 04:01:25 +00:00
|
|
|
first_time_kinematic = true;
|
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
} break;
|
2022-08-25 17:35:52 +00:00
|
|
|
case PhysicsServer2D::BODY_MODE_RIGID: {
|
2014-02-19 14:57:14 +00:00
|
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
2021-06-11 00:37:19 +00:00
|
|
|
if (!calculate_inertia) {
|
|
|
|
_inv_inertia = 1.0 / inertia;
|
|
|
|
}
|
|
|
|
_mass_properties_changed();
|
2014-02-19 14:57:14 +00:00
|
|
|
_set_static(false);
|
2019-10-14 14:19:15 +00:00
|
|
|
set_active(true);
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
} break;
|
2022-08-25 17:35:52 +00:00
|
|
|
case PhysicsServer2D::BODY_MODE_RIGID_LINEAR: {
|
2014-02-19 14:57:14 +00:00
|
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
2019-08-15 11:34:47 +00:00
|
|
|
_inv_inertia = 0;
|
2021-06-11 00:37:19 +00:00
|
|
|
angular_velocity = 0;
|
2014-02-19 14:57:14 +00:00
|
|
|
_set_static(false);
|
2019-10-14 14:19:15 +00:00
|
|
|
set_active(true);
|
2021-06-11 00:37:19 +00:00
|
|
|
}
|
2019-08-15 11:34:47 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
PhysicsServer2D::BodyMode GodotBody2D::get_mode() const {
|
2014-02-19 14:57:14 +00:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::_shapes_changed() {
|
2021-06-11 00:37:19 +00:00
|
|
|
_mass_properties_changed();
|
2021-10-22 00:28:27 +00:00
|
|
|
wakeup();
|
2014-02-19 14:57:14 +00:00
|
|
|
wakeup_neighbours();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::set_state(PhysicsServer2D::BodyState p_state, const Variant &p_variant) {
|
2014-02-19 14:57:14 +00:00
|
|
|
switch (p_state) {
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_STATE_TRANSFORM: {
|
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2016-03-08 23:00:52 +00:00
|
|
|
new_transform = p_variant;
|
2014-02-19 14:57:14 +00:00
|
|
|
//wakeup_neighbours();
|
|
|
|
set_active(true);
|
2014-10-14 04:01:25 +00:00
|
|
|
if (first_time_kinematic) {
|
|
|
|
_set_transform(p_variant);
|
|
|
|
_set_inv_transform(get_transform().affine_inverse());
|
|
|
|
first_time_kinematic = false;
|
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
} else if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
|
2014-02-19 14:57:14 +00:00
|
|
|
_set_transform(p_variant);
|
|
|
|
_set_inv_transform(get_transform().affine_inverse());
|
|
|
|
wakeup_neighbours();
|
|
|
|
} else {
|
2017-01-11 03:52:51 +00:00
|
|
|
Transform2D t = p_variant;
|
2014-02-19 14:57:14 +00:00
|
|
|
t.orthonormalize();
|
|
|
|
new_transform = get_transform(); //used as old to compute motion
|
2020-05-14 14:41:43 +00:00
|
|
|
if (t == new_transform) {
|
2015-05-16 20:54:36 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
_set_transform(t);
|
|
|
|
_set_inv_transform(get_transform().inverse());
|
2021-10-19 23:16:00 +00:00
|
|
|
_update_transform_dependent();
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
2015-04-26 19:20:00 +00:00
|
|
|
wakeup();
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
|
2014-02-19 14:57:14 +00:00
|
|
|
linear_velocity = p_variant;
|
2021-08-30 18:48:43 +00:00
|
|
|
constant_linear_velocity = linear_velocity;
|
2015-04-26 19:20:00 +00:00
|
|
|
wakeup();
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
|
2014-02-19 14:57:14 +00:00
|
|
|
angular_velocity = p_variant;
|
2021-08-30 18:48:43 +00:00
|
|
|
constant_angular_velocity = angular_velocity;
|
2015-04-26 19:20:00 +00:00
|
|
|
wakeup();
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_STATE_SLEEPING: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2014-02-19 14:57:14 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
bool do_sleep = p_variant;
|
|
|
|
if (do_sleep) {
|
|
|
|
linear_velocity = Vector2();
|
|
|
|
//biased_linear_velocity=Vector3();
|
|
|
|
angular_velocity = 0;
|
|
|
|
//biased_angular_velocity=Vector3();
|
|
|
|
set_active(false);
|
|
|
|
} else {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (mode != PhysicsServer2D::BODY_MODE_STATIC) {
|
2014-02-19 14:57:14 +00:00
|
|
|
set_active(true);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
} break;
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
|
2014-02-19 14:57:14 +00:00
|
|
|
can_sleep = p_variant;
|
2022-08-25 17:35:52 +00:00
|
|
|
if (mode >= PhysicsServer2D::BODY_MODE_RIGID && !active && !can_sleep) {
|
2014-02-19 14:57:14 +00:00
|
|
|
set_active(true);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
Variant GodotBody2D::get_state(PhysicsServer2D::BodyState p_state) const {
|
2014-02-19 14:57:14 +00:00
|
|
|
switch (p_state) {
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_STATE_TRANSFORM: {
|
2014-02-19 14:57:14 +00:00
|
|
|
return get_transform();
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
|
2014-02-19 14:57:14 +00:00
|
|
|
return linear_velocity;
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
|
2014-02-19 14:57:14 +00:00
|
|
|
return angular_velocity;
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_STATE_SLEEPING: {
|
2014-02-19 14:57:14 +00:00
|
|
|
return !is_active();
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
|
2014-02-19 14:57:14 +00:00
|
|
|
return can_sleep;
|
2019-07-23 07:14:31 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::set_space(GodotSpace2D *p_space) {
|
2014-02-19 14:57:14 +00:00
|
|
|
if (get_space()) {
|
|
|
|
wakeup_neighbours();
|
|
|
|
|
2021-06-11 00:37:19 +00:00
|
|
|
if (mass_properties_update_list.in_list()) {
|
|
|
|
get_space()->body_remove_from_mass_properties_update_list(&mass_properties_update_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (active_list.in_list()) {
|
2014-02-19 14:57:14 +00:00
|
|
|
get_space()->body_remove_from_active_list(&active_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (direct_state_query_list.in_list()) {
|
2014-02-19 14:57:14 +00:00
|
|
|
get_space()->body_remove_from_state_query_list(&direct_state_query_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_set_space(p_space);
|
|
|
|
|
|
|
|
if (get_space()) {
|
2021-06-11 00:37:19 +00:00
|
|
|
_mass_properties_changed();
|
2023-12-15 19:51:44 +00:00
|
|
|
|
|
|
|
if (active && !active_list.in_list()) {
|
2014-02-19 14:57:14 +00:00
|
|
|
get_space()->body_add_to_active_list(&active_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 23:16:00 +00:00
|
|
|
void GodotBody2D::_update_transform_dependent() {
|
|
|
|
center_of_mass = get_transform().basis_xform(center_of_mass_local);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::integrate_forces(real_t p_step) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
|
2014-02-19 14:57:14 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2023-09-28 09:40:18 +00:00
|
|
|
ERR_FAIL_NULL(get_space());
|
2021-11-05 01:12:00 +00:00
|
|
|
|
2014-02-19 14:57:14 +00:00
|
|
|
int ac = areas.size();
|
2021-11-05 01:12:00 +00:00
|
|
|
|
|
|
|
bool gravity_done = false;
|
|
|
|
bool linear_damp_done = false;
|
|
|
|
bool angular_damp_done = false;
|
|
|
|
|
2015-12-14 01:56:11 +00:00
|
|
|
bool stopped = false;
|
2021-11-05 01:12:00 +00:00
|
|
|
|
2015-12-08 17:44:29 +00:00
|
|
|
gravity = Vector2(0, 0);
|
2020-04-14 17:59:53 +00:00
|
|
|
|
|
|
|
total_linear_damp = 0.0;
|
|
|
|
total_angular_damp = 0.0;
|
|
|
|
|
|
|
|
// Combine gravity and damping from overlapping areas in priority order.
|
2014-02-19 14:57:14 +00:00
|
|
|
if (ac) {
|
2015-04-10 12:25:40 +00:00
|
|
|
areas.sort();
|
2014-02-19 14:57:14 +00:00
|
|
|
const AreaCMP *aa = &areas[0];
|
2015-12-14 01:56:11 +00:00
|
|
|
for (int i = ac - 1; i >= 0 && !stopped; i--) {
|
2021-11-05 01:12:00 +00:00
|
|
|
if (!gravity_done) {
|
|
|
|
PhysicsServer2D::AreaSpaceOverrideMode area_gravity_mode = (PhysicsServer2D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE);
|
|
|
|
if (area_gravity_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
|
|
|
|
Vector2 area_gravity;
|
|
|
|
aa[i].area->compute_gravity(get_transform().get_origin(), area_gravity);
|
|
|
|
switch (area_gravity_mode) {
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
|
|
|
|
gravity += area_gravity;
|
|
|
|
gravity_done = area_gravity_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
|
|
|
|
} break;
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
|
|
|
|
gravity = area_gravity;
|
|
|
|
gravity_done = area_gravity_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 15:08:36 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
2021-11-05 01:12:00 +00:00
|
|
|
if (!linear_damp_done) {
|
|
|
|
PhysicsServer2D::AreaSpaceOverrideMode area_linear_damp_mode = (PhysicsServer2D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE);
|
|
|
|
if (area_linear_damp_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
|
|
|
|
real_t area_linear_damp = aa[i].area->get_linear_damp();
|
|
|
|
switch (area_linear_damp_mode) {
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
|
|
|
|
total_linear_damp += area_linear_damp;
|
|
|
|
linear_damp_done = area_linear_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
|
|
|
|
} break;
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
|
|
|
|
total_linear_damp = area_linear_damp;
|
|
|
|
linear_damp_done = area_linear_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!angular_damp_done) {
|
|
|
|
PhysicsServer2D::AreaSpaceOverrideMode area_angular_damp_mode = (PhysicsServer2D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE);
|
|
|
|
if (area_angular_damp_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
|
|
|
|
real_t area_angular_damp = aa[i].area->get_angular_damp();
|
|
|
|
switch (area_angular_damp_mode) {
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
|
|
|
|
total_angular_damp += area_angular_damp;
|
|
|
|
angular_damp_done = area_angular_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
|
|
|
|
} break;
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
|
|
|
|
total_angular_damp = area_angular_damp;
|
|
|
|
angular_damp_done = area_angular_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stopped = gravity_done && linear_damp_done && angular_damp_done;
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 17:59:53 +00:00
|
|
|
|
2021-11-05 01:12:00 +00:00
|
|
|
// Add default gravity and damping from space area.
|
2015-12-14 01:56:11 +00:00
|
|
|
if (!stopped) {
|
2021-11-05 01:12:00 +00:00
|
|
|
GodotArea2D *default_area = get_space()->get_default_area();
|
2023-09-09 15:04:18 +00:00
|
|
|
ERR_FAIL_NULL(default_area);
|
2020-04-14 17:59:53 +00:00
|
|
|
|
2021-11-05 01:12:00 +00:00
|
|
|
if (!gravity_done) {
|
|
|
|
Vector2 default_gravity;
|
|
|
|
default_area->compute_gravity(get_transform().get_origin(), default_gravity);
|
|
|
|
gravity += default_gravity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!linear_damp_done) {
|
|
|
|
total_linear_damp += default_area->get_linear_damp();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!angular_damp_done) {
|
|
|
|
total_angular_damp += default_area->get_angular_damp();
|
|
|
|
}
|
2015-04-10 12:25:40 +00:00
|
|
|
}
|
2015-01-05 21:37:12 +00:00
|
|
|
|
2020-04-14 17:59:53 +00:00
|
|
|
// Override linear damping with body's value.
|
|
|
|
switch (linear_damp_mode) {
|
|
|
|
case PhysicsServer2D::BODY_DAMP_MODE_COMBINE: {
|
|
|
|
total_linear_damp += linear_damp;
|
|
|
|
} break;
|
|
|
|
case PhysicsServer2D::BODY_DAMP_MODE_REPLACE: {
|
|
|
|
total_linear_damp = linear_damp;
|
|
|
|
} break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-01-05 21:37:12 +00:00
|
|
|
|
2020-04-14 17:59:53 +00:00
|
|
|
// Override angular damping with body's value.
|
|
|
|
switch (angular_damp_mode) {
|
|
|
|
case PhysicsServer2D::BODY_DAMP_MODE_COMBINE: {
|
|
|
|
total_angular_damp += angular_damp;
|
|
|
|
} break;
|
|
|
|
case PhysicsServer2D::BODY_DAMP_MODE_REPLACE: {
|
|
|
|
total_angular_damp = angular_damp;
|
|
|
|
} break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-04-14 17:59:53 +00:00
|
|
|
|
|
|
|
gravity *= gravity_scale;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2021-11-25 15:26:38 +00:00
|
|
|
prev_linear_velocity = linear_velocity;
|
|
|
|
prev_angular_velocity = angular_velocity;
|
|
|
|
|
2014-02-19 14:57:14 +00:00
|
|
|
Vector2 motion;
|
|
|
|
bool do_motion = false;
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2014-02-19 14:57:14 +00:00
|
|
|
//compute motion, angular and etc. velocities from prev transform
|
2017-01-06 05:27:48 +00:00
|
|
|
motion = new_transform.get_origin() - get_transform().get_origin();
|
2021-08-30 18:48:43 +00:00
|
|
|
linear_velocity = constant_linear_velocity + motion / p_step;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2017-01-06 05:27:48 +00:00
|
|
|
real_t rot = new_transform.get_rotation() - get_transform().get_rotation();
|
2021-08-30 18:48:43 +00:00
|
|
|
angular_velocity = constant_angular_velocity + remainder(rot, 2.0 * Math_PI) / p_step;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
do_motion = true;
|
|
|
|
|
|
|
|
} else {
|
2021-06-11 00:37:19 +00:00
|
|
|
if (!omit_force_integration) {
|
2017-03-24 20:45:31 +00:00
|
|
|
//overridden by direct state query
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2021-12-08 01:09:54 +00:00
|
|
|
Vector2 force = gravity * mass + applied_force + constant_force;
|
|
|
|
real_t torque = applied_torque + constant_torque;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2020-04-14 17:59:53 +00:00
|
|
|
real_t damp = 1.0 - p_step * total_linear_damp;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (damp < 0) { // reached zero in the given time
|
2014-02-19 14:57:14 +00:00
|
|
|
damp = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2022-09-29 09:53:28 +00:00
|
|
|
real_t angular_damp_new = 1.0 - p_step * total_angular_damp;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2022-09-29 09:53:28 +00:00
|
|
|
if (angular_damp_new < 0) { // reached zero in the given time
|
|
|
|
angular_damp_new = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
linear_velocity *= damp;
|
2022-09-29 09:53:28 +00:00
|
|
|
angular_velocity *= angular_damp_new;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
linear_velocity += _inv_mass * force * p_step;
|
|
|
|
angular_velocity += _inv_inertia * torque * p_step;
|
|
|
|
}
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
|
2018-11-08 18:21:28 +00:00
|
|
|
motion = linear_velocity * p_step;
|
2014-02-19 14:57:14 +00:00
|
|
|
do_motion = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:09:54 +00:00
|
|
|
applied_force = Vector2();
|
|
|
|
applied_torque = 0.0;
|
|
|
|
|
|
|
|
biased_angular_velocity = 0.0;
|
2014-02-19 14:57:14 +00:00
|
|
|
biased_linear_velocity = Vector2();
|
|
|
|
|
|
|
|
if (do_motion) { //shapes temporarily extend for raycast
|
|
|
|
_update_shapes_with_motion(motion);
|
|
|
|
}
|
|
|
|
|
2016-03-08 23:00:52 +00:00
|
|
|
contact_count = 0;
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::integrate_velocities(real_t p_step) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
|
2014-02-19 14:57:14 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2024-06-18 09:18:11 +00:00
|
|
|
ERR_FAIL_NULL(get_space());
|
|
|
|
|
2023-09-26 19:58:57 +00:00
|
|
|
if (fi_callback_data || body_state_callback.is_valid()) {
|
2014-02-19 14:57:14 +00:00
|
|
|
get_space()->body_add_to_state_query_list(&direct_state_query_list);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2014-02-19 14:57:14 +00:00
|
|
|
_set_transform(new_transform, false);
|
2014-10-03 11:58:41 +00:00
|
|
|
_set_inv_transform(new_transform.affine_inverse());
|
2020-05-14 14:41:43 +00:00
|
|
|
if (contacts.size() == 0 && linear_velocity == Vector2() && angular_velocity == 0) {
|
2014-02-19 14:57:14 +00:00
|
|
|
set_active(false); //stopped moving, deactivate
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t total_angular_velocity = angular_velocity + biased_angular_velocity;
|
|
|
|
Vector2 total_linear_velocity = linear_velocity + biased_linear_velocity;
|
|
|
|
|
2021-10-22 14:07:45 +00:00
|
|
|
real_t angle_delta = total_angular_velocity * p_step;
|
|
|
|
real_t angle = get_transform().get_rotation() + angle_delta;
|
2014-02-19 14:57:14 +00:00
|
|
|
Vector2 pos = get_transform().get_origin() + total_linear_velocity * p_step;
|
|
|
|
|
2021-10-22 14:07:45 +00:00
|
|
|
if (center_of_mass.length_squared() > CMP_EPSILON2) {
|
2021-06-11 00:37:19 +00:00
|
|
|
// Calculate displacement due to center of mass offset.
|
2021-10-22 14:07:45 +00:00
|
|
|
pos += center_of_mass - center_of_mass.rotated(angle_delta);
|
2021-06-11 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
_set_transform(Transform2D(angle, pos), continuous_cd_mode == PhysicsServer2D::CCD_MODE_DISABLED);
|
2014-02-19 14:57:14 +00:00
|
|
|
_set_inv_transform(get_transform().inverse());
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
|
2014-02-19 14:57:14 +00:00
|
|
|
new_transform = get_transform();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-10-19 23:16:00 +00:00
|
|
|
|
|
|
|
_update_transform_dependent();
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::wakeup_neighbours() {
|
|
|
|
for (const Pair<GodotConstraint2D *, int> &E : constraint_list) {
|
|
|
|
const GodotConstraint2D *c = E.first;
|
|
|
|
GodotBody2D **n = c->get_body_ptr();
|
2014-02-19 14:57:14 +00:00
|
|
|
int bc = c->get_body_count();
|
|
|
|
|
|
|
|
for (int i = 0; i < bc; i++) {
|
2021-07-16 03:45:57 +00:00
|
|
|
if (i == E.second) {
|
2014-02-19 14:57:14 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotBody2D *b = n[i];
|
2022-08-25 17:35:52 +00:00
|
|
|
if (b->mode < PhysicsServer2D::BODY_MODE_RIGID) {
|
2014-02-19 14:57:14 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!b->is_active()) {
|
2014-02-19 14:57:14 +00:00
|
|
|
b->set_active(true);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::call_queries() {
|
2022-09-15 12:15:39 +00:00
|
|
|
Variant direct_state_variant = get_direct_state();
|
|
|
|
|
2021-06-04 23:09:41 +00:00
|
|
|
if (fi_callback_data) {
|
2023-09-26 19:58:57 +00:00
|
|
|
if (!fi_callback_data->callable.is_valid()) {
|
2021-03-30 06:22:23 +00:00
|
|
|
set_force_integration_callback(Callable());
|
2014-02-19 14:57:14 +00:00
|
|
|
} else {
|
2021-06-04 23:09:41 +00:00
|
|
|
const Variant *vp[2] = { &direct_state_variant, &fi_callback_data->udata };
|
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
Callable::CallError ce;
|
2021-03-30 06:22:23 +00:00
|
|
|
Variant rv;
|
2021-06-04 23:09:41 +00:00
|
|
|
if (fi_callback_data->udata.get_type() != Variant::NIL) {
|
2022-07-28 20:56:41 +00:00
|
|
|
fi_callback_data->callable.callp(vp, 2, rv, ce);
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
} else {
|
2022-07-28 20:56:41 +00:00
|
|
|
fi_callback_data->callable.callp(vp, 1, rv, ce);
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 23:09:41 +00:00
|
|
|
|
2023-09-26 19:58:57 +00:00
|
|
|
if (body_state_callback.is_valid()) {
|
2023-07-11 14:18:10 +00:00
|
|
|
body_state_callback.call(direct_state_variant);
|
2021-06-04 23:09:41 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
bool GodotBody2D::sleep_test(real_t p_step) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2021-05-20 01:15:07 +00:00
|
|
|
return true;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else if (!can_sleep) {
|
2014-02-19 14:57:14 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2024-06-18 09:18:11 +00:00
|
|
|
ERR_FAIL_NULL_V(get_space(), true);
|
|
|
|
|
2017-07-08 15:12:18 +00:00
|
|
|
if (Math::abs(angular_velocity) < 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 14:57:14 +00:00
|
|
|
still_time += p_step;
|
|
|
|
|
|
|
|
return still_time > get_space()->get_body_time_to_sleep();
|
|
|
|
} else {
|
|
|
|
still_time = 0; //maybe this should be set to 0 on set_active?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 12:15:39 +00:00
|
|
|
void GodotBody2D::set_state_sync_callback(const Callable &p_callable) {
|
|
|
|
body_state_callback = p_callable;
|
2021-06-04 23:09:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void GodotBody2D::set_force_integration_callback(const Callable &p_callable, const Variant &p_udata) {
|
2023-09-26 19:58:57 +00:00
|
|
|
if (p_callable.is_valid()) {
|
2021-06-04 23:09:41 +00:00
|
|
|
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 14:57:14 +00:00
|
|
|
}
|
2021-06-04 23:09:41 +00:00
|
|
|
}
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotPhysicsDirectBodyState2D *GodotBody2D::get_direct_state() {
|
2021-06-04 23:09:41 +00:00
|
|
|
if (!direct_state) {
|
2021-10-18 19:24:30 +00:00
|
|
|
direct_state = memnew(GodotPhysicsDirectBodyState2D);
|
2021-06-04 23:09:41 +00:00
|
|
|
direct_state->body = this;
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
2021-06-04 23:09:41 +00:00
|
|
|
return direct_state;
|
2014-02-19 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotBody2D::GodotBody2D() :
|
|
|
|
GodotCollisionObject2D(TYPE_BODY),
|
2017-12-06 20:36:34 +00:00
|
|
|
active_list(this),
|
2021-06-11 00:37:19 +00:00
|
|
|
mass_properties_update_list(this),
|
2017-12-06 20:36:34 +00:00
|
|
|
direct_state_query_list(this) {
|
2014-02-19 14:57:14 +00:00
|
|
|
_set_static(false);
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotBody2D::~GodotBody2D() {
|
2021-06-04 23:09:41 +00:00
|
|
|
if (fi_callback_data) {
|
|
|
|
memdelete(fi_callback_data);
|
2016-01-03 23:23:44 +00:00
|
|
|
}
|
2021-06-04 23:09:41 +00:00
|
|
|
if (direct_state) {
|
|
|
|
memdelete(direct_state);
|
2016-01-03 23:23:44 +00:00
|
|
|
}
|
|
|
|
}
|