diff --git a/src/kiwano-physics/Contact.h b/src/kiwano-physics/Contact.h index 6d081ace..8a33d049 100644 --- a/src/kiwano-physics/Contact.h +++ b/src/kiwano-physics/Contact.h @@ -146,42 +146,223 @@ namespace kiwano class ContactList : public List { - public: - ContactList() + template + class IteratorImpl + : public std::iterator { - } + using herit = std::iterator; - ContactList(const Contact& first) - { - Contact current = first; - while (current.GetB2Contact()) + public: + IteratorImpl(const _Ty& elem) + : elem_(elem) { - push_back(current); - current = current.GetB2Contact()->GetNext(); } - } - }; + inline typename herit::reference operator*() const + { + return const_cast(elem_); + } + + inline typename herit::pointer operator->() const + { + return std::pointer_traits::pointer_to(**this); + } + + inline IteratorImpl& operator++() + { + elem_ = elem_.GetB2Contact()->GetNext(); + return *this; + } + + inline IteratorImpl operator++(int) + { + IteratorImpl old = *this; + operator++(); + return old; + } + + inline bool operator== (const IteratorImpl& rhs) const + { + return elem_.GetB2Contact() == rhs.elem_.GetB2Contact(); + } + + inline bool operator!= (const IteratorImpl& rhs) const + { + return !operator==(rhs); + } + + private: + _Ty elem_; + }; + + public: + using value_type = Contact; + using iterator = IteratorImpl; + using const_iterator = IteratorImpl; + + inline ContactList() + { + } + + inline ContactList(const value_type& first) + : first_(first) + { + } + + 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_; + }; /// \~chinese /// @brief 物理接触边列表 class ContactEdgeList - : public List { + template + class IteratorImpl + : public std::iterator + { + using herit = std::iterator; + + public: + + inline IteratorImpl(const _Ty& elem) + : elem_(elem) + { + } + + inline typename herit::reference operator*() const + { + return const_cast(elem_); + } + + inline typename herit::pointer operator->() const + { + return std::pointer_traits::pointer_to(**this); + } + + inline IteratorImpl& operator++() + { + elem_ = elem_.GetB2ContactEdge()->next; + return *this; + } + + inline IteratorImpl operator++(int) + { + IteratorImpl old = *this; + operator++(); + return old; + } + + inline bool operator== (const IteratorImpl& rhs) const + { + return elem_.GetB2ContactEdge() == rhs.elem_.GetB2ContactEdge(); + } + + inline bool operator!= (const IteratorImpl& rhs) const + { + return !operator==(rhs); + } + + private: + _Ty elem_; + }; + public: - ContactEdgeList() + using value_type = ContactEdge; + using iterator = IteratorImpl; + using const_iterator = IteratorImpl; + + inline ContactEdgeList() { } - ContactEdgeList(const ContactEdge& first) + inline ContactEdgeList(const value_type& first) + : first_(first) { - ContactEdge current = first; - while (current.GetB2ContactEdge()) - { - push_back(current); - current = current.GetB2ContactEdge()->next; - } } + + 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_; }; /** @} */ diff --git a/src/kiwano-physics/Fixture.h b/src/kiwano-physics/Fixture.h index 3464ad55..0ff4dea2 100644 --- a/src/kiwano-physics/Fixture.h +++ b/src/kiwano-physics/Fixture.h @@ -126,20 +126,111 @@ namespace kiwano class FixtureList : public List { + template + class IteratorImpl + : public std::iterator + { + using herit = std::iterator; + + public: + IteratorImpl(const _Ty& elem) + : elem_(elem) + { + } + + inline typename herit::reference operator*() const + { + return const_cast(elem_); + } + + inline typename herit::pointer operator->() const + { + return std::pointer_traits::pointer_to(**this); + } + + inline IteratorImpl& operator++() + { + elem_ = elem_.GetB2Contact()->GetNext(); + return *this; + } + + inline IteratorImpl operator++(int) + { + IteratorImpl old = *this; + operator++(); + return old; + } + + inline bool operator== (const IteratorImpl& rhs) const + { + return elem_.GetB2Contact() == rhs.elem_.GetB2Contact(); + } + + inline bool operator!= (const IteratorImpl& rhs) const + { + return !operator==(rhs); + } + + private: + _Ty elem_; + }; + public: - FixtureList() + using value_type = Fixture; + using iterator = IteratorImpl; + using const_iterator = IteratorImpl; + + inline FixtureList() { } - FixtureList(const Fixture& first) + inline FixtureList(const value_type& first) + : first_(first) { - Fixture current = first; - while (current.GetB2Fixture()) - { - push_back(current); - current = current.GetB2Fixture()->GetNext(); - } } + + 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_; }; /** @} */ diff --git a/src/kiwano-physics/World.cpp b/src/kiwano-physics/World.cpp index 8abf156b..8c47eee4 100644 --- a/src/kiwano-physics/World.cpp +++ b/src/kiwano-physics/World.cpp @@ -225,6 +225,11 @@ namespace kiwano world_.SetGravity(b2Vec2(gravity.x, gravity.y)); } + ContactList World::GetContactList() + { + return ContactList(Contact(world_.GetContactList())); + } + void World::Update(Duration dt) { world_.Step(dt.Seconds(), vel_iter_, pos_iter_); diff --git a/src/kiwano-physics/World.h b/src/kiwano-physics/World.h index 8a86d080..ef5914c2 100644 --- a/src/kiwano-physics/World.h +++ b/src/kiwano-physics/World.h @@ -61,6 +61,10 @@ namespace kiwano /// @brief 设置重力 [N] void SetGravity(Vec2 gravity); + /// \~chinese + /// @brief 获取物理接触列表 + ContactList GetContactList(); + /// \~chinese /// @brief 获取全局缩放比例 /// @details 缩放比例是指由物理世界的单位米转换到屏幕像素的比例,默认比例为1:100