Magic_Game/src/kiwano-physics/Fixture.h

361 lines
8.5 KiB
C
Raw Normal View History

2019-10-22 16:49:34 +08:00
// Copyright (c) 2018-2019 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
2019-10-22 16:49:34 +08:00
// 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:
2020-01-21 10:09:55 +08:00
//
2019-10-22 16:49:34 +08:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
2019-10-22 16:49:34 +08:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2020-01-21 10:09:55 +08:00
#include <kiwano-physics/helper.h>
2019-10-22 16:49:34 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
namespace physics
{
class Body;
2020-02-06 16:54:47 +08:00
KGE_DECLARE_SMART_PTR(Fixture);
2020-01-21 10:09:55 +08:00
/**
* \addtogroup Physics
* @{
*/
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 物理夹具
2020-02-06 16:54:47 +08:00
class Fixture : public virtual ObjectBase
2020-01-21 10:09:55 +08:00
{
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 夹具参数
2020-01-21 10:09:55 +08:00
struct Param
{
2020-02-10 17:32:04 +08:00
float density = 0.0f; ///< 密度
float friction = 0.2f; ///< 摩擦力
float restitution = 0.0f; ///< 弹性恢复
bool is_sensor = false; ///< 是否是接触传感器
2020-01-21 10:09:55 +08:00
Param() {}
Param(float density, float friction = 0.2f, float restitution = 0.f, bool is_sensor = false)
: density(density)
, friction(friction)
, restitution(restitution)
, is_sensor(is_sensor)
{
}
};
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建圆形夹具
/// @param body 添加夹具的物体
/// @param param 夹具参数
/// @param radius 圆形半径
/// @param offset 偏移量
2020-02-06 16:54:47 +08:00
static FixturePtr CreateCircle(Body* body, Param const& param, float radius, Point const& offset = Point());
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建矩形夹具
/// @param body 添加夹具的物体
/// @param param 夹具参数
/// @param size 矩形大小
/// @param offset 偏移量
/// @param rotation 旋转角度
2020-02-06 16:54:47 +08:00
static FixturePtr CreateRect(Body* body, Param const& param, Size const& size, Point const& offset = Point(),
float rotation = 0.f);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建多边形夹具
/// @param body 添加夹具的物体
/// @param param 夹具参数
/// @param vertexs 多边形顶点
2020-02-06 16:54:47 +08:00
static FixturePtr CreatePolygon(Body* body, Param const& param, Vector<Point> const& vertexs);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建边夹具
/// @param body 添加夹具的物体
/// @param param 夹具参数
/// @param p1 边的起点
/// @param p2 边的终点
2020-02-06 16:54:47 +08:00
static FixturePtr CreateEdge(Body* body, Param const& param, Point const& p1, Point const& p2);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建链条夹具
/// @param body 添加夹具的物体
/// @param param 夹具参数
/// @param vertexs 链条顶点
/// @param loop 是否连接链条的起点和终点
2020-02-06 16:54:47 +08:00
static FixturePtr CreateChain(Body* body, Param const& param, Vector<Point> const& vertexs, bool loop = false);
2020-01-21 10:09:55 +08:00
Fixture();
2020-02-06 16:54:47 +08:00
virtual ~Fixture();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否有效
2020-01-21 10:09:55 +08:00
bool IsValid() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取夹具所在的物体
2020-01-21 10:09:55 +08:00
Body* GetBody() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否是接触传感器
2020-01-21 10:09:55 +08:00
bool IsSensor() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置夹具是否是接触传感器
/// @details 接触传感器只会产生物理接触,而不会影响物体运动
2020-01-21 10:09:55 +08:00
void SetSensor(bool sensor);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取夹具的质量数据
2020-01-21 10:09:55 +08:00
void GetMassData(float* mass, Point* center, float* inertia) const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取密度
2020-01-21 10:09:55 +08:00
float GetDensity() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置密度
2020-01-21 10:09:55 +08:00
void SetDensity(float density);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取摩擦力 [N]
2020-01-21 10:09:55 +08:00
float GetFriction() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置摩擦力 [N]
2020-01-21 10:09:55 +08:00
void SetFriction(float friction);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取弹性恢复
2020-01-21 10:09:55 +08:00
float GetRestitution() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置弹性恢复
2020-01-21 10:09:55 +08:00
void SetRestitution(float restitution);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 点测试
2020-01-21 10:09:55 +08:00
bool TestPoint(const Point& p) const;
b2Fixture* GetB2Fixture() const;
2020-02-06 16:54:47 +08:00
void SetB2Fixture(b2Fixture* fixture);
2020-01-21 10:09:55 +08:00
bool operator==(const Fixture& rhs) const;
bool operator!=(const Fixture& rhs) const;
private:
b2Fixture* fixture_;
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 物理夹具列表
2020-02-06 16:54:47 +08:00
class FixtureList
2020-01-21 10:09:55 +08:00
{
template <typename _Ty>
class IteratorImpl : public std::iterator<std::forward_iterator_tag, _Ty>
{
using herit = std::iterator<std::forward_iterator_tag, _Ty>;
public:
2020-02-06 16:54:47 +08:00
using reference = typename herit::reference;
using pointer = typename herit::pointer;
IteratorImpl(pointer elem)
2020-01-21 10:09:55 +08:00
: elem_(elem)
{
}
2020-02-06 16:54:47 +08:00
inline reference operator*() const
2020-01-21 10:09:55 +08:00
{
2020-02-06 16:54:47 +08:00
return const_cast<typename herit::reference>(*elem_);
2020-01-21 10:09:55 +08:00
}
2020-02-06 16:54:47 +08:00
inline pointer operator->() const
2020-01-21 10:09:55 +08:00
{
return std::pointer_traits<typename herit::pointer>::pointer_to(**this);
}
inline IteratorImpl& operator++()
{
2020-02-06 16:54:47 +08:00
b2Fixture* next = elem_->GetB2Fixture()->GetNext();
elem_ = static_cast<Fixture*>(next->GetUserData());
2020-01-21 10:09:55 +08:00
return *this;
}
inline IteratorImpl operator++(int)
{
IteratorImpl old = *this;
2020-02-06 16:54:47 +08:00
operator++();
2020-01-21 10:09:55 +08:00
return old;
}
inline bool operator==(const IteratorImpl& rhs) const
{
return elem_ == rhs.elem_;
}
inline bool operator!=(const IteratorImpl& rhs) const
{
return !operator==(rhs);
}
private:
2020-02-06 16:54:47 +08:00
pointer elem_;
2020-01-21 10:09:55 +08:00
};
public:
using value_type = Fixture;
using iterator = IteratorImpl<value_type>;
using const_iterator = IteratorImpl<const value_type>;
2020-02-06 16:54:47 +08:00
inline FixtureList()
: first_(nullptr)
{
}
2020-01-21 10:09:55 +08:00
2020-02-06 16:54:47 +08:00
inline FixtureList(value_type* first)
2020-01-21 10:09:55 +08:00
: first_(first)
{
}
inline const value_type& front() const
{
2020-02-06 16:54:47 +08:00
return *first_;
2020-01-21 10:09:55 +08:00
}
inline value_type& front()
{
2020-02-06 16:54:47 +08:00
return *first_;
2020-01-21 10:09:55 +08:00
}
inline iterator begin()
{
return iterator(first_);
}
inline const_iterator begin() const
{
return cbegin();
}
inline const_iterator cbegin() const
{
return const_iterator(first_);
}
inline iterator end()
{
return iterator(nullptr);
}
inline const_iterator end() const
{
return cend();
}
inline const_iterator cend() const
{
return const_iterator(nullptr);
}
private:
2020-02-06 16:54:47 +08:00
value_type* first_;
2020-01-21 10:09:55 +08:00
};
/** @} */
inline bool Fixture::IsSensor() const
{
KGE_ASSERT(fixture_);
return fixture_->IsSensor();
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline void Fixture::SetSensor(bool sensor)
{
KGE_ASSERT(fixture_);
fixture_->SetSensor(sensor);
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline float Fixture::GetDensity() const
{
KGE_ASSERT(fixture_);
return fixture_->GetDensity();
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline void Fixture::SetDensity(float density)
{
KGE_ASSERT(fixture_);
fixture_->SetDensity(density);
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline float Fixture::GetFriction() const
{
KGE_ASSERT(fixture_);
return fixture_->GetFriction();
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline void Fixture::SetFriction(float friction)
{
KGE_ASSERT(fixture_);
fixture_->SetFriction(friction);
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline float Fixture::GetRestitution() const
{
KGE_ASSERT(fixture_);
return fixture_->GetRestitution();
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline void Fixture::SetRestitution(float restitution)
{
KGE_ASSERT(fixture_);
fixture_->SetRestitution(restitution);
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline bool Fixture::IsValid() const
{
return fixture_ != nullptr;
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline b2Fixture* Fixture::GetB2Fixture() const
{
return fixture_;
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline void Fixture::SetB2Fixture(b2Fixture* fixture)
{
fixture_ = fixture;
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline bool Fixture::operator==(const Fixture& rhs) const
{
return fixture_ == rhs.fixture_;
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
inline bool Fixture::operator!=(const Fixture& rhs) const
{
return fixture_ != rhs.fixture_;
2019-10-22 16:49:34 +08:00
}
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
} // namespace physics
} // namespace kiwano