Update ContactList & FixtureList
This commit is contained in:
parent
8bd215403a
commit
fcb4f3bc63
|
|
@ -146,42 +146,223 @@ namespace kiwano
|
|||
class ContactList
|
||||
: public List<Contact>
|
||||
{
|
||||
public:
|
||||
ContactList()
|
||||
template <typename _Ty>
|
||||
class IteratorImpl
|
||||
: public std::iterator<std::forward_iterator_tag, _Ty>
|
||||
{
|
||||
}
|
||||
using herit = std::iterator<std::forward_iterator_tag, _Ty>;
|
||||
|
||||
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<typename herit::reference>(elem_);
|
||||
}
|
||||
|
||||
inline typename herit::pointer operator->() const
|
||||
{
|
||||
return std::pointer_traits<typename herit::pointer>::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<value_type>;
|
||||
using const_iterator = IteratorImpl<const value_type>;
|
||||
|
||||
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<ContactEdge>
|
||||
{
|
||||
template <typename _Ty>
|
||||
class IteratorImpl
|
||||
: public std::iterator<std::forward_iterator_tag, _Ty>
|
||||
{
|
||||
using herit = std::iterator<std::forward_iterator_tag, _Ty>;
|
||||
|
||||
public:
|
||||
|
||||
inline 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++()
|
||||
{
|
||||
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<value_type>;
|
||||
using const_iterator = IteratorImpl<const value_type>;
|
||||
|
||||
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_;
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
|
|
|||
|
|
@ -126,20 +126,111 @@ namespace kiwano
|
|||
class FixtureList
|
||||
: public List<Fixture>
|
||||
{
|
||||
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++()
|
||||
{
|
||||
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<value_type>;
|
||||
using const_iterator = IteratorImpl<const value_type>;
|
||||
|
||||
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_;
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
|
|
|||
|
|
@ -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_);
|
||||
|
|
|
|||
|
|
@ -61,6 +61,10 @@ namespace kiwano
|
|||
/// @brief 设置重力 [N]
|
||||
void SetGravity(Vec2 gravity);
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 获取物理接触列表
|
||||
ContactList GetContactList();
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 获取全局缩放比例
|
||||
/// @details 缩放比例是指由物理世界的单位米转换到屏幕像素的比例,默认比例为1:100
|
||||
|
|
|
|||
Loading…
Reference in New Issue