TDME2  1.9.200
Primitives.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 
6 #include <reactphysics3d/mathematics/Vector3.h>
7 
8 #include <tdme/tdme.h>
12 #include <tdme/math/Vector3.h>
14 
15 using std::string;
16 using std::unordered_map;
17 
27 
28 /**
29  * Helper class to create models from physics primitive bounding volumes
30  * @author Andreas Drewke
31  */
33 {
34 private:
35  static constexpr int32_t SPHERE_SEGMENTS_X { 20 };
36  static constexpr int32_t SPHERE_SEGMENTS_Y { 20 };
37  static constexpr int32_t CAPSULE_SEGMENTS_X { 20 };
38  static constexpr int32_t CAPSULE_SEGMENTS_Y { 20 };
39 
40  /**
41  * Converts a TDME2 vector to ReactPhysics3D vector
42  * @param vector tdme vector
43  * @returns reactphysics3d vector
44  */
45  inline static reactphysics3d::Vector3 toRP3DVector3(const Vector3& vector) {
46  return reactphysics3d::Vector3(vector.getX(), vector.getY(), vector.getZ());
47  }
48 
49  /**
50  * Transforms a given ReactPhysics3D vector with bounding volume transform
51  * @param boundingVolume bounding volume
52  * @param vector vector
53  * @return transformed vector
54  */
55  inline static Vector3 transformVector3(BoundingVolume* boundingVolume, const reactphysics3d::Vector3& vector) {
56  // Note: there is no hurry as LE and ME do not use scale for prototype bounding volumes
57  auto vectorTransformed = boundingVolume->collisionShapeTransform * boundingVolume->collisionShapeLocalTransform * (vector * toRP3DVector3(boundingVolume->scale));
58  return Vector3(vectorTransformed.x, vectorTransformed.y, vectorTransformed.z);
59  }
60 
61  /**
62  * Transforms a given ReactPhysics3D vector with bounding volume transform
63  * @param boundingVolume bounding volume
64  * @param normal vector
65  * @return transformed vector
66  */
67  inline static Vector3 transformVector3Normal(BoundingVolume* boundingVolume, const reactphysics3d::Vector3& normal) {
68  // Note: there is no hurry as LE and ME do not use scale for prototype bounding volumes
69  auto normalTransformed = boundingVolume->collisionShapeTransform.getOrientation() * boundingVolume->collisionShapeLocalTransform.getOrientation() * (normal * toRP3DVector3(boundingVolume->scale));
70  normalTransformed.normalize();
71  return Vector3(normalTransformed.x, normalTransformed.y, normalTransformed.z);
72  }
73 
74  /**
75  * Set up convex mesh material
76  * @param nodes nodes
77  * @param material material
78  */
79  static void setupConvexMeshMaterial(const unordered_map<string, Node*>& nodes, Material* material);
80 
81 public:
82  /**
83  * Creates a model from bounding box
84  * @param boundingBox bounding box
85  * @param id id
86  * @return model
87  */
88  static Model* createBoundingBoxModel(BoundingBox* boundingBox, const string& id);
89 
90  /**
91  * Creates a model from oriented bounding box
92  * @param orientedBoundingBox bounding box
93  * @param id id
94  * @return model
95  */
96  static Model* createOrientedBoundingBoxModel(OrientedBoundingBox* orientedBoundingBox, const string& id);
97 
98  /**
99  * Creates a model from oriented bounding box
100  * @param sphere sphere
101  * @param id id
102  * @param segmentsX number of x segments
103  * @param segmentsY number of y segments
104  * @return model
105  */
106  static Model* createSphereModel(Sphere* sphere, const string& id, int32_t segmentsX, int32_t segmentsY);
107 
108  /**
109  * Creates a model from capsule
110  * @param capsule sphere
111  * @param id id
112  * @param segmentsX number of x segments
113  * @param segmentsY number of y segments
114  * @return model
115  */
116  static Model* createCapsuleModel(Capsule* capsule, const string& id, int32_t segmentsX, int32_t segmentsY);
117 
118  /**
119  * Creates a model from convex mesh
120  * @param mesh convex mesh
121  * @param id id
122  * @return model
123  */
124  static Model* createConvexMeshModel(ConvexMesh* mesh, const string& id);
125 
126  /**
127  * Set up a convex mesh model
128  * @param model model
129  */
130  static void setupConvexMeshModel(Model* model);
131 
132 public:
133  /**
134  * Creates a model from bounding volume
135  * @param boundingVolume bounding volume
136  * @param id id
137  * @return model
138  */
139  static Model* createModel(BoundingBox* boundingVolume, const string& id);
140 
141  /**
142  * Creates a model from bounding volume
143  * @param boundingVolume bounding box
144  * @param id id
145  * @return model
146  */
147  static Model* createModel(BoundingVolume* boundingVolume, const string& id);
148 };
Represents a material.
Definition: Material.h:23
Representation of a 3D model.
Definition: Model.h:35
Model node.
Definition: Node.h:32
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
reactphysics3d::Transform collisionShapeLocalTransform
reactphysics3d::Transform collisionShapeTransform
Capsule physics primitive.
Definition: Capsule.h:19
Convex mesh physics primitive.
Definition: ConvexMesh.h:37
Oriented bounding box physics primitive.
Sphere physics primitive.
Definition: Sphere.h:19
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
float getY() const
Definition: Vector3.h:117
float getX() const
Definition: Vector3.h:100
float getZ() const
Definition: Vector3.h:134
Helper class to create models from physics primitive bounding volumes.
Definition: Primitives.h:33
static void setupConvexMeshMaterial(const unordered_map< string, Node * > &nodes, Material *material)
Set up convex mesh material.
Definition: Primitives.cpp:524
static constexpr int32_t SPHERE_SEGMENTS_X
Definition: Primitives.h:35
static Vector3 transformVector3(BoundingVolume *boundingVolume, const reactphysics3d::Vector3 &vector)
Transforms a given ReactPhysics3D vector with bounding volume transform.
Definition: Primitives.h:55
static void setupConvexMeshModel(Model *model)
Set up a convex mesh model.
Definition: Primitives.cpp:495
static Model * createBoundingBoxModel(BoundingBox *boundingBox, const string &id)
Creates a model from bounding box.
Definition: Primitives.cpp:60
static Model * createSphereModel(Sphere *sphere, const string &id, int32_t segmentsX, int32_t segmentsY)
Creates a model from oriented bounding box.
Definition: Primitives.cpp:234
static Vector3 transformVector3Normal(BoundingVolume *boundingVolume, const reactphysics3d::Vector3 &normal)
Transforms a given ReactPhysics3D vector with bounding volume transform.
Definition: Primitives.h:67
static constexpr int32_t SPHERE_SEGMENTS_Y
Definition: Primitives.h:36
static Model * createModel(BoundingBox *boundingVolume, const string &id)
Creates a model from bounding volume.
Definition: Primitives.cpp:536
static Model * createConvexMeshModel(ConvexMesh *mesh, const string &id)
Creates a model from convex mesh.
Definition: Primitives.cpp:490
static Model * createOrientedBoundingBoxModel(OrientedBoundingBox *orientedBoundingBox, const string &id)
Creates a model from oriented bounding box.
Definition: Primitives.cpp:146
static constexpr int32_t CAPSULE_SEGMENTS_Y
Definition: Primitives.h:38
static constexpr int32_t CAPSULE_SEGMENTS_X
Definition: Primitives.h:37
static Model * createCapsuleModel(Capsule *capsule, const string &id, int32_t segmentsX, int32_t segmentsY)
Creates a model from capsule.
Definition: Primitives.cpp:340
static reactphysics3d::Vector3 toRP3DVector3(const Vector3 &vector)
Converts a TDME2 vector to ReactPhysics3D vector.
Definition: Primitives.h:45