ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
PhysicsSystem2D.h
浏览该文件的文档.
1#pragma once
2#include "../Core/Core.h"
3#include "../Math.h"
4#include "../System/System.h"
5#include <cstdint>
6#include <vector>
7
8namespace Shit {
9 class RigidBody2D;
10
23 class SHIT_API PhysicsSystem2D final : public System {
24 public:
25 explicit PhysicsSystem2D(int priority = 50);
26 ~PhysicsSystem2D() override;
27
28 void init() override;
29 void update() override;
30 void destroy() override;
31
32 // 组件认领:RigidBody2D(创建/销毁物理体)
33 bool onComponentAttached(Component* component) override;
34 void onComponentDetached(Component* component) override;
35
36 // --- 配置(必须在 init() 之前调用) ---
37 void setGravity(const Vector2& gravity);
38 const Vector2& getGravity() const { return m_gravity; }
39
41 void setPixelsPerMeter(float ppm);
43 float getPixelsPerMeter() const { return m_pixelsPerMeter; }
44
45 // --- 供 RigidBody2D 内部调用 ---
52
53 private:
54 friend class RigidBody2D;
55
56 Vector2 m_gravity{ 0.0f, 320.0f };
57 std::vector<RigidBody2D*> m_bodies;
58 float m_accumulator = 0.0f; // 物理固定步长累积器
59
60 // b2WorldId = {uint16_t index1; uint16_t generation;}
61 uint16_t m_worldIndex = 0;
62 uint16_t m_worldGeneration = 0;
63 bool m_initialized = false;
64
65 // 像素↔米比例(实例字段;init() 时应用 b2SetLengthUnitsPerMeter——
66 // 该 API 是 Box2D 进程级全局,多实例不同比例时后者生效,属 Box2D 限制)
67 float m_pixelsPerMeter = 32.0f;
68 };
69}
#define SHIT_API
定义 Core.h:14
组件基类
定义 Component.h:20
void registerRigidBody(RigidBody2D *body)
void setGravity(const Vector2 &gravity)
重力(像素/秒²),默认 {0, 320}
~PhysicsSystem2D() override
void destroyRigidBody(RigidBody2D *body)
销毁 RigidBody2D 的物理体并注销(组件卸下时调用)
const Vector2 & getGravity() const
定义 PhysicsSystem2D.h:38
void update() override
每帧更新(纯虚)
PhysicsSystem2D(int priority=50)
float getPixelsPerMeter() const
当前像素↔米比例(实例字段,各物理系统独立)
定义 PhysicsSystem2D.h:43
void unregisterRigidBody(RigidBody2D *body)
void setPixelsPerMeter(float ppm)
设置像素↔米比例(必须在 init() 之前调用),默认 32
bool onComponentAttached(Component *component) override
组件挂载时调用。返回 true 表示本系统认领并处理了该组件(组件据此确定已注册)。
void onComponentDetached(Component *component) override
组件卸下时调用。
void createRigidBody(RigidBody2D *body)
为 RigidBody2D 创建物理体(组件认领时调用,从 Transform 读初始位姿)
void init() override
初始化(可覆写)
friend class RigidBody2D
定义 PhysicsSystem2D.h:54
void destroy() override
销毁(纯虚)
刚体组件
定义 RigidBody2D.h:24
System(int priority=0)
定义 AudioPlayer.h:10
glm::vec2 Vector2
定义 Math.h:5