2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* body_2d_sw.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* 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
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
#include "body_2d_sw.h"
|
|
|
|
#include "area_2d_sw.h"
|
2020-03-27 19:21:27 +01:00
|
|
|
#include "physics_server_2d_sw.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "space_2d_sw.h"
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
void Body2DSW::_update_inertia() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!user_inertia && get_space() && !inertia_update_list.in_list()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
get_space()->body_add_to_inertia_update_list(&inertia_update_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Body2DSW::update_inertias() {
|
|
|
|
//update shapes and motions
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (mode) {
|
2021-05-20 03:15:07 +02:00
|
|
|
case PhysicsServer2D::BODY_MODE_DYNAMIC: {
|
2019-08-15 13:34:47 +02:00
|
|
|
if (user_inertia) {
|
|
|
|
_inv_inertia = inertia > 0 ? (1.0 / inertia) : 0;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
//update tensor for allshapes, 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++) {
|
|
|
|
total_area += get_shape_aabb(i).get_area();
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2019-08-15 13:34:47 +02:00
|
|
|
inertia = 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++) {
|
2018-12-07 22:09:39 +01:00
|
|
|
if (is_shape_disabled(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Shape2DSW *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_aabb(i).get_area();
|
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-01-11 04:52:51 +01:00
|
|
|
Transform2D mtx = get_shape_transform(i);
|
2014-11-02 15:31:01 +01:00
|
|
|
Vector2 scale = mtx.get_scale();
|
2019-08-15 13:34:47 +02:00
|
|
|
inertia += shape->get_moment_of_inertia(mass, scale) + mass * mtx.get_origin().length_squared();
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2019-08-15 13:34:47 +02:00
|
|
|
_inv_inertia = inertia > 0 ? (1.0 / inertia) : 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (mass) {
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = 1.0 / mass;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_MODE_KINEMATIC:
|
|
|
|
case PhysicsServer2D::BODY_MODE_STATIC: {
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_inertia = 0;
|
|
|
|
_inv_mass = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
} break;
|
2021-05-20 03:15:07 +02:00
|
|
|
case PhysicsServer2D::BODY_MODE_DYNAMIC_LOCKED: {
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_inertia = 0;
|
|
|
|
_inv_mass = 1.0 / mass;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
//_update_inertia_tensor();
|
|
|
|
|
|
|
|
//_update_shapes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Body2DSW::set_active(bool p_active) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (active == p_active) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
active = p_active;
|
2021-04-20 03:38:11 +02:00
|
|
|
|
|
|
|
if (active) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
|
2021-04-20 03:38:11 +02:00
|
|
|
// Static bodies can't be active.
|
|
|
|
active = false;
|
|
|
|
} else if (get_space()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
get_space()->body_add_to_active_list(&active_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-04-20 03:38:11 +02:00
|
|
|
} else if (get_space()) {
|
|
|
|
get_space()->body_remove_from_active_list(&active_list);
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body2DSW::set_param(PhysicsServer2D::BodyParameter p_param, real_t p_value) {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_param) {
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_BOUNCE: {
|
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 PhysicsServer2D::BODY_PARAM_FRICTION: {
|
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 PhysicsServer2D::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 PhysicsServer2D::BODY_PARAM_INERTIA: {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_value <= 0) {
|
2016-04-26 14:15:15 +02:00
|
|
|
user_inertia = false;
|
|
|
|
_update_inertia();
|
|
|
|
} else {
|
|
|
|
user_inertia = true;
|
2019-08-15 13:34:47 +02:00
|
|
|
inertia = p_value;
|
2016-04-26 14:15:15 +02:00
|
|
|
_inv_inertia = 1.0 / p_value;
|
|
|
|
}
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
|
2017-03-05 16:44:50 +01:00
|
|
|
gravity_scale = p_value;
|
2015-01-05 22:37:12 +01:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_damp = p_value;
|
2015-01-05 22:37:12 +01:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
|
2017-03-05 16:44:50 +01:00
|
|
|
angular_damp = p_value;
|
2015-01-05 22:37:12 +01: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 Body2DSW::get_param(PhysicsServer2D::BodyParameter p_param) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_param) {
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_BOUNCE: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return bounce;
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_FRICTION: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return friction;
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_MASS: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return mass;
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_INERTIA: {
|
2019-08-15 13:34:47 +02:00
|
|
|
return inertia;
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
|
2015-01-05 22:37:12 +01:00
|
|
|
return gravity_scale;
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
|
2015-01-05 22:37:12 +01:00
|
|
|
return linear_damp;
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
|
2015-01-05 22:37:12 +01:00
|
|
|
return angular_damp;
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
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 Body2DSW::set_mode(PhysicsServer2D::BodyMode p_mode) {
|
|
|
|
PhysicsServer2D::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) {
|
|
|
|
//CLEAR UP EVERYTHING IN CASE IT NOT WORKS!
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_MODE_STATIC:
|
|
|
|
case PhysicsServer2D::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;
|
2019-08-15 13:34:47 +02:00
|
|
|
_inv_inertia = 0;
|
2020-03-27 19:21:27 +01:00
|
|
|
_set_static(p_mode == PhysicsServer2D::BODY_MODE_STATIC);
|
|
|
|
set_active(p_mode == PhysicsServer2D::BODY_MODE_KINEMATIC && contacts.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_velocity = Vector2();
|
|
|
|
angular_velocity = 0;
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer2D::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;
|
2021-05-20 03:15:07 +02:00
|
|
|
case PhysicsServer2D::BODY_MODE_DYNAMIC: {
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
2019-08-15 13:34:47 +02:00
|
|
|
_inv_inertia = inertia > 0 ? (1.0 / inertia) : 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;
|
2021-05-20 03:15:07 +02:00
|
|
|
case PhysicsServer2D::BODY_MODE_DYNAMIC_LOCKED: {
|
2017-03-05 16:44:50 +01:00
|
|
|
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
|
2019-08-15 13:34:47 +02:00
|
|
|
_inv_inertia = 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 = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
} break;
|
|
|
|
}
|
2021-05-20 03:15:07 +02:00
|
|
|
if (p_mode == PhysicsServer2D::BODY_MODE_DYNAMIC && _inv_inertia == 0) {
|
2019-08-15 13:34:47 +02:00
|
|
|
_update_inertia();
|
|
|
|
}
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
if (get_space())
|
|
|
|
_update_queries();
|
|
|
|
*/
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer2D::BodyMode Body2DSW::get_mode() const {
|
2014-02-19 15:57:14 +01:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Body2DSW::_shapes_changed() {
|
|
|
|
_update_inertia();
|
|
|
|
wakeup_neighbours();
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body2DSW::set_state(PhysicsServer2D::BodyState p_state, const Variant &p_variant) {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_state) {
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_STATE_TRANSFORM: {
|
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_transform = p_variant;
|
2014-02-19 15:57:14 +01: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
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
} else if (mode == PhysicsServer2D::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 {
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D t = p_variant;
|
2014-02-19 15:57:14 +01:00
|
|
|
t.orthonormalize();
|
2017-03-05 16:44:50 +01:00
|
|
|
new_transform = get_transform(); //used as old to compute motion
|
2020-05-14 16:41:43 +02:00
|
|
|
if (t == new_transform) {
|
2015-05-16 22:54:36 +02:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
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 PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
|
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 PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
|
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 PhysicsServer2D::BODY_STATE_SLEEPING: {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2014-02-19 15:57:14 +01:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
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 = Vector2();
|
2014-02-19 15:57:14 +01:00
|
|
|
//biased_linear_velocity=Vector3();
|
2017-03-05 16:44:50 +01:00
|
|
|
angular_velocity = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
//biased_angular_velocity=Vector3();
|
|
|
|
set_active(false);
|
|
|
|
} else {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (mode != PhysicsServer2D::BODY_MODE_STATIC) {
|
2014-02-19 15:57:14 +01:00
|
|
|
set_active(true);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
|
2017-03-05 16:44:50 +01:00
|
|
|
can_sleep = p_variant;
|
2021-05-20 03:15:07 +02:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_DYNAMIC && !active && !can_sleep) {
|
2014-02-19 15:57:14 +01:00
|
|
|
set_active(true);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
Variant Body2DSW::get_state(PhysicsServer2D::BodyState p_state) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_state) {
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_STATE_TRANSFORM: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return get_transform();
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return linear_velocity;
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return angular_velocity;
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_STATE_SLEEPING: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return !is_active();
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
|
2014-02-19 15:57:14 +01:00
|
|
|
return can_sleep;
|
2019-07-23 09:14:31 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Body2DSW::set_space(Space2DSW *p_space) {
|
2014-02-19 15:57:14 +01:00
|
|
|
if (get_space()) {
|
|
|
|
wakeup_neighbours();
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (inertia_update_list.in_list()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
get_space()->body_remove_from_inertia_update_list(&inertia_update_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (active_list.in_list()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
get_space()->body_remove_from_active_list(&active_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (direct_state_query_list.in_list()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
get_space()->body_remove_from_state_query_list(&direct_state_query_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_set_space(p_space);
|
|
|
|
|
|
|
|
if (get_space()) {
|
|
|
|
_update_inertia();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (active) {
|
2014-02-19 15:57:14 +01:00
|
|
|
get_space()->body_add_to_active_list(&active_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
first_integration = false;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2021-08-24 18:30:31 +02:00
|
|
|
void Body2DSW::_compute_area_gravity_and_damping(const Area2DSW *p_area) {
|
|
|
|
Vector2 area_gravity;
|
|
|
|
p_area->compute_gravity(get_transform().get_origin(), area_gravity);
|
|
|
|
gravity += area_gravity;
|
2015-01-05 22:37:12 +01:00
|
|
|
|
2015-12-08 18:44:29 +01:00
|
|
|
area_linear_damp += p_area->get_linear_damp();
|
2016-03-09 00:00:52 +01:00
|
|
|
area_angular_damp += p_area->get_angular_damp();
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Body2DSW::integrate_forces(real_t p_step) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2015-04-10 14:25:40 +02:00
|
|
|
Area2DSW *def_area = get_space()->get_default_area();
|
2015-12-14 02:56:11 +01:00
|
|
|
// Area2DSW *damp_area = def_area;
|
2015-04-10 14:25:40 +02: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 = Vector2(0, 0);
|
2015-12-08 18:44:29 +01:00
|
|
|
area_angular_damp = 0;
|
|
|
|
area_linear_damp = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
if (ac) {
|
2015-04-10 14:25:40 +02: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
|
|
|
PhysicsServer2D::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 PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
|
2021-08-24 18:30:31 +02:00
|
|
|
_compute_area_gravity_and_damping(aa[i].area);
|
2020-03-27 19:21:27 +01:00
|
|
|
stopped = mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
|
2015-12-14 02:56:11 +01:00
|
|
|
} break;
|
2020-03-27 19:21:27 +01:00
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
|
|
|
|
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
|
2017-03-05 16:44:50 +01:00
|
|
|
gravity = Vector2(0, 0);
|
2015-12-14 02:56:11 +01:00
|
|
|
area_angular_damp = 0;
|
|
|
|
area_linear_damp = 0;
|
2021-08-24 18:30:31 +02:00
|
|
|
_compute_area_gravity_and_damping(aa[i].area);
|
2020-03-27 19:21:27 +01:00
|
|
|
stopped = mode == PhysicsServer2D::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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!stopped) {
|
2021-08-24 18:30:31 +02:00
|
|
|
_compute_area_gravity_and_damping(def_area);
|
2015-04-10 14:25:40 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
gravity *= gravity_scale;
|
2015-01-05 22:37:12 +01:00
|
|
|
|
2015-12-08 18:44:29 +01:00
|
|
|
// If less than 0, override dampenings with that of the Body2D
|
2020-05-14 16:41:43 +02:00
|
|
|
if (angular_damp >= 0) {
|
2015-12-08 18:44:29 +01:00
|
|
|
area_angular_damp = angular_damp;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
else
|
|
|
|
area_angular_damp=damp_area->get_angular_damp();
|
|
|
|
*/
|
2015-01-05 22:37:12 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (linear_damp >= 0) {
|
2015-12-08 18:44:29 +01:00
|
|
|
area_linear_damp = linear_damp;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
else
|
|
|
|
area_linear_damp=damp_area->get_linear_damp();
|
|
|
|
*/
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
Vector2 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 == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2014-02-19 15:57:14 +01:00
|
|
|
//compute motion, angular and etc. velocities from prev transform
|
2017-01-06 06:27:48 +01:00
|
|
|
motion = new_transform.get_origin() - get_transform().get_origin();
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_velocity = motion / p_step;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-01-06 06:27:48 +01:00
|
|
|
real_t rot = new_transform.get_rotation() - get_transform().get_rotation();
|
2020-08-26 04:00:23 +02:00
|
|
|
angular_velocity = remainder(rot, 2.0 * Math_PI) / p_step;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
do_motion = true;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
for(int i=0;i<get_shape_count();i++) {
|
|
|
|
set_shape_kinematic_advance(i,Vector2());
|
|
|
|
set_shape_kinematic_retreat(i,0);
|
|
|
|
}
|
|
|
|
*/
|
2014-02-19 15:57:14 +01: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
|
|
|
Vector2 force = gravity * mass;
|
|
|
|
force += applied_force;
|
|
|
|
real_t torque = applied_torque;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2015-01-05 22:37:12 +01:00
|
|
|
real_t damp = 1.0 - p_step * area_linear_damp;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (damp < 0) { // reached zero in the given time
|
2017-03-05 16:44:50 +01:00
|
|
|
damp = 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2015-01-05 22:37:12 +01:00
|
|
|
real_t angular_damp = 1.0 - p_step * area_angular_damp;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (angular_damp < 0) { // reached zero in the given time
|
2017-03-05 16:44:50 +01:00
|
|
|
angular_damp = 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_velocity *= damp;
|
|
|
|
angular_velocity *= angular_damp;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
linear_velocity += _inv_mass * force * p_step;
|
|
|
|
angular_velocity += _inv_inertia * torque * p_step;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
|
2018-11-08 19:21:28 +01:00
|
|
|
motion = linear_velocity * p_step;
|
2017-03-05 16:44:50 +01:00
|
|
|
do_motion = true;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//motion=linear_velocity*p_step;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
first_integration = false;
|
|
|
|
biased_angular_velocity = 0;
|
|
|
|
biased_linear_velocity = Vector2();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (do_motion) { //shapes temporarily extend for raycast
|
2014-02-19 15:57:14 +01:00
|
|
|
_update_shapes_with_motion(motion);
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
// damp_area=nullptr; // clear the area, so it is set in the next frame
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Body2DSW::integrate_velocities(real_t p_step) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (fi_callback) {
|
2014-02-19 15:57:14 +01:00
|
|
|
get_space()->body_add_to_state_query_list(&direct_state_query_list);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2017-03-05 16:44:50 +01:00
|
|
|
_set_transform(new_transform, false);
|
2014-10-03 13:58:41 +02:00
|
|
|
_set_inv_transform(new_transform.affine_inverse());
|
2020-05-14 16:41:43 +02:00
|
|
|
if (contacts.size() == 0 && linear_velocity == Vector2() && angular_velocity == 0) {
|
2014-02-19 15:57:14 +01:00
|
|
|
set_active(false); //stopped moving, deactivate
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t total_angular_velocity = angular_velocity + biased_angular_velocity;
|
|
|
|
Vector2 total_linear_velocity = linear_velocity + biased_linear_velocity;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-01-06 06:27:48 +01:00
|
|
|
real_t angle = get_transform().get_rotation() + total_angular_velocity * p_step;
|
2014-02-19 15:57:14 +01:00
|
|
|
Vector2 pos = get_transform().get_origin() + total_linear_velocity * p_step;
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
_set_transform(Transform2D(angle, pos), continuous_cd_mode == PhysicsServer2D::CCD_MODE_DISABLED);
|
2014-02-19 15:57:14 +01:00
|
|
|
_set_inv_transform(get_transform().inverse());
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_transform = get_transform();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
//_update_inertia_tensor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Body2DSW::wakeup_neighbours() {
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const Pair<Constraint2DSW *, int> &E : constraint_list) {
|
2021-07-16 05:45:57 +02:00
|
|
|
const Constraint2DSW *c = E.first;
|
2014-02-19 15:57:14 +01:00
|
|
|
Body2DSW **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++) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (i == E.second) {
|
2014-02-19 15:57:14 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
Body2DSW *b = n[i];
|
2021-05-20 03:15:07 +02:00
|
|
|
if (b->mode != PhysicsServer2D::BODY_MODE_DYNAMIC) {
|
2014-02-19 15:57:14 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!b->is_active()) {
|
2014-02-19 15:57:14 +01:00
|
|
|
b->set_active(true);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Body2DSW::call_queries() {
|
|
|
|
if (fi_callback) {
|
2020-03-28 13:19:05 +01:00
|
|
|
PhysicsDirectBodyState2DSW *dbs = PhysicsDirectBodyState2DSW::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;
|
|
|
|
const Variant *vp[2] = { &v, &fi_callback->callback_udata };
|
2014-10-03 13:58:41 +02:00
|
|
|
|
2021-03-30 08:22:23 +02:00
|
|
|
Object *obj = fi_callback->callable.get_object();
|
2014-02-19 15:57:14 +01:00
|
|
|
if (!obj) {
|
2021-03-30 08:22:23 +02:00
|
|
|
set_force_integration_callback(Callable());
|
2014-02-19 15:57:14 +01:00
|
|
|
} else {
|
2020-02-19 20:27:19 +01:00
|
|
|
Callable::CallError ce;
|
2021-03-30 08:22:23 +02:00
|
|
|
Variant rv;
|
2019-08-07 12:54:30 +02:00
|
|
|
if (fi_callback->callback_udata.get_type() != Variant::NIL) {
|
2021-03-30 08:22:23 +02:00
|
|
|
fi_callback->callable.call(vp, 2, rv, ce);
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
} else {
|
2021-03-30 08:22:23 +02:00
|
|
|
fi_callback->callable.call(vp, 1, rv, ce);
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool Body2DSW::sleep_test(real_t p_step) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2021-05-20 03:15:07 +02:00
|
|
|
return true;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (!can_sleep) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-07-08 17:12:18 +02: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()) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 08:22:23 +02:00
|
|
|
void Body2DSW::set_force_integration_callback(const Callable &p_callable, 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
|
|
|
}
|
|
|
|
|
2021-03-30 08:22:23 +02:00
|
|
|
if (p_callable.get_object()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
fi_callback = memnew(ForceIntegrationCallback);
|
2021-03-30 08:22:23 +02:00
|
|
|
fi_callback->callable = p_callable;
|
2017-03-05 16:44:50 +01:00
|
|
|
fi_callback->callback_udata = p_udata;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
Body2DSW::Body2DSW() :
|
|
|
|
CollisionObject2DSW(TYPE_BODY),
|
|
|
|
active_list(this),
|
|
|
|
inertia_update_list(this),
|
|
|
|
direct_state_query_list(this) {
|
2021-05-20 03:15:07 +02:00
|
|
|
mode = PhysicsServer2D::BODY_MODE_DYNAMIC;
|
2017-03-05 16:44:50 +01:00
|
|
|
active = true;
|
|
|
|
angular_velocity = 0;
|
|
|
|
biased_angular_velocity = 0;
|
|
|
|
mass = 1;
|
2019-08-23 19:13:05 +02:00
|
|
|
inertia = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
user_inertia = false;
|
|
|
|
_inv_inertia = 0;
|
|
|
|
_inv_mass = 1;
|
|
|
|
bounce = 0;
|
|
|
|
friction = 1;
|
|
|
|
omit_force_integration = false;
|
|
|
|
applied_torque = 0;
|
|
|
|
island_step = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
_set_static(false);
|
2017-03-05 16:44:50 +01:00
|
|
|
first_time_kinematic = false;
|
|
|
|
linear_damp = -1;
|
|
|
|
angular_damp = -1;
|
|
|
|
area_angular_damp = 0;
|
|
|
|
area_linear_damp = 0;
|
|
|
|
contact_count = 0;
|
|
|
|
gravity_scale = 1.0;
|
|
|
|
first_integration = false;
|
|
|
|
|
|
|
|
still_time = 0;
|
2020-03-27 19:21:27 +01:00
|
|
|
continuous_cd_mode = PhysicsServer2D::CCD_MODE_DISABLED;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Body2DSW::~Body2DSW() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (fi_callback) {
|
2014-02-19 15:57:14 +01:00
|
|
|
memdelete(fi_callback);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
PhysicsDirectBodyState2DSW *PhysicsDirectBodyState2DSW::singleton = nullptr;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-28 13:19:05 +01:00
|
|
|
PhysicsDirectSpaceState2D *PhysicsDirectBodyState2DSW::get_space_state() {
|
2014-02-19 15:57:14 +01:00
|
|
|
return body->get_space()->get_direct_state();
|
|
|
|
}
|
2016-01-04 00:23:44 +01:00
|
|
|
|
2020-03-28 13:19:05 +01:00
|
|
|
Variant PhysicsDirectBodyState2DSW::get_contact_collider_shape_metadata(int p_contact_idx) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Variant());
|
2016-01-04 00:23:44 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (!PhysicsServer2DSW::singletonsw->body_owner.owns(body->contacts[p_contact_idx].collider)) {
|
2016-01-04 00:23:44 +01:00
|
|
|
return Variant();
|
|
|
|
}
|
2020-03-27 19:21:27 +01:00
|
|
|
Body2DSW *other = PhysicsServer2DSW::singletonsw->body_owner.getornull(body->contacts[p_contact_idx].collider);
|
2016-01-04 00:23:44 +01:00
|
|
|
|
|
|
|
int sidx = body->contacts[p_contact_idx].collider_shape;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (sidx < 0 || sidx >= other->get_shape_count()) {
|
2016-01-04 00:23:44 +01:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
|
|
|
return other->get_shape_metadata(sidx);
|
|
|
|
}
|