Skip to content

Commit

Permalink
Integrate Jolt Physics Engine (BoomingTech#215)
Browse files Browse the repository at this point in the history
Integrate Jolt Physics Engine,
- RigidBody creation and destroy (only support static object currently)
- Scene Query: raycast, sweep, overlap
- Simulation
- Physics Debug Renderer: (work in progress)

Co-authored-by: Olorin <[email protected]>
  • Loading branch information
2 people authored and KaygNas committed Feb 10, 2023
1 parent 545b910 commit 7577e86
Show file tree
Hide file tree
Showing 28 changed files with 1,047 additions and 27 deletions.
29 changes: 29 additions & 0 deletions engine/3rdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,32 @@ if(NOT TARGET tinyobjloader)
set_target_properties(tinyobjloader PROPERTIES FOLDER ${third_party_folder}/tinyobjloader)
set_target_properties(uninstall PROPERTIES FOLDER ${third_party_folder}/tinyobjloader)
endif()

if(NOT TARGET Jolt)
option(TARGET_HELLO_WORLD "" OFF)
option(TARGET_PERFORMANCE_TEST "" OFF)
option(TARGET_SAMPLES "" OFF)
option(TARGET_UNIT_TESTS "" OFF)
option(TARGET_VIEWER "" OFF)

if(ENABLE_PHYSICS_DEBUG_RENDERER)
option(TARGET_TEST_FRAMEWORK "" ON)
else()
option(TARGET_TEST_FRAMEWORK "" OFF)
endif()

add_subdirectory(JoltPhysics/Build)

if(ENABLE_PHYSICS_DEBUG_RENDERER)
set_target_properties(Jolt TestFramework
PROPERTIES
FOLDER ${third_party_folder}/JoltPhysics
MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
else()
set_target_properties(Jolt
PROPERTIES
FOLDER ${third_party_folder}/JoltPhysics
MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
endif()

endif()
10 changes: 10 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ set(THIRD_PARTY_DIR "${ENGINE_ROOT_DIR}/3rdparty")
set(ENGINE_ASSET_DIR "/asset")
set(ENGINE_SCHEMA_DIR "/schema")

option(ENABLE_PHYSICS_DEBUG_RENDERER "Enable Physics Debug Renderer" OFF)

# only support physics debug render at windows platform
if(NOT WIN32)
if(ENABLE_PHYSICS_DEBUG_RENDERER)
message(WARNING "Disable Physics Debug Renderer")
set(ENABLE_PHYSICS_DEBUG_RENDERER OFF CACHE BOOL "" FORCE)
endif()
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_compile_options("/MP")
set_property(DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT PilotEditor)
Expand Down
36 changes: 36 additions & 0 deletions engine/asset/objects/environment/floor/floor.object.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@
}
},
"$typeName": "MeshComponent"
},
{
"$typeName": "RigidBodyComponent",
"$context": {
"rigidbody_res": {
"actor_type": 1,
"inverse_mass": 0,
"shapes": [
{
"geometry": {
"$context": {
"half_extents": {
"x": 43,
"y": 24,
"z": 0.5
}
},
"$typeName": "Box"
},
"local_transform": {
"position": {
"x": 0,
"y": 0,
"z": -0.5
},
"rotate": {},
"scale": {
"x": 1,
"y": 1,
"z": 1
}
}
}
]
}
}
}
]
}
6 changes: 6 additions & 0 deletions engine/source/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ target_link_libraries(${TARGET_NAME} PRIVATE tinyobjloader stb)
target_link_libraries(${TARGET_NAME} PUBLIC glm)
target_link_libraries(${TARGET_NAME} PUBLIC glfw)
target_link_libraries(${TARGET_NAME} PUBLIC imgui)
target_link_libraries(${TARGET_NAME} PUBLIC Jolt)
target_link_libraries(${TARGET_NAME} PUBLIC ${vulkan_lib})
target_link_libraries(${TARGET_NAME} PRIVATE $<BUILD_INTERFACE:json11>)

if(ENABLE_PHYSICS_DEBUG_RENDERER)
add_compile_definitions(ENABLE_PHYSICS_DEBUG_RENDERER)
target_link_libraries(${TARGET_NAME} PUBLIC TestFramework d3d12.lib shcore.lib)
endif()

target_include_directories(
${TARGET_NAME}
PUBLIC $<BUILD_INTERFACE:${vulkan_include}>)
Expand Down
6 changes: 6 additions & 0 deletions engine/source/runtime/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "runtime/function/framework/world/world_manager.h"
#include "runtime/function/global/global_context.h"
#include "runtime/function/input/input_system.h"
#include "runtime/function/physics/physics_manager.h"
#include "runtime/function/render/render_system.h"
#include "runtime/function/render/window_system.h"

Expand Down Expand Up @@ -88,8 +89,13 @@ namespace Pilot

rendererTick();

#ifdef ENABLE_PHYSICS_DEBUG_RENDERER
g_runtime_global_context.m_physics_manager->renderPhysicsWorld(delta_time);
#endif

g_runtime_global_context.m_window_system->pollEvents();


g_runtime_global_context.m_window_system->setTile(
std::string("Pilot - " + std::to_string(getFPS()) + " FPS").c_str());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#include "runtime/function/controller/character_controller.h"

#include "runtime/function/framework/component/motor/motor_component.h"
#include "runtime/function/physics/physics_system.h"
#include "runtime/function/global/global_context.h"
#include "runtime/function/physics/physics_system.h"

namespace Pilot
{
Vector3 CharacterController::move(const Vector3& current_position, const Vector3& displacement)
{
const float gap = 0.1f;

Capsule test_capsule;
test_capsule.m_half_height = m_capsule.m_half_height - gap;
test_capsule.m_radius = m_capsule.m_radius - gap;

Vector3 desired_position = current_position + displacement;
if (g_runtime_global_context.m_physics_system->overlapByCapsule(desired_position, m_capsule))
if (g_runtime_global_context.m_legacy_physics_system->overlapByCapsule(desired_position, test_capsule))
{
desired_position = current_position;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

#include "runtime/function/framework/component/transform/transform_component.h"
#include "runtime/function/framework/object/object.h"
#include "runtime/function/physics/physics_system.h"
#include "runtime/function/framework/world/world_manager.h"
#include "runtime/function/global/global_context.h"
#include "runtime/function/physics/physics_scene.h"
#include "runtime/function/physics/physics_system.h"

namespace Pilot
{
Expand All @@ -20,15 +22,31 @@ namespace Pilot
return;
}

m_physics_actor = g_runtime_global_context.m_physics_system->createPhysicsActor(
m_physics_actor = g_runtime_global_context.m_legacy_physics_system->createPhysicsActor(
parent_object, parent_transform->getTransformConst(), m_rigidbody_res);

std::shared_ptr<PhysicsScene> physics_scene =
g_runtime_global_context.m_world_manager->getCurrentActivePhysicsScene().lock();
ASSERT(physics_scene);

const uint32_t body_id = physics_scene->createRigidBody(parent_transform->getTransformConst(), m_rigidbody_res);
m_physics_actor->setBodyID(body_id);
}

RigidBodyComponent::~RigidBodyComponent()
{
if (m_physics_actor)
{
g_runtime_global_context.m_physics_system->removePhyicsActor(m_physics_actor);
g_runtime_global_context.m_legacy_physics_system->removePhyicsActor(m_physics_actor);

const uint32_t body_id = m_physics_actor->getBodyID();

std::shared_ptr<PhysicsScene> physics_scene =
g_runtime_global_context.m_world_manager->getCurrentActivePhysicsScene().lock();
ASSERT(physics_scene);

physics_scene->removeRigidBody(body_id);

m_physics_actor = nullptr;
}
}
Expand Down
17 changes: 16 additions & 1 deletion engine/source/runtime/function/framework/level/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "runtime/engine.h"
#include "runtime/function/character/character.h"
#include "runtime/function/framework/object/object.h"
#include "runtime/function/physics/physics_manager.h"
#include "runtime/function/physics/physics_scene.h"

#include <limits>

Expand All @@ -19,6 +21,9 @@ namespace Pilot
{
m_current_active_character.reset();
m_gobjects.clear();

ASSERT(g_runtime_global_context.m_physics_manager);
g_runtime_global_context.m_physics_manager->deletePhysicsScene(m_physics_scene);
}

GObjectID Level::createObject(const ObjectInstanceRes& object_instance_res)
Expand Down Expand Up @@ -62,6 +67,9 @@ namespace Pilot
return false;
}

ASSERT(g_runtime_global_context.m_physics_manager);
m_physics_scene = g_runtime_global_context.m_physics_manager->createPhysicsScene();

for (const ObjectInstanceRes& object_instance_res : level_res.m_objects)
{
createObject(object_instance_res);
Expand Down Expand Up @@ -113,7 +121,8 @@ namespace Pilot
}
}

const bool is_save_success = g_runtime_global_context.m_asset_manager->saveAsset(output_level_res, m_level_res_url);
const bool is_save_success =
g_runtime_global_context.m_asset_manager->saveAsset(output_level_res, m_level_res_url);

if (is_save_success == false)
{
Expand Down Expand Up @@ -146,6 +155,12 @@ namespace Pilot
{
m_current_active_character->tick(delta_time);
}

std::shared_ptr<PhysicsScene> physics_scene = m_physics_scene.lock();
if (physics_scene)
{
physics_scene->tick(delta_time);
}
}

std::weak_ptr<GObject> Level::getGObjectByID(GObjectID go_id) const
Expand Down
5 changes: 5 additions & 0 deletions engine/source/runtime/function/framework/level/level.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Pilot
class Character;
class GObject;
class ObjectInstanceRes;
class PhysicsScene;

using LevelObjectsMap = std::unordered_map<GObjectID, std::shared_ptr<GObject>>;

Expand All @@ -37,6 +38,8 @@ namespace Pilot
GObjectID createObject(const ObjectInstanceRes& object_instance_res);
void deleteGObjectByID(GObjectID go_id);

std::weak_ptr<PhysicsScene> getPhysicsScene() const { return m_physics_scene; }

protected:
void clear();

Expand All @@ -47,5 +50,7 @@ namespace Pilot
LevelObjectsMap m_gobjects;

std::shared_ptr<Character> m_current_active_character;

std::weak_ptr<PhysicsScene> m_physics_scene;
};
} // namespace Pilot
20 changes: 17 additions & 3 deletions engine/source/runtime/function/framework/world/world_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ namespace Pilot
}
}

std::weak_ptr<PhysicsScene> WorldManager::getCurrentActivePhysicsScene() const
{
std::shared_ptr<Level> active_level = m_current_active_level.lock();
if (!active_level)
{
return std::weak_ptr<PhysicsScene>();
}

return active_level->getPhysicsScene();
}

bool WorldManager::loadWorld(const std::string& world_url)
{
LOG_INFO("loading world: {}", world_url);
Expand Down Expand Up @@ -84,14 +95,17 @@ namespace Pilot

bool WorldManager::loadLevel(const std::string& level_url)
{
std::unique_ptr<Level> level(new Level);
const bool is_level_load_success = level->load(level_url);
std::shared_ptr<Level> level = std::make_shared<Level>();
// set current level temporary
m_current_active_level = level;

const bool is_level_load_success = level->load(level_url);
if (is_level_load_success == false)
{
return false;
}

m_loaded_levels.emplace(level_url, std::move(level));
m_loaded_levels.emplace(level_url, level);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Pilot
{
class Level;
class PhysicsScene;

/// Manage all game worlds, it should be support multiple worlds, including game world and editor world.
/// Currently, the implement just supports one active world and one active level
Expand All @@ -25,6 +26,8 @@ namespace Pilot
void tick(float delta_time);
std::weak_ptr<Level> getCurrentActiveLevel() const { return m_current_active_level; }

std::weak_ptr<PhysicsScene> getCurrentActivePhysicsScene() const;

private:
bool loadWorld(const std::string& world_url);
bool loadLevel(const std::string& level_url);
Expand Down
10 changes: 8 additions & 2 deletions engine/source/runtime/function/global/global_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "runtime/function/framework/world/world_manager.h"
#include "runtime/function/input/input_system.h"
#include "runtime/function/physics/physics_system.h"
#include "runtime/function/physics/physics_manager.h"
#include "runtime/function/render/render_system.h"
#include "runtime/function/render/window_system.h"

Expand All @@ -31,7 +32,10 @@ namespace Pilot

m_asset_manager = std::make_shared<AssetManager>();

m_physics_system = std::make_shared<PhysicsSystem>();
m_legacy_physics_system = std::make_shared<PhysicsSystem>();

m_physics_manager = std::make_shared<PhysicsManager>();
m_physics_manager->initialize();

m_world_manager = std::make_shared<WorldManager>();
m_world_manager->initialize();
Expand Down Expand Up @@ -59,7 +63,9 @@ namespace Pilot

m_world_manager.reset();

m_physics_system.reset();
m_legacy_physics_system.reset();

m_physics_manager.reset();

m_input_system.reset();

Expand Down
Loading

0 comments on commit 7577e86

Please sign in to comment.