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
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
class KGE_API World
|
|
|
|
|
|
: public Stage
|
|
|
|
|
|
{
|
2019-10-21 11:15:22 +08:00
|
|
|
|
friend class Body;
|
|
|
|
|
|
friend class Joint;
|
|
|
|
|
|
|
2019-10-18 11:50:46 +08:00
|
|
|
|
public:
|
|
|
|
|
|
World();
|
|
|
|
|
|
|
|
|
|
|
|
World(Vec2 gravity);
|
|
|
|
|
|
|
|
|
|
|
|
virtual ~World();
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-10-21 11:15:22 +08:00
|
|
|
|
BodyPtr CreateBody(ActorPtr actor);
|
2019-10-18 11:50:46 +08:00
|
|
|
|
BodyPtr CreateBody(Actor* actor);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD>
|
|
|
|
|
|
JointPtr CreateJoint(b2JointDef* joint_def);
|
|
|
|
|
|
|
|
|
|
|
|
// <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>
|
|
|
|
|
|
inline float GetGlobalScale() const { return global_scale_; }
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD>
|
|
|
|
|
|
inline void SetGlobalScale(float scale) { global_scale_ = scale; }
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>Ϸ<EFBFBD><CFB7><EFBFBD>絥λת<CEBB><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>絥λ
|
|
|
|
|
|
inline float World2Stage(float value) { return value * GetGlobalScale(); }
|
|
|
|
|
|
inline Point World2Stage(const b2Vec2& pos) { return Point(World2Stage(pos.x), World2Stage(pos.y)); }
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>絥λת<CEBB><D7AA>Ϊ<EFBFBD><CEAA>Ϸ<EFBFBD><CFB7><EFBFBD>絥λ
|
|
|
|
|
|
inline float Stage2World(float value) { return value / GetGlobalScale(); }
|
|
|
|
|
|
inline b2Vec2 Stage2World(const Point& pos) { return b2Vec2(Stage2World(pos.x), Stage2World(pos.y)); }
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶȵ<D9B6><C8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
inline void SetVelocityIterations(int vel_iter) { vel_iter_ = vel_iter; }
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>λ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
inline void SetPositionIterations(int pos_iter) { pos_iter_ = pos_iter; }
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ Box2D <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
b2World* GetB2World();
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ Box2D <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
const b2World* GetB2World() const;
|
|
|
|
|
|
|
2019-10-21 11:15:22 +08:00
|
|
|
|
protected:
|
|
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
void RemoveBody(Body* body);
|
|
|
|
|
|
|
|
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD>и<EFBFBD><D0B8><EFBFBD>
|
|
|
|
|
|
void RemoveAllBodies();
|
|
|
|
|
|
|
|
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3>ؽ<EFBFBD>
|
|
|
|
|
|
void RemoveJoint(Joint* joint);
|
|
|
|
|
|
|
|
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD>йؽ<D0B9>
|
|
|
|
|
|
void RemoveAllJoints();
|
|
|
|
|
|
|
2019-10-18 11:50:46 +08:00
|
|
|
|
protected:
|
|
|
|
|
|
void Update(Duration dt) override;
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
b2World world_;
|
|
|
|
|
|
int vel_iter_;
|
|
|
|
|
|
int pos_iter_;
|
|
|
|
|
|
float global_scale_;
|
2019-10-21 11:15:22 +08:00
|
|
|
|
Vector<Body*> bodies_;
|
|
|
|
|
|
Vector<Joint*> joints_;
|
2019-10-18 11:50:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
KGE_DECLARE_SMART_PTR(World);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|