Magic_Game/src/kiwano-physics/Fixture.h

256 lines
6.4 KiB
C
Raw Normal View History

2019-10-22 16:49:34 +08:00
// Copyright (c) 2018-2019 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include <kiwano-physics/helper.h>
#include <kiwano-physics/Shape.h>
namespace kiwano
{
namespace physics
{
2019-11-06 16:29:36 +08:00
class Body;
2019-10-22 16:49:34 +08:00
2019-12-31 16:01:41 +08:00
/**
* \addtogroup Physics
* @{
*/
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>о<EFBFBD>
2019-11-06 16:29:36 +08:00
class Fixture
2019-10-22 16:49:34 +08:00
{
public:
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20>о߲<D0BE><DFB2><EFBFBD>
2019-11-01 16:23:46 +08:00
struct Param
{
2019-12-31 16:01:41 +08:00
float density = 0.f; ///< <20>ܶ<EFBFBD>
float friction = 0.2f; ///< Ħ<><C4A6><EFBFBD><EFBFBD>
float restitution = 0.f; ///< <20><><EFBFBD>Իָ<D4BB>
bool is_sensor = false; ///< <20>Ƿ<EFBFBD><C7B7>ǽӴ<C7BD><D3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-11-01 16:23:46 +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)
{}
};
2019-11-06 16:29:36 +08:00
Fixture();
Fixture(b2Fixture* fixture);
Fixture(Body* body, Shape* shape, const Param& param);
2019-10-22 16:49:34 +08:00
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
bool IsValid() const;
/// \~chinese
/// @brief <20><>ȡ<EFBFBD>о<EFBFBD><D0BE><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>
Body* GetBody() const;
2019-10-31 20:34:38 +08:00
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20><>״
2019-11-06 16:29:36 +08:00
Shape GetShape() const;
2019-10-31 20:34:38 +08:00
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7>ǽӴ<C7BD><D3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool IsSensor() const;
2019-10-22 16:49:34 +08:00
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>üо<C3BC><D0BE>Ƿ<EFBFBD><C7B7>ǽӴ<C7BD><D3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @details <20>Ӵ<EFBFBD><D3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><D3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӱ<EFBFBD><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>
void SetSensor(bool sensor);
2019-11-01 16:23:46 +08:00
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD>оߵ<D0BE><DFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-11-01 16:23:46 +08:00
void GetMassData(float* mass, Point* center, float* inertia) const;
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD>ܶ<EFBFBD>
float GetDensity() const;
2019-11-01 16:23:46 +08:00
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ܶ<EFBFBD>
void SetDensity(float density);
2019-11-01 16:23:46 +08:00
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20><>ȡĦ<C8A1><C4A6><EFBFBD><EFBFBD> [N]
float GetFriction() const;
2019-11-01 16:23:46 +08:00
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>Ħ<EFBFBD><C4A6><EFBFBD><EFBFBD> [N]
void SetFriction(float friction);
2019-11-01 16:23:46 +08:00
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD>Իָ<D4BB>
float GetRestitution() const;
2019-10-23 16:49:34 +08:00
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>õ<EFBFBD><C3B5>Իָ<D4BB>
void SetRestitution(float restitution);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool TestPoint(const Point& p) const;
b2Fixture* GetB2Fixture() const;
void SetB2Fixture(b2Fixture* fixture);
2019-10-22 16:49:34 +08:00
2020-01-07 17:44:51 +08:00
bool operator== (const Fixture& rhs) const;
bool operator!= (const Fixture& rhs) const;
2019-12-23 18:05:08 +08:00
private:
2019-10-22 16:49:34 +08:00
b2Fixture* fixture_;
};
2019-12-31 16:01:41 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>о<EFBFBD><D0BE>б<EFBFBD>
class FixtureList
: public List<Fixture>
{
2020-01-06 00:15:47 +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:
IteratorImpl(const _Ty& elem)
: elem_(elem)
{
}
inline typename herit::reference operator*() const
{
return const_cast<typename herit::reference>(elem_);
}
inline typename herit::pointer operator->() const
{
return std::pointer_traits<typename herit::pointer>::pointer_to(**this);
}
inline IteratorImpl& operator++()
{
2020-01-07 17:44:51 +08:00
elem_ = elem_.GetB2Fixture()->GetNext();
2020-01-06 00:15:47 +08:00
return *this;
}
inline IteratorImpl operator++(int)
{
IteratorImpl old = *this;
operator++();
return old;
}
inline bool operator== (const IteratorImpl& rhs) const
{
2020-01-07 17:44:51 +08:00
return elem_ == rhs.elem_;
2020-01-06 00:15:47 +08:00
}
inline bool operator!= (const IteratorImpl& rhs) const
{
return !operator==(rhs);
}
private:
_Ty elem_;
};
2019-12-31 16:01:41 +08:00
public:
2020-01-06 00:15:47 +08:00
using value_type = Fixture;
using iterator = IteratorImpl<value_type>;
using const_iterator = IteratorImpl<const value_type>;
inline FixtureList()
2019-12-31 16:01:41 +08:00
{
}
2020-01-06 00:15:47 +08:00
inline FixtureList(const value_type& first)
: first_(first)
2019-12-31 16:01:41 +08:00
{
}
2020-01-06 00:15:47 +08:00
inline const value_type& front() const
{
return first_;
}
inline value_type& front()
{
return first_;
}
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:
value_type first_;
2019-12-31 16:01:41 +08:00
};
/** @} */
2020-01-07 17:44:51 +08:00
inline bool Fixture::IsSensor() const { KGE_ASSERT(fixture_); return fixture_->IsSensor(); }
inline void Fixture::SetSensor(bool sensor) { KGE_ASSERT(fixture_); fixture_->SetSensor(sensor); }
inline float Fixture::GetDensity() const { KGE_ASSERT(fixture_); return fixture_->GetDensity(); }
inline void Fixture::SetDensity(float density) { KGE_ASSERT(fixture_); fixture_->SetDensity(density); }
inline float Fixture::GetFriction() const { KGE_ASSERT(fixture_); return fixture_->GetFriction(); }
inline void Fixture::SetFriction(float friction) { KGE_ASSERT(fixture_); fixture_->SetFriction(friction); }
inline float Fixture::GetRestitution() const { KGE_ASSERT(fixture_); return fixture_->GetRestitution(); }
inline void Fixture::SetRestitution(float restitution) { KGE_ASSERT(fixture_); fixture_->SetRestitution(restitution); }
inline bool Fixture::IsValid() const { return fixture_ != nullptr; }
inline b2Fixture* Fixture::GetB2Fixture() const { return fixture_; }
inline void Fixture::SetB2Fixture(b2Fixture* fixture) { fixture_ = fixture; }
inline bool Fixture::operator==(const Fixture& rhs) const { return fixture_ == rhs.fixture_; }
inline bool Fixture::operator!=(const Fixture& rhs) const { return fixture_ != rhs.fixture_; }
2019-10-22 16:49:34 +08:00
}
}