1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306 | /**************************************************************************/
/* rigid_body_bullet.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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 RIGID_BODY_BULLET_H
#define RIGID_BODY_BULLET_H
#include "collision_object_bullet.h"
#include "space_bullet.h"
#include <BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h>
#include <LinearMath/btTransform.h>
/**
@author AndreaCatania
*/
class AreaBullet;
class SpaceBullet;
class btRigidBody;
class GodotMotionState;
class BulletPhysicsDirectBodyState : public PhysicsDirectBodyState {
GDCLASS(BulletPhysicsDirectBodyState, PhysicsDirectBodyState);
public:
RigidBodyBullet *body = nullptr;
BulletPhysicsDirectBodyState() {}
virtual Vector3 get_total_gravity() const;
virtual float get_total_angular_damp() const;
virtual float get_total_linear_damp() const;
virtual Vector3 get_center_of_mass() const;
virtual Basis get_principal_inertia_axes() const;
// get the mass
virtual float get_inverse_mass() const;
// get density of this body space
virtual Vector3 get_inverse_inertia() const;
// get density of this body space
virtual Basis get_inverse_inertia_tensor() const;
virtual void set_linear_velocity(const Vector3 &p_velocity);
virtual Vector3 get_linear_velocity() const;
virtual void set_angular_velocity(const Vector3 &p_velocity);
virtual Vector3 get_angular_velocity() const;
virtual void set_transform(const Transform &p_transform);
virtual Transform get_transform() const;
virtual Vector3 get_velocity_at_local_position(const Vector3 &p_position) const;
virtual void add_central_force(const Vector3 &p_force);
virtual void add_force(const Vector3 &p_force, const Vector3 &p_pos);
virtual void add_torque(const Vector3 &p_torque);
virtual void apply_central_impulse(const Vector3 &p_impulse);
virtual void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse);
virtual void apply_torque_impulse(const Vector3 &p_impulse);
virtual void set_sleep_state(bool p_enable);
virtual bool is_sleeping() const;
virtual int get_contact_count() const;
virtual Vector3 get_contact_local_position(int p_contact_idx) const;
virtual Vector3 get_contact_local_normal(int p_contact_idx) const;
virtual float get_contact_impulse(int p_contact_idx) const;
virtual int get_contact_local_shape(int p_contact_idx) const;
virtual RID get_contact_collider(int p_contact_idx) const;
virtual Vector3 get_contact_collider_position(int p_contact_idx) const;
virtual ObjectID get_contact_collider_id(int p_contact_idx) const;
virtual int get_contact_collider_shape(int p_contact_idx) const;
virtual Vector3 get_contact_collider_velocity_at_position(int p_contact_idx) const;
virtual real_t get_step() const;
virtual void integrate_forces() {
// Skip the execution of this function
}
virtual PhysicsDirectSpaceState *get_space_state();
};
class RigidBodyBullet : public RigidCollisionObjectBullet {
public:
struct CollisionData {
RigidBodyBullet *otherObject;
int other_object_shape;
int local_shape;
Vector3 hitLocalLocation;
Vector3 hitWorldLocation;
Vector3 hitNormal;
float appliedImpulse;
};
struct ForceIntegrationCallback {
ObjectID id;
StringName method;
Variant udata;
};
/// Used to hold shapes
struct KinematicShape {
class btConvexShape *shape;
btTransform transform;
KinematicShape() :
shape(nullptr) {}
bool is_active() const { return shape; }
};
struct KinematicUtilities {
RigidBodyBullet *owner;
btScalar safe_margin;
Vector<KinematicShape> shapes;
KinematicUtilities(RigidBodyBullet *p_owner);<--- Struct 'KinematicUtilities' has a constructor with 1 argument that is not explicit. [+]Struct 'KinematicUtilities' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
~KinematicUtilities();
void setSafeMargin(btScalar p_margin);
/// Used to set the default shape to ghost
void copyAllOwnerShapes();
private:
void just_delete_shapes(int new_size);
};
private:
BulletPhysicsDirectBodyState *direct_access = nullptr;
friend class BulletPhysicsDirectBodyState;
// This is required only for Kinematic movement
KinematicUtilities *kinematic_utilities;
PhysicsServer::BodyMode mode;
GodotMotionState *godotMotionState;
btRigidBody *btBody;
uint16_t locked_axis;
real_t mass;
real_t gravity_scale;
real_t linearDamp;
real_t angularDamp;
Vector3 total_gravity;
real_t total_linear_damp;
real_t total_angular_damp;
bool can_sleep;
bool omit_forces_integration;
bool can_integrate_forces;
Vector<CollisionData> collisions;
Vector<RigidBodyBullet *> collision_traces_1;
Vector<RigidBodyBullet *> collision_traces_2;
Vector<RigidBodyBullet *> *prev_collision_traces;
Vector<RigidBodyBullet *> *curr_collision_traces;
// these parameters are used to avoid vector resize
int maxCollisionsDetection;
int collisionsCount;
int prev_collision_count;
Vector<AreaBullet *> areasWhereIam;
// these parameters are used to avoid vector resize
int maxAreasWhereIam;
int areaWhereIamCount;
// Used to know if the area is used as gravity point
int countGravityPointSpaces;
bool isScratchedSpaceOverrideModificator;
bool previousActiveState; // Last check state
ForceIntegrationCallback *force_integration_callback;
public:
RigidBodyBullet();
~RigidBodyBullet();
BulletPhysicsDirectBodyState *get_direct_state() const { return direct_access; }
void init_kinematic_utilities();
void destroy_kinematic_utilities();
_FORCE_INLINE_ KinematicUtilities *get_kinematic_utilities() const { return kinematic_utilities; }
_FORCE_INLINE_ btRigidBody *get_bt_rigid_body() { return btBody; }
virtual void main_shape_changed();<--- Function in derived class
virtual void reload_body();<--- Function in derived class
virtual void set_space(SpaceBullet *p_space);<--- Function in derived class
virtual void dispatch_callbacks();<--- Function in derived class
void set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata = Variant());
void scratch_space_override_modificator();
virtual void on_collision_filters_change();<--- Function in derived class
virtual void on_collision_checker_start();<--- Function in derived class
virtual void on_collision_checker_end();<--- Function in derived class
void set_max_collisions_detection(int p_maxCollisionsDetection) {
ERR_FAIL_COND(0 > p_maxCollisionsDetection);
maxCollisionsDetection = p_maxCollisionsDetection;
collisions.resize(p_maxCollisionsDetection);
collision_traces_1.resize(p_maxCollisionsDetection);
collision_traces_2.resize(p_maxCollisionsDetection);
collisionsCount = 0;
prev_collision_count = MIN(prev_collision_count, p_maxCollisionsDetection);
}
int get_max_collisions_detection() {
return maxCollisionsDetection;
}
bool can_add_collision() { return collisionsCount < maxCollisionsDetection; }
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);
bool was_colliding(RigidBodyBullet *p_other_object);
void set_activation_state(bool p_active);
bool is_active() const;
void set_omit_forces_integration(bool p_omit);
_FORCE_INLINE_ bool get_omit_forces_integration() const { return omit_forces_integration; }
void set_param(PhysicsServer::BodyParameter p_param, real_t);
real_t get_param(PhysicsServer::BodyParameter p_param) const;
void set_mode(PhysicsServer::BodyMode p_mode);
PhysicsServer::BodyMode get_mode() const;
void set_state(PhysicsServer::BodyState p_state, const Variant &p_variant);
Variant get_state(PhysicsServer::BodyState p_state) const;
void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse);
void apply_central_impulse(const Vector3 &p_impulse);
void apply_torque_impulse(const Vector3 &p_impulse);
void apply_force(const Vector3 &p_force, const Vector3 &p_pos);
void apply_central_force(const Vector3 &p_force);
void apply_torque(const Vector3 &p_torque);
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;
void set_axis_lock(PhysicsServer::BodyAxis p_axis, bool lock);
bool is_axis_locked(PhysicsServer::BodyAxis p_axis) const;
void reload_axis_lock();
/// Doc:
/// https://web.archive.org/web/20180404091446/http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Anti_tunneling_by_Motion_Clamping
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;
virtual void set_transform__bullet(const btTransform &p_global_transform);<--- Function in derived class
virtual const btTransform &get_transform__bullet() const;<--- Function in derived class
virtual void reload_shapes();<--- Function in derived class<--- reload_shapes is a virtual function
virtual void on_enter_area(AreaBullet *p_area);<--- Function in derived class
virtual void on_exit_area(AreaBullet *p_area);<--- Function in derived class
void reload_space_override_modificator();
/// Kinematic
void reload_kinematic_shapes();
virtual void notify_transform_changed();<--- Function in derived class
private:
void _internal_set_mass(real_t p_mass);
};
#endif // RIGID_BODY_BULLET_H
|