2017-11-05 03:52:59 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* space_bullet.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2018-01-05 07:50:27 +08:00
|
|
|
/* https://godotengine.org */
|
2017-11-05 03:52:59 +08:00
|
|
|
/*************************************************************************/
|
2021-01-02 03:13:46 +08:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-11-05 03:52:59 +08: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 SPACE_BULLET_H
|
|
|
|
#define SPACE_BULLET_H
|
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/templates/vector.h"
|
|
|
|
#include "core/variant/variant.h"
|
2017-11-05 03:52:59 +08:00
|
|
|
#include "godot_result_callbacks.h"
|
|
|
|
#include "rid_bullet.h"
|
2020-03-28 02:21:27 +08:00
|
|
|
#include "servers/physics_server_3d.h"
|
2017-11-05 03:52:59 +08:00
|
|
|
|
2018-01-05 07:50:27 +08:00
|
|
|
#include <BulletCollision/BroadphaseCollision/btBroadphaseProxy.h>
|
|
|
|
#include <BulletCollision/BroadphaseCollision/btOverlappingPairCache.h>
|
|
|
|
#include <LinearMath/btScalar.h>
|
|
|
|
#include <LinearMath/btTransform.h>
|
|
|
|
#include <LinearMath/btVector3.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
@author AndreaCatania
|
|
|
|
*/
|
|
|
|
|
2017-11-05 03:52:59 +08:00
|
|
|
class AreaBullet;
|
|
|
|
class btBroadphaseInterface;
|
|
|
|
class btCollisionDispatcher;
|
|
|
|
class btConstraintSolver;
|
|
|
|
class btDefaultCollisionConfiguration;
|
|
|
|
class btDynamicsWorld;
|
|
|
|
class btDiscreteDynamicsWorld;
|
|
|
|
class btEmptyShape;
|
|
|
|
class btGhostPairCallback;
|
|
|
|
class btSoftRigidDynamicsWorld;
|
2018-10-01 22:46:50 +08:00
|
|
|
struct btSoftBodyWorldInfo;
|
2017-11-05 03:52:59 +08:00
|
|
|
class ConstraintBullet;
|
|
|
|
class CollisionObjectBullet;
|
|
|
|
class RigidBodyBullet;
|
|
|
|
class SpaceBullet;
|
|
|
|
class SoftBodyBullet;
|
2017-11-07 22:22:09 +08:00
|
|
|
class btGjkEpaPenetrationDepthSolver;
|
2017-11-05 03:52:59 +08:00
|
|
|
|
2018-08-31 15:40:50 +08:00
|
|
|
extern ContactAddedCallback gContactAddedCallback;
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
class BulletPhysicsDirectSpaceState : public PhysicsDirectSpaceState3D {
|
|
|
|
GDCLASS(BulletPhysicsDirectSpaceState, PhysicsDirectSpaceState3D);
|
2019-03-20 02:35:57 +08:00
|
|
|
|
2017-11-05 03:52:59 +08:00
|
|
|
private:
|
|
|
|
SpaceBullet *space;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BulletPhysicsDirectSpaceState(SpaceBullet *p_space);
|
|
|
|
|
2020-07-10 18:34:39 +08:00
|
|
|
virtual int intersect_point(const Vector3 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) override;
|
|
|
|
virtual bool intersect_ray(const Vector3 &p_from, const Vector3 &p_to, RayResult &r_result, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, bool p_pick_ray = false) override;
|
2021-01-28 14:34:26 +08:00
|
|
|
virtual int intersect_shape(const RID &p_shape, const Transform &p_xform, real_t p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) override;
|
|
|
|
virtual bool cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, real_t p_margin, real_t &r_closest_safe, real_t &r_closest_unsafe, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, ShapeRestInfo *r_info = nullptr) override;
|
2017-11-05 03:52:59 +08:00
|
|
|
/// Returns the list of contacts pairs in this order: Local contact, other body contact
|
2021-01-28 14:34:26 +08:00
|
|
|
virtual bool collide_shape(RID p_shape, const Transform &p_shape_xform, real_t p_margin, Vector3 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) override;
|
|
|
|
virtual bool rest_info(RID p_shape, const Transform &p_shape_xform, real_t p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) override;
|
2020-07-10 18:34:39 +08:00
|
|
|
virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const override;
|
2017-11-05 03:52:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class SpaceBullet : public RIDBullet {
|
|
|
|
friend class AreaBullet;
|
|
|
|
friend void onBulletTickCallback(btDynamicsWorld *world, btScalar timeStep);
|
|
|
|
friend class BulletPhysicsDirectSpaceState;
|
|
|
|
|
2020-05-12 23:01:17 +08:00
|
|
|
btBroadphaseInterface *broadphase = nullptr;
|
|
|
|
btDefaultCollisionConfiguration *collisionConfiguration = nullptr;
|
|
|
|
btCollisionDispatcher *dispatcher = nullptr;
|
|
|
|
btConstraintSolver *solver = nullptr;
|
|
|
|
btDiscreteDynamicsWorld *dynamicsWorld = nullptr;
|
|
|
|
btSoftBodyWorldInfo *soft_body_world_info = nullptr;
|
|
|
|
btGhostPairCallback *ghostPairCallback = nullptr;
|
|
|
|
GodotFilterCallback *godotFilterCallback = nullptr;
|
2017-11-05 03:52:59 +08:00
|
|
|
|
2021-02-08 17:57:18 +08:00
|
|
|
btGjkEpaPenetrationDepthSolver *gjk_epa_pen_solver = nullptr;
|
|
|
|
btVoronoiSimplexSolver *gjk_simplex_solver = nullptr;
|
2017-11-07 22:22:09 +08:00
|
|
|
|
2017-11-05 03:52:59 +08:00
|
|
|
BulletPhysicsDirectSpaceState *direct_access;
|
2020-05-12 23:01:17 +08:00
|
|
|
Vector3 gravityDirection = Vector3(0, -1, 0);
|
2021-02-08 17:57:18 +08:00
|
|
|
real_t gravityMagnitude = 10.0;
|
2017-11-05 03:52:59 +08:00
|
|
|
|
2020-05-12 23:01:17 +08:00
|
|
|
real_t linear_damp = 0.0;
|
|
|
|
real_t angular_damp = 0.0;
|
2020-03-26 19:32:20 +08:00
|
|
|
|
2020-10-08 18:16:12 +08:00
|
|
|
Vector<AreaBullet *> areas;
|
2017-11-05 03:52:59 +08:00
|
|
|
|
2020-10-08 18:16:12 +08:00
|
|
|
Vector<Vector3> contactDebug;
|
|
|
|
int contactDebugCount = 0;
|
2020-05-12 23:01:17 +08:00
|
|
|
real_t delta_time = 0.;
|
2017-11-05 03:52:59 +08:00
|
|
|
|
|
|
|
public:
|
2017-11-21 08:36:32 +08:00
|
|
|
SpaceBullet();
|
2017-11-05 03:52:59 +08:00
|
|
|
virtual ~SpaceBullet();
|
|
|
|
|
|
|
|
void flush_queries();
|
2017-11-21 08:36:32 +08:00
|
|
|
real_t get_delta_time() { return delta_time; }
|
2017-11-05 03:52:59 +08:00
|
|
|
void step(real_t p_delta_time);
|
|
|
|
|
2020-06-22 00:29:45 +08:00
|
|
|
_FORCE_INLINE_ btBroadphaseInterface *get_broadphase() const { return broadphase; }
|
|
|
|
_FORCE_INLINE_ btDefaultCollisionConfiguration *get_collision_configuration() const { return collisionConfiguration; }
|
|
|
|
_FORCE_INLINE_ btCollisionDispatcher *get_dispatcher() const { return dispatcher; }
|
|
|
|
_FORCE_INLINE_ btConstraintSolver *get_solver() const { return solver; }
|
|
|
|
_FORCE_INLINE_ btDiscreteDynamicsWorld *get_dynamic_world() const { return dynamicsWorld; }
|
|
|
|
_FORCE_INLINE_ btSoftBodyWorldInfo *get_soft_body_world_info() const { return soft_body_world_info; }
|
2017-11-05 03:52:59 +08:00
|
|
|
_FORCE_INLINE_ bool is_using_soft_world() { return soft_body_world_info; }
|
|
|
|
|
|
|
|
/// Used to set some parameters to Bullet world
|
|
|
|
/// @param p_param:
|
|
|
|
/// AREA_PARAM_GRAVITY to set the gravity magnitude of entire world
|
|
|
|
/// AREA_PARAM_GRAVITY_VECTOR to set the gravity direction of entire world
|
2020-03-28 02:21:27 +08:00
|
|
|
void set_param(PhysicsServer3D::AreaParameter p_param, const Variant &p_value);
|
2017-11-05 03:52:59 +08:00
|
|
|
/// Used to get some parameters to Bullet world
|
|
|
|
/// @param p_param:
|
|
|
|
/// AREA_PARAM_GRAVITY to get the gravity magnitude of entire world
|
|
|
|
/// AREA_PARAM_GRAVITY_VECTOR to get the gravity direction of entire world
|
2020-03-28 02:21:27 +08:00
|
|
|
Variant get_param(PhysicsServer3D::AreaParameter p_param);
|
2017-11-05 03:52:59 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void set_param(PhysicsServer3D::SpaceParameter p_param, real_t p_value);
|
|
|
|
real_t get_param(PhysicsServer3D::SpaceParameter p_param);
|
2017-11-05 03:52:59 +08:00
|
|
|
|
|
|
|
void add_area(AreaBullet *p_area);
|
|
|
|
void remove_area(AreaBullet *p_area);
|
|
|
|
void reload_collision_filters(AreaBullet *p_area);
|
|
|
|
|
|
|
|
void add_rigid_body(RigidBodyBullet *p_body);
|
2021-01-13 21:30:42 +08:00
|
|
|
void remove_rigid_body_constraints(RigidBodyBullet *p_body);
|
2017-11-05 03:52:59 +08:00
|
|
|
void remove_rigid_body(RigidBodyBullet *p_body);
|
|
|
|
void reload_collision_filters(RigidBodyBullet *p_body);
|
|
|
|
|
|
|
|
void add_soft_body(SoftBodyBullet *p_body);
|
|
|
|
void remove_soft_body(SoftBodyBullet *p_body);
|
|
|
|
void reload_collision_filters(SoftBodyBullet *p_body);
|
|
|
|
|
|
|
|
void add_constraint(ConstraintBullet *p_constraint, bool disableCollisionsBetweenLinkedBodies = false);
|
|
|
|
void remove_constraint(ConstraintBullet *p_constraint);
|
|
|
|
|
|
|
|
int get_num_collision_objects() const;
|
|
|
|
void remove_all_collision_objects();
|
|
|
|
|
|
|
|
BulletPhysicsDirectSpaceState *get_direct_state();
|
|
|
|
|
|
|
|
void set_debug_contacts(int p_amount) { contactDebug.resize(p_amount); }
|
2020-12-15 20:04:21 +08:00
|
|
|
_FORCE_INLINE_ bool is_debugging_contacts() const { return !contactDebug.is_empty(); }
|
2017-11-05 03:52:59 +08:00
|
|
|
_FORCE_INLINE_ void reset_debug_contact_count() {
|
|
|
|
contactDebugCount = 0;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ void add_debug_contact(const Vector3 &p_contact) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (contactDebugCount < contactDebug.size()) {
|
2020-10-08 18:16:12 +08:00
|
|
|
contactDebug.write[contactDebugCount++] = p_contact;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-11-05 03:52:59 +08:00
|
|
|
}
|
|
|
|
_FORCE_INLINE_ Vector<Vector3> get_debug_contacts() { return contactDebug; }
|
|
|
|
_FORCE_INLINE_ int get_debug_contact_count() { return contactDebugCount; }
|
|
|
|
|
|
|
|
const Vector3 &get_gravity_direction() const { return gravityDirection; }
|
|
|
|
real_t get_gravity_magnitude() const { return gravityMagnitude; }
|
|
|
|
|
|
|
|
void update_gravity();
|
|
|
|
|
2020-03-26 19:32:20 +08:00
|
|
|
real_t get_linear_damp() const { return linear_damp; }
|
|
|
|
real_t get_angular_damp() const { return angular_damp; }
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
bool test_body_motion(RigidBodyBullet *p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, PhysicsServer3D::MotionResult *r_result, bool p_exclude_raycast_shapes);
|
2021-01-28 14:34:26 +08:00
|
|
|
int test_ray_separation(RigidBodyBullet *p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, PhysicsServer3D::SeparationResult *r_results, int p_result_max, real_t p_margin);
|
2017-11-05 03:52:59 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void create_empty_world(bool p_create_soft_world);
|
|
|
|
void destroy_world();
|
|
|
|
void check_ghost_overlaps();
|
|
|
|
void check_body_collision();
|
|
|
|
|
2017-11-07 22:22:09 +08:00
|
|
|
struct RecoverResult {
|
2020-05-12 23:01:17 +08:00
|
|
|
bool hasPenetration = false;
|
|
|
|
btVector3 normal = btVector3(0, 0, 0);
|
|
|
|
btVector3 pointWorld = btVector3(0, 0, 0);
|
|
|
|
btScalar penetration_distance = 1e20; // Negative mean penetration
|
|
|
|
int other_compound_shape_index = 0;
|
|
|
|
const btCollisionObject *other_collision_object = nullptr;
|
|
|
|
int local_shape_most_recovered = 0;
|
|
|
|
|
|
|
|
RecoverResult() {}
|
2017-11-07 22:22:09 +08:00
|
|
|
};
|
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
bool recover_from_penetration(RigidBodyBullet *p_body, const btTransform &p_body_position, btScalar p_recover_movement_scale, bool p_infinite_inertia, btVector3 &r_delta_recover_movement, RecoverResult *r_recover_result = nullptr);
|
2017-11-19 09:04:49 +08:00
|
|
|
/// This is an API that recover a kinematic object from penetration
|
|
|
|
/// This allow only Convex Convex test and it always use GJK algorithm, With this API we don't benefit of Bullet special accelerated functions
|
2020-04-02 07:20:12 +08:00
|
|
|
bool RFP_convex_convex_test(const btConvexShape *p_shapeA, const btConvexShape *p_shapeB, btCollisionObject *p_objectB, int p_shapeId_A, int p_shapeId_B, const btTransform &p_transformA, const btTransform &p_transformB, btScalar p_recover_movement_scale, btVector3 &r_delta_recover_movement, RecoverResult *r_recover_result = nullptr);
|
2017-11-19 09:04:49 +08:00
|
|
|
/// This is an API that recover a kinematic object from penetration
|
|
|
|
/// Using this we leave Bullet to select the best algorithm, For example GJK in case we have Convex Convex, or a Bullet accelerated algorithm
|
2020-04-02 07:20:12 +08:00
|
|
|
bool RFP_convex_world_test(const btConvexShape *p_shapeA, const btCollisionShape *p_shapeB, btCollisionObject *p_objectA, btCollisionObject *p_objectB, int p_shapeId_A, int p_shapeId_B, const btTransform &p_transformA, const btTransform &p_transformB, btScalar p_recover_movement_scale, btVector3 &r_delta_recover_movement, RecoverResult *r_recover_result = nullptr);
|
2018-08-15 01:20:48 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
int add_separation_result(PhysicsServer3D::SeparationResult *r_results, const SpaceBullet::RecoverResult &p_recover_result, int p_shape_id, const btCollisionObject *p_other_object) const;
|
|
|
|
int recover_from_penetration_ray(RigidBodyBullet *p_body, const btTransform &p_body_position, btScalar p_recover_movement_scale, bool p_infinite_inertia, int p_result_max, btVector3 &r_delta_recover_movement, PhysicsServer3D::SeparationResult *r_results);
|
2017-11-05 03:52:59 +08:00
|
|
|
};
|
|
|
|
#endif
|