2019-10-18 11:50:46 +08:00
|
|
|
|
// Copyright (c) 2018-2019 Kiwano - Nomango
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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:
|
|
|
|
|
|
//
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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
|
|
|
|
|
|
{
|
|
|
|
|
|
namespace physics
|
|
|
|
|
|
{
|
2019-12-31 16:01:41 +08:00
|
|
|
|
KGE_DECLARE_SMART_PTR(World);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \~chinese
|
|
|
|
|
|
* \defgroup Physics <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \addtogroup Physics
|
|
|
|
|
|
* @{
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \~chinese
|
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
*/
|
2019-11-06 16:29:36 +08:00
|
|
|
|
class KGE_API World
|
2019-10-18 11:50:46 +08:00
|
|
|
|
: public Stage
|
|
|
|
|
|
{
|
2019-11-06 16:29:36 +08:00
|
|
|
|
friend class Body;
|
|
|
|
|
|
friend class Joint;
|
2019-10-21 11:15:22 +08:00
|
|
|
|
|
2019-10-18 11:50:46 +08:00
|
|
|
|
public:
|
2019-11-06 16:29:36 +08:00
|
|
|
|
World();
|
2019-10-18 11:50:46 +08:00
|
|
|
|
|
2019-11-06 16:29:36 +08:00
|
|
|
|
virtual ~World();
|
2019-10-18 11:50:46 +08:00
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> [N]
|
2019-10-18 11:50:46 +08:00
|
|
|
|
Vec2 GetGravity() const;
|
|
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [N]
|
2019-10-18 11:50:46 +08:00
|
|
|
|
void SetGravity(Vec2 gravity);
|
|
|
|
|
|
|
2020-01-06 00:15:47 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><D3B4>б<EFBFBD>
|
|
|
|
|
|
ContactList GetContactList();
|
|
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ȡȫ<C8A1><C8AB><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD>
|
|
|
|
|
|
/// @details <20><><EFBFBD>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>λ<EFBFBD><CEBB>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>صı<D8B5><C4B1><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϱ<EFBFBD><CFB1><EFBFBD>Ϊ1:100
|
|
|
|
|
|
float GetGlobalScale() const;
|
2019-10-18 11:50:46 +08:00
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD>
|
|
|
|
|
|
/// @details <20><><EFBFBD>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>λ<EFBFBD><CEBB>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>صı<D8B5><C4B1><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϱ<EFBFBD><CFB1><EFBFBD>Ϊ1:100
|
|
|
|
|
|
void SetGlobalScale(float scale);
|
2019-10-18 11:50:46 +08:00
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>Ϸ<EFBFBD><CFB7><EFBFBD>絥λת<CEBB><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>絥λ
|
|
|
|
|
|
/// @details <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>λ<EFBFBD><CEBB>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD>ص<EFBFBD>λ
|
|
|
|
|
|
float World2Stage(float value) const;
|
2019-10-18 11:50:46 +08:00
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>Ϸ<EFBFBD><CFB7><EFBFBD>絥λת<CEBB><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>絥λ
|
|
|
|
|
|
/// @details <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>λ<EFBFBD><CEBB>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD>ص<EFBFBD>λ
|
|
|
|
|
|
Vec2 World2Stage(const b2Vec2& pos) const;
|
2019-10-18 11:50:46 +08:00
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>絥λת<CEBB><D7AA>Ϊ<EFBFBD><CEAA>Ϸ<EFBFBD><CFB7><EFBFBD>絥λ
|
|
|
|
|
|
/// @details <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>λת<CEBB><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
|
float Stage2World(float value) const;
|
2019-10-18 11:50:46 +08:00
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>絥λת<CEBB><D7AA>Ϊ<EFBFBD><CEAA>Ϸ<EFBFBD><CFB7><EFBFBD>絥λ
|
|
|
|
|
|
/// @details <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>λת<CEBB><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
|
b2Vec2 Stage2World(const Vec2& pos) const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ٶȵ<D9B6><C8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, Ĭ<><C4AC>Ϊ 6
|
|
|
|
|
|
void SetVelocityIterations(int vel_iter);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD>λ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, Ĭ<><C4AC>Ϊ 2
|
|
|
|
|
|
void SetPositionIterations(int pos_iter);
|
2019-10-18 11:50:46 +08:00
|
|
|
|
|
|
|
|
|
|
b2World* GetB2World();
|
2019-12-31 16:01:41 +08:00
|
|
|
|
|
2019-10-18 11:50:46 +08:00
|
|
|
|
const b2World* GetB2World() const;
|
|
|
|
|
|
|
2019-10-31 20:34:38 +08:00
|
|
|
|
private:
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>
|
2019-11-06 16:29:36 +08:00
|
|
|
|
void RemoveBody(Body* body);
|
2019-10-21 11:15:22 +08:00
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-10-21 11:15:22 +08:00
|
|
|
|
void RemoveAllBodies();
|
|
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD>ӹؽ<D3B9>
|
2019-11-06 16:29:36 +08:00
|
|
|
|
void AddJoint(Joint* joint);
|
2019-10-28 17:25:06 +08:00
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20>Ƴ<EFBFBD><C6B3>ؽ<EFBFBD>
|
2019-11-06 16:29:36 +08:00
|
|
|
|
void RemoveJoint(Joint* joint);
|
2019-10-21 11:15:22 +08:00
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD>йؽ<D0B9>
|
2019-10-21 11:15:22 +08:00
|
|
|
|
void RemoveAllJoints();
|
|
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20>ؽڱ<D8BD><DAB1>Ƴ<EFBFBD>
|
2019-10-28 17:25:06 +08:00
|
|
|
|
void JointRemoved(b2Joint* joint);
|
|
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
protected:
|
2019-10-18 11:50:46 +08:00
|
|
|
|
void Update(Duration dt) override;
|
|
|
|
|
|
|
2019-10-31 20:34:38 +08:00
|
|
|
|
private:
|
2019-10-18 11:50:46 +08:00
|
|
|
|
b2World world_;
|
|
|
|
|
|
int vel_iter_;
|
|
|
|
|
|
int pos_iter_;
|
|
|
|
|
|
float global_scale_;
|
2019-10-31 20:34:38 +08:00
|
|
|
|
|
|
|
|
|
|
class DestructionListener;
|
|
|
|
|
|
friend DestructionListener;
|
2019-10-28 17:25:06 +08:00
|
|
|
|
DestructionListener* destruction_listener_;
|
|
|
|
|
|
|
2019-10-31 20:34:38 +08:00
|
|
|
|
class ContactListener;
|
|
|
|
|
|
friend ContactListener;
|
|
|
|
|
|
ContactListener* contact_listener_;
|
|
|
|
|
|
|
2019-10-28 17:25:06 +08:00
|
|
|
|
bool removing_joint_;
|
2019-11-06 16:29:36 +08:00
|
|
|
|
Vector<Joint*> joints_;
|
2019-10-18 11:50:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2019-12-31 16:01:41 +08:00
|
|
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|