Magic_Game/src/kiwano-physics/World.h

114 lines
3.2 KiB
C
Raw Normal View History

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>
2019-10-31 20:34:38 +08:00
#include <kiwano-physics/ContactListener.h>
2019-10-18 11:50:46 +08:00
namespace kiwano
{
namespace physics
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-10-30 23:12:18 +08:00
class KGE_API PhysicWorld
2019-10-18 11:50:46 +08:00
: public Stage
2019-10-31 20:34:38 +08:00
, public PhysicContactDispatcher
2019-10-18 11:50:46 +08:00
{
2019-10-30 23:12:18 +08:00
friend class PhysicBody;
friend class PhysicJoint;
2019-10-21 11:15:22 +08:00
2019-10-18 11:50:46 +08:00
public:
2019-10-30 23:12:18 +08:00
PhysicWorld();
2019-10-18 11:50:46 +08:00
2019-10-30 23:12:18 +08:00
virtual ~PhysicWorld();
2019-10-18 11:50:46 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
Vec2 GetGravity() const;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void SetGravity(Vec2 gravity);
// <20><>ȡȫ<C8A1><C8AB><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD>
2019-10-31 20:34:38 +08:00
inline float GetGlobalScale() const { return global_scale_; }
2019-10-18 11:50:46 +08:00
// <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD>
2019-10-31 20:34:38 +08:00
inline void SetGlobalScale(float scale) { global_scale_ = scale; }
2019-10-18 11:50:46 +08:00
// <20><>Ϸ<EFBFBD><CFB7><EFBFBD>絥λת<CEBB><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>絥λ
2019-10-31 20:34:38 +08:00
inline float World2Stage(float value) const { return value * GetGlobalScale(); }
inline Point World2Stage(const b2Vec2& pos) const { return Point(World2Stage(pos.x), World2Stage(pos.y)); }
2019-10-18 11:50:46 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>絥λת<CEBB><D7AA>Ϊ<EFBFBD><CEAA>Ϸ<EFBFBD><CFB7><EFBFBD>絥λ
2019-10-31 20:34:38 +08:00
inline float Stage2World(float value) const { return value / GetGlobalScale(); }
inline b2Vec2 Stage2World(const Point& pos) const { return b2Vec2(Stage2World(pos.x), Stage2World(pos.y)); }
2019-10-18 11:50:46 +08:00
2019-10-28 17:25:06 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶȵ<D9B6><C8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, Ĭ<><C4AC>Ϊ 6
2019-10-31 20:34:38 +08:00
inline void SetVelocityIterations(int vel_iter) { vel_iter_ = vel_iter; }
2019-10-18 11:50:46 +08:00
2019-10-28 17:25:06 +08:00
// <20><><EFBFBD><EFBFBD>λ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, Ĭ<><C4AC>Ϊ 2
2019-10-31 20:34:38 +08:00
inline void SetPositionIterations(int pos_iter) { pos_iter_ = pos_iter; }
2019-10-18 11:50:46 +08:00
b2World* GetB2World();
const b2World* GetB2World() const;
2019-10-31 20:34:38 +08:00
private:
2019-10-30 23:12:18 +08:00
// <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>
void RemoveBody(PhysicBody* body);
2019-10-21 11:15:22 +08:00
2019-10-30 23:12:18 +08:00
// <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-10-21 11:15:22 +08:00
void RemoveAllBodies();
2019-10-28 17:25:06 +08:00
// <20><><EFBFBD>ӹؽ<D3B9>
2019-10-30 23:12:18 +08:00
void AddJoint(PhysicJoint* joint);
2019-10-28 17:25:06 +08:00
2019-10-21 11:15:22 +08:00
// <20>Ƴ<EFBFBD><C6B3>ؽ<EFBFBD>
2019-10-30 23:12:18 +08:00
void RemoveJoint(PhysicJoint* joint);
2019-10-21 11:15:22 +08:00
// <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD>йؽ<D0B9>
void RemoveAllJoints();
2019-10-28 17:25:06 +08:00
// <20>ؽڱ<D8BD><DAB1>Ƴ<EFBFBD>
void JointRemoved(b2Joint* joint);
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-10-30 23:12:18 +08:00
Vector<PhysicJoint*> joints_;
2019-10-18 11:50:46 +08:00
};
2019-10-30 23:12:18 +08:00
KGE_DECLARE_SMART_PTR(PhysicWorld);
2019-10-18 11:50:46 +08:00
}
}