ShitEngine
基于 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
45 void setGravityScale(float scale) { m_gravityScale = scale; }
46 float getGravityScale() const { return m_gravityScale; }
47
48 void setLinearDamping(float damping) { m_linearDamping = damping; }
49 float getLinearDamping() const { return m_linearDamping; }
50
51 void setFixedRotation(bool fixed) { m_fixedRotation = fixed; }
52 bool isFixedRotation() const { return m_fixedRotation; }
53
54 // --- 力的接口 ---
55 void applyForce(const Vector2& force, bool wake = true);
56 void applyForceToCenter(const Vector2& force, bool wake = true);
57 void applyImpulse(const Vector2& impulse, bool wake = true);
58 void setLinearVelocity(const Vector2& velocity);
60
65 void setTransform(const Vector2& position, float rotationDegrees);
66
68 bool hasValidBody() const { return m_bodyValid; }
69
70 // --- 供 BoxCollider2D / CircleCollider2D 内部使用 ---
71 int32_t getBodyIndex() const { return m_bodyIndex; }
72 uint16_t getBodyWorld0() const { return m_bodyWorld0; }
73 uint16_t getBodyGeneration() const { return m_bodyGeneration; }
74
75 private:
76 friend class PhysicsSystem2D;
77
78 // b2BodyId = {int32_t index1; uint16_t world0; uint16_t generation;}
79 SHIT_META(Disable)
80 int32_t m_bodyIndex = 0;
81 SHIT_META(Disable)
82 uint16_t m_bodyWorld0 = 0;
83 SHIT_META(Disable)
84 uint16_t m_bodyGeneration = 0;
85 SHIT_META(Disable)
86 bool m_bodyValid = false;
87
88 SHIT_META(({.displayName = "Body Type", .tooltip = "Static=不动, Kinematic=用户控制, Dynamic=物理驱动"}))
89 Type m_type = Type::Static;
90 SHIT_META(({.displayName = "Gravity Scale", .tooltip = "重力影响系数", .range = {0, 10}, .step = 0.1}))
91 float m_gravityScale = 1.0f;
92 SHIT_META(({.displayName = "Linear Damping", .tooltip = "线性阻尼", .range = {0, 10}, .step = 0.1}))
93 float m_linearDamping = 0.0f;
94 SHIT_META(({.displayName = "Fixed Rotation", .tooltip = "锁定旋转"}))
95 bool m_fixedRotation = false;
96 };
97}
#define SHIT_API
定义 Core.h:14
#define SHIT_REFLECT_BODY(Type)
定义 Macros.h:62
#define SHIT_REFLECT(Mode)
定义 Macros.h:59
#define SHIT_META(...)
定义 Macros.h:68
2D 物理系统
定义 PhysicsSystem2D.h:23
Vector2 getLinearVelocity() const
Type
定义 RigidBody2D.h:27
bool isFixedRotation() const
定义 RigidBody2D.h:52
int32_t getBodyIndex() const
定义 RigidBody2D.h:71
uint16_t getBodyWorld0() const
定义 RigidBody2D.h:72
float getLinearDamping() const
定义 RigidBody2D.h:49
void setTransform(const Vector2 &position, float rotationDegrees)
传送/设置刚体位置与旋转(角度制,度)。Dynamic/Kinematic 会同时唤醒。
void setGravityScale(float scale)
定义 RigidBody2D.h:45
void applyForceToCenter(const Vector2 &force, bool wake=true)
~RigidBody2D() override
void setLinearVelocity(const Vector2 &velocity)
void setLinearDamping(float damping)
定义 RigidBody2D.h:48
void setBodyType(Type type)
uint16_t getBodyGeneration() const
定义 RigidBody2D.h:73
friend class PhysicsSystem2D
定义 RigidBody2D.h:76
void applyImpulse(const Vector2 &impulse, bool wake=true)
bool hasValidBody() const
物理体是否已创建
定义 RigidBody2D.h:68
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:46
void setFixedRotation(bool fixed)
定义 RigidBody2D.h:51
定义 AudioPlayer.h:10
glm::vec2 Vector2
定义 Math.h:5