ShitEngine 1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
Joint2D.h
浏览该文件的文档.
1#pragma once
2#include "../Core/Core.h"
3#include "../Math.h"
6#include <cstdint>
7
8namespace Shit {
9 class PhysicsSystem2D;
10 class RigidBody2D; // 前向声明(ComponentRef 模板与 setter 参数仅需不完整类型)
11
13 enum class SHIT_ENUM(JointType) JointType : int {
14 Distance = 0,
16 Weld = 2,
18 };
19
36 class SHIT_API SHIT_REFLECT(BlackList) Joint2D : public Component {
38 public:
40 ~Joint2D() override;
41
42 void onCreate() override;
43 void onAttach() override;
44 void onDetach() override;
45 void onDestroy() override;
46
48 void onFieldChanged(const std::string& fieldName) override;
49
50 // --- 配置(可在 onAttach 前后调用) ---
51 void setType(JointType type);
52 JointType getType() const { return m_type; }
53
57
59 void setAnchor(const Vector2& anchor);
60 const Vector2& getAnchor() const { return m_anchor; }
61
62 // --- Distance 关节参数 ---
63 void setLength(float length);
64 float getLength() const { return m_length; }
65 void setEnableSpring(bool enable);
66 void setHertz(float hertz);
67 void setDampingRatio(float ratio);
68
69 // --- Revolute 关节参数 ---
70 void setEnableMotor(bool enable);
71 void setMotorSpeed(float speed);
72 void setMaxMotorTorque(float torque);
73 void setEnableLimit(bool enable);
74 void setLowerAngle(float deg);
75 void setUpperAngle(float deg);
76
77 // --- Prismatic 关节参数 ---
78 void setAxisAngle(float deg);
79 void setLowerTranslation(float v);
80 void setUpperTranslation(float v);
81 void setMaxMotorForce(float force);
82
83 // --- 内部 / 供 PhysicsSystem2D 使用 ---
84 bool hasValidJoint() const { return m_jointValid; }
85 int32_t getJointIndex() const { return m_jointIndex; }
86 uint16_t getJointWorld0() const { return m_jointWorld0; }
87 uint16_t getJointGeneration() const { return m_jointGeneration; }
88
89 private:
90 friend class PhysicsSystem2D;
91
92 // b2JointId = {int32_t index1; uint16_t world0; uint16_t generation;}
93 SHIT_META(Disable)
94 int32_t m_jointIndex = 0;
95 SHIT_META(Disable)
96 uint16_t m_jointWorld0 = 0;
97 SHIT_META(Disable)
98 uint16_t m_jointGeneration = 0;
99 SHIT_META(Disable)
100 bool m_jointValid = false;
101
102 SHIT_META(({.displayName = "Type", .tooltip = "Distance=距离, Revolute=铰链, Weld=焊接, Prismatic=滑动"}))
103 JointType m_type = JointType::Distance;
104
105 SHIT_META(({.displayName = "Connected Body", .tooltip = "另一端刚体(bodyB);留空则暂不创建"}))
106 ComponentRef<RigidBody2D> m_connectedBody;
107
108 SHIT_META(({.displayName = "Anchor", .tooltip = "世界锚点(像素)"}))
109 Vector2 m_anchor{ 0.0f, 0.0f };
110
111 // --- Distance 参数 ---
112 SHIT_META(({.displayName = "Length", .tooltip = "距离关节自然长度(像素)", .range = {0, 4096}, .step = 1}))
113 float m_length = 0.0f;
114 SHIT_META(({.displayName = "Spring", .tooltip = "作为弹簧(false=刚性距离)"}))
115 bool m_enableSpring = false;
116 SHIT_META(({.displayName = "Hertz", .tooltip = "弹簧刚度 (Hz)", .range = {0, 60}, .step = 0.1}))
117 float m_hertz = 4.0f;
118 SHIT_META(({.displayName = "Damping Ratio", .tooltip = "弹簧阻尼比", .range = {0, 2}, .step = 0.01}))
119 float m_dampingRatio = 0.5f;
120
121 // --- Revolute 参数 ---
122 SHIT_META(({.displayName = "Motor", .tooltip = "启用电机"}))
123 bool m_enableMotor = false;
124 SHIT_META(({.displayName = "Motor Speed", .tooltip = "目标角速度(度/秒)", .range = {-720, 720}, .step = 1}))
125 float m_motorSpeed = 0.0f;
126 SHIT_META(({.displayName = "Max Motor Torque", .tooltip = "最大电机扭矩", .range = {0, 100000}, .step = 1}))
127 float m_maxMotorTorque = 0.0f;
128 SHIT_META(({.displayName = "Limit", .tooltip = "启用角度限制"}))
129 bool m_enableLimit = false;
130 SHIT_META(({.displayName = "Lower Angle", .tooltip = "角度下限(度)", .range = {-360, 360}, .step = 1}))
131 float m_lowerAngle = -45.0f;
132 SHIT_META(({.displayName = "Upper Angle", .tooltip = "角度上限(度)", .range = {-360, 360}, .step = 1}))
133 float m_upperAngle = 45.0f;
134
135 // --- Prismatic 参数 ---
136 SHIT_META(({.displayName = "Axis Angle", .tooltip = "滑动轴方向(度,相对 bodyA 局部)", .range = {-360, 360}, .step = 1}))
137 float m_axisAngle = 0.0f;
138 SHIT_META(({.displayName = "Lower Translation", .tooltip = "滑动下限(像素)", .range = {-4096, 4096}, .step = 1}))
139 float m_lowerTranslation = -100.0f;
140 SHIT_META(({.displayName = "Upper Translation", .tooltip = "滑动上限(像素)", .range = {-4096, 4096}, .step = 1}))
141 float m_upperTranslation = 100.0f;
142 SHIT_META(({.displayName = "Max Motor Force", .tooltip = "最大电机力", .range = {0, 100000}, .step = 1}))
143 float m_maxMotorForce = 0.0f;
144 };
145}
#define SHIT_API
定义 Core.h:14
#define SHIT_ENUM(Type)
定义 Macros.h:92
#define SHIT_REFLECT_BODY(Type)
定义 Macros.h:89
#define SHIT_REFLECT(Mode)
定义 Macros.h:86
#define SHIT_META(...)
定义 Macros.h:95
void setEnableSpring(bool enable)
是否作为弹簧(false=刚性距离)
void setDampingRatio(float ratio)
弹簧阻尼比
void setLowerTranslation(float v)
滑动下限(像素)
void setConnectedBody(RigidBody2D *body)
设置另一端刚体(bodyB)。传 nullptr 清空。
void setMaxMotorTorque(float torque)
void setUpperTranslation(float v)
滑动上限(像素)
void setAxisAngle(float deg)
滑动轴方向(度,相对 bodyA 局部)
uint16_t getJointWorld0() const
定义 Joint2D.h:86
uint16_t getJointGeneration() const
定义 Joint2D.h:87
int32_t getJointIndex() const
定义 Joint2D.h:85
float getLength() const
定义 Joint2D.h:64
void setMotorSpeed(float speed)
目标角速度(度/秒)
void setHertz(float hertz)
弹簧刚度(Hz)
void setEnableLimit(bool enable)
JointType getType() const
定义 Joint2D.h:52
void setLowerAngle(float deg)
角度下限(度)
void setLength(float length)
自然长度(像素),默认取两锚点当前距离
friend class PhysicsSystem2D
定义 Joint2D.h:90
void setUpperAngle(float deg)
角度上限(度)
void onFieldChanged(const std::string &fieldName) override
检查器直写反射字段后回调:把运行时字段改动同步到已创建的 Box2D 关节
RigidBody2D * getConnectedBody() const
const Vector2 & getAnchor() const
定义 Joint2D.h:60
void setEnableMotor(bool enable)
bool hasValidJoint() const
定义 Joint2D.h:84
void setType(JointType type)
void setAnchor(const Vector2 &anchor)
世界锚点(像素)。默认取本刚体位置;revolute/weld/distance 共用心智模型。
void onCreate() override
void onAttach() override
void setMaxMotorForce(float force)
void onDetach() override
void onDestroy() override
~Joint2D() override
2D 物理系统
定义 PhysicsSystem2D.h:30
刚体组件
定义 RigidBody2D.h:24
定义 AnimationClip.h:12
enum JointType Weld
焊接关节:把两刚体刚性连在一起(可做软体)
定义 Joint2D.h:16
enum JointType JointType
关节类型(决定 Box2D 约束行为)
定义 Joint2D.h:13
enum JointType Revolute
旋转/铰链关节:两刚体绕锚点相对旋转
定义 Joint2D.h:15
enum JointType Prismatic
棱柱关节:沿局部轴相对滑动(活塞/滑轨)
定义 Joint2D.h:17
glm::vec2 Vector2
定义 Math.h:7