Magic_Game/src/kiwano-physics/PhysicWorld.h

186 lines
4.4 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/PhysicBody.h>
2019-10-18 11:50:46 +08:00
#include <kiwano-physics/Joint.h>
namespace kiwano
{
2020-01-21 10:09:55 +08:00
namespace physics
{
/**
* \~chinese
* \defgroup Physics <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>
2020-01-21 10:09:55 +08:00
*/
/**
* \addtogroup Physics
* @{
*/
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-01-21 10:09:55 +08:00
*/
class KGE_API PhysicWorld : public Component
2020-01-21 10:09:55 +08:00
{
friend class PhysicBody;
2020-01-21 10:09:55 +08:00
friend class Joint;
public:
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static PhysicWorldPtr Create();
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param gravity <20><><EFBFBD><EFBFBD>
static PhysicWorldPtr Create(const Vec2& gravity);
2020-01-21 10:09:55 +08:00
PhysicWorld();
2020-01-21 10:09:55 +08:00
virtual ~PhysicWorld();
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void AddBody(PhysicBodyPtr body);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>
void RemoveBody(PhysicBodyPtr body);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void RemoveAllBodies();
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
const List<PhysicBodyPtr>& GetAllBodies() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>ӹؽ<D3B9>
void AddJoint(JointPtr joint);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƴ<EFBFBD><C6B3>ؽ<EFBFBD>
void RemoveJoint(JointPtr joint);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD>йؽ<D0B9>
void RemoveAllJoints();
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD>йؽ<D0B9>
const List<JointPtr>& GetAllJoints() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> [N]
Vec2 GetGravity() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [N]
void SetGravity(Vec2 gravity);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><D3B4>б<EFBFBD>
ContactList GetContactList();
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ٶȵ<D9B6><C8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, Ĭ<><C4AC>Ϊ 6
void SetVelocityIterations(int vel_iter);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>λ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, Ĭ<><C4AC>Ϊ 2
void SetPositionIterations(int pos_iter);
2020-01-21 10:09:55 +08:00
2020-02-23 14:56:14 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD>Ϣ
2020-02-23 14:56:14 +08:00
void ShowDebugInfo(bool show);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡb2World
b2World* GetB2World();
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡb2World
const b2World* GetB2World() const;
2020-01-21 10:09:55 +08:00
protected:
2020-02-22 17:24:31 +08:00
/// \~chinese
/// @brief <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-02-22 17:24:31 +08:00
void InitComponent(Actor* actor) override;
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void OnUpdate(Duration dt) override;
2020-02-23 14:56:14 +08:00
/// \~chinese
/// @brief <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
2020-02-23 14:56:14 +08:00
void OnRender(RenderContext& ctx) override;
2020-02-22 17:24:31 +08:00
/// \~chinese
/// @brief <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
void DispatchEvent(Event* evt);
2020-02-22 17:24:31 +08:00
/// \~chinese
/// @brief <20>ؽ<EFBFBD><D8BD>Ƴ<EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
void JointRemoved(b2Joint* b2joint);
2020-01-21 10:09:55 +08:00
2020-02-22 17:24:31 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ
2020-02-22 17:24:31 +08:00
void BeforeSimulation(Actor* parent, const Matrix3x2& parent_to_world, float parent_rotation);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-02-22 17:24:31 +08:00
void AfterSimulation(Actor* parent, const Matrix3x2& parent_to_world, float parent_rotation);
2020-01-21 10:09:55 +08:00
private:
2020-02-23 14:56:14 +08:00
bool debug_;
2020-01-21 10:09:55 +08:00
int vel_iter_;
int pos_iter_;
2020-02-23 14:56:14 +08:00
b2World world_;
class DebugDrawer;
std::unique_ptr<DebugDrawer> drawer_;
2020-01-21 10:09:55 +08:00
List<PhysicBodyPtr> bodies_;
List<JointPtr> joints_;
2020-01-21 10:09:55 +08:00
std::unique_ptr<b2DestructionListener> destroy_listener_;
std::unique_ptr<b2ContactListener> contact_listener_;
2020-01-21 10:09:55 +08:00
};
/** @} */
inline void PhysicWorld::SetVelocityIterations(int vel_iter)
2020-01-21 10:09:55 +08:00
{
vel_iter_ = vel_iter;
}
inline void PhysicWorld::SetPositionIterations(int pos_iter)
2020-01-21 10:09:55 +08:00
{
pos_iter_ = pos_iter;
2019-10-18 11:50:46 +08:00
}
2020-01-21 10:09:55 +08:00
} // namespace physics
} // namespace kiwano