Kiwano Engine  v1.2.x
Contact.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/Fixture.h>
23 
24 namespace kiwano
25 {
26 namespace physics
27 {
28 
34 class KGE_API Contact
37 {
38 public:
39  Contact();
40 
43  bool IsTouching() const;
44 
47  void SetEnabled(bool flag);
48 
51  bool IsEnabled() const;
52 
55  Fixture* GetFixtureA() const;
56 
59  Fixture* GetFixtureB() const;
60 
63  PhysicBody* GetBodyA() const;
64 
67  PhysicBody* GetBodyB() const;
68 
71  void SetFriction(float friction);
72 
75  float GetFriction() const;
76 
79  void ResetFriction();
80 
83  void SetRestitution(float restitution);
84 
87  float GetRestitution() const;
88 
91  void ResetRestitution();
92 
95  void SetTangentSpeed(float speed);
96 
99  float GetTangentSpeed() const;
100 
103  b2Contact* GetB2Contact() const;
104 
107  void SetB2Contact(b2Contact* contact);
108 
109  bool operator==(const Contact& rhs) const;
110  bool operator!=(const Contact& rhs) const;
111 
112 private:
113  b2Contact* contact_;
114 };
115 
119 {
120  template <typename _Ty>
121  class IteratorImpl
122  {
123  public:
124  using iterator_category = std::forward_iterator_tag;
125  using value_type = _Ty;
126  using pointer = _Ty*;
127  using reference = _Ty&;
128  using difference_type = ptrdiff_t;
129 
130  IteratorImpl(const _Ty& elem)
131  : elem_(elem)
132  {
133  }
134 
135  inline reference operator*() const
136  {
137  return const_cast<reference>(elem_);
138  }
139 
140  inline pointer operator->() const
141  {
142  return std::pointer_traits<pointer>::pointer_to(**this);
143  }
144 
145  inline IteratorImpl& operator++()
146  {
147  elem_.SetB2Contact(elem_.GetB2Contact()->GetNext());
148  return *this;
149  }
150 
151  inline IteratorImpl operator++(int)
152  {
153  IteratorImpl old = *this;
154  operator++();
155  return old;
156  }
157 
158  inline bool operator==(const IteratorImpl& rhs) const
159  {
160  return elem_ == rhs.elem_;
161  }
162 
163  inline bool operator!=(const IteratorImpl& rhs) const
164  {
165  return !operator==(rhs);
166  }
167 
168  private:
169  _Ty elem_;
170  };
171 
172 public:
173  using value_type = Contact;
174  using iterator = IteratorImpl<value_type>;
175  using const_iterator = IteratorImpl<const value_type>;
176 
177  inline ContactList() {}
178 
179  inline ContactList(const value_type& first)
180  : first_(first)
181  {
182  }
183 
184  inline const value_type& front() const
185  {
186  return first_;
187  }
188 
189  inline value_type& front()
190  {
191  return first_;
192  }
193 
194  inline iterator begin()
195  {
196  return iterator(first_);
197  }
198 
199  inline const_iterator begin() const
200  {
201  return cbegin();
202  }
203 
204  inline const_iterator cbegin() const
205  {
206  return const_iterator(first_);
207  }
208 
209  inline iterator end()
210  {
211  return iterator(Contact());
212  }
213 
214  inline const_iterator end() const
215  {
216  return cend();
217  }
218 
219  inline const_iterator cend() const
220  {
221  return const_iterator(Contact());
222  }
223 
224 private:
225  value_type first_;
226 };
227 
230 inline bool Contact::IsTouching() const
231 {
232  KGE_ASSERT(contact_);
233  return contact_->IsTouching();
234 }
235 
236 inline void Contact::SetEnabled(bool flag)
237 {
238  KGE_ASSERT(contact_);
239  contact_->SetEnabled(flag);
240 }
241 
242 inline bool Contact::IsEnabled() const
243 {
244  KGE_ASSERT(contact_);
245  return contact_->IsEnabled();
246 }
247 
248 inline void Contact::SetFriction(float friction)
249 {
250  KGE_ASSERT(contact_);
251  contact_->SetFriction(friction);
252 }
253 
254 inline float Contact::GetFriction() const
255 {
256  KGE_ASSERT(contact_);
257  return contact_->GetFriction();
258 }
259 
261 {
262  KGE_ASSERT(contact_);
263  contact_->ResetFriction();
264 }
265 
266 inline void Contact::SetRestitution(float restitution)
267 {
268  KGE_ASSERT(contact_);
269  contact_->SetRestitution(restitution);
270 }
271 
272 inline float Contact::GetRestitution() const
273 {
274  KGE_ASSERT(contact_);
275  return contact_->GetRestitution();
276 }
277 
279 {
280  KGE_ASSERT(contact_);
281  contact_->ResetRestitution();
282 }
283 
284 inline b2Contact* Contact::GetB2Contact() const
285 {
286  return contact_;
287 }
288 
289 inline void Contact::SetB2Contact(b2Contact* contact)
290 {
291  contact_ = contact;
292 }
293 
294 inline bool Contact::operator==(const Contact& rhs) const
295 {
296  return contact_ == rhs.contact_;
297 }
298 
299 inline bool Contact::operator!=(const Contact& rhs) const
300 {
301  return contact_ != rhs.contact_;
302 }
303 
304 } // namespace physics
305 } // namespace kiwano
void ResetRestitution()
重置弹性恢复
Definition: Contact.h:278
void SetB2Contact(b2Contact *contact)
设置b2Contact
Definition: Contact.h:289
void SetRestitution(float restitution)
设置弹性恢复
Definition: Contact.h:266
void ResetFriction()
重置摩擦力
Definition: Contact.h:260
bool IsTouching() const
是否是接触
Definition: Contact.h:230
物理接触列表
Definition: Contact.h:118
Definition: Actor.cpp:26
b2Contact * GetB2Contact() const
获取b2Contact
Definition: Contact.h:284
float GetFriction() const
获取摩擦力
Definition: Contact.h:254
void SetFriction(float friction)
设置摩擦力
Definition: Contact.h:248
void SetEnabled(bool flag)
启用或禁用 (仅作用于一个时间步)
Definition: Contact.h:236
float GetRestitution() const
获取弹性恢复
Definition: Contact.h:272
bool IsEnabled() const
是否启用
Definition: Contact.h:242
物理接触
Definition: Contact.h:36