Files
HelloVulkan/Engine/include/Engine/Physics/PhysicsDebugDraw.h
T
atdunbg 1c4b6f37ff feat: Jolt 物理系统——刚体/碰撞体/物理调试图元 + 引擎时间单源
- PhysicsWorld:Jolt 集成,Dynamic/Kinematic/Static 刚体,姿态写回
  Transform(Unity FixedUpdate 语义,吃 scaled dt,暂停自动冻结)
- 碰撞体:Box(默认/手动 halfExtents)+ Mesh 碰撞体(Static→精确
  MeshShape / Dynamic·Kinematic→ConvexHull,数据源 = Mesh ctor 缓存
  的 CPU 位置副本,退化三角剔除,失败回退 Box 并告警)
- PhysicsDebugPass:物理调试图元每帧上传绘制(debug_line shader)
- Time:引擎时间单源(BeginFrame 落定 FrameTime,scaled/unscaled 分离)
- JoltPhysics 以 submodule 接入(官方 url)
2026-10-02 20:08:54 +08:00

92 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// PhysicsDebugDraw:Jolt 调试图元收集器(JPH::DebugRendererSimple 派生)。
//
// 职责只有"收集":Jolt 各调试源(BodyManager::Draw 的形状线框/速度箭头、
// ConstraintManager::DrawConstraints、PhysicsWorld 手绘的重力/加速度箭头)
// 触发的 DrawLine/DrawTriangle 回调全部收进顶点数组;渲染侧
// (PhysicsDebugPass)每帧上传到 ring VB 绘制。收集器不理解 Vulkan,
// 渲染 pass 不理解 Jolt——图元契约见 PhysicsDebugTypes.h。
//
// 生命周期:PhysicsWorld 持有并安装为 JPH::DebugRenderer::sInstance;
// DebugRendererSimple 的构造即 DebugRenderer::Initialize()(预烘焙
// box/sphere/cylinder 等通用几何 batch),NextFrame() 每帧释放不再
// 引用的缓存几何。
//
// 扩展性(物理调试的唯一图元出口,新调试源零新通道):
// 软体布料 → DrawSettings 的 mDrawSoftBody* 开关原生生效
// 角色控制器 → CharacterVirtual::DebugDraw 走同一 sInstance
// 载具控制器 → VehicleConstraint::Draw 走同一 sInstance
//
#ifndef HELLOVULKAN_PHYSICSDEBUGDRAW_H
#define HELLOVULKAN_PHYSICSDEBUGDRAW_H
#include <Engine/Physics/PhysicsDebugTypes.h>
#ifdef JPH_DEBUG_RENDERER
#include <Jolt/Jolt.h>
#include <Jolt/Renderer/DebugRendererSimple.h>
namespace Engine {
class PhysicsDebugDraw final : public JPH::DebugRendererSimple {
public:
// 析构交给基类:~DebugRenderer 自己断言并清 sInstance(JPH_ASSERT(sInstance == this)),
// 派生类手动清会让基类断言看到 nullptr 而失败
PhysicsDebugFrame& Frame() { return m_Frame; }
// 每次收集前调用:清空上一帧图元(容量保留,稳态零堆分配)
void BeginCollect() { m_Frame.Clear(); }
// ---- DebugRenderer 回调(基类其余绘制原语全部路由到这里)----
void DrawLine(JPH::RVec3Arg inFrom, JPH::RVec3Arg inTo,
JPH::ColorArg inColor) override;
void DrawTriangle(JPH::RVec3Arg inV1, JPH::RVec3Arg inV2,
JPH::RVec3Arg inV3, JPH::ColorArg inColor,
JPH::DebugRenderer::ECastShadow inCastShadow) override;
void DrawText3D(JPH::RVec3Arg, const JPH::string_view&,
JPH::ColorArg, float) override {
// 3D 文本暂不支持(遮挡 UI 噪音,需要时再挂 ImGui 调试层)
}
private:
static glm::vec3 ToGlm(JPH::RVec3Arg v)
{
return {static_cast<f32>(v.GetX()), static_cast<f32>(v.GetY()),
static_cast<f32>(v.GetZ())};
}
static glm::vec4 ToGlm(JPH::ColorArg c)
{
constexpr f32 kInv = 1.0f / 255.0f;
return {c.r * kInv, c.g * kInv, c.b * kInv, c.a * kInv};
}
PhysicsDebugFrame m_Frame;
};
} // namespace Engine
#else // !JPH_DEBUG_RENDERER
// Distribution 构建:Jolt 调试渲染被整体裁剪(DEBUG_RENDERER_IN_* 均关),
// 收集器退化为空实现——所有消费方照常编译,零绘制零开销
namespace Engine {
class PhysicsDebugDraw {
public:
PhysicsDebugFrame& Frame() { return m_Frame; }
void BeginCollect() { m_Frame.Clear(); }
void NextFrame() {}
private:
PhysicsDebugFrame m_Frame;
};
} // namespace Engine
#endif // JPH_DEBUG_RENDERER
#endif //HELLOVULKAN_PHYSICSDEBUGDRAW_H