2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2021-10-18 21:24:30 +02:00
|
|
|
/* godot_body_2d.h */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 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
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
#ifndef GODOT_BODY_2D_H
|
|
|
|
#define GODOT_BODY_2D_H
|
|
|
|
|
|
|
|
#include "godot_area_2d.h"
|
|
|
|
#include "godot_collision_object_2d.h"
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-12-05 05:12:47 +01:00
|
|
|
#include "core/templates/list.h"
|
|
|
|
#include "core/templates/pair.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/templates/vset.h"
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
class GodotConstraint2D;
|
|
|
|
class GodotPhysicsDirectBodyState2D;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
class GodotBody2D : public GodotCollisionObject2D {
|
2022-08-25 19:35:52 +02:00
|
|
|
PhysicsServer2D::BodyMode mode = PhysicsServer2D::BODY_MODE_RIGID;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
Vector2 biased_linear_velocity;
|
2021-08-30 20:48:43 +02:00
|
|
|
real_t biased_angular_velocity = 0.0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
Vector2 linear_velocity;
|
2021-08-30 20:48:43 +02:00
|
|
|
real_t angular_velocity = 0.0;
|
|
|
|
|
2021-11-25 16:26:38 +01:00
|
|
|
Vector2 prev_linear_velocity;
|
|
|
|
real_t prev_angular_velocity = 0.0;
|
|
|
|
|
2021-08-30 20:48:43 +02:00
|
|
|
Vector2 constant_linear_velocity;
|
|
|
|
real_t constant_angular_velocity = 0.0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-04-14 19:59:53 +02:00
|
|
|
PhysicsServer2D::BodyDampMode linear_damp_mode = PhysicsServer2D::BODY_DAMP_MODE_COMBINE;
|
|
|
|
PhysicsServer2D::BodyDampMode angular_damp_mode = PhysicsServer2D::BODY_DAMP_MODE_COMBINE;
|
|
|
|
|
|
|
|
real_t linear_damp = 0.0;
|
|
|
|
real_t angular_damp = 0.0;
|
|
|
|
|
|
|
|
real_t total_linear_damp = 0.0;
|
|
|
|
real_t total_angular_damp = 0.0;
|
|
|
|
|
2021-06-11 02:37:19 +02:00
|
|
|
real_t gravity_scale = 1.0;
|
2015-01-05 22:37:12 +01:00
|
|
|
|
2021-06-11 02:37:19 +02:00
|
|
|
real_t bounce = 0.0;
|
|
|
|
real_t friction = 1.0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-06-11 02:37:19 +02:00
|
|
|
real_t mass = 1.0;
|
|
|
|
real_t _inv_mass = 1.0;
|
|
|
|
|
|
|
|
real_t inertia = 0.0;
|
|
|
|
real_t _inv_inertia = 0.0;
|
|
|
|
|
2021-10-20 01:16:00 +02:00
|
|
|
Vector2 center_of_mass_local;
|
2021-06-11 02:37:19 +02:00
|
|
|
Vector2 center_of_mass;
|
|
|
|
|
|
|
|
bool calculate_inertia = true;
|
|
|
|
bool calculate_center_of_mass = true;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
Vector2 gravity;
|
|
|
|
|
2021-06-11 02:37:19 +02:00
|
|
|
real_t still_time = 0.0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
Vector2 applied_force;
|
2021-06-11 02:37:19 +02:00
|
|
|
real_t applied_torque = 0.0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-12-08 02:09:54 +01:00
|
|
|
Vector2 constant_force;
|
|
|
|
real_t constant_torque = 0.0;
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
SelfList<GodotBody2D> active_list;
|
|
|
|
SelfList<GodotBody2D> mass_properties_update_list;
|
|
|
|
SelfList<GodotBody2D> direct_state_query_list;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
VSet<RID> exceptions;
|
2021-06-11 02:37:19 +02:00
|
|
|
PhysicsServer2D::CCDMode continuous_cd_mode = PhysicsServer2D::CCD_MODE_DISABLED;
|
|
|
|
bool omit_force_integration = false;
|
|
|
|
bool active = true;
|
|
|
|
bool can_sleep = true;
|
|
|
|
bool first_time_kinematic = false;
|
|
|
|
void _mass_properties_changed();
|
2022-04-05 12:40:26 +02:00
|
|
|
virtual void _shapes_changed() override;
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D new_transform;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
List<Pair<GodotConstraint2D *, int>> constraint_list;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
struct AreaCMP {
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotArea2D *area = nullptr;
|
2021-09-14 11:01:49 +02:00
|
|
|
int refCount = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ bool operator==(const AreaCMP &p_cmp) const { return area->get_self() == p_cmp.area->get_self(); }
|
|
|
|
_FORCE_INLINE_ bool operator<(const AreaCMP &p_cmp) const { return area->get_priority() < p_cmp.area->get_priority(); }
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ AreaCMP() {}
|
2021-10-18 21:24:30 +02:00
|
|
|
_FORCE_INLINE_ AreaCMP(GodotArea2D *p_area) {
|
2017-03-05 16:44:50 +01:00
|
|
|
area = p_area;
|
|
|
|
refCount = 1;
|
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
};
|
|
|
|
|
2015-04-10 14:25:40 +02:00
|
|
|
Vector<AreaCMP> areas;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
struct Contact {
|
|
|
|
Vector2 local_pos;
|
|
|
|
Vector2 local_normal;
|
2021-09-14 11:01:49 +02:00
|
|
|
real_t depth = 0.0;
|
|
|
|
int local_shape = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
Vector2 collider_pos;
|
2021-09-14 11:01:49 +02:00
|
|
|
int collider_shape = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
ObjectID collider_instance_id;
|
|
|
|
RID collider;
|
|
|
|
Vector2 collider_velocity_at_pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Contact> contacts; //no contacts by default
|
2021-06-11 02:37:19 +02:00
|
|
|
int contact_count = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-06-05 01:09:41 +02:00
|
|
|
void *body_state_callback_instance = nullptr;
|
|
|
|
PhysicsServer2D::BodyStateCallback body_state_callback = nullptr;
|
|
|
|
|
|
|
|
struct ForceIntegrationCallbackData {
|
2021-03-30 08:22:23 +02:00
|
|
|
Callable callable;
|
2021-06-05 01:09:41 +02:00
|
|
|
Variant udata;
|
2014-02-19 15:57:14 +01:00
|
|
|
};
|
|
|
|
|
2021-06-05 01:09:41 +02:00
|
|
|
ForceIntegrationCallbackData *fi_callback_data = nullptr;
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotPhysicsDirectBodyState2D *direct_state = nullptr;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-06-11 02:37:19 +02:00
|
|
|
uint64_t island_step = 0;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-10-20 01:16:00 +02:00
|
|
|
void _update_transform_dependent();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
friend class GodotPhysicsDirectBodyState2D; // i give up, too many functions to expose
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
public:
|
2021-06-05 01:09:41 +02:00
|
|
|
void set_state_sync_callback(void *p_instance, PhysicsServer2D::BodyStateCallback p_callback);
|
2021-03-30 08:22:23 +02:00
|
|
|
void set_force_integration_callback(const Callable &p_callable, const Variant &p_udata = Variant());
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotPhysicsDirectBodyState2D *get_direct_state();
|
2021-06-05 01:09:41 +02:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
_FORCE_INLINE_ void add_area(GodotArea2D *p_area) {
|
2015-12-01 14:30:39 +01:00
|
|
|
int index = areas.find(AreaCMP(p_area));
|
2017-03-05 16:44:50 +01:00
|
|
|
if (index > -1) {
|
2018-07-25 03:11:03 +02:00
|
|
|
areas.write[index].refCount += 1;
|
2015-12-01 14:30:39 +01:00
|
|
|
} else {
|
|
|
|
areas.ordered_insert(AreaCMP(p_area));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
_FORCE_INLINE_ void remove_area(GodotArea2D *p_area) {
|
2015-12-01 14:30:39 +01:00
|
|
|
int index = areas.find(AreaCMP(p_area));
|
2017-03-05 16:44:50 +01:00
|
|
|
if (index > -1) {
|
2018-07-25 03:11:03 +02:00
|
|
|
areas.write[index].refCount -= 1;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (areas[index].refCount < 1) {
|
2021-07-04 00:17:03 +02:00
|
|
|
areas.remove_at(index);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-12-01 14:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_max_contacts_reported(int p_size) {
|
|
|
|
contacts.resize(p_size);
|
|
|
|
contact_count = 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC && p_size) {
|
2020-05-10 12:56:01 +02:00
|
|
|
set_active(true);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
2014-10-03 13:58:41 +02:00
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ int get_max_contacts_reported() const { return contacts.size(); }
|
|
|
|
|
2020-12-15 13:04:21 +01:00
|
|
|
_FORCE_INLINE_ bool can_report_contacts() const { return !contacts.is_empty(); }
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void add_contact(const Vector2 &p_local_pos, const Vector2 &p_local_normal, real_t p_depth, int p_local_shape, const Vector2 &p_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector2 &p_collider_velocity_at_pos);
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void add_exception(const RID &p_exception) { exceptions.insert(p_exception); }
|
|
|
|
_FORCE_INLINE_ void remove_exception(const RID &p_exception) { exceptions.erase(p_exception); }
|
|
|
|
_FORCE_INLINE_ bool has_exception(const RID &p_exception) const { return exceptions.has(p_exception); }
|
|
|
|
_FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
|
2014-02-19 15:57:14 +01:00
|
|
|
|
|
|
|
_FORCE_INLINE_ uint64_t get_island_step() const { return island_step; }
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_island_step(uint64_t p_step) { island_step = p_step; }
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
_FORCE_INLINE_ void add_constraint(GodotConstraint2D *p_constraint, int p_pos) { constraint_list.push_back({ p_constraint, p_pos }); }
|
|
|
|
_FORCE_INLINE_ void remove_constraint(GodotConstraint2D *p_constraint, int p_pos) { constraint_list.erase({ p_constraint, p_pos }); }
|
|
|
|
const List<Pair<GodotConstraint2D *, int>> &get_constraint_list() const { return constraint_list; }
|
2020-12-05 05:12:47 +01:00
|
|
|
_FORCE_INLINE_ void clear_constraint_list() { constraint_list.clear(); }
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_omit_force_integration(bool p_omit_force_integration) { omit_force_integration = p_omit_force_integration; }
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ bool get_omit_force_integration() const { return omit_force_integration; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_linear_velocity(const Vector2 &p_velocity) { linear_velocity = p_velocity; }
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ Vector2 get_linear_velocity() const { return linear_velocity; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_angular_velocity(real_t p_velocity) { angular_velocity = p_velocity; }
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ real_t get_angular_velocity() const { return angular_velocity; }
|
|
|
|
|
2021-11-25 16:26:38 +01:00
|
|
|
_FORCE_INLINE_ Vector2 get_prev_linear_velocity() const { return prev_linear_velocity; }
|
|
|
|
_FORCE_INLINE_ real_t get_prev_angular_velocity() const { return prev_angular_velocity; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_biased_linear_velocity(const Vector2 &p_velocity) { biased_linear_velocity = p_velocity; }
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ Vector2 get_biased_linear_velocity() const { return biased_linear_velocity; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_biased_angular_velocity(real_t p_velocity) { biased_angular_velocity = p_velocity; }
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ real_t get_biased_angular_velocity() const { return biased_angular_velocity; }
|
|
|
|
|
2018-07-24 09:49:12 +02:00
|
|
|
_FORCE_INLINE_ void apply_central_impulse(const Vector2 &p_impulse) {
|
|
|
|
linear_velocity += p_impulse * _inv_mass;
|
|
|
|
}
|
|
|
|
|
2020-03-26 05:23:34 +01:00
|
|
|
_FORCE_INLINE_ void apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) {
|
2016-04-24 10:35:21 +02:00
|
|
|
linear_velocity += p_impulse * _inv_mass;
|
2021-06-11 02:37:19 +02:00
|
|
|
angular_velocity += _inv_inertia * (p_position - center_of_mass).cross(p_impulse);
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2018-07-24 09:49:12 +02:00
|
|
|
_FORCE_INLINE_ void apply_torque_impulse(real_t p_torque) {
|
|
|
|
angular_velocity += _inv_inertia * p_torque;
|
|
|
|
}
|
|
|
|
|
2021-12-03 18:38:40 +01:00
|
|
|
_FORCE_INLINE_ void apply_bias_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2(), real_t p_max_delta_av = -1.0) {
|
2020-03-26 05:23:34 +01:00
|
|
|
biased_linear_velocity += p_impulse * _inv_mass;
|
2021-12-03 18:38:40 +01:00
|
|
|
if (p_max_delta_av != 0.0) {
|
|
|
|
real_t delta_av = _inv_inertia * (p_position - center_of_mass).cross(p_impulse);
|
|
|
|
if (p_max_delta_av > 0 && delta_av > p_max_delta_av) {
|
|
|
|
delta_av = p_max_delta_av;
|
|
|
|
}
|
|
|
|
biased_angular_velocity += delta_av;
|
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 02:09:54 +01:00
|
|
|
_FORCE_INLINE_ void apply_central_force(const Vector2 &p_force) {
|
|
|
|
applied_force += p_force;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void apply_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) {
|
|
|
|
applied_force += p_force;
|
|
|
|
applied_torque += (p_position - center_of_mass).cross(p_force);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void apply_torque(real_t p_torque) {
|
|
|
|
applied_torque += p_torque;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void add_constant_central_force(const Vector2 &p_force) {
|
|
|
|
constant_force += p_force;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void add_constant_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) {
|
|
|
|
constant_force += p_force;
|
|
|
|
constant_torque += (p_position - center_of_mass).cross(p_force);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void add_constant_torque(real_t p_torque) {
|
|
|
|
constant_torque += p_torque;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_constant_force(const Vector2 &p_force) { constant_force = p_force; }
|
|
|
|
Vector2 get_constant_force() const { return constant_force; }
|
|
|
|
|
|
|
|
void set_constant_torque(real_t p_torque) { constant_torque = p_torque; }
|
|
|
|
real_t get_constant_torque() const { return constant_torque; }
|
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
void set_active(bool p_active);
|
|
|
|
_FORCE_INLINE_ bool is_active() const { return active; }
|
|
|
|
|
2015-04-26 21:20:00 +02:00
|
|
|
_FORCE_INLINE_ void wakeup() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if ((!get_space()) || mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2015-04-26 21:20:00 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-04-26 21:20:00 +02:00
|
|
|
set_active(true);
|
|
|
|
}
|
|
|
|
|
2021-06-11 02:37:19 +02:00
|
|
|
void set_param(PhysicsServer2D::BodyParameter p_param, const Variant &p_value);
|
|
|
|
Variant get_param(PhysicsServer2D::BodyParameter p_param) const;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void set_mode(PhysicsServer2D::BodyMode p_mode);
|
|
|
|
PhysicsServer2D::BodyMode get_mode() const;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void set_state(PhysicsServer2D::BodyState p_state, const Variant &p_variant);
|
|
|
|
Variant get_state(PhysicsServer2D::BodyState p_state) const;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
_FORCE_INLINE_ void set_continuous_collision_detection_mode(PhysicsServer2D::CCDMode p_mode) { continuous_cd_mode = p_mode; }
|
|
|
|
_FORCE_INLINE_ PhysicsServer2D::CCDMode get_continuous_collision_detection_mode() const { return continuous_cd_mode; }
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2022-04-05 12:40:26 +02:00
|
|
|
void set_space(GodotSpace2D *p_space) override;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2021-06-11 02:37:19 +02:00
|
|
|
void update_mass_properties();
|
|
|
|
void reset_mass_properties();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-04-14 19:59:53 +02:00
|
|
|
_FORCE_INLINE_ const Vector2 &get_center_of_mass() const { return center_of_mass; }
|
2021-10-22 20:56:00 +02:00
|
|
|
_FORCE_INLINE_ const Vector2 &get_center_of_mass_local() const { return center_of_mass_local; }
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ real_t get_inv_mass() const { return _inv_mass; }
|
|
|
|
_FORCE_INLINE_ real_t get_inv_inertia() const { return _inv_inertia; }
|
|
|
|
_FORCE_INLINE_ real_t get_friction() const { return friction; }
|
|
|
|
_FORCE_INLINE_ real_t get_bounce() const { return bounce; }
|
2015-01-05 22:37:12 +01:00
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
void integrate_forces(real_t p_step);
|
|
|
|
void integrate_velocities(real_t p_step);
|
|
|
|
|
2021-08-09 21:14:24 +02:00
|
|
|
_FORCE_INLINE_ Vector2 get_velocity_in_local_point(const Vector2 &rel_pos) const {
|
|
|
|
return linear_velocity + Vector2(-angular_velocity * rel_pos.y, angular_velocity * rel_pos.x);
|
|
|
|
}
|
|
|
|
|
2014-02-19 15:57:14 +01:00
|
|
|
_FORCE_INLINE_ Vector2 get_motion() const {
|
2020-03-27 19:21:27 +01:00
|
|
|
if (mode > PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return new_transform.get_origin() - get_transform().get_origin();
|
2020-03-27 19:21:27 +01:00
|
|
|
} else if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
|
2017-03-05 16:44:50 +01:00
|
|
|
return get_transform().get_origin() - new_transform.get_origin(); //kinematic simulates forward
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
return Vector2();
|
|
|
|
}
|
|
|
|
|
|
|
|
void call_queries();
|
|
|
|
void wakeup_neighbours();
|
|
|
|
|
|
|
|
bool sleep_test(real_t p_step);
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotBody2D();
|
|
|
|
~GodotBody2D();
|
2014-02-19 15:57:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//add contact inline
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
void GodotBody2D::add_contact(const Vector2 &p_local_pos, const Vector2 &p_local_normal, real_t p_depth, int p_local_shape, const Vector2 &p_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector2 &p_collider_velocity_at_pos) {
|
2017-03-05 16:44:50 +01:00
|
|
|
int c_max = contacts.size();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (c_max == 0) {
|
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
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
Contact *c = contacts.ptrw();
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = -1;
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (contact_count < c_max) {
|
|
|
|
idx = contact_count++;
|
2014-02-19 15:57:14 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t least_depth = 1e20;
|
|
|
|
int least_deep = -1;
|
|
|
|
for (int i = 0; i < c_max; i++) {
|
|
|
|
if (i == 0 || c[i].depth < least_depth) {
|
|
|
|
least_deep = i;
|
|
|
|
least_depth = c[i].depth;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (least_deep >= 0 && least_depth < p_depth) {
|
|
|
|
idx = least_deep;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
if (idx == -1) {
|
2014-02-19 15:57:14 +01:00
|
|
|
return; //none least deepe than this
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
c[idx].local_pos = p_local_pos;
|
|
|
|
c[idx].local_normal = p_local_normal;
|
|
|
|
c[idx].depth = p_depth;
|
|
|
|
c[idx].local_shape = p_local_shape;
|
|
|
|
c[idx].collider_pos = p_collider_pos;
|
|
|
|
c[idx].collider_shape = p_collider_shape;
|
|
|
|
c[idx].collider_instance_id = p_collider_instance_id;
|
|
|
|
c[idx].collider = p_collider;
|
|
|
|
c[idx].collider_velocity_at_pos = p_collider_velocity_at_pos;
|
2014-02-19 15:57:14 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
#endif // GODOT_BODY_2D_H
|