Intagrated Tracy, debugged animations

This commit is contained in:
2026-02-12 14:00:05 +03:00
parent 7947690e80
commit 74a1adfb27
25 changed files with 1553 additions and 2109 deletions

View File

@@ -19,6 +19,7 @@
#include "EventModule.h"
#include "TerrainModule.h"
#include "PhysicsModule.h"
#include <tracy/Tracy.hpp>
namespace ECS
{
struct PhysicsShape {
@@ -54,8 +55,18 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
ecs.component<PhysicsMeshPtr>();
ecs.component<PhysicsHeightfieldData>();
ecs.component<CharacterBody>();
ecs.component<TriggerBody>();
ecs.component<CharacterBody>().on_remove([](flecs::entity e,
CharacterBody &body) {
JPH::Character *ch =
static_cast<JPH::Character *>(body.ch.get());
if (ch) {
if (e.has<JPH::BodyID>())
e.remove<JPH::BodyID>();
JoltPhysicsWrapper::getSingleton().destroyCharacter(ch);
body.ch = nullptr;
}
});
ecs.component<TriggerBody>();
ecs.component<CharacterVelocity>();
ecs.component<WaterBody>().add(flecs::Singleton);
ecs.component<CachedMass>();
@@ -64,6 +75,7 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
ecs.system<EngineData, Physics>("physics_update")
.kind(PhysicsUpdate)
.each([&](EngineData &e, Physics &ph) {
ZoneScopedN("physics");
ph.physics->update(e.delta);
});
ecs.observer<const EngineData, PhysicsMeshName>(
@@ -152,6 +164,9 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
JoltPhysicsWrapper::getSingleton().removeBody(id);
if (e.has<CharacterBase>() || e.has<Character>())
return;
if (JoltPhysicsWrapper::getSingleton().bodyIsCharacter(
id))
return;
JoltPhysicsWrapper::getSingleton().destroyBody(id);
std::cout << "body destroyed" << std::endl;
});
@@ -164,7 +179,8 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.write<JPH::BodyID>()
.each([](flecs::entity e, const EngineData &eng,
const CharacterBase &base) {
CharacterBody &b = e.ensure<CharacterBody>();
ZoneScopedN("SetupCharacterPh");
CharacterBody &b = e.ensure<CharacterBody>();
b.ch.reset(JoltPhysicsWrapper::getSingleton()
.createCharacter(base.mBodyNode,
1.75f, 0.23f));
@@ -267,20 +283,23 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
ECS::modified<LuaEvent>();
});
});
// FIXME: convert to normal configure to prevent multiple instances
ecs.system<const EngineData>("init_water")
.kind(PhysicsPreUpdate)
.with<TerrainReady>()
.with<WaterAlmostReady>()
.without<WaterBody>()
.each([this](const EngineData &eng) {
ECS::get().set<WaterBody>({});
ZoneScopedN("init_water");
ECS::get().set<WaterBody>({});
});
ecs.system<const EngineData, WaterBody>("update_water")
.kind(PhysicsPostUpdate)
.with<TerrainReady>()
.with<WaterAlmostReady>()
.each([this](const EngineData &eng, WaterBody &body) {
const WaterSurface &water = ECS::get<WaterSurface>();
ZoneScopedN("update_water");
const WaterSurface &water = ECS::get<WaterSurface>();
body.inWater.clear();
JoltPhysicsWrapper::getSingleton().broadphaseQuery(
eng.delta,
@@ -295,8 +314,11 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.with<InWater>()
.each([this](flecs::entity e, const JPH::BodyID &id,
const WaterBody &body) {
if (!body.isInWater(id))
ZoneScopedN("update_water_status1");
if (!body.isInWater(id)) {
e.remove<InWater>();
ZoneTextF("in water");
}
});
ecs.system<const JPH::BodyID, const WaterBody>("update_water_status2")
.kind(PhysicsPostUpdate)
@@ -305,8 +327,11 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.without<InWater>()
.each([this](flecs::entity e, const JPH::BodyID &id,
const WaterBody &body) {
if (body.isInWater(id))
ZoneScopedN("update_water_status2");
if (body.isInWater(id)) {
e.add<InWater>();
ZoneTextF("not in water");
}
});
ecs.system<const CharacterBody, const WaterBody>(
"update_water_character1")
@@ -316,10 +341,13 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.with<InWater>()
.each([this](flecs::entity e, const CharacterBody &ch,
const WaterBody &body) {
JPH::Character *chptr =
ZoneScopedN("update_water_character1");
JPH::Character *chptr =
static_cast<JPH::Character *>(ch.ch.get());
if (!body.isInWater(chptr->GetBodyID()))
if (!body.isInWater(chptr->GetBodyID())) {
e.remove<InWater>();
ZoneTextF("not in water");
}
});
ecs.system<const CharacterBody, const WaterBody>(
"update_water_character2")
@@ -329,10 +357,13 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.without<InWater>()
.each([this](flecs::entity e, const CharacterBody &ch,
const WaterBody &body) {
JPH::Character *chptr =
ZoneScopedN("update_water_character2");
JPH::Character *chptr =
static_cast<JPH::Character *>(ch.ch.get());
if (body.isInWater(chptr->GetBodyID()))
if (body.isInWater(chptr->GetBodyID())) {
e.add<InWater>();
ZoneTextF("in water");
}
});
ecs.system<const EngineData, const BoatBase, const WaterBody,
const JPH::BodyID>("update_water_boat_enable")
@@ -342,7 +373,8 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.each([this](flecs::entity e, const EngineData &eng,
const BoatBase &boat, const WaterBody &body,
const JPH::BodyID &id) {
if (!JoltPhysicsWrapper::getSingleton().isAdded(id))
ZoneScopedN("update_water_boat_enable");
if (!JoltPhysicsWrapper::getSingleton().isAdded(id))
JoltPhysicsWrapper::getSingleton().addBody(
id, JPH::EActivation::Activate);
});
@@ -355,7 +387,8 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.each([this](flecs::entity e, const EngineData &eng,
const BoatBase &boat, const WaterBody &body,
const JPH::BodyID &id) {
if (!JoltPhysicsWrapper::getSingleton().isActive(id))
ZoneScopedN("update_water_boat_activation");
if (!JoltPhysicsWrapper::getSingleton().isActive(id))
JoltPhysicsWrapper::getSingleton().activate(id);
});
ecs.system<const EngineData, const BoatBase, const WaterBody,
@@ -368,7 +401,8 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.each([this](flecs::entity e, const EngineData &eng,
const BoatBase &boat, const WaterBody &body,
const JPH::BodyID &id, const CachedMass &mass) {
const WaterSurface &water = ECS::get<WaterSurface>();
ZoneScopedN("update_water_boat_buoyancy");
const WaterSurface &water = ECS::get<WaterSurface>();
float b = 1.0f, drag = 0.5f, adrag = 0.5f;
float level = 0.25f;
float my = JoltPhysicsWrapper::getSingleton()
@@ -425,7 +459,8 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.with<CharacterBuoyancy>()
.each([this](flecs::entity e, const EngineData &eng,
const CharacterBody &ch, const WaterBody &body) {
JPH::Character *chptr =
ZoneScopedN("update_water_character_buoyancy");
JPH::Character *chptr =
static_cast<JPH::Character *>(ch.ch.get());
JPH::BodyID id = chptr->GetBodyID();
if (JoltPhysicsWrapper::getSingleton().isActive(id)) {
@@ -467,13 +502,15 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
eng.delta);
}
});
ecs.system<const EngineData, const CharacterBody>("UpdatePhysics")
ecs.system<const EngineData, const CharacterBody>(
"UpdateCharacterPhysicsState")
.kind(flecs::OnUpdate)
.with<CharacterUpdatePhysicsState>()
.write<CharacterUpdatePhysicsState>()
.each([](flecs::entity e, const EngineData &eng,
const CharacterBody &body) {
if (e.has<CharacterDisablePhysics>())
ZoneScopedN("UpdateCharacterPhysicsState");
if (e.has<CharacterDisablePhysics>())
PhysicsModule::controlPhysics(e, false);
else
PhysicsModule::controlPhysics(e, true);
@@ -489,7 +526,8 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.each([this](flecs::entity e, const EngineData &eng,
const CharacterBase &chbase,
const CharacterBody &body, CharacterVelocity &gr) {
if (e.has<InWater>() &&
ZoneScopedN("HandleVelocity");
if (e.has<InWater>() &&
chbase.mBodyNode->_getDerivedPosition().y > -0.5f)
e.remove<InWater>();
Ogre::Vector3 v = gr.velocity;
@@ -519,7 +557,27 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
JoltPhysics::convert<JPH::Vec3>(v));
gr.velocity = Ogre::Vector3::ZERO;
});
ecs.system<const EngineData, CharacterBase, const CharacterBody,
ecs.system<CharacterBase>("HandleSubmerge")
.kind(flecs::OnUpdate)
.with<TerrainReady>()
.with<WaterReady>()
.with<InWater>()
.each([this](flecs::entity e, CharacterBase &ch) {
ZoneScopedNC("HandleSubmerge", 0xFF3030);
float full_subm = 2.0f;
Ogre::Vector3 pos = ch.mBodyNode->getPosition();
float current_subm = -Ogre::Math::Clamp(
pos.y + Ogre::Math::Sin(ch.mTimer * 0.13f +
130.0f) *
0.07f,
-full_subm, 0.0f);
if (current_subm > 0.9f)
ch.is_submerged = true;
else if (current_subm < 0.8f)
ch.is_submerged = false;
ZoneTextF("is submerged: %d", (int)ch.is_submerged);
});
ecs.system<const EngineData, CharacterBase, const CharacterBody,
CharacterVelocity>("HandleVelocityNoPhysics")
.kind(PhysicsPostUpdate)
.with<TerrainReady>()
@@ -529,7 +587,8 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
.each([this](flecs::entity e, const EngineData &eng,
CharacterBase &ch, const CharacterBody &body,
CharacterVelocity &gr) {
Ogre::Vector3 v = gr.velocity;
ZoneScopedNC("HandleVelocityNoPhysics", 0xFF4040);
Ogre::Vector3 v = gr.velocity;
// v.y = 0.0f;
ch.mBodyNode->_setDerivedPosition(
ch.mBodyNode->_getDerivedPosition() +
@@ -548,9 +607,12 @@ PhysicsModule::PhysicsModule(flecs::world &ecs)
ch.mBodyNode->_getDerivedPosition().y > -0.5f) {
e.remove<InWater>();
ch.is_submerged = false;
}
if (!e.has<InWater>() && ch.is_submerged)
ZoneTextF("remove in water");
}
if (!e.has<InWater>() && ch.is_submerged) {
ch.is_submerged = false;
ZoneTextF("not submerged");
}
});
}
flecs::entity PhysicsModule::createTerrainChunkBody(Ogre::SceneNode *node,