ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
Toggle main menu visibility
载入中...
搜索中...
未找到
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
8
namespace
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 内部调用 ---
46
void
registerRigidBody
(
RigidBody2D
* body);
47
void
unregisterRigidBody
(
RigidBody2D
* body);
49
void
createRigidBody
(
RigidBody2D
* body);
51
void
destroyRigidBody
(
RigidBody2D
* body);
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
}
Core.h
SHIT_API
#define SHIT_API
定义
Core.h:14
Math.h
System.h
Shit::Component
组件基类
定义
Component.h:20
Shit::PhysicsSystem2D::registerRigidBody
void registerRigidBody(RigidBody2D *body)
Shit::PhysicsSystem2D::setGravity
void setGravity(const Vector2 &gravity)
重力(像素/秒²),默认 {0, 320}
Shit::PhysicsSystem2D::~PhysicsSystem2D
~PhysicsSystem2D() override
Shit::PhysicsSystem2D::destroyRigidBody
void destroyRigidBody(RigidBody2D *body)
销毁 RigidBody2D 的物理体并注销(组件卸下时调用)
Shit::PhysicsSystem2D::getGravity
const Vector2 & getGravity() const
定义
PhysicsSystem2D.h:38
Shit::PhysicsSystem2D::update
void update() override
每帧更新(纯虚)
Shit::PhysicsSystem2D::PhysicsSystem2D
PhysicsSystem2D(int priority=50)
Shit::PhysicsSystem2D::getPixelsPerMeter
float getPixelsPerMeter() const
当前像素↔米比例(实例字段,各物理系统独立)
定义
PhysicsSystem2D.h:43
Shit::PhysicsSystem2D::unregisterRigidBody
void unregisterRigidBody(RigidBody2D *body)
Shit::PhysicsSystem2D::setPixelsPerMeter
void setPixelsPerMeter(float ppm)
设置像素↔米比例(必须在 init() 之前调用),默认 32
Shit::PhysicsSystem2D::onComponentAttached
bool onComponentAttached(Component *component) override
组件挂载时调用。返回 true 表示本系统认领并处理了该组件(组件据此确定已注册)。
Shit::PhysicsSystem2D::onComponentDetached
void onComponentDetached(Component *component) override
组件卸下时调用。
Shit::PhysicsSystem2D::createRigidBody
void createRigidBody(RigidBody2D *body)
为 RigidBody2D 创建物理体(组件认领时调用,从 Transform 读初始位姿)
Shit::PhysicsSystem2D::init
void init() override
初始化(可覆写)
Shit::PhysicsSystem2D::RigidBody2D
friend class RigidBody2D
定义
PhysicsSystem2D.h:54
Shit::PhysicsSystem2D::destroy
void destroy() override
销毁(纯虚)
Shit::RigidBody2D
刚体组件
定义
RigidBody2D.h:24
Shit::System::System
System(int priority=0)
Shit
定义
AudioPlayer.h:10
Shit::Vector2
glm::vec2 Vector2
定义
Math.h:5
include
ShitEngine
Physics
PhysicsSystem2D.h
制作者
1.17.0