summaryrefslogtreecommitdiff
path: root/modules/bullet/shape_bullet.h
blob: 5080d13d99a1137e8a687f4625bc85fe9be8808c (plain)
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
/*************************************************************************/
/*  shape_bullet.h                                                       */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* 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 SHAPE_BULLET_H
#define SHAPE_BULLET_H

#include "core/math/geometry_3d.h"
#include "core/variant/variant.h"
#include "rid_bullet.h"
#include "servers/physics_server_3d.h"

#include <LinearMath/btAlignedObjectArray.h>
#include <LinearMath/btScalar.h>
#include <LinearMath/btVector3.h>

/**
	@author AndreaCatania
*/

class ShapeBullet;
class btCollisionShape;
class ShapeOwnerBullet;
class btBvhTriangleMeshShape;

class ShapeBullet : public RIDBullet {
	Map<ShapeOwnerBullet *, int> owners;
	real_t margin = 0.04;

protected:
	/// return self
	btCollisionShape *prepare(btCollisionShape *p_btShape) const;
	void notifyShapeChanged();

public:
	ShapeBullet();
	virtual ~ShapeBullet();

	btCollisionShape *create_bt_shape(const Vector3 &p_implicit_scale, real_t p_extra_edge = 0);
	virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0) = 0;

	void add_owner(ShapeOwnerBullet *p_owner);
	void remove_owner(ShapeOwnerBullet *p_owner, bool p_permanentlyFromThisBody = false);
	bool is_owner(ShapeOwnerBullet *p_owner) const;
	const Map<ShapeOwnerBullet *, int> &get_owners() const;

	void set_margin(real_t p_margin);
	real_t get_margin() const;

	/// Setup the shape
	virtual void set_data(const Variant &p_data) = 0;
	virtual Variant get_data() const = 0;

	virtual PhysicsServer3D::ShapeType get_type() const = 0;

public:
	static class btEmptyShape *create_shape_empty();
	static class btStaticPlaneShape *create_shape_plane(const btVector3 &planeNormal, btScalar planeConstant);
	static class btSphereShape *create_shape_sphere(btScalar radius);
	static class btBoxShape *create_shape_box(const btVector3 &boxHalfExtents);
	static class btCapsuleShape *create_shape_capsule(btScalar radius, btScalar height);
	static class btCylinderShape *create_shape_cylinder(btScalar radius, btScalar height);
	/// IMPORTANT: Remember to delete the shape interface by calling: delete my_shape->getMeshInterface();
	static class btConvexPointCloudShape *create_shape_convex(btAlignedObjectArray<btVector3> &p_vertices, const btVector3 &p_local_scaling = btVector3(1, 1, 1));
	static class btScaledBvhTriangleMeshShape *create_shape_concave(btBvhTriangleMeshShape *p_mesh_shape, const btVector3 &p_local_scaling = btVector3(1, 1, 1));
	static class btHeightfieldTerrainShape *create_shape_height_field(Vector<float> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height);
	static class btRayShape *create_shape_ray(real_t p_length, bool p_slips_on_slope);
};

class PlaneShapeBullet : public ShapeBullet {
	Plane plane;

public:
	PlaneShapeBullet();

	virtual void set_data(const Variant &p_data);
	virtual Variant get_data() const;
	virtual PhysicsServer3D::ShapeType get_type() const;
	virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);

private:
	void setup(const Plane &p_plane);
};

class SphereShapeBullet : public ShapeBullet {
	real_t radius;

public:
	SphereShapeBullet();

	_FORCE_INLINE_ real_t get_radius() { return radius; }
	virtual void set_data(const Variant &p_data);
	virtual Variant get_data() const;
	virtual PhysicsServer3D::ShapeType get_type() const;
	virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);

private:
	void setup(real_t p_radius);
};

class BoxShapeBullet : public ShapeBullet {
	btVector3 half_extents;

public:
	BoxShapeBullet();

	_FORCE_INLINE_ const btVector3 &get_half_extents() { return half_extents; }
	virtual void set_data(const Variant &p_data);
	virtual Variant get_data() const;
	virtual PhysicsServer3D::ShapeType get_type() const;
	virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);

private:
	void setup(const Vector3 &p_half_extents);
};

class CapsuleShapeBullet : public ShapeBullet {
	real_t height;
	real_t radius;

public:
	CapsuleShapeBullet();

	_FORCE_INLINE_ real_t get_height() { return height; }
	_FORCE_INLINE_ real_t get_radius() { return radius; }
	virtual void set_data(const Variant &p_data);
	virtual Variant get_data() const;
	virtual PhysicsServer3D::ShapeType get_type() const;
	virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);

private:
	void setup(real_t p_height, real_t p_radius);
};

class CylinderShapeBullet : public ShapeBullet {
	real_t height;
	real_t radius;

public:
	CylinderShapeBullet();

	_FORCE_INLINE_ real_t get_height() { return height; }
	_FORCE_INLINE_ real_t get_radius() { return radius; }
	virtual void set_data(const Variant &p_data);
	virtual Variant get_data() const;
	virtual PhysicsServer3D::ShapeType get_type() const;
	virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_margin = 0);

private:
	void setup(real_t p_height, real_t p_radius);
};

class ConvexPolygonShapeBullet : public ShapeBullet {
public:
	btAlignedObjectArray<btVector3> vertices;

	ConvexPolygonShapeBullet();

	virtual void set_data(const Variant &p_data);
	void get_vertices(Vector<Vector3> &out_vertices);
	virtual Variant get_data() const;
	virtual PhysicsServer3D::ShapeType get_type() const;
	virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);

private:
	void setup(const Vector<Vector3> &p_vertices);
};

class ConcavePolygonShapeBullet : public ShapeBullet {
	class btBvhTriangleMeshShape *meshShape = nullptr;

public:
	Vector<Vector3> faces;

	ConcavePolygonShapeBullet();
	virtual ~ConcavePolygonShapeBullet();

	virtual void set_data(const Variant &p_data);
	virtual Variant get_data() const;
	virtual PhysicsServer3D::ShapeType get_type() const;
	virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);

private:
	void setup(Vector<Vector3> p_faces);
};

class HeightMapShapeBullet : public ShapeBullet {
public:
	Vector<float> heights;
	int width = 0;
	int depth = 0;
	real_t min_height = 0.0;
	real_t max_height = 0.0;

	HeightMapShapeBullet();

	virtual void set_data(const Variant &p_data);
	virtual Variant get_data() const;
	virtual PhysicsServer3D::ShapeType get_type() const;
	virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);

private:
	void setup(Vector<float> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height);
};

class RayShapeBullet : public ShapeBullet {
public:
	real_t length = 1.0;
	bool slips_on_slope = false;

	RayShapeBullet();

	virtual void set_data(const Variant &p_data);
	virtual Variant get_data() const;
	virtual PhysicsServer3D::ShapeType get_type() const;
	virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0);

private:
	void setup(real_t p_length, bool p_slips_on_slope);
};
#endif