Magic_Game/src/kiwano-physics/Body.h

519 lines
12 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-07 17:44:51 +08:00
#include <kiwano-physics/ContactEdge.h>
2020-01-21 10:09:55 +08:00
#include <kiwano-physics/Fixture.h>
#include <kiwano-physics/helper.h>
2019-10-18 11:50:46 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
namespace physics
{
class World;
2019-10-31 20:34:38 +08:00
2020-01-21 10:09:55 +08:00
KGE_DECLARE_SMART_PTR(Body);
2019-12-31 16:01:41 +08:00
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 物体
2020-01-21 10:09:55 +08:00
class KGE_API Body : public virtual ObjectBase
{
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
2020-02-10 17:32:04 +08:00
/// @brief 初始化
/// @param[in] world 物理世界
/// @param[in] actor 绑定的角色
/// @param[in] type 物体类型
2020-02-06 16:54:47 +08:00
static BodyPtr Create(World* world, ActorPtr actor, Type type);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 初始化
/// @param[in] world 物理世界
/// @param[in] actor 绑定的角色
/// @param[in] type 物体类型
2020-02-06 16:54:47 +08:00
static BodyPtr Create(World* world, Actor* actor, Type type);
2020-01-21 10:09:55 +08:00
Body();
virtual ~Body();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 初始化
/// @param[in] world 物理世界
/// @param[in] actor 绑定的角色
2020-01-21 10:09:55 +08:00
bool InitBody(World* world, ActorPtr actor);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 初始化
/// @param[in] world 物理世界
/// @param[in] actor 绑定的角色
2020-01-21 10:09:55 +08:00
bool InitBody(World* world, Actor* actor);
/// \~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 物体密度
2020-02-06 16:54:47 +08:00
/// @param
Fixture* AddCircleShape(float radius, float density, float friction = 0.2f, float restitution = 0.f,
bool is_sensor = false);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加矩形夹具
/// @param size 矩形大小
/// @param density 物体密度
2020-02-06 16:54:47 +08:00
Fixture* AddRectShape(Vec2 const& size, float density, float friction = 0.2f, float restitution = 0.f,
bool is_sensor = false);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加多边形夹具
/// @param vertexs 多边形端点
/// @param density 物体密度
2020-02-06 16:54:47 +08:00
Fixture* AddPolygonShape(Vector<Point> const& vertexs, float density, float friction = 0.2f,
float restitution = 0.f, bool is_sensor = false);
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-06 16:54:47 +08:00
Fixture* AddEdgeShape(Point const& p1, Point const& p2, float density, float friction = 0.2f,
float restitution = 0.f, bool is_sensor = false);
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-06 16:54:47 +08:00
Fixture* AddChainShape(Vector<Point> const& vertexs, bool loop, float density, float friction = 0.2f,
float restitution = 0.f, bool is_sensor = false);
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
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 获取旋转角度
2020-01-21 10:09:55 +08:00
float GetBodyRotation() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置旋转角度
2020-01-21 10:09:55 +08:00
void SetBodyRotation(float angle);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物体位置
2020-01-21 10:09:55 +08:00
Point GetBodyPosition() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置物体位置
2020-01-21 10:09:55 +08:00
void SetBodyPosition(Point const& pos);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 位置和旋转变换
2020-01-21 10:09:55 +08:00
void SetBodyTransform(Point const& pos, float angle);
/// \~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-01-21 10:09:55 +08:00
void SetMassData(float mass, Point const& center, float inertia);
/// \~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-01-21 10:09:55 +08:00
Point GetLocalPoint(Point const& world) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物体上的点在世界坐标系的位置
2020-01-21 10:09:55 +08:00
Point GetWorldPoint(Point const& local) const;
/// \~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);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 施力
/// @param force 力的大小和方向
/// @param point 施力点
/// @param wake 是否唤醒物体
2020-01-21 10:09:55 +08:00
void ApplyForce(Vec2 const& force, Point const& point, bool wake = true);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 给物体中心施力
/// @param force 力的大小和方向
/// @param wake 是否唤醒物体
2020-01-21 10:09:55 +08:00
void ApplyForceToCenter(Vec2 const& force, bool wake = true);
/// \~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 获取物体所在物理世界
2020-01-21 10:09:55 +08:00
World* GetWorld() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物体绑定的角色
2020-01-21 10:09:55 +08:00
Actor* GetActor() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置物体绑定的角色
2020-01-21 10:09:55 +08:00
void SetActor(Actor* actor);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 将物体信息更新到角色
2020-01-21 10:09:55 +08:00
void UpdateActor();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 将角色信息更新到物体
2020-01-21 10:09:55 +08:00
void UpdateFromActor();
b2Body* GetB2Body() const;
void SetB2Body(b2Body* body);
private:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 销毁物体
2020-01-21 10:09:55 +08:00
void UpdateFixtureFilter(b2Fixture* fixture);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 销毁物体
2020-01-21 10:09:55 +08:00
void Destroy();
private:
Actor* actor_;
World* world_;
b2Body* body_;
uint16_t category_bits_;
uint16_t mask_bits_;
int16_t group_index_;
2020-02-06 16:54:47 +08:00
Vector<FixturePtr> fixtures_;
2020-01-21 10:09:55 +08:00
};
/** @} */
2020-02-06 16:54:47 +08:00
inline BodyPtr Body::Create(World* world, ActorPtr actor, Type type)
{
return Body::Create(world, actor.get(), type);
}
2020-01-21 10:09:55 +08:00
inline bool Body::InitBody(World* world, ActorPtr actor)
{
return InitBody(world, actor.get());
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline FixtureList Body::GetFixtureList() const
{
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
2020-01-21 10:09:55 +08:00
inline ContactEdgeList Body::GetContactList() const
{
KGE_ASSERT(body_);
return ContactEdgeList(ContactEdge(body_->GetContactList()));
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline uint16_t Body::GetCategoryBits() const
{
return category_bits_;
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline uint16_t Body::GetMaskBits() const
{
return mask_bits_;
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline int16_t Body::GetGroupIndex() const
{
return group_index_;
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline float Body::GetBodyRotation() const
{
KGE_ASSERT(body_);
return math::Radian2Degree(body_->GetAngle());
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline void Body::SetBodyRotation(float angle)
{
SetBodyTransform(GetBodyPosition(), angle);
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline void Body::SetBodyPosition(Point const& pos)
{
SetBodyTransform(pos, GetBodyRotation());
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline float Body::GetMass() const
{
KGE_ASSERT(body_);
return body_->GetMass();
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline float Body::GetInertia() const
{
KGE_ASSERT(body_);
return body_->GetInertia();
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline Body::Type Body::GetType() const
{
KGE_ASSERT(body_);
return Type(body_->GetType());
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline void Body::SetType(Type type)
{
KGE_ASSERT(body_);
body_->SetType(static_cast<b2BodyType>(type));
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline float Body::GetGravityScale() const
{
KGE_ASSERT(body_);
return body_->GetGravityScale();
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline void Body::SetGravityScale(float scale)
{
KGE_ASSERT(body_);
body_->SetGravityScale(scale);
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline bool Body::IsIgnoreRotation() const
{
KGE_ASSERT(body_);
return body_->IsFixedRotation();
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline void Body::SetIgnoreRotation(bool flag)
{
KGE_ASSERT(body_);
body_->SetFixedRotation(flag);
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline bool Body::IsBullet() const
{
KGE_ASSERT(body_);
return body_->IsBullet();
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline void Body::SetBullet(bool flag)
{
KGE_ASSERT(body_);
body_->SetBullet(flag);
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline bool Body::IsAwake() const
{
KGE_ASSERT(body_);
return body_->IsAwake();
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline void Body::SetAwake(bool flag)
{
KGE_ASSERT(body_);
body_->SetAwake(flag);
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline bool Body::IsSleepingAllowed() const
{
KGE_ASSERT(body_);
return body_->IsSleepingAllowed();
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline void Body::SetSleepingAllowed(bool flag)
{
KGE_ASSERT(body_);
body_->SetSleepingAllowed(flag);
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline bool Body::IsActive() const
{
KGE_ASSERT(body_);
return body_->IsActive();
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline void Body::SetActive(bool flag)
{
KGE_ASSERT(body_);
body_->SetActive(flag);
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline Actor* Body::GetActor() const
{
return actor_;
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline void Body::SetActor(Actor* actor)
{
actor_ = actor;
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline b2Body* Body::GetB2Body() const
{
return body_;
}
2019-12-31 16:01:41 +08:00
2020-01-21 10:09:55 +08:00
inline World* Body::GetWorld() const
{
return world_;
2019-10-18 11:50:46 +08:00
}
2020-01-21 10:09:55 +08:00
} // namespace physics
} // namespace kiwano