Kiwano Engine v1.2.x
ContactEdge.h
1// Copyright (c) 2018-2019 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano-physics/Contact.h>
23
24namespace kiwano
25{
26namespace physics
27{
35class KGE_API ContactEdge
36{
37public:
39
42 PhysicBody* GetOtherBody() const;
43
46 Contact GetContact() const;
47
50 b2ContactEdge* GetB2ContactEdge() const;
51
54 void SetB2ContactEdge(b2ContactEdge* edge);
55
56 bool operator==(const ContactEdge& rhs) const;
57 bool operator!=(const ContactEdge& rhs) const;
58
59private:
60 b2ContactEdge* edge_;
61};
62
66{
67 template <typename _Ty>
68 class IteratorImpl
69 {
70 public:
71 using iterator_category = std::forward_iterator_tag;
72 using value_type = _Ty;
73 using pointer = _Ty*;
74 using reference = _Ty&;
75 using difference_type = ptrdiff_t;
76
77 inline IteratorImpl(const _Ty& elem)
78 : elem_(elem)
79 {
80 }
81
82 inline reference operator*() const
83 {
84 return const_cast<reference>(elem_);
85 }
86
87 inline pointer operator->() const
88 {
89 return std::pointer_traits<pointer>::pointer_to(**this);
90 }
91
92 inline IteratorImpl& operator++()
93 {
94 elem_.SetB2ContactEdge(elem_.GetB2ContactEdge()->next);
95 return *this;
96 }
97
98 inline IteratorImpl operator++(int)
99 {
100 IteratorImpl old = *this;
101 operator++();
102 return old;
103 }
104
105 inline bool operator==(const IteratorImpl& rhs) const
106 {
107 return elem_ == rhs.elem_;
108 }
109
110 inline bool operator!=(const IteratorImpl& rhs) const
111 {
112 return !operator==(rhs);
113 }
114
115 private:
116 _Ty elem_;
117 };
118
119public:
120 using value_type = ContactEdge;
121 using iterator = IteratorImpl<value_type>;
122 using const_iterator = IteratorImpl<const value_type>;
123
124 inline ContactEdgeList() {}
125
126 inline ContactEdgeList(const value_type& first)
127 : first_(first)
128 {
129 }
130
131 inline const value_type& front() const
132 {
133 return first_;
134 }
135
136 inline value_type& front()
137 {
138 return first_;
139 }
140
141 inline iterator begin()
142 {
143 return iterator(first_);
144 }
145
146 inline const_iterator begin() const
147 {
148 return cbegin();
149 }
150
151 inline const_iterator cbegin() const
152 {
153 return const_iterator(first_);
154 }
155
156 inline iterator end()
157 {
158 return iterator(ContactEdge());
159 }
160
161 inline const_iterator end() const
162 {
163 return cend();
164 }
165
166 inline const_iterator cend() const
167 {
168 return const_iterator(ContactEdge());
169 }
170
171private:
172 value_type first_;
173};
174
178{
179 KGE_ASSERT(edge_);
180 return static_cast<PhysicBody*>(edge_->other->GetUserData());
181}
182
183inline b2ContactEdge* ContactEdge::GetB2ContactEdge() const
184{
185 return edge_;
186}
187
188inline void ContactEdge::SetB2ContactEdge(b2ContactEdge* edge)
189{
190 edge_ = edge;
191}
192
193inline bool ContactEdge::operator==(const ContactEdge& rhs) const
194{
195 return edge_ == rhs.edge_;
196}
197
198inline bool ContactEdge::operator!=(const ContactEdge& rhs) const
199{
200 return edge_ != rhs.edge_;
201}
202
203} // namespace physics
204} // namespace kiwano
物理接触边列表
Definition: ContactEdge.h:66
接触边
Definition: ContactEdge.h:36
b2ContactEdge * GetB2ContactEdge() const
获取b2ContactEdge
Definition: ContactEdge.h:183
void SetB2ContactEdge(b2ContactEdge *edge)
设置b2ContactEdge
Definition: ContactEdge.h:188
PhysicBody * GetOtherBody() const
获取接触物体
Definition: ContactEdge.h:177
物理接触
Definition: Contact.h:37
物体
Definition: PhysicBody.h:40