Kiwano Engine v1.2.x
Fixture.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 <memory>
23#include <kiwano-physics/Global.h>
24
25namespace kiwano
26{
27namespace physics
28{
29
30KGE_DECLARE_SMART_PTR(Fixture);
31
39class KGE_API Fixture : public ObjectBase
40{
41public:
44 struct Param
45 {
46 float density = 0.0f;
47 float friction = 0.2f;
48 float restitution = 0.0f;
49 bool is_sensor = false;
50
51 Param() {}
52
53 Param(float density, float friction = 0.2f, float restitution = 0.f, bool is_sensor = false)
54 : density(density)
55 , friction(friction)
56 , restitution(restitution)
57 , is_sensor(is_sensor)
58 {
59 }
60 };
61
67 static FixturePtr CreateCircle(const Param& param, float radius, const Point& offset = Point());
68
75 static FixturePtr CreateRect(const Param& param, const Size& size, const Point& offset = Point(),
76 float rotation = 0.f);
77
82 static FixturePtr CreatePolygon(const Param& param, const Vector<Point>& vertexs);
83
89 static FixturePtr CreateEdge(const Param& param, const Point& p1, const Point& p2);
90
96 static FixturePtr CreateChain(const Param& param, const Vector<Point>& vertices, bool loop = false);
97
98 Fixture();
99
100 virtual ~Fixture();
101
104 bool Init(PhysicBodyPtr body);
105
108 bool Init(PhysicBody* body);
109
112 PhysicBody* GetBody() const;
113
116 bool IsSensor() const;
117
121 void SetSensor(bool sensor);
122
125 void GetMassData(float* mass, Point* center, float* inertia) const;
126
129 float GetDensity() const;
130
133 void SetDensity(float density);
134
137 float GetFriction() const;
138
141 void SetFriction(float friction);
142
145 float GetRestitution() const;
146
149 void SetRestitution(float restitution);
150
153 bool TestPoint(const Point& p) const;
154
157 void Destroy();
158
161 b2Fixture* GetB2Fixture() const;
162
165 void SetB2Fixture(b2Fixture* fixture);
166
167 bool operator==(const Fixture& rhs) const;
168 bool operator!=(const Fixture& rhs) const;
169
170private:
171 b2Fixture* fixture_;
172 std::unique_ptr<b2Shape> shape_;
173 Fixture::Param param_;
174};
175
179{
180 template <typename _Ty>
181 class IteratorImpl
182 {
183 public:
184 using iterator_category = std::forward_iterator_tag;
185 using value_type = _Ty;
186 using pointer = _Ty*;
187 using reference = _Ty&;
188 using difference_type = ptrdiff_t;
189
190 IteratorImpl(pointer elem)
191 : elem_(elem)
192 {
193 }
194
195 inline reference operator*() const
196 {
197 return const_cast<reference>(*elem_);
198 }
199
200 inline pointer operator->() const
201 {
202 return std::pointer_traits<pointer>::pointer_to(**this);
203 }
204
205 inline IteratorImpl& operator++()
206 {
207 b2Fixture* next = elem_->GetB2Fixture()->GetNext();
208
209 elem_ = static_cast<Fixture*>(next->GetUserData());
210 return *this;
211 }
212
213 inline IteratorImpl operator++(int)
214 {
215 IteratorImpl old = *this;
216 operator++();
217 return old;
218 }
219
220 inline bool operator==(const IteratorImpl& rhs) const
221 {
222 return elem_ == rhs.elem_;
223 }
224
225 inline bool operator!=(const IteratorImpl& rhs) const
226 {
227 return !operator==(rhs);
228 }
229
230 private:
231 pointer elem_;
232 };
233
234public:
235 using value_type = Fixture;
236 using reference = value_type&;
237 using pointer = value_type*;
238 using iterator = IteratorImpl<value_type>;
239 using const_iterator = IteratorImpl<const value_type>;
240
241 inline FixtureList()
242 : first_(nullptr)
243 {
244 }
245
246 inline FixtureList(pointer first)
247 : first_(first)
248 {
249 }
250
251 inline const value_type& front() const
252 {
253 return *first_;
254 }
255
256 inline value_type& front()
257 {
258 return *first_;
259 }
260
261 inline iterator begin()
262 {
263 return iterator(first_);
264 }
265
266 inline const_iterator begin() const
267 {
268 return cbegin();
269 }
270
271 inline const_iterator cbegin() const
272 {
273 return const_iterator(first_);
274 }
275
276 inline iterator end()
277 {
278 return iterator(nullptr);
279 }
280
281 inline const_iterator end() const
282 {
283 return cend();
284 }
285
286 inline const_iterator cend() const
287 {
288 return const_iterator(nullptr);
289 }
290
291private:
292 pointer first_;
293};
294
297inline bool Fixture::IsSensor() const
298{
299 return param_.is_sensor;
300}
301
302inline void Fixture::SetSensor(bool sensor)
303{
304 if (param_.is_sensor != sensor)
305 {
306 param_.is_sensor = sensor;
307 if (fixture_)
308 {
309 fixture_->SetSensor(sensor);
310 }
311 }
312}
313
314inline float Fixture::GetDensity() const
315{
316 return param_.density;
317}
318
319inline void Fixture::SetDensity(float density)
320{
321 if (param_.density != density)
322 {
323 param_.density = density;
324 if (fixture_)
325 {
326 fixture_->SetDensity(density);
327 }
328 }
329}
330
331inline float Fixture::GetFriction() const
332{
333 return param_.friction;
334}
335
336inline void Fixture::SetFriction(float friction)
337{
338 if (param_.friction != friction)
339 {
340 param_.friction = friction;
341 if (fixture_)
342 {
343 fixture_->SetFriction(friction);
344 }
345 }
346}
347
348inline float Fixture::GetRestitution() const
349{
350 return param_.restitution;
351}
352
353inline void Fixture::SetRestitution(float restitution)
354{
355 if (param_.restitution != restitution)
356 {
357 param_.restitution = restitution;
358 if (fixture_)
359 {
360 fixture_->SetRestitution(restitution);
361 }
362 }
363}
364
365inline b2Fixture* Fixture::GetB2Fixture() const
366{
367 return fixture_;
368}
369
370inline void Fixture::SetB2Fixture(b2Fixture* fixture)
371{
372 fixture_ = fixture;
373 if (fixture)
374 {
375 fixture->SetUserData(this);
376 }
377}
378
379inline bool Fixture::operator==(const Fixture& rhs) const
380{
381 return fixture_ == rhs.fixture_;
382}
383
384inline bool Fixture::operator!=(const Fixture& rhs) const
385{
386 return fixture_ != rhs.fixture_;
387}
388
389} // namespace physics
390} // namespace kiwano
基础对象
Definition: ObjectBase.h:137
物理夹具列表
Definition: Fixture.h:179
物理夹具
Definition: Fixture.h:40
b2Fixture * GetB2Fixture() const
获取b2Fixture
Definition: Fixture.h:365
void SetB2Fixture(b2Fixture *fixture)
设置b2Fixture
Definition: Fixture.h:370
void SetDensity(float density)
设置密度
Definition: Fixture.h:319
void SetSensor(bool sensor)
设置夹具是否是接触传感器
Definition: Fixture.h:302
bool IsSensor() const
是否是接触传感器
Definition: Fixture.h:297
void SetFriction(float friction)
设置摩擦力 [N]
Definition: Fixture.h:336
float GetRestitution() const
获取弹性恢复
Definition: Fixture.h:348
void SetRestitution(float restitution)
设置弹性恢复
Definition: Fixture.h:353
float GetDensity() const
获取密度
Definition: Fixture.h:314
float GetFriction() const
获取摩擦力 [N]
Definition: Fixture.h:331
夹具参数
Definition: Fixture.h:45
float density
密度
Definition: Fixture.h:46
float restitution
弹性恢复
Definition: Fixture.h:48
bool is_sensor
是否是接触传感器
Definition: Fixture.h:49
float friction
摩擦力
Definition: Fixture.h:47