2017-11-04 19:52:59 +00:00
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
/* rigid_body_bullet.h */
|
2017-11-04 19:52:59 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2018-01-04 23:50:27 +00:00
|
|
|
/* https://godotengine.org */
|
2017-11-04 19:52:59 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-11-04 19:52:59 +00: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#ifndef BODYBULLET_H
|
|
|
|
#define BODYBULLET_H
|
|
|
|
|
|
|
|
#include "collision_object_bullet.h"
|
|
|
|
#include "space_bullet.h"
|
|
|
|
|
2018-01-04 23:50:27 +00:00
|
|
|
#include <BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h>
|
|
|
|
#include <LinearMath/btTransform.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
@author AndreaCatania
|
|
|
|
*/
|
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
class AreaBullet;
|
|
|
|
class SpaceBullet;
|
|
|
|
class btRigidBody;
|
|
|
|
class GodotMotionState;
|
2020-03-30 16:22:57 +00:00
|
|
|
class BulletPhysicsDirectBodyState3D;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
/// This class could be used in multi thread with few changes but currently
|
2018-01-18 20:37:17 +00:00
|
|
|
/// is set to be only in one single thread.
|
2017-11-04 19:52:59 +00:00
|
|
|
///
|
|
|
|
/// In the system there is only one object at a time that manage all bodies and is
|
2020-03-27 18:21:27 +00:00
|
|
|
/// created by BulletPhysicsServer3D and is held by the "singleton" variable of this class
|
2018-01-18 20:37:17 +00:00
|
|
|
/// Each time something require it, the body must be set again.
|
2020-03-30 16:22:57 +00:00
|
|
|
class BulletPhysicsDirectBodyState3D : public PhysicsDirectBodyState3D {
|
|
|
|
GDCLASS(BulletPhysicsDirectBodyState3D, PhysicsDirectBodyState3D);
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-03-30 16:22:57 +00:00
|
|
|
static BulletPhysicsDirectBodyState3D *singleton;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// This class avoid the creation of more object of this class
|
|
|
|
static void initSingleton() {
|
|
|
|
if (!singleton) {
|
2020-03-30 16:22:57 +00:00
|
|
|
singleton = memnew(BulletPhysicsDirectBodyState3D);
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroySingleton() {
|
|
|
|
memdelete(singleton);
|
2020-04-01 23:20:12 +00:00
|
|
|
singleton = nullptr;
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void singleton_setDeltaTime(real_t p_deltaTime) {
|
|
|
|
singleton->deltaTime = p_deltaTime;
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:22:57 +00:00
|
|
|
static BulletPhysicsDirectBodyState3D *get_singleton(RigidBodyBullet *p_body) {
|
2017-11-04 19:52:59 +00:00
|
|
|
singleton->body = p_body;
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
RigidBodyBullet *body;
|
|
|
|
real_t deltaTime;
|
|
|
|
|
|
|
|
private:
|
2020-03-30 16:22:57 +00:00
|
|
|
BulletPhysicsDirectBodyState3D() {}
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
public:
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual Vector3 get_total_gravity() const override;
|
|
|
|
virtual float get_total_angular_damp() const override;
|
|
|
|
virtual float get_total_linear_damp() const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual Vector3 get_center_of_mass() const override;
|
|
|
|
virtual Basis get_principal_inertia_axes() const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
// get the mass
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual float get_inverse_mass() const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
// get density of this body space
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual Vector3 get_inverse_inertia() const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
// get density of this body space
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual Basis get_inverse_inertia_tensor() const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual void set_linear_velocity(const Vector3 &p_velocity) override;
|
|
|
|
virtual Vector3 get_linear_velocity() const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual void set_angular_velocity(const Vector3 &p_velocity) override;
|
|
|
|
virtual Vector3 get_angular_velocity() const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual void set_transform(const Transform &p_transform) override;
|
|
|
|
virtual Transform get_transform() const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual void add_central_force(const Vector3 &p_force) override;
|
|
|
|
virtual void add_force(const Vector3 &p_force, const Vector3 &p_position = Vector3()) override;
|
|
|
|
virtual void add_torque(const Vector3 &p_torque) override;
|
|
|
|
virtual void apply_central_impulse(const Vector3 &p_impulse) override;
|
|
|
|
virtual void apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position = Vector3()) override;
|
|
|
|
virtual void apply_torque_impulse(const Vector3 &p_impulse) override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual void set_sleep_state(bool p_sleep) override;
|
|
|
|
virtual bool is_sleeping() const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual int get_contact_count() const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual Vector3 get_contact_local_position(int p_contact_idx) const override;
|
|
|
|
virtual Vector3 get_contact_local_normal(int p_contact_idx) const override;
|
|
|
|
virtual float get_contact_impulse(int p_contact_idx) const override;
|
|
|
|
virtual int get_contact_local_shape(int p_contact_idx) const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual RID get_contact_collider(int p_contact_idx) const override;
|
|
|
|
virtual Vector3 get_contact_collider_position(int p_contact_idx) const override;
|
|
|
|
virtual ObjectID get_contact_collider_id(int p_contact_idx) const override;
|
|
|
|
virtual int get_contact_collider_shape(int p_contact_idx) const override;
|
|
|
|
virtual Vector3 get_contact_collider_velocity_at_position(int p_contact_idx) const override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual real_t get_step() const override { return deltaTime; }
|
|
|
|
virtual void integrate_forces() override {
|
2017-11-04 19:52:59 +00:00
|
|
|
// Skip the execution of this function
|
|
|
|
}
|
|
|
|
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual PhysicsDirectSpaceState3D *get_space_state() override;
|
2017-11-04 19:52:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class RigidBodyBullet : public RigidCollisionObjectBullet {
|
|
|
|
public:
|
|
|
|
struct CollisionData {
|
|
|
|
RigidBodyBullet *otherObject;
|
|
|
|
int other_object_shape;
|
|
|
|
int local_shape;
|
|
|
|
Vector3 hitLocalLocation;
|
|
|
|
Vector3 hitWorldLocation;
|
|
|
|
Vector3 hitNormal;
|
2018-01-12 21:27:45 +00:00
|
|
|
float appliedImpulse;
|
2017-11-04 19:52:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ForceIntegrationCallback {
|
|
|
|
ObjectID id;
|
|
|
|
StringName method;
|
|
|
|
Variant udata;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Used to hold shapes
|
|
|
|
struct KinematicShape {
|
2020-05-12 15:01:17 +00:00
|
|
|
class btConvexShape *shape = nullptr;
|
2017-11-04 19:52:59 +00:00
|
|
|
btTransform transform;
|
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
KinematicShape() {}
|
2019-02-21 19:24:29 +00:00
|
|
|
bool is_active() const { return shape; }
|
2017-11-04 19:52:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct KinematicUtilities {
|
2017-11-07 14:22:09 +00:00
|
|
|
RigidBodyBullet *owner;
|
|
|
|
btScalar safe_margin;
|
2020-10-08 10:16:12 +00:00
|
|
|
Vector<KinematicShape> shapes;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
KinematicUtilities(RigidBodyBullet *p_owner);
|
|
|
|
~KinematicUtilities();
|
|
|
|
|
2017-11-07 14:22:09 +00:00
|
|
|
void setSafeMargin(btScalar p_margin);
|
2017-11-04 19:52:59 +00:00
|
|
|
/// Used to set the default shape to ghost
|
|
|
|
void copyAllOwnerShapes();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void just_delete_shapes(int new_size);
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2020-03-30 16:22:57 +00:00
|
|
|
friend class BulletPhysicsDirectBodyState3D;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
// This is required only for Kinematic movement
|
2020-05-12 15:01:17 +00:00
|
|
|
KinematicUtilities *kinematic_utilities = nullptr;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
PhysicsServer3D::BodyMode mode;
|
2017-11-04 19:52:59 +00:00
|
|
|
GodotMotionState *godotMotionState;
|
|
|
|
btRigidBody *btBody;
|
2020-05-12 15:01:17 +00:00
|
|
|
uint16_t locked_axis = 0;
|
|
|
|
real_t mass = 1;
|
|
|
|
real_t gravity_scale = 1;
|
|
|
|
real_t linearDamp = 0;
|
|
|
|
real_t angularDamp = 0;
|
|
|
|
bool can_sleep = true;
|
|
|
|
bool omit_forces_integration = false;
|
|
|
|
bool can_integrate_forces = false;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-10-08 10:16:12 +00:00
|
|
|
Vector<CollisionData> collisions;
|
|
|
|
Vector<RigidBodyBullet *> collision_traces_1;
|
|
|
|
Vector<RigidBodyBullet *> collision_traces_2;
|
|
|
|
Vector<RigidBodyBullet *> *prev_collision_traces;
|
|
|
|
Vector<RigidBodyBullet *> *curr_collision_traces;
|
2018-11-22 08:03:21 +00:00
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
// these parameters are used to avoid vector resize
|
2020-10-08 10:16:12 +00:00
|
|
|
int maxCollisionsDetection = 0;
|
|
|
|
int collisionsCount = 0;
|
|
|
|
int prev_collision_count = 0;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-10-08 10:16:12 +00:00
|
|
|
Vector<AreaBullet *> areasWhereIam;
|
2017-11-04 19:52:59 +00:00
|
|
|
// these parameters are used to avoid vector resize
|
2020-05-12 15:01:17 +00:00
|
|
|
int maxAreasWhereIam = 10;
|
|
|
|
int areaWhereIamCount = 0;
|
2017-11-04 19:52:59 +00:00
|
|
|
// Used to know if the area is used as gravity point
|
2020-05-12 15:01:17 +00:00
|
|
|
int countGravityPointSpaces = 0;
|
|
|
|
bool isScratchedSpaceOverrideModificator = false;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
bool previousActiveState = true; // Last check state
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
ForceIntegrationCallback *force_integration_callback = nullptr;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
RigidBodyBullet();
|
|
|
|
~RigidBodyBullet();
|
|
|
|
|
|
|
|
void init_kinematic_utilities();
|
|
|
|
void destroy_kinematic_utilities();
|
2018-10-01 14:46:50 +00:00
|
|
|
_FORCE_INLINE_ KinematicUtilities *get_kinematic_utilities() const { return kinematic_utilities; }
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ btRigidBody *get_bt_rigid_body() { return btBody; }
|
|
|
|
|
2020-10-08 10:16:12 +00:00
|
|
|
virtual void main_shape_changed();
|
2020-10-08 10:22:59 +00:00
|
|
|
virtual void reload_body();
|
2020-10-08 10:16:12 +00:00
|
|
|
virtual void set_space(SpaceBullet *p_space);
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-10-08 10:16:12 +00:00
|
|
|
virtual void dispatch_callbacks();
|
2017-11-04 19:52:59 +00:00
|
|
|
void set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata = Variant());
|
|
|
|
void scratch_space_override_modificator();
|
|
|
|
|
2020-10-08 10:16:12 +00:00
|
|
|
virtual void on_collision_filters_change();
|
|
|
|
virtual void on_collision_checker_start();
|
|
|
|
virtual void on_collision_checker_end();
|
|
|
|
|
|
|
|
void set_max_collisions_detection(int p_maxCollisionsDetection) {
|
|
|
|
ERR_FAIL_COND(0 > p_maxCollisionsDetection);
|
2018-11-22 08:03:21 +00:00
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
maxCollisionsDetection = p_maxCollisionsDetection;
|
2018-11-22 08:03:21 +00:00
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
collisions.resize(p_maxCollisionsDetection);
|
2018-11-22 08:03:21 +00:00
|
|
|
collision_traces_1.resize(p_maxCollisionsDetection);
|
|
|
|
collision_traces_2.resize(p_maxCollisionsDetection);
|
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
collisionsCount = 0;
|
2018-11-22 08:03:21 +00:00
|
|
|
prev_collision_count = MIN(prev_collision_count, p_maxCollisionsDetection);
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
int get_max_collisions_detection() {
|
|
|
|
return maxCollisionsDetection;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool can_add_collision() { return collisionsCount < maxCollisionsDetection; }
|
2018-01-12 21:27:45 +00:00
|
|
|
bool add_collision_object(RigidBodyBullet *p_otherObject, const Vector3 &p_hitWorldLocation, const Vector3 &p_hitLocalLocation, const Vector3 &p_hitNormal, const float &p_appliedImpulse, int p_other_shape_index, int p_local_shape_index);
|
2018-11-22 08:03:21 +00:00
|
|
|
bool was_colliding(RigidBodyBullet *p_other_object);
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
void assert_no_constraints();
|
|
|
|
|
|
|
|
void set_activation_state(bool p_active);
|
|
|
|
bool is_active() const;
|
|
|
|
|
2018-02-27 07:41:17 +00:00
|
|
|
void set_omit_forces_integration(bool p_omit);
|
|
|
|
_FORCE_INLINE_ bool get_omit_forces_integration() const { return omit_forces_integration; }
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void set_param(PhysicsServer3D::BodyParameter p_param, real_t);
|
|
|
|
real_t get_param(PhysicsServer3D::BodyParameter p_param) const;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void set_mode(PhysicsServer3D::BodyMode p_mode);
|
|
|
|
PhysicsServer3D::BodyMode get_mode() const;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void set_state(PhysicsServer3D::BodyState p_state, const Variant &p_variant);
|
|
|
|
Variant get_state(PhysicsServer3D::BodyState p_state) const;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2018-02-01 08:57:10 +00:00
|
|
|
void apply_central_impulse(const Vector3 &p_impulse);
|
2020-03-26 04:23:34 +00:00
|
|
|
void apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position = Vector3());
|
2017-11-04 19:52:59 +00:00
|
|
|
void apply_torque_impulse(const Vector3 &p_impulse);
|
|
|
|
|
|
|
|
void apply_central_force(const Vector3 &p_force);
|
2020-03-26 04:23:34 +00:00
|
|
|
void apply_force(const Vector3 &p_force, const Vector3 &p_position = Vector3());
|
2018-02-01 08:57:10 +00:00
|
|
|
void apply_torque(const Vector3 &p_torque);
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
void set_applied_force(const Vector3 &p_force);
|
|
|
|
Vector3 get_applied_force() const;
|
|
|
|
void set_applied_torque(const Vector3 &p_torque);
|
|
|
|
Vector3 get_applied_torque() const;
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool lock);
|
|
|
|
bool is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const;
|
2017-12-10 16:21:14 +00:00
|
|
|
void reload_axis_lock();
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
/// Doc:
|
2019-07-24 00:06:12 +00:00
|
|
|
/// https://web.archive.org/web/20180404091446/http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Anti_tunneling_by_Motion_Clamping
|
2017-11-04 19:52:59 +00:00
|
|
|
void set_continuous_collision_detection(bool p_enable);
|
|
|
|
bool is_continuous_collision_detection_enabled() const;
|
|
|
|
|
|
|
|
void set_linear_velocity(const Vector3 &p_velocity);
|
|
|
|
Vector3 get_linear_velocity() const;
|
|
|
|
|
|
|
|
void set_angular_velocity(const Vector3 &p_velocity);
|
|
|
|
Vector3 get_angular_velocity() const;
|
|
|
|
|
2020-10-08 10:16:12 +00:00
|
|
|
virtual void set_transform__bullet(const btTransform &p_global_transform);
|
|
|
|
virtual const btTransform &get_transform__bullet() const;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-10-08 10:22:59 +00:00
|
|
|
virtual void reload_shapes();
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-10-08 10:16:12 +00:00
|
|
|
virtual void on_enter_area(AreaBullet *p_area);
|
|
|
|
virtual void on_exit_area(AreaBullet *p_area);
|
2017-11-04 19:52:59 +00:00
|
|
|
void reload_space_override_modificator();
|
|
|
|
|
|
|
|
/// Kinematic
|
|
|
|
void reload_kinematic_shapes();
|
|
|
|
|
2020-10-08 10:16:12 +00:00
|
|
|
virtual void notify_transform_changed();
|
2018-10-07 05:14:38 +00:00
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
private:
|
|
|
|
void _internal_set_mass(real_t p_mass);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|