TDME2  1.9.200
OrientedBoundingBox.cpp
Go to the documentation of this file.
2 
3 #include <array>
4 #include <vector>
5 
6 #include <reactphysics3d/collision/shapes/BoxShape.h>
7 
8 #include <tdme/tdme.h>
11 #include <tdme/math/Math.h>
12 #include <tdme/math/Vector3.h>
13 #include <tdme/utilities/Console.h>
14 
15 using std::array;
16 using std::vector;
17 
21 using tdme::math::Math;
24 
25 const array<int32_t, 3> OrientedBoundingBox::FACE0_INDICES = {{ 0, 4, 7 }};
26 const array<int32_t, 3> OrientedBoundingBox::FACE1_INDICES = {{ 7, 3, 0 }};
27 const array<int32_t, 3> OrientedBoundingBox::FACE2_INDICES = {{ 6, 5, 1 }};
28 const array<int32_t, 3> OrientedBoundingBox::FACE3_INDICES = {{ 1, 2, 6 }};
29 const array<int32_t, 3> OrientedBoundingBox::FACE4_INDICES = {{ 5, 4, 0 }};
30 const array<int32_t, 3> OrientedBoundingBox::FACE5_INDICES = {{ 0, 1, 5 }};
31 const array<int32_t, 3> OrientedBoundingBox::FACE6_INDICES = {{ 3, 7, 6 }};
32 const array<int32_t, 3> OrientedBoundingBox::FACE7_INDICES = {{ 6, 2, 3 }};
33 const array<int32_t, 3> OrientedBoundingBox::FACE8_INDICES = {{ 2, 1, 0 }};
34 const array<int32_t, 3> OrientedBoundingBox::FACE9_INDICES = {{ 0, 3, 2 }};
35 const array<int32_t, 3> OrientedBoundingBox::FACE10_INDICES = {{ 4, 5, 6 }};
36 const array<int32_t, 3> OrientedBoundingBox::FACE11_INDICES = {{ 6, 7, 4 }};
37 const array<array<int32_t,3>,12> OrientedBoundingBox::facesVerticesIndexes =
38 {{
39  FACE0_INDICES, FACE1_INDICES, FACE2_INDICES, FACE3_INDICES,
40  FACE4_INDICES, FACE5_INDICES, FACE6_INDICES, FACE7_INDICES,
41  FACE8_INDICES, FACE9_INDICES, FACE10_INDICES, FACE11_INDICES
42 }};
43 
44 const Vector3 OrientedBoundingBox::AABB_AXIS_X(1.0f, 0.0f, 0.0f);
45 const Vector3 OrientedBoundingBox::AABB_AXIS_Y(0.0f, 1.0f, 0.0f);
46 const Vector3 OrientedBoundingBox::AABB_AXIS_Z(0.0f, 0.0f, 1.0f);
47 
48 OrientedBoundingBox::OrientedBoundingBox(const Vector3& center, const Vector3& axis0, const Vector3& axis1, const Vector3& axis2, const Vector3& halfExtension, const Vector3& scale)
49 {
50  this->center.set(center);
51  this->axes[0].set(axis0);
52  this->axes[1].set(axis1);
53  this->axes[2].set(axis2);
54  this->halfExtension.set(halfExtension);
55  setScale(scale);
56 }
57 
59 {
60  this->halfExtension.set(bb->getMax()).sub(bb->getMin()).scale(0.5f);
61  this->center.set(bb->getMin()).add(halfExtension);
62  this->axes[0].set(AABB_AXIS_X);
63  this->axes[1].set(AABB_AXIS_Y);
64  this->axes[2].set(AABB_AXIS_Z);
65  setScale(scale);
66 }
67 
69 {
70  this->center.set(0.0f, 0.0f, 0.0f);
71  this->axes[0].set(AABB_AXIS_X);
72  this->axes[1].set(AABB_AXIS_Y);
73  this->axes[2].set(AABB_AXIS_Z);
74  this->halfExtension.set(0.0f, 0.0f, 0.0f);
75  setScale(Vector3(1.0f, 1.0f, 1.0f));
76 }
77 
80 }
81 
82 const array<Vector3, 8> OrientedBoundingBox::getVertices() const {
83  array<Vector3, 8> vertices;
84  array<Vector3, 3> xyzAxes;
85  xyzAxes[0] = AABB_AXIS_X;
86  xyzAxes[1] = AABB_AXIS_Y;
87  xyzAxes[2] = AABB_AXIS_Z;
88  Vector3 axis;
89  // just for my imagination
90  // near left top
91  vertices[0].set(0.0f, 0.0f, 0.0f);
92  vertices[0].add(axis.set(xyzAxes[0]).scale(-halfExtension[0]).scale(scale[0]));
93  vertices[0].add(axis.set(xyzAxes[1]).scale(-halfExtension[1]).scale(scale[1]));
94  vertices[0].add(axis.set(xyzAxes[2]).scale(-halfExtension[2]).scale(scale[2]));
95  // just for my imagination
96  // near right top
97  vertices[1].set(0.0f, 0.0f, 0.0f);
98  vertices[1].add(axis.set(xyzAxes[0]).scale(+halfExtension[0]).scale(scale[0]));
99  vertices[1].add(axis.set(xyzAxes[1]).scale(-halfExtension[1]).scale(scale[1]));
100  vertices[1].add(axis.set(xyzAxes[2]).scale(-halfExtension[2]).scale(scale[2]));
101  // just for my imagination
102  // near right bottom
103  vertices[2].set(0.0f, 0.0f, 0.0f);
104  vertices[2].add(axis.set(xyzAxes[0]).scale(+halfExtension[0]).scale(scale[0]));
105  vertices[2].add(axis.set(xyzAxes[1]).scale(+halfExtension[1]).scale(scale[1]));
106  vertices[2].add(axis.set(xyzAxes[2]).scale(-halfExtension[2]).scale(scale[2]));
107  // just for my imagination
108  // near left bottom
109  vertices[3].set(0.0f, 0.0f, 0.0f);
110  vertices[3].add(axis.set(xyzAxes[0]).scale(-halfExtension[0]).scale(scale[0]));
111  vertices[3].add(axis.set(xyzAxes[1]).scale(+halfExtension[1]).scale(scale[1]));
112  vertices[3].add(axis.set(xyzAxes[2]).scale(-halfExtension[2]).scale(scale[2]));
113  // just for my imagination
114  // far left top
115  vertices[4].set(0.0f, 0.0f, 0.0f);
116  vertices[4].add(axis.set(xyzAxes[0]).scale(-halfExtension[0]).scale(scale[0]));
117  vertices[4].add(axis.set(xyzAxes[1]).scale(-halfExtension[1]).scale(scale[1]));
118  vertices[4].add(axis.set(xyzAxes[2]).scale(+halfExtension[2]).scale(scale[2]));
119  // just for my imagination
120  // far right top
121  vertices[5].set(0.0f, 0.0f, 0.0f);
122  vertices[5].add(axis.set(xyzAxes[0]).scale(+halfExtension[0]).scale(scale[0]));
123  vertices[5].add(axis.set(xyzAxes[1]).scale(-halfExtension[1]).scale(scale[1]));
124  vertices[5].add(axis.set(xyzAxes[2]).scale(+halfExtension[2]).scale(scale[2]));
125  // just for my imagination
126  // far right bottom
127  vertices[6].set(0.0f, 0.0f, 0.0f);
128  vertices[6].add(axis.set(xyzAxes[0]).scale(+halfExtension[0]).scale(scale[0]));
129  vertices[6].add(axis.set(xyzAxes[1]).scale(+halfExtension[1]).scale(scale[1]));
130  vertices[6].add(axis.set(xyzAxes[2]).scale(+halfExtension[2]).scale(scale[2]));
131  // just for my imagination
132  // far left bottom
133  vertices[7].set(0.0f, 0.0f, 0.0f);
134  vertices[7].add(axis.set(xyzAxes[0]).scale(-halfExtension[0]).scale(scale[0]));
135  vertices[7].add(axis.set(xyzAxes[1]).scale(+halfExtension[1]).scale(scale[1]));
136  vertices[7].add(axis.set(xyzAxes[2]).scale(+halfExtension[2]).scale(scale[2]));
137  //
138  return vertices;
139 }
140 
141 /**
142  * @return faces vertices indexes
143  */
144 const array<array<int32_t,3>,12>& OrientedBoundingBox::getFacesVerticesIndexes() {
145  return facesVerticesIndexes;
146 }
147 
148 const array<Vector3, 3>& OrientedBoundingBox::getAxes() const
149 {
150  return axes;
151 }
152 
154 {
155  return halfExtension;
156 }
157 
159  // store new scale
160  this->scale.set(scale);
161 
162  //
165  collisionShapeLocalTransform.setOrientation(
166  reactphysics3d::Quaternion(
167  reactphysics3d::Matrix3x3(
168  this->axes[0].getX(),
169  this->axes[0].getY(),
170  this->axes[0].getZ(),
171  this->axes[1].getX(),
172  this->axes[1].getY(),
173  this->axes[1].getZ(),
174  this->axes[2].getX(),
175  this->axes[2].getY(),
176  this->axes[2].getZ()
177  )
178  )
179  );
180 }
181 
183  if (collisionShape == nullptr) return;
184  this->world->physicsCommon.destroyBoxShape(static_cast<reactphysics3d::BoxShape*>(collisionShape));
185  collisionShape = nullptr;
186  world = nullptr;
187 }
188 
190  if (this->world != nullptr && this->world != world) {
191  Console::println("OrientedBoundingBox::createCollisionShape(): already attached to a world.");
192  }
193  this->world = world;
194 
195  //
196  collisionShape = world->physicsCommon.createBoxShape(
197  reactphysics3d::Vector3(
198  Math::max(Math::EPSILON, Math::abs(halfExtension.getX() * scale.getX())),
199  Math::max(Math::EPSILON, Math::abs(halfExtension.getY() * scale.getY())),
200  Math::max(Math::EPSILON, Math::abs(halfExtension.getZ() * scale.getZ()))
201  )
202  );
203 }
204 
206 {
207  return new OrientedBoundingBox(
208  center,
209  axes[0],
210  axes[1],
211  axes[2],
213  scale
214  );
215 }
Dynamic physics world class.
Definition: World.h:38
reactphysics3d::PhysicsCommon physicsCommon
Definition: World.h:53
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
reactphysics3d::Transform collisionShapeLocalTransform
reactphysics3d::CollisionShape * collisionShape
Oriented bounding box physics primitive.
static STATIC_DLL_IMPEXT const array< array< int32_t, 3 >, 12 > facesVerticesIndexes
const array< Vector3, 8 > getVertices() const
void destroyCollisionShape() override
Destroy collision shape.
void setScale(const Vector3 &scale) override
Set up oriented bounding box from oriented bounding box.
static STATIC_DLL_IMPEXT const Vector3 AABB_AXIS_Y
static const array< array< int32_t, 3 >, 12 > & getFacesVerticesIndexes()
const array< Vector3, 3 > & getAxes() const
void createCollisionShape(World *world) override
Create collision shap.
BoundingVolume * clone() const override
Clones this bounding volume.
static STATIC_DLL_IMPEXT const Vector3 AABB_AXIS_Z
static STATIC_DLL_IMPEXT const Vector3 AABB_AXIS_X
Standard math functions.
Definition: Math.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
Vector3 & add(float scalar)
Adds a scalar.
Definition: Vector3.h:153
Vector3 & sub(float scalar)
Subtracts a scalar.
Definition: Vector3.h:177
Vector3 & scale(float scalar)
Scales by scalar.
Definition: Vector3.h:201
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
Console class.
Definition: Console.h:29