2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-03-28 13:19:05 +01:00
|
|
|
/* body_3d_sw.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
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-05-14 06:22:15 +02:00
|
|
|
#ifndef BODY_SW_H
|
|
|
|
#define BODY_SW_H
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
#include "area_3d_sw.h"
|
|
|
|
#include "collision_object_3d_sw.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/vset.h"
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
class Constraint3DSW;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
class Body3DSW : public CollisionObject3DSW {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::BodyMode mode;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
Vector3 linear_velocity;
|
|
|
|
Vector3 angular_velocity;
|
|
|
|
|
|
|
|
Vector3 biased_linear_velocity;
|
|
|
|
Vector3 biased_angular_velocity;
|
|
|
|
real_t mass;
|
|
|
|
real_t bounce;
|
|
|
|
real_t friction;
|
|
|
|
|
2015-08-30 23:57:17 +02:00
|
|
|
real_t linear_damp;
|
|
|
|
real_t angular_damp;
|
|
|
|
real_t gravity_scale;
|
|
|
|
|
2017-12-10 17:21:14 +01:00
|
|
|
uint16_t locked_axis;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-11-07 15:22:09 +01:00
|
|
|
real_t kinematic_safe_margin;
|
2014-05-14 06:22:15 +02:00
|
|
|
real_t _inv_mass;
|
2016-12-31 15:39:25 +01:00
|
|
|
Vector3 _inv_inertia; // Relative to the principal axes of inertia
|
|
|
|
|
|
|
|
// Relative to the local frame of reference
|
2017-01-11 04:52:51 +01:00
|
|
|
Basis principal_inertia_axes_local;
|
2016-12-31 15:39:25 +01:00
|
|
|
Vector3 center_of_mass_local;
|
|
|
|
|
|
|
|
// In world orientation with local origin
|
2017-01-11 04:52:51 +01:00
|
|
|
Basis _inv_inertia_tensor;
|
|
|
|
Basis principal_inertia_axes;
|
2016-12-31 15:39:25 +01:00
|
|
|
Vector3 center_of_mass;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
Vector3 gravity;
|
|
|
|
|
|
|
|
real_t still_time;
|
|
|
|
|
|
|
|
Vector3 applied_force;
|
|
|
|
Vector3 applied_torque;
|
|
|
|
|
2017-02-14 00:25:05 +01:00
|
|
|
real_t area_angular_damp;
|
|
|
|
real_t area_linear_damp;
|
2015-08-30 23:57:17 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
SelfList<Body3DSW> active_list;
|
|
|
|
SelfList<Body3DSW> inertia_update_list;
|
|
|
|
SelfList<Body3DSW> direct_state_query_list;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
VSet<RID> exceptions;
|
|
|
|
bool omit_force_integration;
|
|
|
|
bool active;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2016-01-02 17:28:18 +01:00
|
|
|
bool first_integration;
|
|
|
|
|
2014-05-14 06:22:15 +02:00
|
|
|
bool continuous_cd;
|
|
|
|
bool can_sleep;
|
2014-10-14 06:01:25 +02:00
|
|
|
bool first_time_kinematic;
|
2014-05-14 06:22:15 +02:00
|
|
|
void _update_inertia();
|
|
|
|
virtual void _shapes_changed();
|
2014-09-03 04:13:40 +02:00
|
|
|
Transform new_transform;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
Map<Constraint3DSW *, int> constraint_map;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
struct AreaCMP {
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
Area3DSW *area;
|
2015-12-01 14:30:39 +01:00
|
|
|
int refCount;
|
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-05-14 06:22:15 +02:00
|
|
|
_FORCE_INLINE_ AreaCMP() {}
|
2020-03-27 19:21:27 +01:00
|
|
|
_FORCE_INLINE_ AreaCMP(Area3DSW *p_area) {
|
2017-03-05 16:44:50 +01:00
|
|
|
area = p_area;
|
|
|
|
refCount = 1;
|
|
|
|
}
|
2014-05-14 06:22:15 +02:00
|
|
|
};
|
|
|
|
|
2015-03-20 00:53:54 +01:00
|
|
|
Vector<AreaCMP> areas;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
struct Contact {
|
|
|
|
|
|
|
|
Vector3 local_pos;
|
|
|
|
Vector3 local_normal;
|
2017-02-14 00:25:05 +01:00
|
|
|
real_t depth;
|
2014-05-14 06:22:15 +02:00
|
|
|
int local_shape;
|
|
|
|
Vector3 collider_pos;
|
|
|
|
int collider_shape;
|
|
|
|
ObjectID collider_instance_id;
|
|
|
|
RID collider;
|
|
|
|
Vector3 collider_velocity_at_pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Contact> contacts; //no contacts by default
|
|
|
|
int contact_count;
|
|
|
|
|
|
|
|
struct ForceIntegrationCallback {
|
|
|
|
|
|
|
|
ObjectID id;
|
|
|
|
StringName method;
|
|
|
|
Variant udata;
|
|
|
|
};
|
|
|
|
|
|
|
|
ForceIntegrationCallback *fi_callback;
|
|
|
|
|
|
|
|
uint64_t island_step;
|
2020-03-27 19:21:27 +01:00
|
|
|
Body3DSW *island_next;
|
|
|
|
Body3DSW *island_list_next;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
_FORCE_INLINE_ void _compute_area_gravity_and_dampenings(const Area3DSW *p_area);
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
_FORCE_INLINE_ void _update_transform_dependant();
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
friend class PhysicsDirectBodyState3DSW; // i give up, too many functions to expose
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
public:
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata = Variant());
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-11-07 15:22:09 +01:00
|
|
|
void set_kinematic_margin(real_t p_margin);
|
|
|
|
_FORCE_INLINE_ real_t get_kinematic_margin() { return kinematic_safe_margin; }
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
_FORCE_INLINE_ void add_area(Area3DSW *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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
_FORCE_INLINE_ void remove_area(Area3DSW *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;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (areas[index].refCount < 1)
|
2015-12-01 14:30:39 +01:00
|
|
|
areas.remove(index);
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 06:22:15 +02: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-03-27 19:21:27 +01:00
|
|
|
if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC && p_size) set_active(true);
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
2014-05-14 06:22:15 +02:00
|
|
|
_FORCE_INLINE_ int get_max_contacts_reported() const { return contacts.size(); }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool can_report_contacts() const { return !contacts.empty(); }
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void add_contact(const Vector3 &p_local_pos, const Vector3 &p_local_normal, real_t p_depth, int p_local_shape, const Vector3 &p_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector3 &p_collider_velocity_at_pos);
|
2014-05-14 06:22:15 +02: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-05-14 06:22:15 +02: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-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
_FORCE_INLINE_ Body3DSW *get_island_next() const { return island_next; }
|
|
|
|
_FORCE_INLINE_ void set_island_next(Body3DSW *p_next) { island_next = p_next; }
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
_FORCE_INLINE_ Body3DSW *get_island_list_next() const { return island_list_next; }
|
|
|
|
_FORCE_INLINE_ void set_island_list_next(Body3DSW *p_next) { island_list_next = p_next; }
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
_FORCE_INLINE_ void add_constraint(Constraint3DSW *p_constraint, int p_pos) { constraint_map[p_constraint] = p_pos; }
|
|
|
|
_FORCE_INLINE_ void remove_constraint(Constraint3DSW *p_constraint) { constraint_map.erase(p_constraint); }
|
|
|
|
const Map<Constraint3DSW *, int> &get_constraint_map() const { return constraint_map; }
|
2017-07-25 04:26:47 +02:00
|
|
|
_FORCE_INLINE_ void clear_constraint_map() { constraint_map.clear(); }
|
2014-05-14 06:22:15 +02: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-05-14 06:22:15 +02:00
|
|
|
_FORCE_INLINE_ bool get_omit_force_integration() const { return omit_force_integration; }
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
_FORCE_INLINE_ Basis get_principal_inertia_axes() const { return principal_inertia_axes; }
|
2016-12-31 15:39:25 +01:00
|
|
|
_FORCE_INLINE_ Vector3 get_center_of_mass() const { return center_of_mass; }
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ Vector3 xform_local_to_principal(const Vector3 &p_pos) const { return principal_inertia_axes_local.xform(p_pos - center_of_mass_local); }
|
2016-12-31 15:39:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_linear_velocity(const Vector3 &p_velocity) { linear_velocity = p_velocity; }
|
2014-05-14 06:22:15 +02:00
|
|
|
_FORCE_INLINE_ Vector3 get_linear_velocity() const { return linear_velocity; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_angular_velocity(const Vector3 &p_velocity) { angular_velocity = p_velocity; }
|
2014-05-14 06:22:15 +02:00
|
|
|
_FORCE_INLINE_ Vector3 get_angular_velocity() const { return angular_velocity; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ const Vector3 &get_biased_linear_velocity() const { return biased_linear_velocity; }
|
|
|
|
_FORCE_INLINE_ const Vector3 &get_biased_angular_velocity() const { return biased_angular_velocity; }
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2018-07-24 09:49:12 +02:00
|
|
|
_FORCE_INLINE_ void apply_central_impulse(const Vector3 &p_j) {
|
|
|
|
linear_velocity += p_j * _inv_mass;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void apply_impulse(const Vector3 &p_pos, const Vector3 &p_j) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
linear_velocity += p_j * _inv_mass;
|
2017-03-05 16:44:50 +01:00
|
|
|
angular_velocity += _inv_inertia_tensor.xform((p_pos - center_of_mass).cross(p_j));
|
2016-12-31 15:39:25 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void apply_torque_impulse(const Vector3 &p_j) {
|
2016-12-31 15:39:25 +01:00
|
|
|
|
|
|
|
angular_velocity += _inv_inertia_tensor.xform(p_j);
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 01:01:19 +02:00
|
|
|
_FORCE_INLINE_ void apply_bias_impulse(const Vector3 &p_pos, const Vector3 &p_j, real_t p_max_delta_av = -1.0) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
biased_linear_velocity += p_j * _inv_mass;
|
2017-09-18 01:01:19 +02:00
|
|
|
if (p_max_delta_av != 0.0) {
|
|
|
|
Vector3 delta_av = _inv_inertia_tensor.xform((p_pos - center_of_mass).cross(p_j));
|
|
|
|
if (p_max_delta_av > 0 && delta_av.length() > p_max_delta_av) {
|
|
|
|
delta_av = delta_av.normalized() * p_max_delta_av;
|
|
|
|
}
|
|
|
|
biased_angular_velocity += delta_av;
|
|
|
|
}
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void apply_bias_torque_impulse(const Vector3 &p_j) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
biased_angular_velocity += _inv_inertia_tensor.xform(p_j);
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:48:07 +01:00
|
|
|
_FORCE_INLINE_ void add_central_force(const Vector3 &p_force) {
|
|
|
|
|
|
|
|
applied_force += p_force;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void add_force(const Vector3 &p_force, const Vector3 &p_pos) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
applied_force += p_force;
|
|
|
|
applied_torque += p_pos.cross(p_force);
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:48:07 +01:00
|
|
|
_FORCE_INLINE_ void add_torque(const Vector3 &p_torque) {
|
|
|
|
applied_torque += p_torque;
|
|
|
|
}
|
|
|
|
|
2014-05-14 06:22:15 +02: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-03-27 19:21:27 +01:00
|
|
|
if ((!get_space()) || mode == PhysicsServer3D::BODY_MODE_STATIC || mode == PhysicsServer3D::BODY_MODE_KINEMATIC)
|
2015-04-26 21:20:00 +02:00
|
|
|
return;
|
|
|
|
set_active(true);
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void set_param(PhysicsServer3D::BodyParameter p_param, real_t);
|
|
|
|
real_t get_param(PhysicsServer3D::BodyParameter p_param) const;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void set_mode(PhysicsServer3D::BodyMode p_mode);
|
|
|
|
PhysicsServer3D::BodyMode get_mode() const;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void set_state(PhysicsServer3D::BodyState p_state, const Variant &p_variant);
|
|
|
|
Variant get_state(PhysicsServer3D::BodyState p_state) const;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_applied_force(const Vector3 &p_force) { applied_force = p_force; }
|
2014-05-14 06:22:15 +02:00
|
|
|
Vector3 get_applied_force() const { return applied_force; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_applied_torque(const Vector3 &p_torque) { applied_torque = p_torque; }
|
2014-05-14 06:22:15 +02:00
|
|
|
Vector3 get_applied_torque() const { return applied_torque; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_continuous_collision_detection(bool p_enable) { continuous_cd = p_enable; }
|
2014-05-14 06:22:15 +02:00
|
|
|
_FORCE_INLINE_ bool is_continuous_collision_detection_enabled() const { return continuous_cd; }
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void set_space(Space3DSW *p_space);
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
void update_inertias();
|
|
|
|
|
|
|
|
_FORCE_INLINE_ real_t get_inv_mass() const { return _inv_mass; }
|
|
|
|
_FORCE_INLINE_ Vector3 get_inv_inertia() const { return _inv_inertia; }
|
2017-01-11 04:52:51 +01:00
|
|
|
_FORCE_INLINE_ Basis get_inv_inertia_tensor() const { return _inv_inertia_tensor; }
|
2014-05-14 06:22:15 +02:00
|
|
|
_FORCE_INLINE_ real_t get_friction() const { return friction; }
|
|
|
|
_FORCE_INLINE_ Vector3 get_gravity() const { return gravity; }
|
|
|
|
_FORCE_INLINE_ real_t get_bounce() const { return bounce; }
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool lock);
|
|
|
|
bool is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
void integrate_forces(real_t p_step);
|
|
|
|
void integrate_velocities(real_t p_step);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ Vector3 get_velocity_in_local_point(const Vector3 &rel_pos) const {
|
2014-09-15 16:33:30 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return linear_velocity + angular_velocity.cross(rel_pos - center_of_mass);
|
2014-09-15 16:33:30 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ real_t compute_impulse_denominator(const Vector3 &p_pos, const Vector3 &p_normal) const {
|
2014-09-15 16:33:30 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 r0 = p_pos - get_transform().origin - center_of_mass;
|
2014-09-15 16:33:30 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 c0 = (r0).cross(p_normal);
|
2014-09-15 16:33:30 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 vec = (_inv_inertia_tensor.xform_inv(c0)).cross(r0);
|
2014-09-15 16:33:30 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return _inv_mass + p_normal.dot(vec);
|
|
|
|
}
|
2014-09-15 16:33:30 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ real_t compute_angular_impulse_denominator(const Vector3 &p_axis) const {
|
2014-09-15 16:33:30 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return p_axis.dot(_inv_inertia_tensor.xform_inv(p_axis));
|
|
|
|
}
|
2014-09-15 16:33:30 +02:00
|
|
|
|
2014-09-03 04:13:40 +02:00
|
|
|
//void simulate_motion(const Transform& p_xform,real_t p_step);
|
2014-05-14 06:22:15 +02:00
|
|
|
void call_queries();
|
|
|
|
void wakeup_neighbours();
|
|
|
|
|
|
|
|
bool sleep_test(real_t p_step);
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
Body3DSW();
|
|
|
|
~Body3DSW();
|
2014-05-14 06:22:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//add contact inline
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void Body3DSW::add_contact(const Vector3 &p_local_pos, const Vector3 &p_local_normal, real_t p_depth, int p_local_shape, const Vector3 &p_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector3 &p_collider_velocity_at_pos) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int c_max = contacts.size();
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c_max == 0)
|
2014-05-14 06:22:15 +02:00
|
|
|
return;
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
Contact *c = contacts.ptrw();
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = -1;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (contact_count < c_max) {
|
|
|
|
idx = contact_count++;
|
2014-05-14 06:22:15 +02: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++) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (i == 0 || c[i].depth < least_depth) {
|
|
|
|
least_deep = i;
|
|
|
|
least_depth = c[i].depth;
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (least_deep >= 0 && least_depth < p_depth) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
idx = least_deep;
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (idx == -1)
|
2014-05-14 06:22:15 +02:00
|
|
|
return; //none least deepe than this
|
|
|
|
}
|
|
|
|
|
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-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
class PhysicsDirectBodyState3DSW : public PhysicsDirectBodyState3D {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
GDCLASS(PhysicsDirectBodyState3DSW, PhysicsDirectBodyState3D);
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
public:
|
2020-03-27 19:21:27 +01:00
|
|
|
static PhysicsDirectBodyState3DSW *singleton;
|
|
|
|
Body3DSW *body;
|
2014-05-14 06:22:15 +02:00
|
|
|
real_t step;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual Vector3 get_total_gravity() const { return body->gravity; } // get gravity vector working on this body space/area
|
|
|
|
virtual real_t get_total_angular_damp() const { return body->area_angular_damp; } // get density of this body space/area
|
|
|
|
virtual real_t get_total_linear_damp() const { return body->area_linear_damp; } // get density of this body space/area
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2016-12-31 15:39:25 +01:00
|
|
|
virtual Vector3 get_center_of_mass() const { return body->get_center_of_mass(); }
|
2017-01-11 04:52:51 +01:00
|
|
|
virtual Basis get_principal_inertia_axes() const { return body->get_principal_inertia_axes(); }
|
2016-12-31 15:39:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual real_t get_inverse_mass() const { return body->get_inv_mass(); } // get the mass
|
|
|
|
virtual Vector3 get_inverse_inertia() const { return body->get_inv_inertia(); } // get density of this body space
|
|
|
|
virtual Basis get_inverse_inertia_tensor() const { return body->get_inv_inertia_tensor(); } // get density of this body space
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void set_linear_velocity(const Vector3 &p_velocity) { body->set_linear_velocity(p_velocity); }
|
|
|
|
virtual Vector3 get_linear_velocity() const { return body->get_linear_velocity(); }
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void set_angular_velocity(const Vector3 &p_velocity) { body->set_angular_velocity(p_velocity); }
|
|
|
|
virtual Vector3 get_angular_velocity() const { return body->get_angular_velocity(); }
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
virtual void set_transform(const Transform &p_transform) { body->set_state(PhysicsServer3D::BODY_STATE_TRANSFORM, p_transform); }
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual Transform get_transform() const { return body->get_transform(); }
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2018-02-16 17:48:07 +01:00
|
|
|
virtual void add_central_force(const Vector3 &p_force) { body->add_central_force(p_force); }
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void add_force(const Vector3 &p_force, const Vector3 &p_pos) { body->add_force(p_force, p_pos); }
|
2018-02-16 17:48:07 +01:00
|
|
|
virtual void add_torque(const Vector3 &p_torque) { body->add_torque(p_torque); }
|
2018-07-24 09:49:12 +02:00
|
|
|
virtual void apply_central_impulse(const Vector3 &p_j) { body->apply_central_impulse(p_j); }
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void apply_impulse(const Vector3 &p_pos, const Vector3 &p_j) { body->apply_impulse(p_pos, p_j); }
|
|
|
|
virtual void apply_torque_impulse(const Vector3 &p_j) { body->apply_torque_impulse(p_j); }
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void set_sleep_state(bool p_enable) { body->set_active(!p_enable); }
|
|
|
|
virtual bool is_sleeping() const { return !body->is_active(); }
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int get_contact_count() const { return body->contact_count; }
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
virtual Vector3 get_contact_local_position(int p_contact_idx) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
|
2014-05-14 06:22:15 +02:00
|
|
|
return body->contacts[p_contact_idx].local_pos;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual Vector3 get_contact_local_normal(int p_contact_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
|
|
|
|
return body->contacts[p_contact_idx].local_normal;
|
|
|
|
}
|
2018-01-12 22:27:45 +01:00
|
|
|
virtual float get_contact_impulse(int p_contact_idx) const {
|
|
|
|
return 0.0f; // Only implemented for bullet
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual int get_contact_local_shape(int p_contact_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, -1);
|
|
|
|
return body->contacts[p_contact_idx].local_shape;
|
|
|
|
}
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual RID get_contact_collider(int p_contact_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, RID());
|
|
|
|
return body->contacts[p_contact_idx].collider;
|
|
|
|
}
|
2017-09-10 15:37:49 +02:00
|
|
|
virtual Vector3 get_contact_collider_position(int p_contact_idx) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
|
|
|
|
return body->contacts[p_contact_idx].collider_pos;
|
|
|
|
}
|
|
|
|
virtual ObjectID get_contact_collider_id(int p_contact_idx) const {
|
2020-02-12 18:24:06 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, ObjectID());
|
2017-03-05 16:44:50 +01:00
|
|
|
return body->contacts[p_contact_idx].collider_instance_id;
|
|
|
|
}
|
|
|
|
virtual int get_contact_collider_shape(int p_contact_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, 0);
|
|
|
|
return body->contacts[p_contact_idx].collider_shape;
|
|
|
|
}
|
2017-09-10 15:37:49 +02:00
|
|
|
virtual Vector3 get_contact_collider_velocity_at_position(int p_contact_idx) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Vector3());
|
|
|
|
return body->contacts[p_contact_idx].collider_velocity_at_pos;
|
|
|
|
}
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
virtual PhysicsDirectSpaceState3D *get_space_state();
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
virtual real_t get_step() const { return step; }
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsDirectBodyState3DSW() {
|
2017-03-05 16:44:50 +01:00
|
|
|
singleton = this;
|
2020-04-02 01:20:12 +02:00
|
|
|
body = nullptr;
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
2014-05-14 06:22:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BODY__SW_H
|