Skylicht Engine
Loading...
Searching...
No Matches
CBulletUtils.h
Go to the documentation of this file.
1#pragma once
2
3#include "pch.h"
4
9
10#ifdef USE_BULLET_PHYSIC_ENGINE
11
12#include <btBulletCollisionCommon.h>
13#include <btBulletDynamicsCommon.h>
14
19namespace Bullet
20{
26 inline static btVector3 irrVectorToBulletVector(const core::vector3df& toConvert)
27 {
28 return btVector3(toConvert.X, toConvert.Y, toConvert.Z);
29 }
30
36 inline static core::vector3df bulletVectorToIrrVector(const btVector3& toConvert)
37 {
38 return core::vector3df(toConvert.x(), toConvert.y(), toConvert.z());
39 }
40
46 inline static void quaternionToEuler(const btQuaternion& TQuat, btVector3& TEuler)
47 {
48 btScalar W = TQuat.getW();
49 btScalar X = TQuat.getX();
50 btScalar Y = TQuat.getY();
51 btScalar Z = TQuat.getZ();
52 float WSquared = W * W;
53 float XSquared = X * X;
54 float YSquared = Y * Y;
55 float ZSquared = Z * Z;
56 TEuler.setX(atan2f(2.0f * (Y * Z + X * W), -XSquared - YSquared + ZSquared + WSquared));
57 TEuler.setY(asinf(-2.0f * (X * Z - Y * W)));
58 TEuler.setZ(atan2f(2.0f * (X * Y + Z * W), XSquared - YSquared - ZSquared + WSquared));
59 TEuler *= core::RADTODEG;
60 }
61
67 static core::vector3df quaternionToIrrEuler(const btQuaternion& TQuat)
68 {
69 btVector3 bulletEuler;
70 quaternionToEuler(TQuat, bulletEuler);
71 return Bullet::bulletVectorToIrrVector(bulletEuler);
72 }
73
79 inline static core::vector3df bulletTransformToIrrRotation(const btTransform& tr)
80 {
81 core::matrix4 mat;
82
83#ifdef BT_USE_NEON
84 float ptr[16] __attribute__((aligned(16)));
85 tr.getOpenGLMatrix(ptr);
86
87 float* m = mat.pointer();
88 for (int i = 0; i < 16; i++)
89 {
90 m[i] = (float)ptr[i];
91 }
92#else
93 f32* ptr;
94 ptr = mat.pointer();
95 tr.getOpenGLMatrix(ptr);
96#endif
97 return mat.getRotationDegrees();
98 }
99
105 inline static void bulletTransformToIrrMatrix(const btTransform& tr, core::matrix4& mat)
106 {
107#ifdef BT_USE_NEON
108 float ptr[16] __attribute__((aligned(16)));
109 tr.getOpenGLMatrix(ptr);
110
111 float* m = mat.pointer();
112 for (int i = 0; i < 16; i++)
113 m[i] = ptr[i];
114#else
115 f32* ptr;
116 ptr = mat.pointer();
117 tr.getOpenGLMatrix(ptr);
118#endif
119 }
120
126 inline static btTransform irrRotationToBulletTransform(const core::vector3df& rotation)
127 {
128 core::matrix4 mat;
129 mat.setRotationDegrees(rotation);
130 btTransform tr;
131 tr.setFromOpenGLMatrix(mat.pointer());
132
133 return tr;
134 }
135
141 inline static btTransform glMatrixToBulletTransform(float* matrix)
142 {
143 btTransform tr;
144 tr.setFromOpenGLMatrix(matrix);
145
146 return tr;
147 }
148}
149
150#endif
const T * pointer() const
Returns pointer to internal array.
Definition matrix4.h:99
CMatrix4< T > & setRotationDegrees(const vector3d< T > &rotation)
Make a rotation matrix from Euler angles. The 4th row and column are unmodified.
Definition matrix4.h:816
core::vector3d< T > getRotationDegrees() const
Returns the rotation, as set by setRotation().
Definition matrix4.h:863
T X
X coordinate of the vector.
Definition vector3d.h:408
T Z
Z coordinate of the vector.
Definition vector3d.h:414
T Y
Y coordinate of the vector.
Definition vector3d.h:411
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition vector3d.h:445
CMatrix4< f32 > matrix4
Typedef for f32 matrix.
Definition matrix4.h:2241
const f32 RADTODEG
32bit constant for converting from radians to degrees (formally known as GRAD_PI)
Definition irrMath.h:77
float f32
32 bit floating point variable.
Definition irrTypes.h:104