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.
|
|
|
|
|
|
|
|
|
|
#include <kiwano-physics/Body.h>
|
|
|
|
|
#include <kiwano-physics/World.h>
|
|
|
|
|
|
|
|
|
|
namespace kiwano
|
|
|
|
|
{
|
|
|
|
|
namespace physics
|
|
|
|
|
{
|
2019-10-22 16:49:34 +08:00
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicBody::PhysicBody()
|
|
|
|
|
: body_(nullptr)
|
2019-10-18 11:50:46 +08:00
|
|
|
, actor_(nullptr)
|
|
|
|
|
, world_(nullptr)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicBody::PhysicBody(b2Body* body, Actor* actor)
|
|
|
|
|
: PhysicBody()
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
SetB2Body(body);
|
|
|
|
|
SetActor(actor);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicBody::PhysicBody(PhysicWorld* world, Actor* actor)
|
|
|
|
|
: PhysicBody()
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
2019-10-30 23:12:18 +08:00
|
|
|
Init(world, actor);
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicBody::~PhysicBody()
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
2019-10-30 23:12:18 +08:00
|
|
|
Destroy();
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
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)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(body_ && world_);
|
|
|
|
|
|
|
|
|
|
if (shape)
|
|
|
|
|
{
|
2019-10-30 23:12:18 +08:00
|
|
|
return PhysicFixture::Create(this, shape, density, friction, restitution);
|
2019-10-22 16:49:34 +08:00
|
|
|
}
|
2019-10-30 23:12:18 +08:00
|
|
|
return PhysicFixture();
|
2019-10-22 16:49:34 +08:00
|
|
|
}
|
2019-10-18 11:50:46 +08:00
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicFixture PhysicBody::AddCircleShape(float radius, float density)
|
2019-10-22 16:49:34 +08:00
|
|
|
{
|
2019-10-30 23:12:18 +08:00
|
|
|
return AddShape(&PhysicCircleShape(radius), density);
|
2019-10-22 16:49:34 +08:00
|
|
|
}
|
2019-10-18 11:50:46 +08:00
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicFixture PhysicBody::AddBoxShape(Vec2 const& size, float density)
|
2019-10-22 16:49:34 +08:00
|
|
|
{
|
2019-10-30 23:12:18 +08:00
|
|
|
return AddShape(&PhysicBoxShape(size), density);
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicFixture PhysicBody::AddPolygonShape(Vector<Point> const& vertexs, float density)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
2019-10-30 23:12:18 +08:00
|
|
|
return AddShape(&PhysicPolygonShape(vertexs), density);
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicFixture PhysicBody::AddEdgeShape(Point const& p1, Point const& p2, float density)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
2019-10-30 23:12:18 +08:00
|
|
|
return AddShape(&PhysicEdgeShape(p1, p2), density);
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicFixture PhysicBody::AddChainShape(Vector<Point> const& vertexs, bool loop, float density)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
2019-10-30 23:12:18 +08:00
|
|
|
return AddShape(&PhysicChainShape(vertexs, loop), density);
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicBody::RemoveFixture(PhysicFixture const& fixture)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
2019-10-22 16:49:34 +08:00
|
|
|
if (fixture.GetB2Fixture())
|
|
|
|
|
{
|
|
|
|
|
b2Fixture* ptr = const_cast<b2Fixture*>(fixture.GetB2Fixture());
|
|
|
|
|
body_->DestroyFixture(ptr);
|
|
|
|
|
}
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
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
|
2019-10-28 17:25:06 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(body_ && world_);
|
|
|
|
|
return world_->World2Stage(body_->GetPosition());
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
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
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
2019-10-22 16:49:34 +08:00
|
|
|
KGE_ASSERT(body_ && world_);
|
|
|
|
|
return world_->World2Stage(body_->GetLocalPoint(world_->Stage2World(world)));
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
Point PhysicBody::GetWorldPoint(Point const& local) const
|
2019-10-22 16:49:34 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(body_ && world_);
|
|
|
|
|
return world_->World2Stage(body_->GetWorldPoint(world_->Stage2World(local)));
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
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)
|
2019-10-23 16:49:34 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(body_ && world_);
|
|
|
|
|
body_->ApplyForce(b2Vec2(force.x, force.y), world_->Stage2World(point), wake);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicBody::ApplyForceToCenter(Vec2 const& force, bool wake)
|
2019-10-23 16:49:34 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(body_ && world_);
|
|
|
|
|
body_->ApplyForceToCenter(b2Vec2(force.x, force.y), wake);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicBody::ApplyTorque(float torque, bool wake)
|
2019-10-23 16:49:34 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(body_ && world_);
|
|
|
|
|
body_->ApplyTorque(torque, wake);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicBody::SetB2Body(b2Body* body)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
body_ = body;
|
|
|
|
|
if (body_)
|
|
|
|
|
{
|
|
|
|
|
body_->SetUserData(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicBody::Destroy()
|
|
|
|
|
{
|
|
|
|
|
if (world_)
|
|
|
|
|
{
|
|
|
|
|
world_->RemoveBody(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body_ = nullptr;
|
|
|
|
|
world_ = nullptr;
|
|
|
|
|
actor_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PhysicBody::UpdateActor()
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
if (actor_ && body_)
|
|
|
|
|
{
|
|
|
|
|
if (world_)
|
|
|
|
|
{
|
|
|
|
|
actor_->SetPosition(world_->World2Stage(body_->GetPosition()));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
actor_->SetPosition(World2Stage(body_->GetPosition()));
|
|
|
|
|
}
|
2019-10-30 23:12:18 +08:00
|
|
|
actor_->SetRotation(math::Radian2Degree(body_->GetAngle()));
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicBody::UpdateFromActor()
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
if (actor_ && body_)
|
|
|
|
|
{
|
|
|
|
|
if (world_)
|
|
|
|
|
{
|
|
|
|
|
body_->SetTransform(
|
|
|
|
|
world_->Stage2World(actor_->GetPosition()),
|
2019-10-30 23:12:18 +08:00
|
|
|
math::Degree2Radian(actor_->GetRotation())
|
2019-10-18 11:50:46 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
body_->SetTransform(
|
|
|
|
|
Stage2World(actor_->GetPosition()),
|
2019-10-30 23:12:18 +08:00
|
|
|
math::Degree2Radian(actor_->GetRotation())
|
2019-10-18 11:50:46 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 16:49:34 +08:00
|
|
|
}
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|