Skip to content

Commit

Permalink
#81 Add some function convert transform from btBullet to irrlicht
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Dec 10, 2023
1 parent 6c2366e commit cb356eb
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ endif()
# bullet engine
option(BUILD_BULLET_PHYSIC_LIB "Build bullet physic" ON)

if (BUILD_RECAST_NAVIGATION_LIB)
add_definitions(-DUSE_BULLET_PHYSIC_ENGINE)
endif()

# skylicht recast navigation
option(BUILD_RECAST_NAVIGATION_LIB "Build Recast Navigation" ON)

Expand Down
14 changes: 14 additions & 0 deletions Projects/Skylicht/Physics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ include_directories(
${SKYLICHT_ENGINE_SOURCE_DIR}/Projects/Skylicht/System/Source
${SKYLICHT_ENGINE_SOURCE_DIR}/Projects/Skylicht/Audio/Source
${SKYLICHT_ENGINE_SOURCE_DIR}/Projects/Skylicht/Engine/Source
${SKYLICHT_ENGINE_SOURCE_DIR}/Projects/Bullet3/src
)

if (BUILD_BULLET_PHYSIC_LIB)
include_directories(${SKYLICHT_ENGINE_SOURCE_DIR}/Projects/Bullet3/src)
endif()

file(GLOB_RECURSE skylicht_physics
./Source/*.cpp
./Source/*.hpp
Expand All @@ -29,4 +34,13 @@ elseif(MSVC)
target_link_libraries(Physics Engine System)
elseif(CYGWIN OR MINGW)
target_link_libraries(Physics Engine System)
endif()

if (BUILD_BULLET_PHYSIC_LIB)
target_link_libraries(Physics BulletInverseDynamics BulletSoftBody BulletCollision BulletDynamics LinearMath Bullet3Common)

IF(BUILD_BULLET3)
target_link_libraries(Physics Bullet3OpenCL Bullet2FileLoader Bullet3Dynamics Bullet3Collision Bullet3Geometry )
ENDIF(BUILD_BULLET3)

endif()
101 changes: 101 additions & 0 deletions Projects/Skylicht/Physics/Source/Bullet/CBulletUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#pragma once

#include "pch.h"

#ifdef USE_BULLET_PHYSIC_ENGINE

#include <btBulletCollisionCommon.h>
#include <btBulletDynamicsCommon.h>

namespace Bullet
{
inline static btVector3 irrVectorToBulletVector(const core::vector3df& toConvert)
{
return btVector3(toConvert.X, toConvert.Y, toConvert.Z);
}

inline static core::vector3df bulletVectorToIrrVector(const btVector3& toConvert)
{
return core::vector3df(toConvert.x(), toConvert.y(), toConvert.z());
}

inline static void quaternionToEuler(const btQuaternion& TQuat, btVector3& TEuler)
{
btScalar W = TQuat.getW();
btScalar X = TQuat.getX();
btScalar Y = TQuat.getY();
btScalar Z = TQuat.getZ();
float WSquared = W * W;
float XSquared = X * X;
float YSquared = Y * Y;
float ZSquared = Z * Z;
TEuler.setX(atan2f(2.0f * (Y * Z + X * W), -XSquared - YSquared + ZSquared + WSquared));
TEuler.setY(asinf(-2.0f * (X * Z - Y * W)));
TEuler.setZ(atan2f(2.0f * (X * Y + Z * W), XSquared - YSquared - ZSquared + WSquared));
TEuler *= core::RADTODEG;
}

static core::vector3df quaternionToIrrEuler(const btQuaternion& TQuat)
{
btVector3 bulletEuler;
quaternionToEuler(TQuat, bulletEuler);
return Bullet::bulletVectorToIrrVector(bulletEuler);
}

inline static core::vector3df bulletTransformToIrrRotation(const btTransform& tr)
{
core::matrix4 mat;

#ifdef BT_USE_NEON
float ptr[16] __attribute__((aligned(16)));
tr.getOpenGLMatrix(ptr);

float* m = mat.pointer();
for (int i = 0; i < 16; i++)
{
m[i] = (float)ptr[i];
}
#else
f32* ptr;
ptr = mat.pointer();
tr.getOpenGLMatrix(ptr);
#endif
return mat.getRotationDegrees();
}

inline static void bulletTransformToIrrMatrix(const btTransform& tr, core::matrix4& mat)
{
#ifdef BT_USE_NEON
float ptr[16] __attribute__((aligned(16)));
tr.getOpenGLMatrix(ptr);

float* m = mat.pointer();
for (int i = 0; i < 16; i++)
m[i] = ptr[i];
#else
f32* ptr;
ptr = mat.pointer();
tr.getOpenGLMatrix(ptr);
#endif
}

inline static btTransform irrRotationToBulletTransform(const core::vector3df& rotation)
{
core::matrix4 mat;
mat.setRotationDegrees(rotation);
btTransform tr;
tr.setFromOpenGLMatrix(mat.pointer());

return tr;
}

inline static btTransform glMatrixToBulletTransform(float* matrix)
{
btTransform tr;
tr.setFromOpenGLMatrix(matrix);

return tr;
}
}

#endif

0 comments on commit cb356eb

Please sign in to comment.