Magic_Game/src/kiwano-physics/World.h

198 lines
5.0 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
#include <kiwano-physics/Body.h>
#include <kiwano-physics/Joint.h>
namespace kiwano
{
2020-01-21 10:09:55 +08:00
namespace physics
{
KGE_DECLARE_SMART_PTR(World);
/**
* \~chinese
2020-02-14 22:01:56 +08:00
* \defgroup Physics
2020-01-21 10:09:55 +08:00
*/
/**
* \addtogroup Physics
* @{
*/
/**
* \~chinese
2020-02-10 17:32:04 +08:00
* @brief
2020-01-21 10:09:55 +08:00
*/
class KGE_API World : public Stage
{
friend class Body;
friend class Joint;
public:
World();
virtual ~World();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取重力 [N]
2020-01-21 10:09:55 +08:00
Vec2 GetGravity() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置重力 [N]
2020-01-21 10:09:55 +08:00
void SetGravity(Vec2 gravity);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取物理接触列表
2020-01-21 10:09:55 +08:00
ContactList GetContactList();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取全局缩放比例
/// @details 缩放比例是指由物理世界的单位米转换到屏幕像素的比例默认比例为1:100
2020-01-21 10:09:55 +08:00
float GetGlobalScale() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置全局缩放比例
/// @details 缩放比例是指由物理世界的单位米转换到屏幕像素的比例默认比例为1:100
2020-01-21 10:09:55 +08:00
void SetGlobalScale(float scale);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 游戏世界单位转换为物理世界单位
/// @details 根据全局缩放比例将物理世界的单位米转换为像素单位
2020-01-21 10:09:55 +08:00
float World2Stage(float value) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 游戏世界单位转换为物理世界单位
/// @details 根据全局缩放比例将物理世界的单位米转换为像素单位
2020-01-21 10:09:55 +08:00
Vec2 World2Stage(const b2Vec2& pos) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 物理世界单位转换为游戏世界单位
/// @details 根据全局缩放比例将像素单位转换为物理世界的单位米
2020-01-21 10:09:55 +08:00
float Stage2World(float value) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 物理世界单位转换为游戏世界单位
/// @details 根据全局缩放比例将像素单位转换为物理世界的单位米
2020-01-21 10:09:55 +08:00
b2Vec2 Stage2World(const Vec2& pos) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置速度迭代次数, 默认为 6
2020-01-21 10:09:55 +08:00
void SetVelocityIterations(int vel_iter);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置位置迭代次数, 默认为 2
2020-01-21 10:09:55 +08:00
void SetPositionIterations(int pos_iter);
b2World* GetB2World();
const b2World* GetB2World() const;
private:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 移除物体
2020-01-21 10:09:55 +08:00
void RemoveBody(Body* body);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 移除所有物体
2020-01-21 10:09:55 +08:00
void RemoveAllBodies();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加关节
2020-01-21 10:09:55 +08:00
void AddJoint(Joint* joint);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 移除关节
2020-01-21 10:09:55 +08:00
void RemoveJoint(Joint* joint);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 移除所有关节
2020-01-21 10:09:55 +08:00
void RemoveAllJoints();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 关节被移除
2020-01-21 10:09:55 +08:00
void JointRemoved(b2Joint* joint);
protected:
void Update(Duration dt) override;
private:
b2World world_;
int vel_iter_;
int pos_iter_;
float global_scale_;
class DestructionListener;
friend DestructionListener;
DestructionListener* destruction_listener_;
class ContactListener;
friend ContactListener;
ContactListener* contact_listener_;
bool removing_joint_;
Vector<Joint*> joints_;
};
/** @} */
inline float World::GetGlobalScale() const
{
return global_scale_;
}
inline void World::SetGlobalScale(float scale)
{
global_scale_ = scale;
}
inline float World::World2Stage(float value) const
{
return value * GetGlobalScale();
}
inline Vec2 World::World2Stage(const b2Vec2& pos) const
{
return Point(World2Stage(pos.x), World2Stage(pos.y));
}
inline float World::Stage2World(float value) const
{
return value / GetGlobalScale();
}
inline b2Vec2 World::Stage2World(const Vec2& pos) const
{
return b2Vec2(Stage2World(pos.x), Stage2World(pos.y));
}
inline void World::SetVelocityIterations(int vel_iter)
{
vel_iter_ = vel_iter;
}
inline void World::SetPositionIterations(int pos_iter)
{
pos_iter_ = pos_iter;
2019-10-18 11:50:46 +08:00
}
2020-01-21 10:09:55 +08:00
} // namespace physics
} // namespace kiwano