ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
RigidBody2D类 参考

刚体组件 更多...

#include <RigidBody2D.h>

类 RigidBody2D 继承关系图:
Component

Public 类型

enum class  Type {
  Static = 0 , Kinematic = 1 ,
  Dynamic = 2
}

Public 成员函数

 RigidBody2D ()
 ~RigidBody2D () override
void onCreate () override
void onAttach () override
void onDetach () override
void onDestroy () override
void setBodyType (Type type)
Type getBodyType () const
void setGravityScale (float scale)
float getGravityScale () const
void setLinearDamping (float damping)
float getLinearDamping () const
void setFixedRotation (bool fixed)
bool isFixedRotation () const
void applyForce (const Vector2 &force, bool wake=true)
void applyForceToCenter (const Vector2 &force, bool wake=true)
void applyImpulse (const Vector2 &impulse, bool wake=true)
void setLinearVelocity (const Vector2 &velocity)
Vector2 getLinearVelocity () const
void setTransform (const Vector2 &position, float rotationDegrees)
 传送/设置刚体位置与旋转(角度制,度)。Dynamic/Kinematic 会同时唤醒。
bool hasValidBody () const
 物理体是否已创建
int32_t getBodyIndex () const
uint16_t getBodyWorld0 () const
uint16_t getBodyGeneration () const
Public 成员函数 继承自 Component
 Component ()
virtual ~Component ()=default
virtual void onAfterDeserialize ()
 反序列化完成后回调(Prefab::apply 逐组件调用一次,字段已写入)。 用于重建"直写字段绕过了 setter、无法自动触发的引擎状态"(如精灵纹理加载)。
 Component (const Component &)=delete
Componentoperator= (const Component &)=delete
 Component (Component &&)=delete
Componentoperator= (Component &&)=delete
GameObjectgetOwner () const
bool isRegistered () const

友元

class PhysicsSystem2D

额外继承的成员函数

Protected 成员函数 继承自 Component
void setRegistered (bool registered)
Protected 属性 继承自 Component
GameObjectm_owner = nullptr
bool m_isRegistered = false

详细描述

刚体组件

GameObject 提供物理刚体属性。必须与 TransformComponent 同挂一个 GameObject 上(由 onAttach 从中读取初始位置)。

使用方式: auto* body = go->addComponent<Shit::RigidBody2D>(); body->setBodyType(Shit::RigidBody2D::Type::Dynamic); body->setGravityScale(1.0f);

创建 Collider 前必须先挂 RigidBody2D(BoxCollider2D/CircleCollider2D 的 onAttach 会自动查找同 GameObject 上的 RigidBody2D)。

成员枚举类型说明

◆ Type

enum class Type
strong
枚举值
Static 

不受力,不可移动(默认)

Kinematic 

用户控制位置,可与 Dynamic 碰撞

Dynamic 

受重力/力/碰撞影响

构造及析构函数说明

◆ RigidBody2D()

RigidBody2D ( )

◆ ~RigidBody2D()

~RigidBody2D ( )
override

成员函数说明

◆ applyForce()

void applyForce ( const Vector2 & force,
bool wake = true )

◆ applyForceToCenter()

void applyForceToCenter ( const Vector2 & force,
bool wake = true )

◆ applyImpulse()

void applyImpulse ( const Vector2 & impulse,
bool wake = true )

◆ getBodyGeneration()

uint16_t getBodyGeneration ( ) const
inline

◆ getBodyIndex()

int32_t getBodyIndex ( ) const
inline

◆ getBodyType()

Type getBodyType ( ) const
inline

◆ getBodyWorld0()

uint16_t getBodyWorld0 ( ) const
inline

◆ getGravityScale()

float getGravityScale ( ) const
inline

◆ getLinearDamping()

float getLinearDamping ( ) const
inline

◆ getLinearVelocity()

Vector2 getLinearVelocity ( ) const

◆ hasValidBody()

bool hasValidBody ( ) const
inline

物理体是否已创建

◆ isFixedRotation()

bool isFixedRotation ( ) const
inline

◆ onAttach()

void onAttach ( )
overridevirtual

重载 Component .

◆ onCreate()

void onCreate ( )
overridevirtual

重载 Component .

◆ onDestroy()

void onDestroy ( )
overridevirtual

重载 Component .

◆ onDetach()

void onDetach ( )
overridevirtual

重载 Component .

◆ setBodyType()

void setBodyType ( Type type)

◆ setFixedRotation()

void setFixedRotation ( bool fixed)
inline

◆ setGravityScale()

void setGravityScale ( float scale)
inline

◆ setLinearDamping()

void setLinearDamping ( float damping)
inline

◆ setLinearVelocity()

void setLinearVelocity ( const Vector2 & velocity)

◆ setTransform()

void setTransform ( const Vector2 & position,
float rotationDegrees )

传送/设置刚体位置与旋转(角度制,度)。Dynamic/Kinematic 会同时唤醒。

注解
TransformComponent 互相同步:此接口直接驱动 Box2D 刚体, 下一帧物理步进后会回写 Transform。若仅移动 Transform 而不调本接口, Kinematic/Dynamic 刚体会被每帧物理回写还原。

◆ PhysicsSystem2D

friend class PhysicsSystem2D
friend

该类的文档由以下文件生成: