ShitEngine 1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
RigidBody2D.h
浏览该文件的文档.
1#pragma once
2#include "../Core/Core.h"
3#include "../Math.h"
5#include <cstdint>
6
7namespace Shit {
8 class PhysicsSystem2D;
9
24 class SHIT_API SHIT_REFLECT(BlackList) RigidBody2D : public Component {
26 public:
27 enum class Type {
28 Static = 0,
29 Kinematic = 1,
30 Dynamic = 2,
31 };
32
34 ~RigidBody2D() override;
35
36 void onCreate() override;
37 void onAttach() override;
38 void onDetach() override;
39 void onDestroy() override;
40
41 // --- 属性 ---
42 void setBodyType(Type type);
43 Type getBodyType() const { return m_type; }
44
46 void onFieldChanged(const std::string& fieldName) override;
47
48 void setGravityScale(float scale) { m_gravityScale = scale; }
49 float getGravityScale() const { return m_gravityScale; }
50
51 void setLinearDamping(float damping) { m_linearDamping = damping; }
52 float getLinearDamping() const { return m_linearDamping; }
53
54 void setFixedRotation(bool fixed) { m_fixedRotation = fixed; }
55 bool isFixedRotation() const { return m_fixedRotation; }
56
57 // --- 力的接口 ---
58 void applyForce(const Vector2& force, bool wake = true);
59 void applyForceToCenter(const Vector2& force, bool wake = true);
60 void applyImpulse(const Vector2& impulse, bool wake = true);
61 void setLinearVelocity(const Vector2& velocity);
63
68 void setTransform(const Vector2& position, float rotationDegrees);
69
71 bool hasValidBody() const { return m_bodyValid; }
72
73 // --- 供 BoxCollider2D / CircleCollider2D 内部使用 ---
74 int32_t getBodyIndex() const { return m_bodyIndex; }
75 uint16_t getBodyWorld0() const { return m_bodyWorld0; }
76 uint16_t getBodyGeneration() const { return m_bodyGeneration; }
77
78 private:
79 friend class PhysicsSystem2D;
80
83 void ensureBody();
84
85 // b2BodyId = {int32_t index1; uint16_t world0; uint16_t generation;}
86 SHIT_META(Disable)
87 int32_t m_bodyIndex = 0;
88 SHIT_META(Disable)
89 uint16_t m_bodyWorld0 = 0;
90 SHIT_META(Disable)
91 uint16_t m_bodyGeneration = 0;
92 SHIT_META(Disable)
93 bool m_bodyValid = false;
94
95 SHIT_META(({.displayName = "Body Type", .tooltip = "Static=不动, Kinematic=用户控制, Dynamic=物理驱动"}))
96 Type m_type = Type::Static;
97 SHIT_META(({.displayName = "Gravity Scale", .tooltip = "重力影响系数", .range = {0, 10}, .step = 0.1}))
98 float m_gravityScale = 1.0f;
99 SHIT_META(({.displayName = "Linear Damping", .tooltip = "线性阻尼", .range = {0, 10}, .step = 0.1}))
100 float m_linearDamping = 0.0f;
101 SHIT_META(({.displayName = "Fixed Rotation", .tooltip = "锁定旋转"}))
102 bool m_fixedRotation = false;
103 };
104}
#define SHIT_API
定义 Core.h:14
#define SHIT_REFLECT_BODY(Type)
定义 Macros.h:89
#define SHIT_REFLECT(Mode)
定义 Macros.h:86
#define SHIT_META(...)
定义 Macros.h:95
2D 物理系统
定义 PhysicsSystem2D.h:30
Vector2 getLinearVelocity() const
Type
定义 RigidBody2D.h:27
bool isFixedRotation() const
定义 RigidBody2D.h:55
int32_t getBodyIndex() const
定义 RigidBody2D.h:74
uint16_t getBodyWorld0() const
定义 RigidBody2D.h:75
float getLinearDamping() const
定义 RigidBody2D.h:52
void setTransform(const Vector2 &position, float rotationDegrees)
传送/设置刚体位置与旋转(角度制,度)。Dynamic/Kinematic 会同时唤醒。
void setGravityScale(float scale)
定义 RigidBody2D.h:48
void applyForceToCenter(const Vector2 &force, bool wake=true)
~RigidBody2D() override
void setLinearVelocity(const Vector2 &velocity)
void setLinearDamping(float damping)
定义 RigidBody2D.h:51
void setBodyType(Type type)
uint16_t getBodyGeneration() const
定义 RigidBody2D.h:76
friend class PhysicsSystem2D
定义 RigidBody2D.h:79
void applyImpulse(const Vector2 &impulse, bool wake=true)
bool hasValidBody() const
物理体是否已创建
定义 RigidBody2D.h:71
void onFieldChanged(const std::string &fieldName) override
检查器直写反射字段后回调:把运行时字段改动同步到已创建的 Box2D 刚体
Type getBodyType() const
定义 RigidBody2D.h:43
void applyForce(const Vector2 &force, bool wake=true)
void onCreate() override
void onAttach() override
void onDetach() override
void onDestroy() override
float getGravityScale() const
定义 RigidBody2D.h:49
void setFixedRotation(bool fixed)
定义 RigidBody2D.h:54
定义 AnimationClip.h:12
glm::vec2 Vector2
定义 Math.h:7