2017-11-04 19:52:59 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* collision_object_bullet.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* 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 COLLISION_OBJECT_BULLET_H
|
|
|
|
#define COLLISION_OBJECT_BULLET_H
|
|
|
|
|
2020-07-10 07:21:12 +00:00
|
|
|
#include "core/local_vector.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/math/transform.h"
|
|
|
|
#include "core/math/vector3.h"
|
|
|
|
#include "core/object.h"
|
2017-11-04 19:52:59 +00:00
|
|
|
#include "core/vset.h"
|
|
|
|
#include "shape_owner_bullet.h"
|
|
|
|
|
2018-01-04 23:50:27 +00:00
|
|
|
#include <LinearMath/btTransform.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
@author AndreaCatania
|
|
|
|
*/
|
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
class AreaBullet;
|
|
|
|
class ShapeBullet;
|
|
|
|
class btCollisionObject;
|
|
|
|
class btCompoundShape;
|
|
|
|
class btCollisionShape;
|
|
|
|
class SpaceBullet;
|
|
|
|
|
|
|
|
class CollisionObjectBullet : public RIDBullet {
|
|
|
|
public:
|
|
|
|
enum GodotObjectFlags {
|
|
|
|
GOF_IS_MONITORING_AREA = 1 << 0
|
|
|
|
// FLAG2 = 1 << 1,
|
|
|
|
// FLAG3 = 1 << 2,
|
|
|
|
// FLAG4 = 1 << 3,
|
|
|
|
// FLAG5 = 1 << 4,
|
|
|
|
// FLAG6 = 1 << 5
|
|
|
|
// etc..
|
|
|
|
};
|
|
|
|
enum Type {
|
|
|
|
TYPE_AREA = 0,
|
|
|
|
TYPE_RIGID_BODY,
|
|
|
|
TYPE_SOFT_BODY,
|
|
|
|
TYPE_KINEMATIC_GHOST_BODY
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ShapeWrapper {
|
2020-05-12 15:01:17 +00:00
|
|
|
ShapeBullet *shape = nullptr;
|
2017-11-04 19:52:59 +00:00
|
|
|
btTransform transform;
|
2018-01-08 00:22:54 +00:00
|
|
|
btVector3 scale;
|
2020-05-12 15:01:17 +00:00
|
|
|
bool active = true;
|
2020-06-21 10:48:40 +00:00
|
|
|
btCollisionShape *bt_shape = nullptr;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-06-21 10:48:40 +00:00
|
|
|
public:
|
2020-05-12 15:01:17 +00:00
|
|
|
ShapeWrapper() {}
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
ShapeWrapper(ShapeBullet *p_shape, const btTransform &p_transform, bool p_active) :
|
|
|
|
shape(p_shape),
|
|
|
|
active(p_active) {
|
2017-11-04 19:52:59 +00:00
|
|
|
set_transform(p_transform);
|
|
|
|
}
|
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
ShapeWrapper(ShapeBullet *p_shape, const Transform &p_transform, bool p_active) :
|
|
|
|
shape(p_shape),
|
|
|
|
active(p_active) {
|
2017-11-04 19:52:59 +00:00
|
|
|
set_transform(p_transform);
|
|
|
|
}
|
|
|
|
~ShapeWrapper();
|
|
|
|
|
|
|
|
ShapeWrapper(const ShapeWrapper &otherShape) {
|
|
|
|
operator=(otherShape);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator=(const ShapeWrapper &otherShape) {
|
|
|
|
shape = otherShape.shape;
|
|
|
|
bt_shape = otherShape.bt_shape;
|
|
|
|
transform = otherShape.transform;
|
2018-01-08 00:22:54 +00:00
|
|
|
scale = otherShape.scale;
|
2017-11-04 19:52:59 +00:00
|
|
|
active = otherShape.active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_transform(const Transform &p_transform);
|
|
|
|
void set_transform(const btTransform &p_transform);
|
2019-04-25 03:03:48 +00:00
|
|
|
btTransform get_adjusted_transform() const;
|
2018-09-06 16:19:05 +00:00
|
|
|
|
|
|
|
void claim_bt_shape(const btVector3 &body_scale);
|
2020-06-21 10:48:40 +00:00
|
|
|
void release_bt_shape();
|
2017-11-04 19:52:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Type type;
|
|
|
|
ObjectID instance_id;
|
2020-05-12 15:01:17 +00:00
|
|
|
uint32_t collisionLayer = 0;
|
|
|
|
uint32_t collisionMask = 0;
|
|
|
|
bool collisionsEnabled = true;
|
|
|
|
bool m_isStatic = false;
|
|
|
|
bool ray_pickable = false;
|
|
|
|
btCollisionObject *bt_collision_object = nullptr;
|
|
|
|
Vector3 body_scale = Vector3(1, 1, 1);
|
|
|
|
bool force_shape_reset = false;
|
|
|
|
SpaceBullet *space = nullptr;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
VSet<RID> exceptions;
|
|
|
|
|
2020-07-10 07:21:12 +00:00
|
|
|
bool needs_body_reload = true;
|
|
|
|
bool needs_collision_filters_reload = true;
|
2020-06-21 10:48:40 +00:00
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
/// This array is used to know all areas where this Object is overlapped in
|
|
|
|
/// New area is added when overlap with new area (AreaBullet::addOverlap), then is removed when it exit (CollisionObjectBullet::onExitArea)
|
|
|
|
/// This array is used mainly to know which area hold the pointer of this object
|
2020-07-10 07:21:12 +00:00
|
|
|
LocalVector<AreaBullet *> areasOverlapped;
|
2020-05-12 15:01:17 +00:00
|
|
|
bool isTransformChanged = false;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-06-21 10:48:40 +00:00
|
|
|
public:
|
|
|
|
bool is_in_world = false;
|
2020-07-10 07:21:12 +00:00
|
|
|
bool is_in_flush_queue = false;
|
2020-06-21 10:48:40 +00:00
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
public:
|
|
|
|
CollisionObjectBullet(Type p_type);
|
|
|
|
virtual ~CollisionObjectBullet();
|
|
|
|
|
|
|
|
Type getType() { return type; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void destroyBulletCollisionObject();
|
|
|
|
void setupBulletCollisionObject(btCollisionObject *p_collisionObject);
|
|
|
|
|
|
|
|
public:
|
|
|
|
_FORCE_INLINE_ btCollisionObject *get_bt_collision_object() { return bt_collision_object; }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }
|
|
|
|
_FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool is_static() const { return m_isStatic; }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void set_ray_pickable(bool p_enable) { ray_pickable = p_enable; }
|
|
|
|
_FORCE_INLINE_ bool is_ray_pickable() const { return ray_pickable; }
|
|
|
|
|
|
|
|
void set_body_scale(const Vector3 &p_new_scale);
|
2017-12-02 17:10:36 +00:00
|
|
|
const Vector3 &get_body_scale() const { return body_scale; }
|
|
|
|
btVector3 get_bt_body_scale() const;
|
2018-10-06 14:50:10 +00:00
|
|
|
virtual void body_scale_changed();
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
void add_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject);
|
|
|
|
void remove_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject);
|
|
|
|
bool has_collision_exception(const CollisionObjectBullet *p_otherCollisionObject) const;
|
|
|
|
_FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void set_collision_layer(uint32_t p_layer) {
|
2019-10-08 09:35:35 +00:00
|
|
|
if (collisionLayer != p_layer) {
|
|
|
|
collisionLayer = p_layer;
|
2020-07-10 07:21:12 +00:00
|
|
|
needs_collision_filters_reload = true;
|
2019-10-08 09:35:35 +00:00
|
|
|
}
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
_FORCE_INLINE_ uint32_t get_collision_layer() const { return collisionLayer; }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void set_collision_mask(uint32_t p_mask) {
|
2019-10-08 09:35:35 +00:00
|
|
|
if (collisionMask != p_mask) {
|
|
|
|
collisionMask = p_mask;
|
2020-07-10 07:21:12 +00:00
|
|
|
needs_collision_filters_reload = true;
|
2019-10-08 09:35:35 +00:00
|
|
|
}
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
_FORCE_INLINE_ uint32_t get_collision_mask() const { return collisionMask; }
|
|
|
|
|
2020-07-10 07:21:12 +00:00
|
|
|
virtual void do_reload_collision_filters() = 0;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ bool test_collision_mask(CollisionObjectBullet *p_other) const {
|
|
|
|
return collisionLayer & p_other->collisionMask || p_other->collisionLayer & collisionMask;
|
|
|
|
}
|
|
|
|
|
2020-06-21 10:48:40 +00:00
|
|
|
bool need_reload_body() const {
|
2020-07-10 07:21:12 +00:00
|
|
|
return needs_body_reload;
|
2020-06-21 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 07:21:12 +00:00
|
|
|
void reload_body();
|
|
|
|
|
2020-06-21 10:48:40 +00:00
|
|
|
virtual void do_reload_body() = 0;
|
2017-11-04 19:52:59 +00:00
|
|
|
virtual void set_space(SpaceBullet *p_space) = 0;
|
|
|
|
_FORCE_INLINE_ SpaceBullet *get_space() const { return space; }
|
2018-10-06 14:50:10 +00:00
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
virtual void on_collision_checker_start() = 0;
|
2018-10-07 05:14:38 +00:00
|
|
|
virtual void on_collision_checker_end() = 0;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2020-07-10 07:21:12 +00:00
|
|
|
virtual void dispatch_callbacks();
|
|
|
|
virtual void pre_process();
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
void set_collision_enabled(bool p_enabled);
|
|
|
|
bool is_collisions_response_enabled();
|
|
|
|
|
|
|
|
void notify_new_overlap(AreaBullet *p_area);
|
|
|
|
virtual void on_enter_area(AreaBullet *p_area) = 0;
|
|
|
|
virtual void on_exit_area(AreaBullet *p_area);
|
|
|
|
|
|
|
|
void set_godot_object_flags(int flags);
|
|
|
|
int get_godot_object_flags() const;
|
|
|
|
|
|
|
|
void set_transform(const Transform &p_global_transform);
|
|
|
|
Transform get_transform() const;
|
|
|
|
virtual void set_transform__bullet(const btTransform &p_global_transform);
|
|
|
|
virtual const btTransform &get_transform__bullet() const;
|
2018-10-07 05:14:38 +00:00
|
|
|
|
|
|
|
bool is_transform_changed() const { return isTransformChanged; }
|
|
|
|
virtual void notify_transform_changed();
|
2017-11-04 19:52:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class RigidCollisionObjectBullet : public CollisionObjectBullet, public ShapeOwnerBullet {
|
|
|
|
protected:
|
2020-05-12 15:01:17 +00:00
|
|
|
btCollisionShape *mainShape = nullptr;
|
2020-07-10 07:21:12 +00:00
|
|
|
LocalVector<ShapeWrapper> shapes;
|
2020-06-21 10:48:40 +00:00
|
|
|
bool need_shape_reload = true;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
public:
|
2020-05-12 15:01:17 +00:00
|
|
|
RigidCollisionObjectBullet(Type p_type) :
|
|
|
|
CollisionObjectBullet(p_type) {}
|
2017-11-04 19:52:59 +00:00
|
|
|
~RigidCollisionObjectBullet();
|
|
|
|
|
2020-07-10 07:21:12 +00:00
|
|
|
_FORCE_INLINE_ const LocalVector<ShapeWrapper> &get_shapes_wrappers() const { return shapes; }
|
2017-11-04 19:52:59 +00:00
|
|
|
|
2018-10-06 14:50:10 +00:00
|
|
|
_FORCE_INLINE_ btCollisionShape *get_main_shape() const { return mainShape; }
|
|
|
|
|
2019-03-24 09:38:31 +00:00
|
|
|
void add_shape(ShapeBullet *p_shape, const Transform &p_transform = Transform(), bool p_disabled = false);
|
2017-11-04 19:52:59 +00:00
|
|
|
void set_shape(int p_index, ShapeBullet *p_shape);
|
2018-09-06 16:19:05 +00:00
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
int get_shape_count() const;
|
|
|
|
ShapeBullet *get_shape(int p_index) const;
|
|
|
|
btCollisionShape *get_bt_shape(int p_index) const;
|
2018-10-06 14:50:10 +00:00
|
|
|
|
2020-07-10 07:21:12 +00:00
|
|
|
virtual int find_shape(ShapeBullet *p_shape) const override;
|
2018-10-06 14:50:10 +00:00
|
|
|
|
2020-07-10 07:21:12 +00:00
|
|
|
virtual void remove_shape_full(ShapeBullet *p_shape) override;
|
2018-10-06 14:50:10 +00:00
|
|
|
void remove_shape_full(int p_index);
|
2018-10-07 05:14:38 +00:00
|
|
|
void remove_all_shapes(bool p_permanentlyFromThisBody = false, bool p_force_not_reload = false);
|
2018-10-06 14:50:10 +00:00
|
|
|
|
|
|
|
void set_shape_transform(int p_index, const Transform &p_transform);
|
|
|
|
|
2018-09-06 16:19:05 +00:00
|
|
|
const btTransform &get_bt_shape_transform(int p_index) const;
|
2017-11-04 19:52:59 +00:00
|
|
|
Transform get_shape_transform(int p_index) const;
|
|
|
|
|
|
|
|
void set_shape_disabled(int p_index, bool p_disabled);
|
|
|
|
bool is_shape_disabled(int p_index);
|
|
|
|
|
2020-07-10 07:21:12 +00:00
|
|
|
virtual void pre_process() override;
|
2020-06-21 10:48:40 +00:00
|
|
|
|
2020-07-10 07:21:12 +00:00
|
|
|
virtual void shape_changed(int p_shape_index) override;
|
|
|
|
virtual void reload_shapes() override;
|
2020-06-21 10:48:40 +00:00
|
|
|
bool need_reload_shapes() const { return need_shape_reload; }
|
|
|
|
virtual void do_reload_shapes();
|
2018-10-06 14:50:10 +00:00
|
|
|
|
|
|
|
virtual void main_shape_changed() = 0;
|
2020-07-10 07:21:12 +00:00
|
|
|
virtual void body_scale_changed() override;
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void internal_shape_destroy(int p_index, bool p_permanentlyFromThisBody = false);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|