diff --git a/CMakeLists.txt b/CMakeLists.txt index d18cf190b..aecff8275 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/Projects/Skylicht/Physics/CMakeLists.txt b/Projects/Skylicht/Physics/CMakeLists.txt index 61ec88755..21c1d9d2e 100644 --- a/Projects/Skylicht/Physics/CMakeLists.txt +++ b/Projects/Skylicht/Physics/CMakeLists.txt @@ -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 @@ -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() \ No newline at end of file diff --git a/Projects/Skylicht/Physics/Source/Bullet/CBulletUtils.h b/Projects/Skylicht/Physics/Source/Bullet/CBulletUtils.h new file mode 100644 index 000000000..099364987 --- /dev/null +++ b/Projects/Skylicht/Physics/Source/Bullet/CBulletUtils.h @@ -0,0 +1,101 @@ +#pragma once + +#include "pch.h" + +#ifdef USE_BULLET_PHYSIC_ENGINE + +#include +#include + +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 \ No newline at end of file