Magic_Game/src/kiwano-physics/PhysicBody.h

566 lines
13 KiB
C
Raw Normal View History

2019-10-18 11:50:46 +08:00
// Copyright (c) 2018-2019 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
2019-10-18 11:50:46 +08:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
2019-10-18 11:50:46 +08:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
2019-10-18 11:50:46 +08:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2020-01-21 10:09:55 +08:00
#include <kiwano-physics/Fixture.h>
#include <kiwano-physics/ContactEdge.h>
2019-10-18 11:50:46 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
namespace physics
{
2019-12-31 16:01:41 +08:00
2020-02-22 17:24:31 +08:00
class PhysicWorld;
2020-01-21 10:09:55 +08:00
/**
* \addtogroup Physics
* @{
*/
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 物体
class KGE_API PhysicBody : public Component
2020-01-21 10:09:55 +08:00
{
2020-02-22 17:24:31 +08:00
friend class PhysicWorld;
2020-01-21 10:09:55 +08:00
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 物体类型
2020-01-21 10:09:55 +08:00
enum class Type
{
2020-02-10 17:32:04 +08:00
Static = 0, ///< 静态物体
Kinematic, ///< 动力学物体
Dynamic, ///< 动态物体
2020-01-21 10:09:55 +08:00
};
2020-02-06 16:54:47 +08:00
/// \~chinese
/// @brief 初始化物体
2020-02-22 17:24:31 +08:00
/// @param world 物理世界
/// @param type 物体类型
2020-02-22 17:24:31 +08:00
static PhysicBodyPtr Create(PhysicWorldPtr world, Type type);
2020-02-06 16:54:47 +08:00
/// \~chinese
/// @brief 初始化物体
2020-02-22 17:24:31 +08:00
/// @param world 物理世界
/// @param type 物体类型
2020-02-22 17:24:31 +08:00
static PhysicBodyPtr Create(PhysicWorld* world, Type type);
2020-02-06 16:54:47 +08:00
PhysicBody();
2020-01-21 10:09:55 +08:00
virtual ~PhysicBody();
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief 初始化物体
2020-02-10 17:32:04 +08:00
/// @param[in] world 物理世界
bool Init(PhysicWorldPtr world);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief 初始化物体
2020-02-10 17:32:04 +08:00
/// @param[in] world 物理世界
bool Init(PhysicWorld* world);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加夹具
2020-02-06 16:54:47 +08:00
void AddFixture(FixturePtr fixture);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加圆形夹具
/// @param radius 圆形半径
/// @param density 物体密度
/// @param
Fixture* AddCircleShape(float radius, float density, float friction = 0.2f);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加矩形夹具
/// @param size 矩形大小
/// @param density 物体密度
2020-02-19 12:09:50 +08:00
Fixture* AddRectShape(const Vec2& size, float density, float friction = 0.2f);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加多边形夹具
/// @param vertexs 多边形端点
/// @param density 物体密度
2020-02-19 12:09:50 +08:00
Fixture* AddPolygonShape(const Vector<Point>& vertexs, float density, float friction = 0.2f);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加线段形夹具
/// @param p1 线段起点
/// @param p2 线段终点
/// @param density 物体密度
2020-02-19 12:09:50 +08:00
Fixture* AddEdgeShape(const Point& p1, const Point& p2, float density, float friction = 0.2f);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加链条形夹具
/// @param vertexs 链条端点
/// @param loop 是否闭合
/// @param density 物体密度
2020-02-23 14:56:14 +08:00
Fixture* AddChainShape(const Vector<Point>& vertices, bool loop, float density, float friction = 0.2f);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 移除夹具
2020-02-06 16:54:47 +08:00
void RemoveFixture(FixturePtr fixture);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief 移除所有夹具
void RemoveAllFixtures();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取夹具列表
2020-02-06 16:54:47 +08:00
FixtureList GetFixtureList() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取接触边列表
2020-01-21 10:09:55 +08:00
ContactEdgeList GetContactList() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取类别码
2020-01-21 10:09:55 +08:00
uint16_t GetCategoryBits() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置类别码
2020-01-21 10:09:55 +08:00
void SetCategoryBits(uint16_t category_bits);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取碰撞掩码
2020-01-21 10:09:55 +08:00
uint16_t GetMaskBits() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置碰撞掩码
2020-01-21 10:09:55 +08:00
void SetMaskBits(uint16_t mask_bits);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取组索引
2020-01-21 10:09:55 +08:00
int16_t GetGroupIndex() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置组索引
2020-01-21 10:09:55 +08:00
void SetGroupIndex(int16_t index);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取旋转角度
float GetRotation() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置旋转角度
void SetRotation(float angle);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物体位置
Point GetPosition() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置物体位置
2020-02-19 12:09:50 +08:00
void SetPosition(const Point& pos);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 位置和旋转变换
2020-02-19 12:09:50 +08:00
void SetTransform(const Point& pos, float angle);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取质量 [kg]
2020-01-21 10:09:55 +08:00
float GetMass() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取惯性
2020-01-21 10:09:55 +08:00
float GetInertia() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取质量数据
/// @param[out] mass 物体质量 [kg]
/// @param[out] center 质心位置
/// @param[out] inertia 惯性
2020-01-21 10:09:55 +08:00
void GetMassData(float* mass, Point* center, float* inertia) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置质量数据
/// @param mass 物体质量 [kg]
/// @param center 质心位置
/// @param inertia 惯性
2020-02-19 12:09:50 +08:00
void SetMassData(float mass, const Point& center, float inertia);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 重置质量数据
2020-01-21 10:09:55 +08:00
void ResetMassData();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取世界坐标系上的点在物体上的位置
2020-02-19 12:09:50 +08:00
Point GetLocalPoint(const Point& world) const;
2020-01-21 10:09:55 +08:00
2020-02-23 14:56:14 +08:00
/// \~chinese
/// @brief 将世界坐标系中的向量转换到物体坐标系下
Vec2 GetLocalVector(const Vec2& world) const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物体上的点在世界坐标系的位置
2020-02-19 12:09:50 +08:00
Point GetWorldPoint(const Point& local) const;
2020-01-21 10:09:55 +08:00
2020-02-23 14:56:14 +08:00
/// \~chinese
/// @brief 将物体坐标系中的向量转换到世界坐标系下
Vec2 GetWorldVector(const Vec2& local) const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物体质心相对于物体的位置
2020-01-21 10:09:55 +08:00
Point GetLocalCenter() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物体质心位置
2020-01-21 10:09:55 +08:00
Point GetWorldCenter() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物体类型
2020-01-21 10:09:55 +08:00
Type GetType() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置物体类型
2020-01-21 10:09:55 +08:00
void SetType(Type type);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物体受重力的比例
2020-01-21 10:09:55 +08:00
float GetGravityScale() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置物体受重力的比例
2020-01-21 10:09:55 +08:00
void SetGravityScale(float scale);
2020-02-23 14:56:14 +08:00
/// \~chinese
/// @brief 获取线性阻尼
float GetLinearDamping() const;
/// \~chinese
/// @brief 设置线性阻尼
void SetLinearDamping(float damping);
/// \~chinese
/// @brief 获取旋转阻尼
float GetAngularDamping() const;
/// \~chinese
/// @brief 设置旋转阻尼
void SetAngularDamping(float damping);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 施力
/// @param force 力的大小和方向
/// @param point 施力点
/// @param wake 是否唤醒物体
2020-02-19 12:09:50 +08:00
void ApplyForce(const Vec2& force, const Point& point, bool wake = true);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 给物体中心施力
/// @param force 力的大小和方向
/// @param wake 是否唤醒物体
2020-02-19 12:09:50 +08:00
void ApplyForceToCenter(const Vec2& force, bool wake = true);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 施加扭矩
/// @param torque 扭矩
/// @param wake 是否唤醒物体
2020-01-21 10:09:55 +08:00
void ApplyTorque(float torque, bool wake = false);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 旋转角度是否固定
2020-01-21 10:09:55 +08:00
bool IsIgnoreRotation() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置是否固定旋转角度
2020-01-21 10:09:55 +08:00
void SetIgnoreRotation(bool flag);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否是子弹物体
2020-01-21 10:09:55 +08:00
bool IsBullet() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置物体是否是子弹物体
2020-01-21 10:09:55 +08:00
void SetBullet(bool flag);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否处于唤醒状态
2020-01-21 10:09:55 +08:00
bool IsAwake() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置唤醒状态
2020-01-21 10:09:55 +08:00
void SetAwake(bool flag);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否启用休眠
2020-01-21 10:09:55 +08:00
bool IsSleepingAllowed() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置是否允许休眠
2020-01-21 10:09:55 +08:00
void SetSleepingAllowed(bool flag);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否启用
2020-01-21 10:09:55 +08:00
bool IsActive() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置启用状态
2020-01-21 10:09:55 +08:00
void SetActive(bool flag);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物体所在物理世界
PhysicWorld* GetWorld() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief 销毁物体
void Destroy();
/// \~chinese
/// @brief 获取b2Body
2020-01-21 10:09:55 +08:00
b2Body* GetB2Body() const;
/// \~chinese
/// @brief 设置b2Body
void SetB2Body(b2Body* body);
2020-01-21 10:09:55 +08:00
protected:
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief 初始化组件
void InitComponent(Actor* actor) override;
/// \~chinese
/// @brief 销毁组件
void DestroyComponent() override;
2020-02-22 17:24:31 +08:00
/// \~chinese
/// @brief 更新物体状态
void UpdateFromActor(Actor* actor);
/// \~chinese
/// @brief 更新物体状态
void UpdateFromActor(Actor* actor, const Matrix3x2& actor_to_world, float parent_rotation);
/// \~chinese
/// @brief 更新夹具过滤器
void UpdateFixtureFilter(b2Fixture* fixture);
2020-01-21 10:09:55 +08:00
2020-02-22 17:24:31 +08:00
/// \~chinese
/// @brief 更新物理身体前
void BeforeSimulation(Actor* actor, const Matrix3x2& parent_to_world, const Matrix3x2& actor_to_world,
float parent_rotation);
2020-01-21 10:09:55 +08:00
2020-02-22 17:24:31 +08:00
/// \~chinese
/// @brief 更新物理身体后
void AfterSimulation(Actor* actorconst, const Matrix3x2& parent_to_world, float parent_rotation);
private:
PhysicWorld* world_;
b2Body* body_;
uint16_t category_bits_;
uint16_t mask_bits_;
int16_t group_index_;
2020-02-06 16:54:47 +08:00
2020-02-22 17:24:31 +08:00
PhysicBody::Type type_;
2020-02-06 16:54:47 +08:00
Vector<FixturePtr> fixtures_;
2020-02-22 17:24:31 +08:00
Point offset_;
Point position_cached_;
2020-01-21 10:09:55 +08:00
};
/** @} */
inline FixtureList PhysicBody::GetFixtureList() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
2020-02-06 16:54:47 +08:00
if (!body_->GetFixtureList())
return FixtureList();
Fixture* fixture = static_cast<Fixture*>(body_->GetFixtureList()->GetUserData());
return FixtureList(fixture);
2020-01-21 10:09:55 +08:00
}
2019-12-31 16:01:41 +08:00
inline ContactEdgeList PhysicBody::GetContactList() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
ContactEdge edge;
edge.SetB2ContactEdge(body_->GetContactList());
return ContactEdgeList(edge);
2020-01-21 10:09:55 +08:00
}
2019-12-31 16:01:41 +08:00
inline uint16_t PhysicBody::GetCategoryBits() const
2020-01-21 10:09:55 +08:00
{
return category_bits_;
}
2019-12-31 16:01:41 +08:00
inline uint16_t PhysicBody::GetMaskBits() const
2020-01-21 10:09:55 +08:00
{
return mask_bits_;
}
2019-12-31 16:01:41 +08:00
inline int16_t PhysicBody::GetGroupIndex() const
2020-01-21 10:09:55 +08:00
{
return group_index_;
}
2019-12-31 16:01:41 +08:00
inline float PhysicBody::GetRotation() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
return math::Radian2Degree(body_->GetAngle());
}
2019-12-31 16:01:41 +08:00
inline void PhysicBody::SetRotation(float angle)
2020-01-21 10:09:55 +08:00
{
SetTransform(GetPosition(), angle);
2020-01-21 10:09:55 +08:00
}
2019-12-31 16:01:41 +08:00
2020-02-19 12:09:50 +08:00
inline void PhysicBody::SetPosition(const Point& pos)
2020-01-21 10:09:55 +08:00
{
SetTransform(pos, GetRotation());
2020-01-21 10:09:55 +08:00
}
2019-12-31 16:01:41 +08:00
inline float PhysicBody::GetMass() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
return body_->GetMass();
}
2019-12-31 16:01:41 +08:00
inline float PhysicBody::GetInertia() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
return body_->GetInertia();
}
2019-12-31 16:01:41 +08:00
inline PhysicBody::Type PhysicBody::GetType() const
2020-01-21 10:09:55 +08:00
{
return type_;
2020-01-21 10:09:55 +08:00
}
2019-12-31 16:01:41 +08:00
inline void PhysicBody::SetType(Type type)
2020-01-21 10:09:55 +08:00
{
if (type_ != type)
{
type_ = type;
if (body_)
{
body_->SetType(b2BodyType(type));
}
}
2020-01-21 10:09:55 +08:00
}
2019-12-31 16:01:41 +08:00
inline float PhysicBody::GetGravityScale() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
return body_->GetGravityScale();
}
2019-12-31 16:01:41 +08:00
inline void PhysicBody::SetGravityScale(float scale)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
body_->SetGravityScale(scale);
}
2019-12-31 16:01:41 +08:00
2020-02-23 14:56:14 +08:00
inline float PhysicBody::GetLinearDamping() const
{
KGE_ASSERT(body_);
return body_->GetLinearDamping();
}
inline void PhysicBody::SetLinearDamping(float damping)
{
KGE_ASSERT(body_);
body_->SetLinearDamping(damping);
}
inline float PhysicBody::GetAngularDamping() const
{
KGE_ASSERT(body_);
return body_->GetAngularDamping();
}
inline void PhysicBody::SetAngularDamping(float damping)
{
KGE_ASSERT(body_);
body_->SetAngularDamping(damping);
}
inline bool PhysicBody::IsIgnoreRotation() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
return body_->IsFixedRotation();
}
2019-12-31 16:01:41 +08:00
inline void PhysicBody::SetIgnoreRotation(bool flag)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
body_->SetFixedRotation(flag);
}
2019-12-31 16:01:41 +08:00
inline bool PhysicBody::IsBullet() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
return body_->IsBullet();
}
2019-12-31 16:01:41 +08:00
inline void PhysicBody::SetBullet(bool flag)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
body_->SetBullet(flag);
}
2019-12-31 16:01:41 +08:00
inline bool PhysicBody::IsAwake() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
return body_->IsAwake();
}
2019-12-31 16:01:41 +08:00
inline void PhysicBody::SetAwake(bool flag)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
body_->SetAwake(flag);
}
2019-12-31 16:01:41 +08:00
inline bool PhysicBody::IsSleepingAllowed() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
return body_->IsSleepingAllowed();
}
2019-12-31 16:01:41 +08:00
inline void PhysicBody::SetSleepingAllowed(bool flag)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
body_->SetSleepingAllowed(flag);
}
2019-12-31 16:01:41 +08:00
inline bool PhysicBody::IsActive() const
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
return body_->IsActive();
}
2019-12-31 16:01:41 +08:00
inline void PhysicBody::SetActive(bool flag)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(body_);
body_->SetActive(flag);
}
2019-12-31 16:01:41 +08:00
inline b2Body* PhysicBody::GetB2Body() const
2020-01-21 10:09:55 +08:00
{
return body_;
}
2019-12-31 16:01:41 +08:00
inline PhysicWorld* PhysicBody::GetWorld() const
2020-01-21 10:09:55 +08:00
{
return world_;
2019-10-18 11:50:46 +08:00
}
2020-01-21 10:09:55 +08:00
} // namespace physics
} // namespace kiwano