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/Shape.h>
|
|
|
|
|
#include <kiwano-physics/World.h>
|
|
|
|
|
|
|
|
|
|
namespace kiwano
|
|
|
|
|
{
|
|
|
|
|
namespace physics
|
|
|
|
|
{
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicShape::PhysicShape()
|
2019-10-18 11:50:46 +08:00
|
|
|
: shape_(nullptr)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicShape::PhysicShape(b2Shape* shape)
|
2019-10-18 11:50:46 +08:00
|
|
|
: shape_(shape)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
b2Shape* PhysicShape::GetB2Shape()
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
return shape_;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
const b2Shape* PhysicShape::GetB2Shape() const
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
return shape_;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicShape::SetB2Shape(b2Shape* shape)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
shape_ = shape;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2019-10-30 23:12:18 +08:00
|
|
|
// PhysicCircleShape
|
2019-10-18 11:50:46 +08:00
|
|
|
//
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicCircleShape::PhysicCircleShape()
|
|
|
|
|
: PhysicShape(&circle_)
|
2019-10-18 11:50:46 +08:00
|
|
|
, circle_()
|
|
|
|
|
, radius_(0.f)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicCircleShape::PhysicCircleShape(float radius, Point const& offset)
|
|
|
|
|
: PhysicCircleShape()
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
2019-10-22 16:49:34 +08:00
|
|
|
Set(radius, offset);
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicCircleShape::Set(float radius, Point const& offset)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
radius_ = radius;
|
2019-10-22 16:49:34 +08:00
|
|
|
offset_ = offset;
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicCircleShape::FitWorld(PhysicWorld* world)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(world);
|
|
|
|
|
circle_.m_radius = world->Stage2World(radius_);
|
2019-10-22 16:49:34 +08:00
|
|
|
circle_.m_p = world->Stage2World(offset_);
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2019-10-30 23:12:18 +08:00
|
|
|
// PhysicBoxShape
|
2019-10-18 11:50:46 +08:00
|
|
|
//
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicBoxShape::PhysicBoxShape()
|
|
|
|
|
: PhysicShape(&polygon_)
|
2019-10-18 11:50:46 +08:00
|
|
|
, polygon_()
|
2019-10-22 16:49:34 +08:00
|
|
|
, rotation_(0.f)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicBoxShape::PhysicBoxShape(Vec2 const& size, Point const& offset, float rotation)
|
|
|
|
|
: PhysicBoxShape()
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
2019-10-22 16:49:34 +08:00
|
|
|
Set(size, offset, rotation);
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicBoxShape::Set(Vec2 const& size, Point const& offset, float rotation)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
box_size_ = size;
|
2019-10-22 16:49:34 +08:00
|
|
|
offset_ = offset;
|
|
|
|
|
rotation_ = rotation;
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicBoxShape::FitWorld(PhysicWorld* world)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(world);
|
|
|
|
|
|
|
|
|
|
b2Vec2 box = world->Stage2World(box_size_);
|
2019-10-22 16:49:34 +08:00
|
|
|
b2Vec2 offset = world->Stage2World(offset_);
|
|
|
|
|
polygon_.SetAsBox(box.x / 2, box.y / 2, offset, rotation_);
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2019-10-30 23:12:18 +08:00
|
|
|
// PhysicPolygonShape
|
2019-10-18 11:50:46 +08:00
|
|
|
//
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicPolygonShape::PhysicPolygonShape()
|
|
|
|
|
: PhysicShape(&polygon_)
|
2019-10-18 11:50:46 +08:00
|
|
|
, polygon_()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicPolygonShape::PhysicPolygonShape(Vector<Point> const& vertexs)
|
|
|
|
|
: PhysicPolygonShape()
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
Set(vertexs);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicPolygonShape::Set(Vector<Point> const& vertexs)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
vertexs_ = vertexs;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicPolygonShape::FitWorld(PhysicWorld* world)
|
2019-10-18 11:50:46 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(world);
|
|
|
|
|
|
|
|
|
|
Vector<b2Vec2> b2vertexs;
|
|
|
|
|
b2vertexs.reserve(vertexs_.size());
|
|
|
|
|
for (const auto& v : vertexs_)
|
|
|
|
|
{
|
|
|
|
|
b2vertexs.push_back(world->Stage2World(v));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
polygon_.Set(&b2vertexs[0], static_cast<int32>(b2vertexs.size()));
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 16:49:34 +08:00
|
|
|
//
|
2019-10-30 23:12:18 +08:00
|
|
|
// PhysicEdgeShape
|
2019-10-22 16:49:34 +08:00
|
|
|
//
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicEdgeShape::PhysicEdgeShape()
|
|
|
|
|
: PhysicShape(&edge_)
|
2019-10-22 16:49:34 +08:00
|
|
|
, edge_()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicEdgeShape::PhysicEdgeShape(Point const& p1, Point const& p2)
|
|
|
|
|
: PhysicEdgeShape()
|
2019-10-22 16:49:34 +08:00
|
|
|
{
|
|
|
|
|
Set(p1, p2);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicEdgeShape::Set(Point const& p1, Point const& p2)
|
2019-10-22 16:49:34 +08:00
|
|
|
{
|
|
|
|
|
p_[0] = p1;
|
|
|
|
|
p_[1] = p2;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicEdgeShape::FitWorld(PhysicWorld* world)
|
2019-10-22 16:49:34 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(world);
|
|
|
|
|
|
|
|
|
|
b2Vec2 p1 = world->Stage2World(p_[0]);
|
|
|
|
|
b2Vec2 p2 = world->Stage2World(p_[1]);
|
|
|
|
|
edge_.Set(p1, p2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2019-10-30 23:12:18 +08:00
|
|
|
// PhysicChainShape
|
2019-10-22 16:49:34 +08:00
|
|
|
//
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicChainShape::PhysicChainShape()
|
|
|
|
|
: PhysicShape(&chain_)
|
2019-10-22 16:49:34 +08:00
|
|
|
, chain_()
|
|
|
|
|
, loop_(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
PhysicChainShape::PhysicChainShape(Vector<Point> const& vertexs, bool loop)
|
|
|
|
|
: PhysicChainShape()
|
2019-10-22 16:49:34 +08:00
|
|
|
{
|
|
|
|
|
Set(vertexs, loop);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicChainShape::Set(Vector<Point> const& vertexs, bool loop)
|
2019-10-22 16:49:34 +08:00
|
|
|
{
|
|
|
|
|
vertexs_ = vertexs;
|
|
|
|
|
loop_ = loop;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 23:12:18 +08:00
|
|
|
void PhysicChainShape::FitWorld(PhysicWorld* world)
|
2019-10-22 16:49:34 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(world);
|
|
|
|
|
|
|
|
|
|
Vector<b2Vec2> b2vertexs;
|
|
|
|
|
b2vertexs.reserve(vertexs_.size());
|
|
|
|
|
for (const auto& v : vertexs_)
|
|
|
|
|
{
|
|
|
|
|
b2vertexs.push_back(world->Stage2World(v));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loop_)
|
|
|
|
|
{
|
|
|
|
|
chain_.CreateLoop(&b2vertexs[0], static_cast<int32>(b2vertexs.size()));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
chain_.CreateChain(&b2vertexs[0], static_cast<int32>(b2vertexs.size()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2019-10-18 11:50:46 +08:00
|
|
|
}
|