Update physics

This commit is contained in:
Nomango 2019-10-30 23:12:18 +08:00
parent 7bb7dfa8a4
commit eb069a9fef
38 changed files with 653 additions and 554 deletions

View File

@ -26,84 +26,83 @@ namespace kiwano
namespace physics namespace physics
{ {
Body::Body() PhysicBody::PhysicBody()
: ignore_rotation_(false) : body_(nullptr)
, body_(nullptr)
, actor_(nullptr) , actor_(nullptr)
, world_(nullptr) , world_(nullptr)
{ {
} }
Body::Body(b2Body* body, Actor* actor) PhysicBody::PhysicBody(b2Body* body, Actor* actor)
: Body() : PhysicBody()
{ {
SetB2Body(body); SetB2Body(body);
SetActor(actor); SetActor(actor);
} }
Body::~Body() PhysicBody::PhysicBody(PhysicWorld* world, Actor* actor)
: PhysicBody()
{ {
if (world_) Init(world, actor);
{
world_->RemoveBody(this);
}
} }
BodyPtr Body::Create(World* world, Actor* actor) PhysicBody::~PhysicBody()
{ {
BodyPtr body = new Body; Destroy();
if (body)
{
body->world_ = world;
if (world)
{
b2BodyDef def;
b2Body* b2body = world->GetB2World()->CreateBody(&def);
body->SetB2Body(b2body);
}
body->SetActor(actor);
body->UpdateFromActor();
}
return body;
} }
Fixture Body::AddShape(Shape* shape, float density, float friction, float restitution) void PhysicBody::Init(PhysicWorld* world, Actor* actor)
{
KGE_ASSERT(world);
Destroy();
world_ = world;
b2BodyDef def;
b2Body* b2body = world->GetB2World()->CreateBody(&def);
SetB2Body(b2body);
SetActor(actor);
UpdateFromActor();
}
PhysicFixture PhysicBody::AddShape(PhysicShape* shape, float density, float friction, float restitution)
{ {
KGE_ASSERT(body_ && world_); KGE_ASSERT(body_ && world_);
if (shape) if (shape)
{ {
return Fixture::Create(this, shape, density, friction, restitution); return PhysicFixture::Create(this, shape, density, friction, restitution);
} }
return Fixture(); return PhysicFixture();
} }
Fixture Body::AddCircleShape(float radius, float density) PhysicFixture PhysicBody::AddCircleShape(float radius, float density)
{ {
return AddShape(&CircleShape(radius), density); return AddShape(&PhysicCircleShape(radius), density);
} }
Fixture Body::AddBoxShape(Vec2 const& size, float density) PhysicFixture PhysicBody::AddBoxShape(Vec2 const& size, float density)
{ {
return AddShape(&BoxShape(size), density); return AddShape(&PhysicBoxShape(size), density);
} }
Fixture Body::AddPolygonShape(Vector<Point> const& vertexs, float density) PhysicFixture PhysicBody::AddPolygonShape(Vector<Point> const& vertexs, float density)
{ {
return AddShape(&PolygonShape(vertexs), density); return AddShape(&PhysicPolygonShape(vertexs), density);
} }
Fixture Body::AddEdgeShape(Point const& p1, Point const& p2, float density) PhysicFixture PhysicBody::AddEdgeShape(Point const& p1, Point const& p2, float density)
{ {
return AddShape(&EdgeShape(p1, p2), density); return AddShape(&PhysicEdgeShape(p1, p2), density);
} }
Fixture Body::AddChainShape(Vector<Point> const& vertexs, bool loop, float density) PhysicFixture PhysicBody::AddChainShape(Vector<Point> const& vertexs, bool loop, float density)
{ {
return AddShape(&ChainShape(vertexs, loop), density); return AddShape(&PhysicChainShape(vertexs, loop), density);
} }
void Body::RemoveFixture(Fixture const& fixture) void PhysicBody::RemoveFixture(PhysicFixture const& fixture)
{ {
if (fixture.GetB2Fixture()) if (fixture.GetB2Fixture())
{ {
@ -112,43 +111,90 @@ namespace kiwano
} }
} }
Point Body::GetPosition() const void PhysicBody::GetMassData(float* mass, Point* center, float* inertia)
{
KGE_ASSERT(body_ && world_);
b2MassData data;
body_->GetMassData(&data);
if (mass) *mass = data.mass;
if (center) *center = world_->World2Stage(data.center);
if (inertia) *inertia = data.I;
}
void PhysicBody::SetMassData(float mass, Point const& center, float inertia)
{
KGE_ASSERT(body_ && world_);
b2MassData data;
data.mass = mass;
data.center = world_->Stage2World(center);
data.I = inertia;
body_->SetMassData(&data);
}
void PhysicBody::ResetMassData()
{
KGE_ASSERT(body_);
body_->ResetMassData();
}
Point PhysicBody::GetBodyPosition() const
{ {
KGE_ASSERT(body_ && world_); KGE_ASSERT(body_ && world_);
return world_->World2Stage(body_->GetPosition()); return world_->World2Stage(body_->GetPosition());
} }
Point Body::GetLocalPoint(Point const& world) const void PhysicBody::SetBodyTransform(Point const& pos, float angle)
{
KGE_ASSERT(body_ && world_);
body_->SetTransform(world_->Stage2World(pos), math::Degree2Radian(angle));
}
Point PhysicBody::GetLocalPoint(Point const& world) const
{ {
KGE_ASSERT(body_ && world_); KGE_ASSERT(body_ && world_);
return world_->World2Stage(body_->GetLocalPoint(world_->Stage2World(world))); return world_->World2Stage(body_->GetLocalPoint(world_->Stage2World(world)));
} }
Point Body::GetWorldPoint(Point const& local) const Point PhysicBody::GetWorldPoint(Point const& local) const
{ {
KGE_ASSERT(body_ && world_); KGE_ASSERT(body_ && world_);
return world_->World2Stage(body_->GetWorldPoint(world_->Stage2World(local))); return world_->World2Stage(body_->GetWorldPoint(world_->Stage2World(local)));
} }
void Body::ApplyForce(Vec2 const& force, Point const& point, bool wake) Point PhysicBody::GetLocalCenter() const
{
KGE_ASSERT(body_ && world_);
return world_->World2Stage(body_->GetLocalCenter());
}
Point PhysicBody::GetWorldCenter() const
{
KGE_ASSERT(body_ && world_);
return world_->World2Stage(body_->GetWorldCenter());
}
void PhysicBody::ApplyForce(Vec2 const& force, Point const& point, bool wake)
{ {
KGE_ASSERT(body_ && world_); KGE_ASSERT(body_ && world_);
body_->ApplyForce(b2Vec2(force.x, force.y), world_->Stage2World(point), wake); body_->ApplyForce(b2Vec2(force.x, force.y), world_->Stage2World(point), wake);
} }
void Body::ApplyForceToCenter(Vec2 const& force, bool wake) void PhysicBody::ApplyForceToCenter(Vec2 const& force, bool wake)
{ {
KGE_ASSERT(body_ && world_); KGE_ASSERT(body_ && world_);
body_->ApplyForceToCenter(b2Vec2(force.x, force.y), wake); body_->ApplyForceToCenter(b2Vec2(force.x, force.y), wake);
} }
void Body::ApplyTorque(float torque, bool wake) void PhysicBody::ApplyTorque(float torque, bool wake)
{ {
KGE_ASSERT(body_ && world_); KGE_ASSERT(body_ && world_);
body_->ApplyTorque(torque, wake); body_->ApplyTorque(torque, wake);
} }
void Body::SetB2Body(b2Body* body) void PhysicBody::SetB2Body(b2Body* body)
{ {
body_ = body; body_ = body;
if (body_) if (body_)
@ -157,7 +203,19 @@ namespace kiwano
} }
} }
void Body::UpdateActor() void PhysicBody::Destroy()
{
if (world_)
{
world_->RemoveBody(this);
}
body_ = nullptr;
world_ = nullptr;
actor_ = nullptr;
}
void PhysicBody::UpdateActor()
{ {
if (actor_ && body_) if (actor_ && body_)
{ {
@ -169,32 +227,26 @@ namespace kiwano
{ {
actor_->SetPosition(World2Stage(body_->GetPosition())); actor_->SetPosition(World2Stage(body_->GetPosition()));
} }
actor_->SetRotation(math::Radian2Degree(body_->GetAngle()));
if (!ignore_rotation_)
{
actor_->SetRotation(math::Radian2Angle(body_->GetAngle()));
}
} }
} }
void Body::UpdateFromActor() void PhysicBody::UpdateFromActor()
{ {
if (actor_ && body_) if (actor_ && body_)
{ {
float rotation = ignore_rotation_ ? body_->GetAngle() : math::Angle2Radian(actor_->GetRotation());
if (world_) if (world_)
{ {
body_->SetTransform( body_->SetTransform(
world_->Stage2World(actor_->GetPosition()), world_->Stage2World(actor_->GetPosition()),
rotation math::Degree2Radian(actor_->GetRotation())
); );
} }
else else
{ {
body_->SetTransform( body_->SetTransform(
Stage2World(actor_->GetPosition()), Stage2World(actor_->GetPosition()),
rotation math::Degree2Radian(actor_->GetRotation())
); );
} }
} }

View File

@ -27,12 +27,12 @@ namespace kiwano
{ {
namespace physics namespace physics
{ {
class World; class PhysicWorld;
// 먼竟 // 物体
KGE_DECLARE_SMART_PTR(Body); KGE_DECLARE_SMART_PTR(PhysicBody);
class KGE_API Body class KGE_API PhysicBody
: public ObjectBase : public virtual RefCounter
{ {
public: public:
enum class Type enum class Type
@ -42,48 +42,91 @@ namespace kiwano
Dynamic, Dynamic,
}; };
Body(); PhysicBody();
Body(b2Body* body, Actor* actor); PhysicBody(b2Body* body, Actor* actor);
virtual ~Body(); PhysicBody(PhysicWorld* world, Actor* actor);
PhysicBody(PhysicWorld* world, ActorPtr actor) : PhysicBody(world, actor.get()) {}
virtual ~PhysicBody();
static BodyPtr Create(World* world, Actor* actor); // 初始化
void Init(PhysicWorld* world, Actor* actor);
// Ìí¼ÓÐÎ×´ // Ìí¼ÓÐÎ×´
Fixture AddShape(Shape* shape, float density = 0.f, float friction = 0.2f, float restitution = 0.f); PhysicFixture AddShape(PhysicShape* shape, float density = 0.f, float friction = 0.2f, float restitution = 0.f);
Fixture AddCircleShape(float radius, float density = 0.f); PhysicFixture AddCircleShape(float radius, float density = 0.f);
Fixture AddBoxShape(Vec2 const& size, float density = 0.f); PhysicFixture AddBoxShape(Vec2 const& size, float density = 0.f);
Fixture AddPolygonShape(Vector<Point> const& vertexs, float density = 0.f); PhysicFixture AddPolygonShape(Vector<Point> const& vertexs, float density = 0.f);
Fixture AddEdgeShape(Point const& p1, Point const& p2, float density = 0.f); PhysicFixture AddEdgeShape(Point const& p1, Point const& p2, float density = 0.f);
Fixture AddChainShape(Vector<Point> const& vertexs, bool loop, float density = 0.f); PhysicFixture AddChainShape(Vector<Point> const& vertexs, bool loop, float density = 0.f);
// 삿혤셸야죗깊 // 获取夹具
Fixture GetFixture() const { KGE_ASSERT(body_); Fixture(body_->GetFixtureList()); } PhysicFixture GetFixture() const { KGE_ASSERT(body_); PhysicFixture(body_->GetFixtureList()); }
// ÒÆ³ý¼Ð¾ß // ÒÆ³ý¼Ð¾ß
void RemoveFixture(Fixture const& fixture); void RemoveFixture(PhysicFixture const& fixture);
// 삿혤醴좆 // 旋转角度
float GetBodyRotation() const { KGE_ASSERT(body_); return math::Radian2Degree(body_->GetAngle()); }
void SetBodyRotation(float angle) { SetBodyTransform(GetBodyPosition(), angle); }
// 位置
Point GetBodyPosition() const;
void SetBodyPosition(Point const& pos) { SetBodyTransform(pos, GetBodyRotation()); }
// 位置和旋转变换
void SetBodyTransform(Point const& pos, float angle);
// 质量
float GetMass() const { KGE_ASSERT(body_); return body_->GetMass(); } float GetMass() const { KGE_ASSERT(body_); return body_->GetMass(); }
// 삿혤旗瘻실똑 // 惯性
float GetRotation() const { KGE_ASSERT(body_); return math::Radian2Angle(body_->GetAngle()); } float GetInertia() const { KGE_ASSERT(body_); return body_->GetInertia(); }
// 삿혤貫零 // 质量数据
Point GetPosition() const; void GetMassData(float* mass, Point* center, float* inertia);
void SetMassData(float mass, Point const& center, float inertia);
void ResetMassData();
// 坐标转换
Point GetLocalPoint(Point const& world) const; Point GetLocalPoint(Point const& world) const;
Point GetWorldPoint(Point const& local) const; Point GetWorldPoint(Point const& local) const;
// 质心坐标
Point GetLocalCenter() const;
Point GetWorldCenter() const;
// 物体类型
Type GetType() const { KGE_ASSERT(body_); return Type(body_->GetType()); } Type GetType() const { KGE_ASSERT(body_); return Type(body_->GetType()); }
void SetType(Type type) { KGE_ASSERT(body_); body_->SetType(static_cast<b2BodyType>(type)); } void SetType(Type type) { KGE_ASSERT(body_); body_->SetType(static_cast<b2BodyType>(type)); }
// 重力因子
float GetGravityScale() const { KGE_ASSERT(body_); return body_->GetGravityScale(); }
void SetGravityScale(float scale) { KGE_ASSERT(body_); body_->SetGravityScale(scale); }
// 施力
void ApplyForce(Vec2 const& force, Point const& point, bool wake = true); void ApplyForce(Vec2 const& force, Point const& point, bool wake = true);
void ApplyForceToCenter(Vec2 const& force, bool wake = true); void ApplyForceToCenter(Vec2 const& force, bool wake = true);
// 施加扭矩
void ApplyTorque(float torque, bool wake = true); void ApplyTorque(float torque, bool wake = true);
bool IsIgnoreRotation() const { return ignore_rotation_; } // 固定旋转
void SetIgnoreRotation(bool ignore_rotation) { ignore_rotation_ = ignore_rotation; } bool IsIgnoreRotation() const { KGE_ASSERT(body_); return body_->IsFixedRotation(); }
void SetIgnoreRotation(bool flag) { KGE_ASSERT(body_); body_->SetFixedRotation(flag); }
// 子弹
bool IsBullet() const { KGE_ASSERT(body_); return body_->IsBullet(); }
void SetBullet(bool flag) { KGE_ASSERT(body_); body_->SetBullet(flag); }
// 休眠
bool IsAwake() const { KGE_ASSERT(body_); return body_->IsAwake(); }
void SetAwake(bool flag) { KGE_ASSERT(body_); body_->SetAwake(flag); }
bool IsSleepingAllowed() const { KGE_ASSERT(body_); return body_->IsSleepingAllowed(); }
void SetSleepingAllowed(bool flag) { KGE_ASSERT(body_); body_->SetSleepingAllowed(flag); }
// 活动状态
bool IsActive() const { KGE_ASSERT(body_); return body_->IsActive(); }
void SetActive(bool flag) { KGE_ASSERT(body_); body_->SetActive(flag); }
Actor* GetActor() const { return actor_; } Actor* GetActor() const { return actor_; }
void SetActor(Actor* actor) { actor_ = actor; } void SetActor(Actor* actor) { actor_ = actor; }
@ -92,16 +135,17 @@ namespace kiwano
const b2Body* GetB2Body() const { return body_; } const b2Body* GetB2Body() const { return body_; }
void SetB2Body(b2Body* body); void SetB2Body(b2Body* body);
World* GetWorld() { return world_; } PhysicWorld* GetWorld() { return world_; }
const World* GetWorld() const { return world_; } const PhysicWorld* GetWorld() const { return world_; }
void Destroy();
void UpdateActor(); void UpdateActor();
void UpdateFromActor(); void UpdateFromActor();
protected: protected:
bool ignore_rotation_;
Actor* actor_; Actor* actor_;
World* world_; PhysicWorld* world_;
b2Body* body_; b2Body* body_;
}; };
} }

View File

@ -27,18 +27,18 @@ namespace kiwano
namespace physics namespace physics
{ {
Fixture::Fixture() PhysicFixture::PhysicFixture()
: fixture_(nullptr) : fixture_(nullptr)
{ {
} }
Fixture::Fixture(b2Fixture* fixture) PhysicFixture::PhysicFixture(b2Fixture* fixture)
: Fixture() : PhysicFixture()
{ {
SetB2Fixture(fixture); SetB2Fixture(fixture);
} }
Fixture Fixture::Create(Body* body, Shape* shape, float density, float friction, float restitution) PhysicFixture PhysicFixture::Create(PhysicBody* body, PhysicShape* shape, float density, float friction, float restitution)
{ {
KGE_ASSERT(body); KGE_ASSERT(body);
@ -53,21 +53,21 @@ namespace kiwano
fd.restitution = restitution; fd.restitution = restitution;
fd.shape = shape->GetB2Shape(); fd.shape = shape->GetB2Shape();
auto fixture = b2body->CreateFixture(&fd); auto fixture = b2body->CreateFixture(&fd);
return Fixture(fixture); return PhysicFixture(fixture);
} }
return Fixture(); return PhysicFixture();
} }
Shape Fixture::GetShape() const PhysicShape PhysicFixture::GetShape() const
{ {
KGE_ASSERT(fixture_); KGE_ASSERT(fixture_);
return Shape(fixture_->GetShape()); return PhysicShape(fixture_->GetShape());
} }
Fixture Fixture::GetNext() const PhysicFixture PhysicFixture::GetNext() const
{ {
KGE_ASSERT(fixture_); KGE_ASSERT(fixture_);
return Fixture(fixture_->GetNext()); return PhysicFixture(fixture_->GetNext());
} }
} }

View File

@ -26,19 +26,19 @@ namespace kiwano
{ {
namespace physics namespace physics
{ {
class Body; class PhysicBody;
// 셸야 // 셸야
class Fixture class PhysicFixture
{ {
public: public:
Fixture(); PhysicFixture();
Fixture(b2Fixture* fixture); PhysicFixture(b2Fixture* fixture);
static Fixture Create(Body* body, Shape* shape, float density = 0.f, float friction = 0.2f, float restitution = 0.f); static PhysicFixture Create(PhysicBody* body, PhysicShape* shape, float density = 0.f, float friction = 0.2f, float restitution = 0.f);
Shape GetShape() const; PhysicShape GetShape() const;
Fixture GetNext() const; PhysicFixture GetNext() const;
bool IsValid() const { return !!fixture_; } bool IsValid() const { return !!fixture_; }

View File

@ -26,24 +26,37 @@ namespace kiwano
namespace physics namespace physics
{ {
// //
// Joint // PhysicJoint
// //
Joint::Joint() PhysicJoint::PhysicJoint()
: joint_(nullptr) : joint_(nullptr)
, world_(nullptr) , world_(nullptr)
, type_(Type::Unknown) , type_(Type::Unknown)
{ {
} }
Joint::Joint(b2Joint* joint) PhysicJoint::PhysicJoint(b2Joint* joint)
: Joint() : PhysicJoint()
{ {
SetB2Joint(joint); SetB2Joint(joint);
} }
Joint::Joint(World* world, b2JointDef* joint_def) PhysicJoint::PhysicJoint(PhysicWorld* world, b2JointDef* joint_def)
: Joint() : PhysicJoint()
{
Init(world, joint_def);
}
PhysicJoint::~PhysicJoint()
{
if (world_)
{
world_->RemoveJoint(this);
}
}
void PhysicJoint::Init(PhysicWorld* world, b2JointDef* joint_def)
{ {
world_ = world; world_ = world;
if (world_) if (world_)
@ -55,36 +68,28 @@ namespace kiwano
} }
} }
Joint::~Joint() PhysicBodyPtr PhysicJoint::GetBodyA() const
{
if (world_)
{
world_->RemoveJoint(this);
}
}
BodyPtr Joint::GetBodyA() const
{ {
KGE_ASSERT(joint_); KGE_ASSERT(joint_);
b2Body* body = joint_->GetBodyA(); b2Body* body = joint_->GetBodyA();
return BodyPtr(static_cast<Body*>(body->GetUserData())); return PhysicBodyPtr(static_cast<PhysicBody*>(body->GetUserData()));
} }
BodyPtr Joint::GetBodyB() const PhysicBodyPtr PhysicJoint::GetBodyB() const
{ {
KGE_ASSERT(joint_); KGE_ASSERT(joint_);
b2Body* body = joint_->GetBodyB(); b2Body* body = joint_->GetBodyB();
return BodyPtr(static_cast<Body*>(body->GetUserData())); return PhysicBodyPtr(static_cast<PhysicBody*>(body->GetUserData()));
} }
void Joint::SetB2Joint(b2Joint* joint) void PhysicJoint::SetB2Joint(b2Joint* joint)
{ {
joint_ = joint; joint_ = joint;
if (joint_) if (joint_)
{ {
type_ = Joint::Type(joint_->GetType()); type_ = PhysicJoint::Type(joint_->GetType());
} }
} }
@ -93,18 +98,20 @@ namespace kiwano
// //
DistanceJoint::DistanceJoint() DistanceJoint::DistanceJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
DistanceJoint::DistanceJoint(World* world, b2DistanceJointDef* def) DistanceJoint::DistanceJoint(PhysicWorld* world, b2DistanceJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
DistanceJointPtr DistanceJoint::Create(World* world, DistanceJoint::Param const& param) DistanceJoint::DistanceJoint(PhysicWorld* world, DistanceJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.body_a && param.body_b); KGE_ASSERT(param.body_a && param.body_b);
@ -113,9 +120,8 @@ namespace kiwano
def.frequencyHz = param.frequency_hz; def.frequencyHz = param.frequency_hz;
def.dampingRatio = param.damping_ratio; def.dampingRatio = param.damping_ratio;
DistanceJointPtr joint = new DistanceJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2DistanceJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2DistanceJoint*>(GetB2Joint());
return joint;
} }
void DistanceJoint::SetLength(float length) void DistanceJoint::SetLength(float length)
@ -135,18 +141,20 @@ namespace kiwano
// //
FrictionJoint::FrictionJoint() FrictionJoint::FrictionJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
FrictionJoint::FrictionJoint(World* world, b2FrictionJointDef* def) FrictionJoint::FrictionJoint(PhysicWorld* world, b2FrictionJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
FrictionJointPtr FrictionJoint::Create(World* world, FrictionJoint::Param const& param) FrictionJoint::FrictionJoint(PhysicWorld* world, FrictionJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.body_a && param.body_b); KGE_ASSERT(param.body_a && param.body_b);
@ -155,9 +163,8 @@ namespace kiwano
def.maxForce = param.max_force; def.maxForce = param.max_force;
def.maxTorque = world->Stage2World(param.max_torque); def.maxTorque = world->Stage2World(param.max_torque);
FrictionJointPtr joint = new FrictionJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2FrictionJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2FrictionJoint*>(GetB2Joint());
return joint;
} }
void FrictionJoint::SetMaxForce(float length) void FrictionJoint::SetMaxForce(float length)
@ -189,18 +196,20 @@ namespace kiwano
// //
GearJoint::GearJoint() GearJoint::GearJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
GearJoint::GearJoint(World* world, b2GearJointDef* def) GearJoint::GearJoint(PhysicWorld* world, b2GearJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
GearJointPtr GearJoint::Create(World* world, GearJoint::Param const& param) GearJoint::GearJoint(PhysicWorld* world, GearJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.joint_a && param.joint_b); KGE_ASSERT(param.joint_a && param.joint_b);
@ -209,9 +218,8 @@ namespace kiwano
def.joint2 = param.joint_b->GetB2Joint(); def.joint2 = param.joint_b->GetB2Joint();
def.ratio = param.ratio; def.ratio = param.ratio;
GearJointPtr joint = new GearJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2GearJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2GearJoint*>(GetB2Joint());
return joint;
} }
void GearJoint::SetRatio(float ratio) void GearJoint::SetRatio(float ratio)
@ -231,18 +239,20 @@ namespace kiwano
// //
MotorJoint::MotorJoint() MotorJoint::MotorJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
MotorJoint::MotorJoint(World* world, b2MotorJointDef* def) MotorJoint::MotorJoint(PhysicWorld* world, b2MotorJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
MotorJointPtr MotorJoint::Create(World* world, MotorJoint::Param const& param) MotorJoint::MotorJoint(PhysicWorld* world, MotorJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.body_a && param.body_b); KGE_ASSERT(param.body_a && param.body_b);
@ -251,9 +261,8 @@ namespace kiwano
def.maxForce = param.max_force; def.maxForce = param.max_force;
def.maxTorque = world->Stage2World(param.max_torque); def.maxTorque = world->Stage2World(param.max_torque);
MotorJointPtr joint = new MotorJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2MotorJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2MotorJoint*>(GetB2Joint());
return joint;
} }
void MotorJoint::SetMaxForce(float length) void MotorJoint::SetMaxForce(float length)
@ -285,18 +294,20 @@ namespace kiwano
// //
PrismaticJoint::PrismaticJoint() PrismaticJoint::PrismaticJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
PrismaticJoint::PrismaticJoint(World* world, b2PrismaticJointDef* def) PrismaticJoint::PrismaticJoint(PhysicWorld* world, b2PrismaticJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
PrismaticJointPtr PrismaticJoint::Create(World* world, PrismaticJoint::Param const& param) PrismaticJoint::PrismaticJoint(PhysicWorld* world, PrismaticJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.body_a && param.body_b); KGE_ASSERT(param.body_a && param.body_b);
@ -309,9 +320,8 @@ namespace kiwano
def.maxMotorForce = param.max_motor_force; def.maxMotorForce = param.max_motor_force;
def.motorSpeed = world->Stage2World(param.motor_speed); def.motorSpeed = world->Stage2World(param.motor_speed);
PrismaticJointPtr joint = new PrismaticJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2PrismaticJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2PrismaticJoint*>(GetB2Joint());
return joint;
} }
float PrismaticJoint::GetJointTranslation() const float PrismaticJoint::GetJointTranslation() const
@ -349,18 +359,20 @@ namespace kiwano
// //
PulleyJoint::PulleyJoint() PulleyJoint::PulleyJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
PulleyJoint::PulleyJoint(World* world, b2PulleyJointDef* def) PulleyJoint::PulleyJoint(PhysicWorld* world, b2PulleyJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
PulleyJointPtr PulleyJoint::Create(World* world, PulleyJoint::Param const& param) PulleyJoint::PulleyJoint(PhysicWorld* world, PulleyJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.body_a && param.body_b); KGE_ASSERT(param.body_a && param.body_b);
@ -368,9 +380,8 @@ namespace kiwano
def.Initialize(param.body_a->GetB2Body(), param.body_b->GetB2Body(), world->Stage2World(param.ground_anchor_a), world->Stage2World(param.ground_anchor_b), def.Initialize(param.body_a->GetB2Body(), param.body_b->GetB2Body(), world->Stage2World(param.ground_anchor_a), world->Stage2World(param.ground_anchor_b),
world->Stage2World(param.anchor_a), world->Stage2World(param.anchor_b), param.ratio); world->Stage2World(param.anchor_a), world->Stage2World(param.anchor_b), param.ratio);
PulleyJointPtr joint = new PulleyJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2PulleyJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2PulleyJoint*>(GetB2Joint());
return joint;
} }
Point PulleyJoint::GetGroundAnchorA() const Point PulleyJoint::GetGroundAnchorA() const
@ -420,63 +431,64 @@ namespace kiwano
// //
RevoluteJoint::RevoluteJoint() RevoluteJoint::RevoluteJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
RevoluteJoint::RevoluteJoint(World* world, b2RevoluteJointDef* def) RevoluteJoint::RevoluteJoint(PhysicWorld* world, b2RevoluteJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
RevoluteJointPtr RevoluteJoint::Create(World* world, RevoluteJoint::Param const& param) RevoluteJoint::RevoluteJoint(PhysicWorld* world, RevoluteJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.body_a && param.body_b); KGE_ASSERT(param.body_a && param.body_b);
b2RevoluteJointDef def; b2RevoluteJointDef def;
def.Initialize(param.body_a->GetB2Body(), param.body_b->GetB2Body(), world->Stage2World(param.anchor)); def.Initialize(param.body_a->GetB2Body(), param.body_b->GetB2Body(), world->Stage2World(param.anchor));
def.enableLimit = param.enable_limit; def.enableLimit = param.enable_limit;
def.lowerAngle = math::Angle2Radian(param.lower_angle); def.lowerAngle = math::Degree2Radian(param.lower_angle);
def.upperAngle = math::Angle2Radian(param.upper_angle); def.upperAngle = math::Degree2Radian(param.upper_angle);
def.enableMotor = param.enable_motor; def.enableMotor = param.enable_motor;
def.maxMotorTorque = world->Stage2World(param.max_motor_torque); def.maxMotorTorque = world->Stage2World(param.max_motor_torque);
def.motorSpeed = math::Angle2Radian(param.motor_speed); def.motorSpeed = math::Degree2Radian(param.motor_speed);
RevoluteJointPtr joint = new RevoluteJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2RevoluteJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2RevoluteJoint*>(GetB2Joint());
return joint;
} }
float RevoluteJoint::GetJointAngle() const float RevoluteJoint::GetJointAngle() const
{ {
KGE_ASSERT(raw_joint_ && world_); KGE_ASSERT(raw_joint_ && world_);
return math::Radian2Angle(raw_joint_->GetJointAngle()); return math::Radian2Degree(raw_joint_->GetJointAngle());
} }
float RevoluteJoint::GetJointSpeed() const float RevoluteJoint::GetJointSpeed() const
{ {
KGE_ASSERT(raw_joint_ && world_); KGE_ASSERT(raw_joint_ && world_);
return math::Radian2Angle(raw_joint_->GetJointSpeed()); return math::Radian2Degree(raw_joint_->GetJointSpeed());
} }
float RevoluteJoint::GetLowerLimit() const float RevoluteJoint::GetLowerLimit() const
{ {
KGE_ASSERT(raw_joint_ && world_); KGE_ASSERT(raw_joint_ && world_);
return math::Radian2Angle(raw_joint_->GetLowerLimit()); return math::Radian2Degree(raw_joint_->GetLowerLimit());
} }
float RevoluteJoint::GetUpperLimit() const float RevoluteJoint::GetUpperLimit() const
{ {
KGE_ASSERT(raw_joint_ && world_); KGE_ASSERT(raw_joint_ && world_);
return math::Radian2Angle(raw_joint_->GetUpperLimit()); return math::Radian2Degree(raw_joint_->GetUpperLimit());
} }
void RevoluteJoint::SetLimits(float lower, float upper) void RevoluteJoint::SetLimits(float lower, float upper)
{ {
KGE_ASSERT(raw_joint_ && world_); KGE_ASSERT(raw_joint_ && world_);
raw_joint_->SetLimits(math::Angle2Radian(lower), math::Angle2Radian(upper)); raw_joint_->SetLimits(math::Degree2Radian(lower), math::Degree2Radian(upper));
} }
void RevoluteJoint::SetMaxMotorTorque(float torque) void RevoluteJoint::SetMaxMotorTorque(float torque)
@ -496,18 +508,20 @@ namespace kiwano
// //
RopeJoint::RopeJoint() RopeJoint::RopeJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
RopeJoint::RopeJoint(World* world, b2RopeJointDef* def) RopeJoint::RopeJoint(PhysicWorld* world, b2RopeJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
RopeJointPtr RopeJoint::Create(World* world, RopeJoint::Param const& param) RopeJoint::RopeJoint(PhysicWorld* world, RopeJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.body_a && param.body_b); KGE_ASSERT(param.body_a && param.body_b);
@ -518,9 +532,8 @@ namespace kiwano
def.localAnchorB = world->Stage2World(param.local_anchor_b); def.localAnchorB = world->Stage2World(param.local_anchor_b);
def.maxLength = world->Stage2World(param.max_length); def.maxLength = world->Stage2World(param.max_length);
RopeJointPtr joint = new RopeJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2RopeJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2RopeJoint*>(GetB2Joint());
return joint;
} }
void RopeJoint::SetMaxLength(float length) void RopeJoint::SetMaxLength(float length)
@ -540,18 +553,20 @@ namespace kiwano
// //
WeldJoint::WeldJoint() WeldJoint::WeldJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
WeldJoint::WeldJoint(World* world, b2WeldJointDef* def) WeldJoint::WeldJoint(PhysicWorld* world, b2WeldJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
WeldJointPtr WeldJoint::Create(World* world, WeldJoint::Param const& param) WeldJoint::WeldJoint(PhysicWorld* world, WeldJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.body_a && param.body_b); KGE_ASSERT(param.body_a && param.body_b);
@ -560,9 +575,8 @@ namespace kiwano
def.frequencyHz = param.frequency_hz; def.frequencyHz = param.frequency_hz;
def.dampingRatio = param.damping_ratio; def.dampingRatio = param.damping_ratio;
WeldJointPtr joint = new WeldJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2WeldJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2WeldJoint*>(GetB2Joint());
return joint;
} }
// //
@ -570,18 +584,20 @@ namespace kiwano
// //
WheelJoint::WheelJoint() WheelJoint::WheelJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
WheelJoint::WheelJoint(World* world, b2WheelJointDef* def) WheelJoint::WheelJoint(PhysicWorld* world, b2WheelJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
WheelJointPtr WheelJoint::Create(World* world, WheelJoint::Param const& param) WheelJoint::WheelJoint(PhysicWorld* world, WheelJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.body_a && param.body_b); KGE_ASSERT(param.body_a && param.body_b);
@ -593,9 +609,8 @@ namespace kiwano
def.frequencyHz = param.frequency_hz; def.frequencyHz = param.frequency_hz;
def.dampingRatio = param.damping_ratio; def.dampingRatio = param.damping_ratio;
WheelJointPtr joint = new WheelJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2WheelJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2WheelJoint*>(GetB2Joint());
return joint;
} }
float WheelJoint::GetJointTranslation() const float WheelJoint::GetJointTranslation() const
@ -627,18 +642,20 @@ namespace kiwano
// //
MouseJoint::MouseJoint() MouseJoint::MouseJoint()
: Joint() : PhysicJoint()
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
MouseJoint::MouseJoint(World* world, b2MouseJointDef* def) MouseJoint::MouseJoint(PhysicWorld* world, b2MouseJointDef* def)
: Joint(world, def) : PhysicJoint(world, def)
, raw_joint_(nullptr) , raw_joint_(nullptr)
{ {
} }
MouseJointPtr MouseJoint::Create(World* world, MouseJoint::Param const& param) MouseJoint::MouseJoint(PhysicWorld* world, MouseJoint::Param const& param)
: PhysicJoint()
, raw_joint_(nullptr)
{ {
KGE_ASSERT(param.body_a && param.body_b); KGE_ASSERT(param.body_a && param.body_b);
@ -650,9 +667,8 @@ namespace kiwano
def.frequencyHz = param.frequency_hz; def.frequencyHz = param.frequency_hz;
def.dampingRatio = param.damping_ratio; def.dampingRatio = param.damping_ratio;
MouseJointPtr joint = new MouseJoint(world, &def); Init(world, &def);
joint->raw_joint_ = static_cast<b2MouseJoint*>(joint->GetB2Joint()); raw_joint_ = static_cast<b2MouseJoint*>(GetB2Joint());
return joint;
} }
void MouseJoint::SetMaxForce(float length) void MouseJoint::SetMaxForce(float length)

View File

@ -26,7 +26,7 @@ namespace kiwano
{ {
namespace physics namespace physics
{ {
KGE_DECLARE_SMART_PTR(Joint); KGE_DECLARE_SMART_PTR(PhysicJoint);
KGE_DECLARE_SMART_PTR(DistanceJoint); KGE_DECLARE_SMART_PTR(DistanceJoint);
KGE_DECLARE_SMART_PTR(FrictionJoint); KGE_DECLARE_SMART_PTR(FrictionJoint);
KGE_DECLARE_SMART_PTR(GearJoint); KGE_DECLARE_SMART_PTR(GearJoint);
@ -40,8 +40,8 @@ namespace kiwano
KGE_DECLARE_SMART_PTR(WheelJoint); KGE_DECLARE_SMART_PTR(WheelJoint);
// 关节 // 关节
class KGE_API Joint class KGE_API PhysicJoint
: public ObjectBase : public virtual RefCounter
{ {
public: public:
enum class Type enum class Type
@ -62,41 +62,43 @@ namespace kiwano
struct ParamBase struct ParamBase
{ {
Body* body_a; PhysicBody* body_a;
Body* body_b; PhysicBody* body_b;
ParamBase(Body* body_a, Body* body_b) : body_a(body_a), body_b(body_b) {} ParamBase(PhysicBody* body_a, PhysicBody* body_b) : body_a(body_a), body_b(body_b) {}
ParamBase(BodyPtr body_a, BodyPtr body_b) : body_a(body_a.get()), body_b(body_b.get()) {} ParamBase(PhysicBodyPtr body_a, PhysicBodyPtr body_b) : body_a(body_a.get()), body_b(body_b.get()) {}
}; };
Joint(); PhysicJoint();
Joint(b2Joint* joint); PhysicJoint(b2Joint* joint);
Joint(World* world, b2JointDef* joint_def); PhysicJoint(PhysicWorld* world, b2JointDef* joint_def);
virtual ~Joint(); virtual ~PhysicJoint();
BodyPtr GetBodyA() const; void Init(PhysicWorld* world, b2JointDef* joint_def);
BodyPtr GetBodyB() const;
PhysicBodyPtr GetBodyA() const;
PhysicBodyPtr GetBodyB() const;
b2Joint* GetB2Joint() { return joint_; } b2Joint* GetB2Joint() { return joint_; }
const b2Joint* GetB2Joint() const { return joint_; } const b2Joint* GetB2Joint() const { return joint_; }
void SetB2Joint(b2Joint* joint); void SetB2Joint(b2Joint* joint);
World* GetWorld() { return world_; } PhysicWorld* GetWorld() { return world_; }
const World* GetWorld() const { return world_; } const PhysicWorld* GetWorld() const { return world_; }
protected: protected:
b2Joint* joint_; b2Joint* joint_;
World* world_; PhysicWorld* world_;
Type type_; Type type_;
}; };
// 固定距离关节 // 固定距离关节
class KGE_API DistanceJoint class KGE_API DistanceJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
Point anchor_a; Point anchor_a;
Point anchor_b; Point anchor_b;
@ -104,8 +106,8 @@ namespace kiwano
float damping_ratio; float damping_ratio;
Param( Param(
Body* body_a, PhysicBody* body_a,
Body* body_b, PhysicBody* body_b,
Point const& anchor_a, Point const& anchor_a,
Point const& anchor_b, Point const& anchor_b,
float frequency_hz = 0.f, float frequency_hz = 0.f,
@ -119,8 +121,8 @@ namespace kiwano
{} {}
Param( Param(
BodyPtr body_a, PhysicBodyPtr body_a,
BodyPtr body_b, PhysicBodyPtr body_b,
Point const& anchor_a, Point const& anchor_a,
Point const& anchor_b, Point const& anchor_b,
float frequency_hz = 0.f, float frequency_hz = 0.f,
@ -131,9 +133,8 @@ namespace kiwano
}; };
DistanceJoint(); DistanceJoint();
DistanceJoint(World* world, b2DistanceJointDef* def); DistanceJoint(PhysicWorld* world, b2DistanceJointDef* def);
DistanceJoint(PhysicWorld* world, Param const& param);
static DistanceJointPtr Create(World* world, Param const& param);
void SetLength(float length); void SetLength(float length);
float GetLength() const; float GetLength() const;
@ -153,18 +154,18 @@ namespace kiwano
// 摩擦关节 // 摩擦关节
class KGE_API FrictionJoint class KGE_API FrictionJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
Point anchor; Point anchor;
float max_force; float max_force;
float max_torque; float max_torque;
Param( Param(
Body* body_a, PhysicBody* body_a,
Body* body_b, PhysicBody* body_b,
Point const& anchor, Point const& anchor,
float max_force = 0.f, float max_force = 0.f,
float max_torque = 0.f float max_torque = 0.f
@ -176,8 +177,8 @@ namespace kiwano
{} {}
Param( Param(
BodyPtr body_a, PhysicBodyPtr body_a,
BodyPtr body_b, PhysicBodyPtr body_b,
Point const& anchor, Point const& anchor,
float max_force = 0.f, float max_force = 0.f,
float max_torque = 0.f float max_torque = 0.f
@ -187,9 +188,8 @@ namespace kiwano
}; };
FrictionJoint(); FrictionJoint();
FrictionJoint(World* world, b2FrictionJointDef* def); FrictionJoint(PhysicWorld* world, b2FrictionJointDef* def);
FrictionJoint(PhysicWorld* world, Param const& param);
static FrictionJointPtr Create(World* world, Param const& param);
// 设定最大摩擦力 // 设定最大摩擦力
void SetMaxForce(float force); void SetMaxForce(float force);
@ -206,18 +206,18 @@ namespace kiwano
// 齿轮关节 // 齿轮关节
class KGE_API GearJoint class KGE_API GearJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
JointPtr joint_a; PhysicJointPtr joint_a;
JointPtr joint_b; PhysicJointPtr joint_b;
float ratio; float ratio;
Param( Param(
Joint* joint_a, PhysicJoint* joint_a,
Joint* joint_b, PhysicJoint* joint_b,
float ratio = 1.f float ratio = 1.f
) )
: ParamBase(nullptr, nullptr) : ParamBase(nullptr, nullptr)
@ -227,8 +227,8 @@ namespace kiwano
{} {}
Param( Param(
JointPtr joint_a, PhysicJointPtr joint_a,
JointPtr joint_b, PhysicJointPtr joint_b,
float ratio = 1.f float ratio = 1.f
) )
: Param(joint_a.get(), joint_b.get(), ratio) : Param(joint_a.get(), joint_b.get(), ratio)
@ -236,9 +236,8 @@ namespace kiwano
}; };
GearJoint(); GearJoint();
GearJoint(World* world, b2GearJointDef* def); GearJoint(PhysicWorld* world, b2GearJointDef* def);
GearJoint(PhysicWorld* world, Param const& param);
static GearJointPtr Create(World* world, Param const& param);
// 设定齿轮传动比 // 设定齿轮传动比
void SetRatio(float ratio); void SetRatio(float ratio);
@ -251,18 +250,18 @@ namespace kiwano
// 马达关节 // 马达关节
class KGE_API MotorJoint class KGE_API MotorJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
float max_force; float max_force;
float max_torque; float max_torque;
float correction_factor; float correction_factor;
Param( Param(
Body* body_a, PhysicBody* body_a,
Body* body_b, PhysicBody* body_b,
float max_force = 1.f, float max_force = 1.f,
float max_torque = 100.f, float max_torque = 100.f,
float correction_factor = 0.3f float correction_factor = 0.3f
@ -274,8 +273,8 @@ namespace kiwano
{} {}
Param( Param(
BodyPtr body_a, PhysicBodyPtr body_a,
BodyPtr body_b, PhysicBodyPtr body_b,
float max_force = 0.f, float max_force = 0.f,
float max_torque = 0.f, float max_torque = 0.f,
float correction_factor = 0.3f float correction_factor = 0.3f
@ -285,9 +284,8 @@ namespace kiwano
}; };
MotorJoint(); MotorJoint();
MotorJoint(World* world, b2MotorJointDef* def); MotorJoint(PhysicWorld* world, b2MotorJointDef* def);
MotorJoint(PhysicWorld* world, Param const& param);
static MotorJointPtr Create(World* world, Param const& param);
// 设定最大摩擦力 // 设定最大摩擦力
void SetMaxForce(float force); void SetMaxForce(float force);
@ -304,10 +302,10 @@ namespace kiwano
// 平移关节 // 平移关节
class KGE_API PrismaticJoint class KGE_API PrismaticJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
Point anchor; Point anchor;
Vec2 axis; Vec2 axis;
@ -319,8 +317,8 @@ namespace kiwano
float motor_speed; float motor_speed;
Param( Param(
Body* body_a, PhysicBody* body_a,
Body* body_b, PhysicBody* body_b,
Point const& anchor, Point const& anchor,
Vec2 const& axis, Vec2 const& axis,
bool enable_limit = false, bool enable_limit = false,
@ -342,8 +340,8 @@ namespace kiwano
{} {}
Param( Param(
BodyPtr body_a, PhysicBodyPtr body_a,
BodyPtr body_b, PhysicBodyPtr body_b,
Point const& anchor, Point const& anchor,
Vec2 const& axis, Vec2 const& axis,
bool enable_limit = false, bool enable_limit = false,
@ -358,11 +356,10 @@ namespace kiwano
}; };
PrismaticJoint(); PrismaticJoint();
PrismaticJoint(World* world, b2PrismaticJointDef* def); PrismaticJoint(PhysicWorld* world, b2PrismaticJointDef* def);
PrismaticJoint(PhysicWorld* world, Param const& param);
static PrismaticJointPtr Create(World* world, Param const& param); float GetReferenceAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetReferenceAngle()); }
float GetReferenceAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetReferenceAngle()); }
float GetJointTranslation() const; float GetJointTranslation() const;
float GetJointSpeed() const; float GetJointSpeed() const;
@ -377,8 +374,8 @@ namespace kiwano
void EnableMotor(bool flag) { KGE_ASSERT(raw_joint_); raw_joint_->EnableMotor(flag); } void EnableMotor(bool flag) { KGE_ASSERT(raw_joint_); raw_joint_->EnableMotor(flag); }
// 设置马达转速 [degree/s] // 设置马达转速 [degree/s]
void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Angle2Radian(speed)); } void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Degree2Radian(speed)); }
float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetMotorSpeed()); } float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetMotorSpeed()); }
// 设定最大马达力 [N] // 设定最大马达力 [N]
void SetMaxMotorForce(float force) { KGE_ASSERT(raw_joint_); raw_joint_->SetMaxMotorForce(force); } void SetMaxMotorForce(float force) { KGE_ASSERT(raw_joint_); raw_joint_->SetMaxMotorForce(force); }
@ -391,10 +388,10 @@ namespace kiwano
// 滑轮关节 // 滑轮关节
class KGE_API PulleyJoint class KGE_API PulleyJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
Point anchor_a; Point anchor_a;
Point anchor_b; Point anchor_b;
@ -403,8 +400,8 @@ namespace kiwano
float ratio; float ratio;
Param( Param(
Body* body_a, PhysicBody* body_a,
Body* body_b, PhysicBody* body_b,
Point const& anchor_a, Point const& anchor_a,
Point const& anchor_b, Point const& anchor_b,
Point const& ground_anchor_a, Point const& ground_anchor_a,
@ -420,8 +417,8 @@ namespace kiwano
{} {}
Param( Param(
BodyPtr body_a, PhysicBodyPtr body_a,
BodyPtr body_b, PhysicBodyPtr body_b,
Point const& anchor_a, Point const& anchor_a,
Point const& anchor_b, Point const& anchor_b,
Point const& ground_anchor_a, Point const& ground_anchor_a,
@ -433,9 +430,8 @@ namespace kiwano
}; };
PulleyJoint(); PulleyJoint();
PulleyJoint(World* world, b2PulleyJointDef* def); PulleyJoint(PhysicWorld* world, b2PulleyJointDef* def);
PulleyJoint(PhysicWorld* world, Param const& param);
static PulleyJointPtr Create(World* world, Param const& param);
Point GetGroundAnchorA() const; Point GetGroundAnchorA() const;
Point GetGroundAnchorB() const; Point GetGroundAnchorB() const;
@ -455,10 +451,10 @@ namespace kiwano
// 旋转关节 // 旋转关节
class KGE_API RevoluteJoint class KGE_API RevoluteJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
Point anchor; Point anchor;
bool enable_limit; bool enable_limit;
@ -469,8 +465,8 @@ namespace kiwano
float motor_speed; float motor_speed;
Param( Param(
Body* body_a, PhysicBody* body_a,
Body* body_b, PhysicBody* body_b,
Point const& anchor, Point const& anchor,
bool enable_limit = false, bool enable_limit = false,
float lower_angle = 0.0f, float lower_angle = 0.0f,
@ -490,8 +486,8 @@ namespace kiwano
{} {}
Param( Param(
BodyPtr body_a, PhysicBodyPtr body_a,
BodyPtr body_b, PhysicBodyPtr body_b,
Point const& anchor, Point const& anchor,
bool enable_limit = false, bool enable_limit = false,
float lower_angle = 0.0f, float lower_angle = 0.0f,
@ -505,11 +501,10 @@ namespace kiwano
}; };
RevoluteJoint(); RevoluteJoint();
RevoluteJoint(World* world, b2RevoluteJointDef* def); RevoluteJoint(PhysicWorld* world, b2RevoluteJointDef* def);
RevoluteJoint(PhysicWorld* world, Param const& param);
static RevoluteJointPtr Create(World* world, Param const& param); float GetReferenceAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetReferenceAngle()); }
float GetReferenceAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetReferenceAngle()); }
float GetJointAngle() const; float GetJointAngle() const;
float GetJointSpeed() const; float GetJointSpeed() const;
@ -524,8 +519,8 @@ namespace kiwano
void EnableMotor(bool flag) { KGE_ASSERT(raw_joint_); raw_joint_->EnableMotor(flag); } void EnableMotor(bool flag) { KGE_ASSERT(raw_joint_); raw_joint_->EnableMotor(flag); }
// 设置马达转速 [degree/s] // 设置马达转速 [degree/s]
void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Angle2Radian(speed)); } void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Degree2Radian(speed)); }
float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetMotorSpeed()); } float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetMotorSpeed()); }
// 设定最大马达转矩 [N/m] // 设定最大马达转矩 [N/m]
void SetMaxMotorTorque(float torque); void SetMaxMotorTorque(float torque);
@ -538,18 +533,18 @@ namespace kiwano
// 绳关节 // 绳关节
class KGE_API RopeJoint class KGE_API RopeJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
Point local_anchor_a; Point local_anchor_a;
Point local_anchor_b; Point local_anchor_b;
float max_length; float max_length;
Param( Param(
Body* body_a, PhysicBody* body_a,
Body* body_b, PhysicBody* body_b,
Point const& local_anchor_a, Point const& local_anchor_a,
Point const& local_anchor_b, Point const& local_anchor_b,
float max_length = 0.f float max_length = 0.f
@ -561,8 +556,8 @@ namespace kiwano
{} {}
Param( Param(
BodyPtr body_a, PhysicBodyPtr body_a,
BodyPtr body_b, PhysicBodyPtr body_b,
Point const& local_anchor_a, Point const& local_anchor_a,
Point const& local_anchor_b, Point const& local_anchor_b,
float max_length = 0.f float max_length = 0.f
@ -572,9 +567,8 @@ namespace kiwano
}; };
RopeJoint(); RopeJoint();
RopeJoint(World* world, b2RopeJointDef* def); RopeJoint(PhysicWorld* world, b2RopeJointDef* def);
RopeJoint(PhysicWorld* world, Param const& param);
static RopeJointPtr Create(World* world, Param const& param);
void SetMaxLength(float length); void SetMaxLength(float length);
float GetMaxLength() const; float GetMaxLength() const;
@ -586,18 +580,18 @@ namespace kiwano
// 焊接关节 // 焊接关节
class KGE_API WeldJoint class KGE_API WeldJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
Point anchor; Point anchor;
float frequency_hz; float frequency_hz;
float damping_ratio; float damping_ratio;
Param( Param(
Body* body_a, PhysicBody* body_a,
Body* body_b, PhysicBody* body_b,
Point const& anchor, Point const& anchor,
float frequency_hz = 0.f, float frequency_hz = 0.f,
float damping_ratio = 0.f float damping_ratio = 0.f
@ -609,8 +603,8 @@ namespace kiwano
{} {}
Param( Param(
BodyPtr body_a, PhysicBodyPtr body_a,
BodyPtr body_b, PhysicBodyPtr body_b,
Point const& anchor, Point const& anchor,
float frequency_hz = 0.f, float frequency_hz = 0.f,
float damping_ratio = 0.f float damping_ratio = 0.f
@ -620,9 +614,8 @@ namespace kiwano
}; };
WeldJoint(); WeldJoint();
WeldJoint(World* world, b2WeldJointDef* def); WeldJoint(PhysicWorld* world, b2WeldJointDef* def);
WeldJoint(PhysicWorld* world, Param const& param);
static WeldJointPtr Create(World* world, Param const& param);
// 设置弹簧阻尼器频率 [赫兹] // 设置弹簧阻尼器频率 [赫兹]
void SetFrequency(float hz) { KGE_ASSERT(raw_joint_); raw_joint_->SetFrequency(hz); } void SetFrequency(float hz) { KGE_ASSERT(raw_joint_); raw_joint_->SetFrequency(hz); }
@ -639,10 +632,10 @@ namespace kiwano
// 轮关节 // 轮关节
class KGE_API WheelJoint class KGE_API WheelJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
Point anchor; Point anchor;
Vec2 axis; Vec2 axis;
@ -653,8 +646,8 @@ namespace kiwano
float damping_ratio; float damping_ratio;
Param( Param(
Body* body_a, PhysicBody* body_a,
Body* body_b, PhysicBody* body_b,
Point const& anchor, Point const& anchor,
Vec2 const& axis, Vec2 const& axis,
float frequency_hz = 2.0f, float frequency_hz = 2.0f,
@ -674,8 +667,8 @@ namespace kiwano
{} {}
Param( Param(
BodyPtr body_a, PhysicBodyPtr body_a,
BodyPtr body_b, PhysicBodyPtr body_b,
Point const& anchor, Point const& anchor,
Vec2 const& axis, Vec2 const& axis,
float frequency_hz = 2.0f, float frequency_hz = 2.0f,
@ -689,21 +682,20 @@ namespace kiwano
}; };
WheelJoint(); WheelJoint();
WheelJoint(World* world, b2WheelJointDef* def); WheelJoint(PhysicWorld* world, b2WheelJointDef* def);
WheelJoint(PhysicWorld* world, Param const& param);
static WheelJointPtr Create(World* world, Param const& param);
float GetJointTranslation() const; float GetJointTranslation() const;
float GetJointLinearSpeed() const; float GetJointLinearSpeed() const;
float GetJointAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetJointAngle()); } float GetJointAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetJointAngle()); }
float GetJointAngularSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetJointAngularSpeed()); } float GetJointAngularSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetJointAngularSpeed()); }
bool IsMotorEnabled() const { KGE_ASSERT(raw_joint_); return raw_joint_->IsMotorEnabled(); } bool IsMotorEnabled() const { KGE_ASSERT(raw_joint_); return raw_joint_->IsMotorEnabled(); }
void EnableMotor(bool flag) { KGE_ASSERT(raw_joint_); raw_joint_->EnableMotor(flag); } void EnableMotor(bool flag) { KGE_ASSERT(raw_joint_); raw_joint_->EnableMotor(flag); }
// 设置马达转速 [degree/s] // 设置马达转速 [degree/s]
void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Angle2Radian(speed)); } void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Degree2Radian(speed)); }
float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetMotorSpeed()); } float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetMotorSpeed()); }
// 设定最大马达转矩 [N/m] // 设定最大马达转矩 [N/m]
void SetMaxMotorTorque(float torque); void SetMaxMotorTorque(float torque);
@ -723,10 +715,10 @@ namespace kiwano
// 鼠标关节 // 鼠标关节
// 用于使身体的某个点追踪世界上的指定点,例如让物体追踪鼠标位置 // 用于使身体的某个点追踪世界上的指定点,例如让物体追踪鼠标位置
class KGE_API MouseJoint class KGE_API MouseJoint
: public Joint : public PhysicJoint
{ {
public: public:
struct Param : public Joint::ParamBase struct Param : public PhysicJoint::ParamBase
{ {
Point target; Point target;
float max_force; float max_force;
@ -734,8 +726,8 @@ namespace kiwano
float damping_ratio; float damping_ratio;
Param( Param(
Body* body_a, PhysicBody* body_a,
Body* body_b, PhysicBody* body_b,
Point const& target, Point const& target,
float max_force, float max_force,
float frequency_hz = 5.0f, float frequency_hz = 5.0f,
@ -749,8 +741,8 @@ namespace kiwano
{} {}
Param( Param(
BodyPtr body_a, PhysicBodyPtr body_a,
BodyPtr body_b, PhysicBodyPtr body_b,
Point const& target, Point const& target,
float max_force, float max_force,
float frequency_hz = 5.0f, float frequency_hz = 5.0f,
@ -761,9 +753,8 @@ namespace kiwano
}; };
MouseJoint(); MouseJoint();
MouseJoint(World* world, b2MouseJointDef* def); MouseJoint(PhysicWorld* world, b2MouseJointDef* def);
MouseJoint(PhysicWorld* world, Param const& param);
static MouseJointPtr Create(World* world, Param const& param);
// 设定最大摩擦力 [N] // 设定最大摩擦力 [N]
void SetMaxForce(float force); void SetMaxForce(float force);

View File

@ -25,55 +25,55 @@ namespace kiwano
{ {
namespace physics namespace physics
{ {
Shape::Shape() PhysicShape::PhysicShape()
: shape_(nullptr) : shape_(nullptr)
{ {
} }
Shape::Shape(b2Shape* shape) PhysicShape::PhysicShape(b2Shape* shape)
: shape_(shape) : shape_(shape)
{ {
} }
b2Shape* Shape::GetB2Shape() b2Shape* PhysicShape::GetB2Shape()
{ {
return shape_; return shape_;
} }
const b2Shape* Shape::GetB2Shape() const const b2Shape* PhysicShape::GetB2Shape() const
{ {
return shape_; return shape_;
} }
void Shape::SetB2Shape(b2Shape* shape) void PhysicShape::SetB2Shape(b2Shape* shape)
{ {
shape_ = shape; shape_ = shape;
} }
// //
// CircleShape // PhysicCircleShape
// //
CircleShape::CircleShape() PhysicCircleShape::PhysicCircleShape()
: Shape(&circle_) : PhysicShape(&circle_)
, circle_() , circle_()
, radius_(0.f) , radius_(0.f)
{ {
} }
CircleShape::CircleShape(float radius, Point const& offset) PhysicCircleShape::PhysicCircleShape(float radius, Point const& offset)
: CircleShape() : PhysicCircleShape()
{ {
Set(radius, offset); Set(radius, offset);
} }
void CircleShape::Set(float radius, Point const& offset) void PhysicCircleShape::Set(float radius, Point const& offset)
{ {
radius_ = radius; radius_ = radius;
offset_ = offset; offset_ = offset;
} }
void CircleShape::FitWorld(World* world) void PhysicCircleShape::FitWorld(PhysicWorld* world)
{ {
KGE_ASSERT(world); KGE_ASSERT(world);
circle_.m_radius = world->Stage2World(radius_); circle_.m_radius = world->Stage2World(radius_);
@ -81,30 +81,30 @@ namespace kiwano
} }
// //
// BoxShape // PhysicBoxShape
// //
BoxShape::BoxShape() PhysicBoxShape::PhysicBoxShape()
: Shape(&polygon_) : PhysicShape(&polygon_)
, polygon_() , polygon_()
, rotation_(0.f) , rotation_(0.f)
{ {
} }
BoxShape::BoxShape(Vec2 const& size, Point const& offset, float rotation) PhysicBoxShape::PhysicBoxShape(Vec2 const& size, Point const& offset, float rotation)
: BoxShape() : PhysicBoxShape()
{ {
Set(size, offset, rotation); Set(size, offset, rotation);
} }
void BoxShape::Set(Vec2 const& size, Point const& offset, float rotation) void PhysicBoxShape::Set(Vec2 const& size, Point const& offset, float rotation)
{ {
box_size_ = size; box_size_ = size;
offset_ = offset; offset_ = offset;
rotation_ = rotation; rotation_ = rotation;
} }
void BoxShape::FitWorld(World* world) void PhysicBoxShape::FitWorld(PhysicWorld* world)
{ {
KGE_ASSERT(world); KGE_ASSERT(world);
@ -114,27 +114,27 @@ namespace kiwano
} }
// //
// PolygonShape // PhysicPolygonShape
// //
PolygonShape::PolygonShape() PhysicPolygonShape::PhysicPolygonShape()
: Shape(&polygon_) : PhysicShape(&polygon_)
, polygon_() , polygon_()
{ {
} }
PolygonShape::PolygonShape(Vector<Point> const& vertexs) PhysicPolygonShape::PhysicPolygonShape(Vector<Point> const& vertexs)
: PolygonShape() : PhysicPolygonShape()
{ {
Set(vertexs); Set(vertexs);
} }
void PolygonShape::Set(Vector<Point> const& vertexs) void PhysicPolygonShape::Set(Vector<Point> const& vertexs)
{ {
vertexs_ = vertexs; vertexs_ = vertexs;
} }
void PolygonShape::FitWorld(World* world) void PhysicPolygonShape::FitWorld(PhysicWorld* world)
{ {
KGE_ASSERT(world); KGE_ASSERT(world);
@ -149,28 +149,28 @@ namespace kiwano
} }
// //
// EdgeShape // PhysicEdgeShape
// //
EdgeShape::EdgeShape() PhysicEdgeShape::PhysicEdgeShape()
: Shape(&edge_) : PhysicShape(&edge_)
, edge_() , edge_()
{ {
} }
EdgeShape::EdgeShape(Point const& p1, Point const& p2) PhysicEdgeShape::PhysicEdgeShape(Point const& p1, Point const& p2)
: EdgeShape() : PhysicEdgeShape()
{ {
Set(p1, p2); Set(p1, p2);
} }
void EdgeShape::Set(Point const& p1, Point const& p2) void PhysicEdgeShape::Set(Point const& p1, Point const& p2)
{ {
p_[0] = p1; p_[0] = p1;
p_[1] = p2; p_[1] = p2;
} }
void EdgeShape::FitWorld(World* world) void PhysicEdgeShape::FitWorld(PhysicWorld* world)
{ {
KGE_ASSERT(world); KGE_ASSERT(world);
@ -180,29 +180,29 @@ namespace kiwano
} }
// //
// ChainShape // PhysicChainShape
// //
ChainShape::ChainShape() PhysicChainShape::PhysicChainShape()
: Shape(&chain_) : PhysicShape(&chain_)
, chain_() , chain_()
, loop_(false) , loop_(false)
{ {
} }
ChainShape::ChainShape(Vector<Point> const& vertexs, bool loop) PhysicChainShape::PhysicChainShape(Vector<Point> const& vertexs, bool loop)
: ChainShape() : PhysicChainShape()
{ {
Set(vertexs, loop); Set(vertexs, loop);
} }
void ChainShape::Set(Vector<Point> const& vertexs, bool loop) void PhysicChainShape::Set(Vector<Point> const& vertexs, bool loop)
{ {
vertexs_ = vertexs; vertexs_ = vertexs;
loop_ = loop; loop_ = loop;
} }
void ChainShape::FitWorld(World* world) void PhysicChainShape::FitWorld(PhysicWorld* world)
{ {
KGE_ASSERT(world); KGE_ASSERT(world);

View File

@ -25,37 +25,37 @@ namespace kiwano
{ {
namespace physics namespace physics
{ {
class World; class PhysicWorld;
// 形状基类 // 形状基类
class KGE_API Shape class KGE_API PhysicShape
{ {
public: public:
Shape(); PhysicShape();
Shape(b2Shape* shape); PhysicShape(b2Shape* shape);
b2Shape* GetB2Shape(); b2Shape* GetB2Shape();
const b2Shape* GetB2Shape() const; const b2Shape* GetB2Shape() const;
void SetB2Shape(b2Shape* shape); void SetB2Shape(b2Shape* shape);
virtual void FitWorld(World* world) {} virtual void FitWorld(PhysicWorld* world) {}
protected: protected:
b2Shape* shape_; b2Shape* shape_;
}; };
// 圆形形状 // 圆形形状
class KGE_API CircleShape class KGE_API PhysicCircleShape
: public Shape : public PhysicShape
{ {
public: public:
CircleShape(); PhysicCircleShape();
CircleShape(float radius, Point const& offset = Point()); PhysicCircleShape(float radius, Point const& offset = Point());
void Set(float radius, Point const& offset = Point()); void Set(float radius, Point const& offset = Point());
void FitWorld(World* world) override; void FitWorld(PhysicWorld* world) override;
protected: protected:
float radius_; float radius_;
@ -64,17 +64,17 @@ namespace kiwano
}; };
// 盒子形状 // 盒子形状
class KGE_API BoxShape class KGE_API PhysicBoxShape
: public Shape : public PhysicShape
{ {
public: public:
BoxShape(); PhysicBoxShape();
BoxShape(Vec2 const& size, Point const& offset = Point(), float rotation = 0.f); PhysicBoxShape(Vec2 const& size, Point const& offset = Point(), float rotation = 0.f);
void Set(Vec2 const& size, Point const& offset = Point(), float rotation = 0.f); void Set(Vec2 const& size, Point const& offset = Point(), float rotation = 0.f);
void FitWorld(World* world) override; void FitWorld(PhysicWorld* world) override;
protected: protected:
float rotation_; float rotation_;
@ -84,17 +84,17 @@ namespace kiwano
}; };
// 多边形形状 // 多边形形状
class KGE_API PolygonShape class KGE_API PhysicPolygonShape
: public Shape : public PhysicShape
{ {
public: public:
PolygonShape(); PhysicPolygonShape();
PolygonShape(Vector<Point> const& vertexs); PhysicPolygonShape(Vector<Point> const& vertexs);
void Set(Vector<Point> const& vertexs); void Set(Vector<Point> const& vertexs);
void FitWorld(World* world) override; void FitWorld(PhysicWorld* world) override;
protected: protected:
Vector<Point> vertexs_; Vector<Point> vertexs_;
@ -102,17 +102,17 @@ namespace kiwano
}; };
// 线段形状, 用于表示一条边 // 线段形状, 用于表示一条边
class KGE_API EdgeShape class KGE_API PhysicEdgeShape
: public Shape : public PhysicShape
{ {
public: public:
EdgeShape(); PhysicEdgeShape();
EdgeShape(Point const& p1, Point const& p2); PhysicEdgeShape(Point const& p1, Point const& p2);
void Set(Point const& p1, Point const& p2); void Set(Point const& p1, Point const& p2);
void FitWorld(World* world) override; void FitWorld(PhysicWorld* world) override;
protected: protected:
Point p_[2]; Point p_[2];
@ -120,17 +120,17 @@ namespace kiwano
}; };
// 链式形状 // 链式形状
class KGE_API ChainShape class KGE_API PhysicChainShape
: public Shape : public PhysicShape
{ {
public: public:
ChainShape(); PhysicChainShape();
ChainShape(Vector<Point> const& vertexs, bool loop = false); PhysicChainShape(Vector<Point> const& vertexs, bool loop = false);
void Set(Vector<Point> const& vertexs, bool loop = false); void Set(Vector<Point> const& vertexs, bool loop = false);
void FitWorld(World* world) override; void FitWorld(PhysicWorld* world) override;
protected: protected:
bool loop_; bool loop_;

View File

@ -29,12 +29,12 @@ namespace kiwano
const float DefaultGlobalScale = 100.f; // 100 pixels per meters const float DefaultGlobalScale = 100.f; // 100 pixels per meters
} }
class World::DestructionListener : public b2DestructionListener class PhysicWorld::DestructionListener : public b2DestructionListener
{ {
World* world_; PhysicWorld* world_;
public: public:
DestructionListener(World* world) DestructionListener(PhysicWorld* world)
: world_(world) : world_(world)
{ {
} }
@ -53,7 +53,7 @@ namespace kiwano
} }
}; };
World::World() PhysicWorld::PhysicWorld()
: world_(b2Vec2(0, 10.0f)) : world_(b2Vec2(0, 10.0f))
, vel_iter_(6) , vel_iter_(6)
, pos_iter_(2) , pos_iter_(2)
@ -65,7 +65,7 @@ namespace kiwano
world_.SetDestructionListener(destruction_listener_); world_.SetDestructionListener(destruction_listener_);
} }
World::~World() PhysicWorld::~PhysicWorld()
{ {
world_.SetDestructionListener(nullptr); world_.SetDestructionListener(nullptr);
if (destruction_listener_) if (destruction_listener_)
@ -80,18 +80,7 @@ namespace kiwano
RemoveAllJoints(); RemoveAllJoints();
} }
BodyPtr World::CreateBody(ActorPtr actor) void PhysicWorld::RemoveBody(PhysicBody* body)
{
return CreateBody(actor.get());
}
BodyPtr World::CreateBody(Actor* actor)
{
BodyPtr body = Body::Create(this, actor);
return body;
}
void World::RemoveBody(Body* body)
{ {
if (body) if (body)
{ {
@ -102,7 +91,7 @@ namespace kiwano
} }
} }
void World::RemoveAllBodies() void PhysicWorld::RemoveAllBodies()
{ {
if (world_.GetBodyCount()) if (world_.GetBodyCount())
{ {
@ -116,7 +105,7 @@ namespace kiwano
} }
} }
void World::AddJoint(Joint* joint) void PhysicWorld::AddJoint(PhysicJoint* joint)
{ {
if (joint) if (joint)
{ {
@ -124,7 +113,7 @@ namespace kiwano
} }
} }
void World::RemoveJoint(Joint* joint) void PhysicWorld::RemoveJoint(PhysicJoint* joint)
{ {
if (joint) if (joint)
{ {
@ -143,7 +132,7 @@ namespace kiwano
} }
} }
void World::RemoveAllJoints() void PhysicWorld::RemoveAllJoints()
{ {
if (world_.GetJointCount()) if (world_.GetJointCount())
{ {
@ -162,14 +151,14 @@ namespace kiwano
joints_.clear(); joints_.clear();
} }
void World::JointRemoved(b2Joint* joint) void PhysicWorld::JointRemoved(b2Joint* joint)
{ {
if (!removing_joint_ && joint) if (!removing_joint_ && joint)
{ {
auto iter = std::find_if( auto iter = std::find_if(
joints_.begin(), joints_.begin(),
joints_.end(), joints_.end(),
[joint](Joint* j) -> bool { return j->GetB2Joint() == joint; } [joint](PhysicJoint* j) -> bool { return j->GetB2Joint() == joint; }
); );
if (iter != joints_.end()) if (iter != joints_.end())
@ -179,36 +168,36 @@ namespace kiwano
} }
} }
b2World* World::GetB2World() b2World* PhysicWorld::GetB2World()
{ {
return &world_; return &world_;
} }
const b2World* World::GetB2World() const const b2World* PhysicWorld::GetB2World() const
{ {
return &world_; return &world_;
} }
Vec2 World::GetGravity() const Vec2 PhysicWorld::GetGravity() const
{ {
b2Vec2 g = world_.GetGravity(); b2Vec2 g = world_.GetGravity();
return Vec2(g.x, g.y); return Vec2(g.x, g.y);
} }
void World::SetGravity(Vec2 gravity) void PhysicWorld::SetGravity(Vec2 gravity)
{ {
world_.SetGravity(b2Vec2(gravity.x, gravity.y)); world_.SetGravity(b2Vec2(gravity.x, gravity.y));
} }
void World::Update(Duration dt) void PhysicWorld::Update(Duration dt)
{ {
Stage::Update(dt); Stage::Update(dt);
b2Body* b2body = world_.GetBodyList(); b2Body* b2body = world_.GetBodyList();
while (b2body) while (b2body)
{ {
Body* body = static_cast<Body*>(b2body->GetUserData()); PhysicBody* body = static_cast<PhysicBody*>(b2body->GetUserData());
if (body && body->GetType() != Body::Type::Static) if (body && body->GetType() != PhysicBody::Type::Static)
{ {
body->UpdateFromActor(); body->UpdateFromActor();
} }
@ -221,8 +210,8 @@ namespace kiwano
b2body = world_.GetBodyList(); b2body = world_.GetBodyList();
while (b2body) while (b2body)
{ {
Body* body = static_cast<Body*>(b2body->GetUserData()); PhysicBody* body = static_cast<PhysicBody*>(b2body->GetUserData());
if (body && body->GetType() != Body::Type::Static) if (body && body->GetType() != PhysicBody::Type::Static)
{ {
body->UpdateActor(); body->UpdateActor();
} }

View File

@ -27,20 +27,16 @@ namespace kiwano
namespace physics namespace physics
{ {
// 物理世界 // 物理世界
class KGE_API World class KGE_API PhysicWorld
: public Stage : public Stage
{ {
friend class Body; friend class PhysicBody;
friend class Joint; friend class PhysicJoint;
public: public:
World(); PhysicWorld();
virtual ~World(); virtual ~PhysicWorld();
// 创建刚体
BodyPtr CreateBody(ActorPtr actor);
BodyPtr CreateBody(Actor* actor);
// 获取重力 // 获取重力
Vec2 GetGravity() const; Vec2 GetGravity() const;
@ -75,17 +71,17 @@ namespace kiwano
const b2World* GetB2World() const; const b2World* GetB2World() const;
protected: protected:
// 移除 // 移除
void RemoveBody(Body* body); void RemoveBody(PhysicBody* body);
// 移除所有 // 移除所有
void RemoveAllBodies(); void RemoveAllBodies();
// 添加关节 // 添加关节
void AddJoint(Joint* joint); void AddJoint(PhysicJoint* joint);
// 移除关节 // 移除关节
void RemoveJoint(Joint* joint); void RemoveJoint(PhysicJoint* joint);
// 移除所有关节 // 移除所有关节
void RemoveAllJoints(); void RemoveAllJoints();
@ -107,9 +103,9 @@ namespace kiwano
DestructionListener* destruction_listener_; DestructionListener* destruction_listener_;
bool removing_joint_; bool removing_joint_;
Vector<Joint*> joints_; Vector<PhysicJoint*> joints_;
}; };
KGE_DECLARE_SMART_PTR(World); KGE_DECLARE_SMART_PTR(PhysicWorld);
} }
} }

View File

@ -36,14 +36,14 @@ namespace kiwano
, public TimerManager , public TimerManager
, public ActionManager , public ActionManager
, public EventDispatcher , public EventDispatcher
, public intrusive_list_item<ActorPtr> , public IntrusiveListItem<ActorPtr>
{ {
friend class Director; friend class Director;
friend class Transition; friend class Transition;
friend class intrusive_list<ActorPtr>; friend IntrusiveList<ActorPtr>;
public: public:
using Children = intrusive_list<ActorPtr>; using Children = IntrusiveList<ActorPtr>;
using UpdateCallback = Function<void(Duration)>; using UpdateCallback = Function<void(Duration)>;
Actor(); Actor();
@ -70,61 +70,61 @@ namespace kiwano
int GetZOrder() const { return z_order_; } int GetZOrder() const { return z_order_; }
// 获取坐标 // 获取坐标
Point GetPosition() const { return transform_.position; } Point const& GetPosition() const { return transform_.position; }
// 获取 x 坐标 // 获取 x 坐标
float GetPositionX() const { return transform_.position.x; } float GetPositionX() const { return GetPosition().x; }
// 获取 y 坐标 // 获取 y 坐标
float GetPositionY() const { return transform_.position.y; } float GetPositionY() const { return GetPosition().y; }
// 获取缩放比例 // 获取缩放比例
Point GetScale() const { return transform_.scale; } Point const& GetScale() const { return transform_.scale; }
// 获取横向缩放比例 // 获取横向缩放比例
float GetScaleX() const { return transform_.scale.x; } float GetScaleX() const { return GetScale().x; }
// 获取纵向缩放比例 // 获取纵向缩放比例
float GetScaleY() const { return transform_.scale.y; } float GetScaleY() const { return GetScale().y; }
// 获取错切角度 // 获取错切角度
Point GetSkew() const { return transform_.skew; } Point const& GetSkew() const { return transform_.skew; }
// 获取横向错切角度 // 获取横向错切角度
float GetSkewX() const { return transform_.skew.x; } float GetSkewX() const { return GetSkew().x; }
// 获取纵向错切角度 // 获取纵向错切角度
float GetSkewY() const { return transform_.skew.y; } float GetSkewY() const { return GetSkew().y; }
// 获取旋转角度 // 获取旋转角度
float GetRotation() const { return transform_.rotation; } float GetRotation() const { return transform_.rotation; }
// 获取宽度 // 获取宽度
float GetWidth() const { return size_.x; } float GetWidth() const { return GetSize().x; }
// 获取高度 // 获取高度
float GetHeight() const { return size_.y; } float GetHeight() const { return GetSize().y; }
// 获取大小 // 获取大小
Size GetSize() const { return size_; } Size const& GetSize() const { return size_; }
// 获取缩放后的宽度 // 获取缩放后的宽度
float GetScaledWidth() const { return size_.x * transform_.scale.x; } float GetScaledWidth() const { return GetWidth() * GetScaleX(); }
// 获取缩放后的高度 // 获取缩放后的高度
float GetScaledHeight() const { return size_.y * transform_.scale.y; } float GetScaledHeight() const { return GetHeight() * GetScaleY(); }
// 获取缩放后的大小 // 获取缩放后的大小
Size GetScaledSize() const { return Size{ GetScaledWidth(), GetScaledHeight() }; } Size GetScaledSize() const { return Size{ GetScaledWidth(), GetScaledHeight() }; }
// 获取锚点 // 获取锚点
Point GetAnchor() const { return anchor_; } Point const& GetAnchor() const { return anchor_; }
// 获取 x 方向锚点 // 获取 x 方向锚点
float GetAnchorX() const { return anchor_.x; } float GetAnchorX() const { return GetAnchor().x; }
// 获取 y 方向锚点 // 获取 y 方向锚点
float GetAnchorY() const { return anchor_.y; } float GetAnchorY() const { return GetAnchor().y; }
// 获取透明度 // 获取透明度
float GetOpacity() const { return opacity_; } float GetOpacity() const { return opacity_; }

View File

@ -150,6 +150,9 @@ namespace kiwano
StrokeStyle outline_stroke StrokeStyle outline_stroke
); );
// 更新文本布局
void UpdateLayout();
// ÉèÖÃĬÈÏ×ÖÌå // ÉèÖÃĬÈÏ×ÖÌå
static void SetDefaultFont( static void SetDefaultFont(
Font const& font Font const& font
@ -162,9 +165,6 @@ namespace kiwano
void OnRender(RenderTarget* rt) override; void OnRender(RenderTarget* rt) override;
protected:
void UpdateLayout();
protected: protected:
bool format_dirty_; bool format_dirty_;
bool layout_dirty_; bool layout_dirty_;

View File

@ -29,11 +29,11 @@ namespace kiwano
class KGE_API Action class KGE_API Action
: public ObjectBase : public ObjectBase
, protected intrusive_list_item<ActionPtr> , protected IntrusiveListItem<ActionPtr>
{ {
friend class ActionManager; friend class ActionManager;
friend class ActionGroup; friend class ActionGroup;
friend class intrusive_list<ActionPtr>; friend IntrusiveList<ActionPtr>;
public: public:
enum class Status enum class Status

View File

@ -28,7 +28,7 @@ namespace kiwano
: public Action : public Action
{ {
public: public:
using ActionList = intrusive_list<ActionPtr>; using ActionList = IntrusiveList<ActionPtr>;
ActionGroup(); ActionGroup();

View File

@ -25,7 +25,7 @@ namespace kiwano
{ {
class KGE_API ActionManager class KGE_API ActionManager
{ {
using Actions = intrusive_list<ActionPtr>; using Actions = IntrusiveList<ActionPtr>;
public: public:
// 添加动作 // 添加动作

View File

@ -25,7 +25,7 @@ namespace kiwano
{ {
class KGE_API EventDispatcher class KGE_API EventDispatcher
{ {
using Listeners = intrusive_list<EventListenerPtr>; using Listeners = IntrusiveList<EventListenerPtr>;
public: public:
// 添加监听器 // 添加监听器

View File

@ -33,10 +33,10 @@ namespace kiwano
// 事件监听器 // 事件监听器
class KGE_API EventListener class KGE_API EventListener
: public ObjectBase : public ObjectBase
, protected intrusive_list_item<EventListenerPtr> , protected IntrusiveListItem<EventListenerPtr>
{ {
friend class EventDispatcher; friend class EventDispatcher;
friend class intrusive_list<EventListenerPtr>; friend IntrusiveList<EventListenerPtr>;
public: public:
using Callback = Function<void(Event const&)>; using Callback = Function<void(Event const&)>;

View File

@ -29,7 +29,7 @@ namespace kiwano
KGE_DECLARE_SMART_PTR(ObjectBase); KGE_DECLARE_SMART_PTR(ObjectBase);
class KGE_API ObjectBase class KGE_API ObjectBase
: public RefCounter : public virtual RefCounter
{ {
public: public:
ObjectBase(); ObjectBase();

View File

@ -25,7 +25,7 @@
namespace kiwano namespace kiwano
{ {
class KGE_API RefCounter class KGE_API RefCounter
: protected noncopyable : protected core::noncopyable
{ {
public: public:
// 增加引用计数 // 增加引用计数

View File

@ -38,7 +38,7 @@ namespace kiwano
}; };
template <typename _Ty> template <typename _Ty>
using SmartPtr = intrusive_ptr<_Ty, DefaultIntrusivePtrManager>; using SmartPtr = core::intrusive_ptr<_Ty, DefaultIntrusivePtrManager>;
} }

View File

@ -33,10 +33,10 @@ namespace kiwano
// 定时任务 // 定时任务
class KGE_API Timer class KGE_API Timer
: public ObjectBase : public ObjectBase
, protected intrusive_list_item<TimerPtr> , protected IntrusiveListItem<TimerPtr>
{ {
friend class TimerManager; friend class TimerManager;
friend class intrusive_list<TimerPtr>; friend IntrusiveList<TimerPtr>;
using Callback = Function<void()>; using Callback = Function<void()>;

View File

@ -25,7 +25,7 @@ namespace kiwano
{ {
class KGE_API TimerManager class KGE_API TimerManager
{ {
using Timers = intrusive_list<TimerPtr>; using Timers = IntrusiveList<TimerPtr>;
public: public:
// 添加定时器 // 添加定时器

View File

@ -42,6 +42,6 @@ namespace kiwano
template< template<
typename _Ty, typename _Ty,
typename = typename std::enable_if<std::is_base_of<IUnknown, _Ty>::value, int>::type> typename = typename std::enable_if<std::is_base_of<IUnknown, _Ty>::value, int>::type>
using ComPtr = intrusive_ptr<_Ty, ComPtrManager>; using ComPtr = core::intrusive_ptr<_Ty, ComPtrManager>;
} }

View File

@ -26,7 +26,7 @@
namespace kiwano namespace kiwano
{ {
inline namespace core namespace core
{ {
class bad_any_cast : public std::exception class bad_any_cast : public std::exception

View File

@ -28,7 +28,7 @@
namespace kiwano namespace kiwano
{ {
inline namespace core namespace core
{ {
// //
@ -2728,7 +2728,7 @@ private:
__json_detail::json_value<basic_json> value_; __json_detail::json_value<basic_json> value_;
}; };
} // inline namespace core } // namespace core
} // namespace kiwano } // namespace kiwano

View File

@ -78,6 +78,15 @@ namespace kiwano
using Json = kiwano::core::basic_json<kiwano::Map, kiwano::Vector, kiwano::String, using Json = kiwano::core::basic_json<kiwano::Map, kiwano::Vector, kiwano::String,
int, double, bool, std::allocator>; int, double, bool, std::allocator>;
template <typename _Ty>
using Singleton = core::singleton<_Ty>;
template <typename _Ty>
using IntrusiveList = core::intrusive_list<_Ty>;
template <typename _Ty>
using IntrusiveListItem = core::intrusive_list_item<_Ty>;
} }
namespace std namespace std

View File

@ -25,7 +25,7 @@
namespace kiwano namespace kiwano
{ {
inline namespace core namespace core
{ {
// //
// function is a light weight ::std::function<>-like class // function is a light weight ::std::function<>-like class
@ -321,7 +321,7 @@ private:
__function_detail::callable<_Ret, _Args...>* callable_; __function_detail::callable<_Ret, _Args...>* callable_;
}; };
} // inline namespace core } // namespace core
} // namespace kiwano } // namespace kiwano
@ -333,10 +333,11 @@ namespace kiwano
std::is_same<_Ty, _Uty>::value || std::is_base_of<_Ty, _Uty>::value, int std::is_same<_Ty, _Uty>::value || std::is_base_of<_Ty, _Uty>::value, int
>::type, >::type,
typename _Ret, typename _Ret,
typename... _Args> typename... _Args
inline function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret(_Ty::* func)(_Args...)) >
inline core::function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret(_Ty::* func)(_Args...))
{ {
return function<_Ret(_Args...)>(ptr, func); return core::function<_Ret(_Args...)>(ptr, func);
} }
template<typename _Ty, template<typename _Ty,
@ -345,9 +346,10 @@ namespace kiwano
std::is_same<_Ty, _Uty>::value || std::is_base_of<_Ty, _Uty>::value, int std::is_same<_Ty, _Uty>::value || std::is_base_of<_Ty, _Uty>::value, int
>::type, >::type,
typename _Ret, typename _Ret,
typename... _Args> typename... _Args
inline function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret(_Ty::* func)(_Args...) const) >
inline core::function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret(_Ty::* func)(_Args...) const)
{ {
return function<_Ret(_Args...)>(ptr, func); return core::function<_Ret(_Args...)>(ptr, func);
} }
} }

View File

@ -32,7 +32,7 @@
namespace kiwano namespace kiwano
{ {
inline namespace core namespace core
{ {
template <typename _Ty> template <typename _Ty>
@ -285,8 +285,8 @@ public:
using reverse_iterator = std::reverse_iterator<iterator>; using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>;
inline iterator begin() { return iterator(first_item()); } inline iterator begin() { return iterator(first_item(), first_item() == nullptr); }
inline const_iterator begin() const { return const_iterator(first_item()); } inline const_iterator begin() const { return const_iterator(first_item(), first_item() == nullptr); }
inline const_iterator cbegin() const { return begin(); } inline const_iterator cbegin() const { return begin(); }
inline iterator end() { return iterator(last_item(), true); } inline iterator end() { return iterator(last_item(), true); }
inline const_iterator end() const { return const_iterator(last_item(), true); } inline const_iterator end() const { return const_iterator(last_item(), true); }
@ -307,7 +307,7 @@ private:
value_type last_; value_type last_;
}; };
} // inline namespace core } // namespace core
} // namespace kiwano } // namespace kiwano

View File

@ -26,7 +26,7 @@
namespace kiwano namespace kiwano
{ {
inline namespace core namespace core
{ {
template <typename _Ty, typename _ManagerTy> template <typename _Ty, typename _ManagerTy>
@ -141,6 +141,6 @@ inline void swap(intrusive_ptr<_Ty, manager_type>& lhs, intrusive_ptr<_Ty, manag
lhs.swap(rhs); lhs.swap(rhs);
} }
} // inline namespace core } // namespace core
} // namespace kiwano } // namespace kiwano

View File

@ -22,7 +22,7 @@
namespace kiwano namespace kiwano
{ {
inline namespace core namespace core
{ {
class noncopyable class noncopyable
@ -36,5 +36,5 @@ private:
noncopyable& operator=(const noncopyable&) = delete; noncopyable& operator=(const noncopyable&) = delete;
}; };
} // inline namespace core } // namespace core
} // namespace kiwano } // namespace kiwano

View File

@ -27,17 +27,17 @@
#ifndef KGE_DECLARE_SINGLETON #ifndef KGE_DECLARE_SINGLETON
#define KGE_DECLARE_SINGLETON( CLASS ) \ #define KGE_DECLARE_SINGLETON( CLASS ) \
friend ::kiwano::Singleton< CLASS >; \ friend ::kiwano::core::singleton< CLASS >; \
friend typename std::unique_ptr< CLASS >::deleter_type friend typename std::unique_ptr< CLASS >::deleter_type
#endif #endif
namespace kiwano namespace kiwano
{ {
inline namespace core namespace core
{ {
template <typename _Ty> template <typename _Ty>
struct Singleton struct singleton
{ {
public: public:
static inline _Ty* GetInstance() static inline _Ty* GetInstance()
@ -55,12 +55,12 @@ public:
} }
protected: protected:
Singleton() = default; singleton() = default;
private: private:
Singleton(const Singleton&) = delete; singleton(const singleton&) = delete;
Singleton& operator=(const Singleton&) = delete; singleton& operator=(const singleton&) = delete;
static inline void InitInstance() static inline void InitInstance()
{ {
@ -76,10 +76,10 @@ private:
}; };
template <typename _Ty> template <typename _Ty>
std::once_flag Singleton<_Ty>::once_; std::once_flag singleton<_Ty>::once_;
template <typename _Ty> template <typename _Ty>
std::unique_ptr<_Ty> Singleton<_Ty>::instance_; std::unique_ptr<_Ty> singleton<_Ty>::instance_;
} // inline namespace core } // namespace core
} // namespace kiwano } // namespace kiwano

View File

@ -29,7 +29,7 @@
namespace kiwano namespace kiwano
{ {
inline namespace core namespace core
{ {
// //
@ -450,7 +450,7 @@ inline wstring to_wstring(double val) { return to_basic_string<wchar_t>(val);
inline wstring to_wstring(long double val) { return to_basic_string<wchar_t>(val); } inline wstring to_wstring(long double val) { return to_basic_string<wchar_t>(val); }
} // inline namespace core } // namespace core
} // namespace kiwano } // namespace kiwano
namespace kiwano namespace kiwano
@ -515,7 +515,7 @@ namespace __string_details
} }
} }
inline namespace core namespace core
{ {
template <typename _CharTy> template <typename _CharTy>
@ -1159,31 +1159,31 @@ inline namespace core
// //
template <typename _CharTy> template <typename _CharTy>
inline basic_string<_CharTy> basic_string<_CharTy>::parse(int val) { return ::kiwano::to_basic_string<char_type>(val); } inline basic_string<_CharTy> basic_string<_CharTy>::parse(int val) { return ::kiwano::core::to_basic_string<char_type>(val); }
template <typename _CharTy> template <typename _CharTy>
inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned int val) { return ::kiwano::to_basic_string<char_type>(val); } inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned int val) { return ::kiwano::core::to_basic_string<char_type>(val); }
template <typename _CharTy> template <typename _CharTy>
inline basic_string<_CharTy> basic_string<_CharTy>::parse(long val) { return ::kiwano::to_basic_string<char_type>(val); } inline basic_string<_CharTy> basic_string<_CharTy>::parse(long val) { return ::kiwano::core::to_basic_string<char_type>(val); }
template <typename _CharTy> template <typename _CharTy>
inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned long val) { return ::kiwano::to_basic_string<char_type>(val); } inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned long val) { return ::kiwano::core::to_basic_string<char_type>(val); }
template <typename _CharTy> template <typename _CharTy>
inline basic_string<_CharTy> basic_string<_CharTy>::parse(long long val) { return ::kiwano::to_basic_string<char_type>(val); } inline basic_string<_CharTy> basic_string<_CharTy>::parse(long long val) { return ::kiwano::core::to_basic_string<char_type>(val); }
template <typename _CharTy> template <typename _CharTy>
inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned long long val) { return ::kiwano::to_basic_string<char_type>(val); } inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned long long val) { return ::kiwano::core::to_basic_string<char_type>(val); }
template <typename _CharTy> template <typename _CharTy>
inline basic_string<_CharTy> basic_string<_CharTy>::parse(float val) { return ::kiwano::to_basic_string<char_type>(val); } inline basic_string<_CharTy> basic_string<_CharTy>::parse(float val) { return ::kiwano::core::to_basic_string<char_type>(val); }
template <typename _CharTy> template <typename _CharTy>
inline basic_string<_CharTy> basic_string<_CharTy>::parse(double val) { return ::kiwano::to_basic_string<char_type>(val); } inline basic_string<_CharTy> basic_string<_CharTy>::parse(double val) { return ::kiwano::core::to_basic_string<char_type>(val); }
template <typename _CharTy> template <typename _CharTy>
inline basic_string<_CharTy> basic_string<_CharTy>::parse(long double val) { return ::kiwano::to_basic_string<char_type>(val); } inline basic_string<_CharTy> basic_string<_CharTy>::parse(long double val) { return ::kiwano::core::to_basic_string<char_type>(val); }
// //
// details of basic_string::format // details of basic_string::format
@ -1193,7 +1193,7 @@ inline namespace core
template <typename ..._Args> template <typename ..._Args>
inline basic_string<_CharTy> basic_string<_CharTy>::format(const char_type* fmt, _Args&& ... args) inline basic_string<_CharTy> basic_string<_CharTy>::format(const char_type* fmt, _Args&& ... args)
{ {
return ::kiwano::format_string(fmt, std::forward<_Args>(args)...); return ::kiwano::core::format_string(fmt, std::forward<_Args>(args)...);
} }
// //

View File

@ -25,7 +25,7 @@
namespace kiwano namespace kiwano
{ {
inline namespace core namespace core
{ {
@ -284,5 +284,5 @@ namespace __vector_details
}; };
} }
} // inline namespace core } // namespace core
} // namespace kiwano } // namespace kiwano

View File

@ -40,29 +40,29 @@ namespace kiwano
inline float Sign(float val) { return val < 0 ? -1.f : 1.f; } inline float Sign(float val) { return val < 0 ? -1.f : 1.f; }
inline double Sign(double val) { return val < 0 ? -1.0 : 1.0; } inline double Sign(double val) { return val < 0 ? -1.0 : 1.0; }
inline float Angle2Radian(float angle) { return angle * constants::PI_F / 180.f; } inline float Degree2Radian(float angle) { return angle * constants::PI_F / 180.f; }
inline double Angle2Radian(double angle) { return angle * constants::PI_D / 180.0; } inline double Degree2Radian(double angle) { return angle * constants::PI_D / 180.0; }
inline float Radian2Angle(float radian) { return radian * 180.f / math::constants::PI_F; } inline float Radian2Degree(float radian) { return radian * 180.f / math::constants::PI_F; }
inline double Radian2Angle(double radian) { return radian * 180.0 / math::constants::PI_D; } inline double Radian2Degree(double radian) { return radian * 180.0 / math::constants::PI_D; }
inline float Sin(float val) { return ::sinf(Angle2Radian(val)); } inline float Sin(float val) { return ::sinf(Degree2Radian(val)); }
inline double Sin(double val) { return ::sin(Angle2Radian(val)); } inline double Sin(double val) { return ::sin(Degree2Radian(val)); }
inline float Cos(float val) { return ::cosf(Angle2Radian(val)); } inline float Cos(float val) { return ::cosf(Degree2Radian(val)); }
inline double Cos(double val) { return ::cos(Angle2Radian(val)); } inline double Cos(double val) { return ::cos(Degree2Radian(val)); }
inline float Tan(float val) { return ::tanf(Angle2Radian(val)); } inline float Tan(float val) { return ::tanf(Degree2Radian(val)); }
inline double Tan(double val) { return ::tan(Angle2Radian(val)); } inline double Tan(double val) { return ::tan(Degree2Radian(val)); }
inline float Asin(float val) { return Radian2Angle(::asinf(val)); } inline float Asin(float val) { return Radian2Degree(::asinf(val)); }
inline double Asin(double val) { return Radian2Angle(::asin(val)); } inline double Asin(double val) { return Radian2Degree(::asin(val)); }
inline float Acos(float val) { return Radian2Angle(::acosf(val)); } inline float Acos(float val) { return Radian2Degree(::acosf(val)); }
inline double Acos(double val) { return Radian2Angle(::acos(val)); } inline double Acos(double val) { return Radian2Degree(::acos(val)); }
inline float Atan(float val) { return Radian2Angle(::atanf(val)); } inline float Atan(float val) { return Radian2Degree(::atanf(val)); }
inline double Atan(double val) { return Radian2Angle(::atan(val)); } inline double Atan(double val) { return Radian2Degree(::atan(val)); }
inline float Ceil(float val) { return ::ceil(val); } inline float Ceil(float val) { return ::ceil(val); }
inline double Ceil(double val) { return ::ceil(val); } inline double Ceil(double val) { return ::ceil(val); }

View File

@ -50,7 +50,7 @@ namespace kiwano
// 应用 // 应用
class KGE_API Application class KGE_API Application
: protected noncopyable : protected core::noncopyable
{ {
public: public:
Application(); Application();

View File

@ -129,7 +129,7 @@ namespace kiwano
// 几何体生成器 // 几何体生成器
class KGE_API GeometrySink class KGE_API GeometrySink
: protected noncopyable : protected core::noncopyable
{ {
public: public:
GeometrySink(); GeometrySink();

View File

@ -41,7 +41,7 @@ namespace kiwano
// äÖȾĿ±ê // äÖȾĿ±ê
class KGE_API RenderTarget class KGE_API RenderTarget
: public noncopyable : public core::noncopyable
{ {
public: public:
bool IsValid() const; bool IsValid() const;

View File

@ -83,7 +83,7 @@ namespace kiwano
KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str()); KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str());
return false; return false;
} }
catch (json_exception& e) catch (core::json_exception& e)
{ {
KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot parse to JSON. (%s)", string_to_wide(e.what()).c_str()); KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot parse to JSON. (%s)", string_to_wide(e.what()).c_str());
return false; return false;
@ -163,7 +163,7 @@ namespace kiwano
{ {
if (auto root = doc->FirstChildElement(L"resources")) if (auto root = doc->FirstChildElement(L"resources"))
{ {
kiwano::wstring version; String version;
if (auto ver = root->FirstChildElement(L"version")) version = ver->GetText(); if (auto ver = root->FirstChildElement(L"version")) version = ver->GetText();
auto load = load_xml_funcs.find(version); auto load = load_xml_funcs.find(version);