feat: add spherecollidor

This commit is contained in:
2026-06-24 15:55:57 +02:00
parent 9d3d14a54d
commit 81ab2a8a4a
6 changed files with 85 additions and 25 deletions
+1
View File
@@ -12,6 +12,7 @@ set(SRC_FILES
"src/Components/Physics/Rigidbody.cpp" "src/Components/Physics/Rigidbody.cpp"
"src/Components/Physics/BoxCollider.cpp" "src/Components/Physics/BoxCollider.cpp"
"src/Components/Physics/SphereCollider.cpp"
"src/Graphics/BindlessSetManager.cpp" "src/Graphics/BindlessSetManager.cpp"
"src/Graphics/Camera.cpp" "src/Graphics/Camera.cpp"
Binary file not shown.
@@ -2,7 +2,7 @@
#include <algorithm> #include <algorithm>
#include <destrum/Components/Collider.h> #include <destrum/Components/Physics/Collider.h>
class SphereCollider final : public Collider { class SphereCollider final : public Collider {
public: public:
@@ -10,6 +10,8 @@ public:
: Collider(owner) : Collider(owner)
, m_Radius(radius) {} , m_Radius(radius) {}
void Update() override {}
[[nodiscard]] PhysicsShapeDesc BuildPhysicsShape() const override { [[nodiscard]] PhysicsShapeDesc BuildPhysicsShape() const override {
return PhysicsShapeDesc::Sphere(m_Radius, m_CenterOffset, m_IsTrigger); return PhysicsShapeDesc::Sphere(m_Radius, m_CenterOffset, m_IsTrigger);
} }
@@ -0,0 +1 @@
#include <destrum/Components/Physics/SphereCollider.h>
+2 -3
View File
@@ -25,9 +25,8 @@ private:
Camera camera{glm::vec3(0.f, 0.f, -5.f), glm::vec3(0, 1, 0)}; Camera camera{glm::vec3(0.f, 0.f, -5.f), glm::vec3(0, 1, 0)};
CPUMesh testMesh{}; MeshID sphereMesh;
MeshID testMeshID; MaterialID sphereMaterial;
MaterialID testMaterialID;
std::unique_ptr<CubeMap> skyboxCubemap; std::unique_ptr<CubeMap> skyboxCubemap;
+78 -21
View File
@@ -20,6 +20,7 @@
#include <string> #include <string>
#include "imgui.h" #include "imgui.h"
#include "destrum/Components/Physics/SphereCollider.h"
#include "destrum/Util/ModelDocUtils.h" #include "destrum/Util/ModelDocUtils.h"
@@ -52,10 +53,10 @@ void LightKeeper::customInit()
ModelDocUtils::LogModelDocSummary(kittyModel, "kitty.glb"); ModelDocUtils::LogModelDocSummary(kittyModel, "kitty.glb");
const auto& kittyPrimitive = ModelDocUtils::GetFirstPrimitiveOrThrow(kittyModel, "game://kitty.glb"); const auto& kittyPrimitive = ModelDocUtils::GetFirstPrimitiveOrThrow(kittyModel, "game://kitty.glb");
testMesh = kittyPrimitive.mesh; auto testMesh = kittyPrimitive.mesh;
testMesh.name = "Test Mesh"; testMesh.name = "Test Mesh";
testMeshID = meshCache.addMesh(gfxDevice, testMesh); auto testMeshID = meshCache.addMesh(gfxDevice, testMesh);
spdlog::info("TestMesh uploaded with id: {}", testMeshID); spdlog::info("TestMesh uploaded with id: {}", testMeshID);
const auto testTexturePath = ModelDocUtils::PickTexturePath( const auto testTexturePath = ModelDocUtils::PickTexturePath(
@@ -67,13 +68,13 @@ void LightKeeper::customInit()
const auto testimgID = gfxDevice.loadImageFromFile(testTexturePath); const auto testimgID = gfxDevice.loadImageFromFile(testTexturePath);
spdlog::info("Test image loaded from '{}' with id: {}", testTexturePath.generic_string(), testimgID); spdlog::info("Test image loaded from '{}' with id: {}", testTexturePath.generic_string(), testimgID);
testMaterialID = materialCache.addMaterial(gfxDevice, { auto testMaterialID = materialCache.addMaterial(gfxDevice, {
.baseColor = ModelDocUtils::GetImportedBaseColor( .baseColor = ModelDocUtils::GetImportedBaseColor(
kittyModel, kittyPrimitive), kittyModel, kittyPrimitive),
.diffuseTexture = testimgID, .diffuseTexture = testimgID,
.name = ModelDocUtils::GetImportedMaterialName( .name = ModelDocUtils::GetImportedMaterialName(
kittyModel, kittyPrimitive, "TestMaterial"), kittyModel, kittyPrimitive, "TestMaterial"),
}); });
spdlog::info("Test material created with id: {}", testMaterialID); spdlog::info("Test material created with id: {}", testMaterialID);
camera.SetRotation(glm::radians(glm::vec2(90.f, 0.f))); camera.SetRotation(glm::radians(glm::vec2(90.f, 0.f)));
@@ -152,7 +153,7 @@ void LightKeeper::customInit()
); );
ModelDocUtils::LogModelDocSummary(charModel, "capybara.fbx"); ModelDocUtils::LogModelDocSummary(charModel, "capybara.fbx");
const auto &charPrimitive = ModelDocUtils::GetFirstSkinnedPrimitiveOrFirstOrThrow( const auto& charPrimitive = ModelDocUtils::GetFirstSkinnedPrimitiveOrFirstOrThrow(
charModel, charModel,
"engine://cotw-capybara-male/source/capybara.fbx" "engine://cotw-capybara-male/source/capybara.fbx"
); );
@@ -185,7 +186,8 @@ void LightKeeper::customInit()
std::string firstAnimationName; std::string firstAnimationName;
for (auto &clip: charModel.animations) { for (auto& clip : charModel.animations)
{
spdlog::info("Loaded animation: '{}' ({:.2f}s)", clip.name, clip.duration); spdlog::info("Loaded animation: '{}' ({:.2f}s)", clip.name, clip.duration);
if (firstAnimationName.empty()) if (firstAnimationName.empty())
@@ -194,7 +196,8 @@ void LightKeeper::customInit()
animator->addClip(std::make_shared<SkeletalAnimation>(std::move(clip))); animator->addClip(std::make_shared<SkeletalAnimation>(std::move(clip)));
} }
if (!firstAnimationName.empty()) { if (!firstAnimationName.empty())
{
spdlog::info("Playing animation: '{}'", firstAnimationName); spdlog::info("Playing animation: '{}'", firstAnimationName);
animator->play(firstAnimationName); animator->play(firstAnimationName);
} }
@@ -275,11 +278,11 @@ void LightKeeper::customInit()
characterOptions characterOptions
); );
const auto &charPrimitive = const auto& charPrimitive =
ModelDocUtils::GetFirstSkinnedPrimitiveOrFirstOrThrow( ModelDocUtils::GetFirstSkinnedPrimitiveOrFirstOrThrow(
charModel, charModel,
"engine://characterMedium.fbx" "engine://characterMedium.fbx"
); );
const auto charMeshID = meshCache.addMesh(gfxDevice, charPrimitive.mesh); const auto charMeshID = meshCache.addMesh(gfxDevice, charPrimitive.mesh);
@@ -312,12 +315,14 @@ void LightKeeper::customInit()
charModel.skeleton charModel.skeleton
); );
for (auto &clip: runClips) { for (auto& clip : runClips)
{
spdlog::info("Loaded animation: '{}' ({:.2f}s)", clip.name, clip.duration); spdlog::info("Loaded animation: '{}' ({:.2f}s)", clip.name, clip.duration);
animator->addClip(std::make_shared<SkeletalAnimation>(std::move(clip))); animator->addClip(std::make_shared<SkeletalAnimation>(std::move(clip)));
} }
if (!runClips.empty()) { if (!runClips.empty())
{
animator->play("Root|Run"); animator->play("Root|Run");
} }
@@ -379,7 +384,7 @@ void LightKeeper::customInit()
// //
// for (int x = 0; x < cubeCount; x++) // for (int x = 0; x < cubeCount; x++)
// { // {
// for (int y = 0; y < cubeCount; y++) // for (int y = 0; y < 3; y++)
// { // {
// for (int z = 0; z < cubeCount; z++) // for (int z = 0; z < cubeCount; z++)
// { // {
@@ -394,7 +399,7 @@ void LightKeeper::customInit()
// //
// cube->GetTransform().SetWorldPosition(glm::vec3( // cube->GetTransform().SetWorldPosition(glm::vec3(
// (x - cubeCount / 2.0f) * spacing, // (x - cubeCount / 2.0f) * spacing,
// y * spacing + 5, // y * spacing + 0,
// (z - cubeCount / 2.0f) * spacing // (z - cubeCount / 2.0f) * spacing
// )); // ));
// //
@@ -405,6 +410,37 @@ void LightKeeper::customInit()
// } // }
// } // }
// } // }
{
auto sphereModel = ModelDoc::LoadModel(
AssetFS::GetInstance().GetFullPath("engine://sphere.fbx").generic_string(),
staticModelOptions
);
ModelDocUtils::LogModelDocSummary(sphereModel, "sphere.fbx");
const auto& spherePrimitive = ModelDocUtils::GetFirstPrimitiveOrThrow(sphereModel, "game://sphere.fbx");
const auto sphereTexturePath = ModelDocUtils::PickTexturePath(
sphereModel,
spherePrimitive,
AssetFS::GetInstance().GetFullPath("game://238882.png")
);
const auto sphereTextureID = gfxDevice.loadImageFromFile(sphereTexturePath);
sphereMaterial = materialCache.addMaterial(gfxDevice, {
.baseColor = ModelDocUtils::GetImportedBaseColor(planeModel, planePrimitive),
.textureFilteringMode =TextureFilteringMode::Anisotropic,
.diffuseTexture = sphereTextureID,
.name = ModelDocUtils::GetImportedMaterialName(
planeModel, planePrimitive,
"GroundPlaneMaterial"),
});
sphereMesh = meshCache.addMesh(gfxDevice, spherePrimitive.mesh);
}
} }
void LightKeeper::customUpdate(float dt) void LightKeeper::customUpdate(float dt)
@@ -416,6 +452,27 @@ void LightKeeper::customUpdate(float dt)
{ {
renderer.setRenderWireframe(!renderer.getRenderWireframe()); renderer.setRenderWireframe(!renderer.getRenderWireframe());
} }
ImGui::Begin("Test");
if (ImGui::Button("SPawn ball"))
{
auto sphere = std::make_shared<GameObject>("Sphere");
sphere->AddComponent<SphereCollider>(1.5f);
auto rb = sphere->AddComponent<Rigidbody>();
rb->SetMass(1000);
auto meshRenderComp = sphere->AddComponent<MeshRendererComponent>();
meshRenderComp->SetMaterialID(sphereMaterial);
meshRenderComp->SetMeshID(sphereMesh);
sphere->GetTransform().SetWorldPosition(0, 100, 0);
sphere->GetTransform().SetWorldScale(glm::vec3{0.015f});
SceneManager::GetInstance().GetCurrentScene().Add(sphere);
SceneManager::GetInstance().GetCurrentScene().GetPhysics().RegisterGameObject(*sphere);
}
ImGui::End();
} }
void LightKeeper::customDraw() void LightKeeper::customDraw()