Kiwano Engine v1.2.x
PhysicBody.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#include <kiwano-physics/ContactEdge.h>
24
25namespace kiwano
26{
27namespace physics
28{
29
30class PhysicWorld;
31
39class KGE_API PhysicBody : public Component
40{
41 friend class PhysicWorld;
42
43public:
46 enum class Type
47 {
48 Static = 0,
49 Kinematic,
50 Dynamic,
51 };
52
57 PhysicBody(PhysicWorld* world, Type type);
58
63 PhysicBody(PhysicWorldPtr world, Type type);
64
65 virtual ~PhysicBody();
66
70 bool Init(PhysicWorld* world);
71
74 void AddFixture(FixturePtr fixture);
75
81 Fixture* AddCircleShape(float radius, float density, float friction = 0.2f);
82
87 Fixture* AddRectShape(const Vec2& size, float density, float friction = 0.2f);
88
93 Fixture* AddPolygonShape(const Vector<Point>& vertexs, float density, float friction = 0.2f);
94
100 Fixture* AddEdgeShape(const Point& p1, const Point& p2, float density, float friction = 0.2f);
101
107 Fixture* AddChainShape(const Vector<Point>& vertices, bool loop, float density, float friction = 0.2f);
108
111 void RemoveFixture(FixturePtr fixture);
112
115 void RemoveAllFixtures();
116
119 FixtureList GetFixtureList() const;
120
123 ContactEdgeList GetContactList() const;
124
127 uint16_t GetCategoryBits() const;
128
131 void SetCategoryBits(uint16_t category_bits);
132
135 uint16_t GetMaskBits() const;
136
139 void SetMaskBits(uint16_t mask_bits);
140
143 int16_t GetGroupIndex() const;
144
147 void SetGroupIndex(int16_t index);
148
151 float GetRotation() const;
152
155 void SetRotation(float angle);
156
159 Point GetPosition() const;
160
163 void SetPosition(const Point& pos);
164
167 void SetTransform(const Point& pos, float angle);
168
171 float GetMass() const;
172
175 float GetInertia() const;
176
182 void GetMassData(float* mass, Point* center, float* inertia) const;
183
189 void SetMassData(float mass, const Point& center, float inertia);
190
193 void ResetMassData();
194
197 Point GetLocalPoint(const Point& world) const;
198
201 Vec2 GetLocalVector(const Vec2& world) const;
202
205 Point GetWorldPoint(const Point& local) const;
206
209 Vec2 GetWorldVector(const Vec2& local) const;
210
213 Point GetLocalCenter() const;
214
217 Point GetWorldCenter() const;
218
221 Type GetType() const;
222
225 void SetType(Type type);
226
229 float GetGravityScale() const;
230
233 void SetGravityScale(float scale);
234
237 float GetLinearDamping() const;
238
241 void SetLinearDamping(float damping);
242
245 float GetAngularDamping() const;
246
249 void SetAngularDamping(float damping);
250
256 void ApplyForce(const Vec2& force, const Point& point, bool wake = true);
257
262 void ApplyForceToCenter(const Vec2& force, bool wake = true);
263
268 void ApplyTorque(float torque, bool wake = false);
269
272 bool IsIgnoreRotation() const;
273
276 void SetIgnoreRotation(bool flag);
277
280 bool IsBullet() const;
281
284 void SetBullet(bool flag);
285
288 bool IsAwake() const;
289
292 void SetAwake(bool flag);
293
296 bool IsSleepingAllowed() const;
297
300 void SetSleepingAllowed(bool flag);
301
304 bool IsActive() const;
305
308 void SetActive(bool flag);
309
312 PhysicWorld* GetWorld() const;
313
316 void Destroy();
317
320 b2Body* GetB2Body() const;
321
324 void SetB2Body(b2Body* body);
325
326protected:
329 void InitComponent(Actor* actor) override;
330
333 void DestroyComponent() override;
334
337 void UpdateFromActor(Actor* actor);
338
341 void UpdateFromActor(Actor* actor, const Matrix3x2& actor_to_world, float parent_rotation);
342
345 void UpdateFixtureFilter(b2Fixture* fixture);
346
349 void BeforeSimulation(Actor* actor, const Matrix3x2& parent_to_world, const Matrix3x2& actor_to_world,
350 float parent_rotation);
351
354 void AfterSimulation(Actor* actorconst, const Matrix3x2& parent_to_world, float parent_rotation);
355
356private:
357 PhysicWorld* world_;
358 b2Body* body_;
359 uint16_t category_bits_;
360 uint16_t mask_bits_;
361 int16_t group_index_;
362
363 PhysicBody::Type type_;
364 Vector<FixturePtr> fixtures_;
365
366 Point offset_;
367 Point position_cached_;
368};
369
373{
374 KGE_ASSERT(body_);
375
376 if (!body_->GetFixtureList())
377 return FixtureList();
378
379 Fixture* fixture = static_cast<Fixture*>(body_->GetFixtureList()->GetUserData());
380 return FixtureList(fixture);
381}
382
384{
385 KGE_ASSERT(body_);
386 ContactEdge edge;
387 edge.SetB2ContactEdge(body_->GetContactList());
388 return ContactEdgeList(edge);
389}
390
391inline uint16_t PhysicBody::GetCategoryBits() const
392{
393 return category_bits_;
394}
395
396inline uint16_t PhysicBody::GetMaskBits() const
397{
398 return mask_bits_;
399}
400
401inline int16_t PhysicBody::GetGroupIndex() const
402{
403 return group_index_;
404}
405
406inline float PhysicBody::GetRotation() const
407{
408 KGE_ASSERT(body_);
409 return math::Radian2Degree(body_->GetAngle());
410}
411
412inline void PhysicBody::SetRotation(float angle)
413{
414 SetTransform(GetPosition(), angle);
415}
416
417inline void PhysicBody::SetPosition(const Point& pos)
418{
420}
421
422inline float PhysicBody::GetMass() const
423{
424 KGE_ASSERT(body_);
425 return body_->GetMass();
426}
427
428inline float PhysicBody::GetInertia() const
429{
430 KGE_ASSERT(body_);
431 return body_->GetInertia();
432}
433
435{
436 return type_;
437}
438
439inline void PhysicBody::SetType(Type type)
440{
441 if (type_ != type)
442 {
443 type_ = type;
444 if (body_)
445 {
446 body_->SetType(b2BodyType(type));
447 }
448 }
449}
450
451inline float PhysicBody::GetGravityScale() const
452{
453 KGE_ASSERT(body_);
454 return body_->GetGravityScale();
455}
456
457inline void PhysicBody::SetGravityScale(float scale)
458{
459 KGE_ASSERT(body_);
460 body_->SetGravityScale(scale);
461}
462
464{
465 KGE_ASSERT(body_);
466 return body_->GetLinearDamping();
467}
468
469inline void PhysicBody::SetLinearDamping(float damping)
470{
471 KGE_ASSERT(body_);
472 body_->SetLinearDamping(damping);
473}
474
476{
477 KGE_ASSERT(body_);
478 return body_->GetAngularDamping();
479}
480
481inline void PhysicBody::SetAngularDamping(float damping)
482{
483 KGE_ASSERT(body_);
484 body_->SetAngularDamping(damping);
485}
486
488{
489 KGE_ASSERT(body_);
490 return body_->IsFixedRotation();
491}
492
493inline void PhysicBody::SetIgnoreRotation(bool flag)
494{
495 KGE_ASSERT(body_);
496 body_->SetFixedRotation(flag);
497}
498
499inline bool PhysicBody::IsBullet() const
500{
501 KGE_ASSERT(body_);
502 return body_->IsBullet();
503}
504
505inline void PhysicBody::SetBullet(bool flag)
506{
507 KGE_ASSERT(body_);
508 body_->SetBullet(flag);
509}
510
511inline bool PhysicBody::IsAwake() const
512{
513 KGE_ASSERT(body_);
514 return body_->IsAwake();
515}
516
517inline void PhysicBody::SetAwake(bool flag)
518{
519 KGE_ASSERT(body_);
520 body_->SetAwake(flag);
521}
522
524{
525 KGE_ASSERT(body_);
526 return body_->IsSleepingAllowed();
527}
528
529inline void PhysicBody::SetSleepingAllowed(bool flag)
530{
531 KGE_ASSERT(body_);
532 body_->SetSleepingAllowed(flag);
533}
534
535inline bool PhysicBody::IsActive() const
536{
537 KGE_ASSERT(body_);
538 return body_->IsActive();
539}
540
541inline void PhysicBody::SetActive(bool flag)
542{
543 KGE_ASSERT(body_);
544 body_->SetActive(flag);
545}
546
547inline b2Body* PhysicBody::GetB2Body() const
548{
549 return body_;
550}
551
553{
554 return world_;
555}
556
557} // namespace physics
558} // namespace kiwano
角色
Definition: Actor.h:70
组件
Definition: Component.h:51
物理接触边列表
Definition: ContactEdge.h:66
接触边
Definition: ContactEdge.h:36
void SetB2ContactEdge(b2ContactEdge *edge)
设置b2ContactEdge
Definition: ContactEdge.h:188
物理夹具列表
Definition: Fixture.h:179
物理夹具
Definition: Fixture.h:40
物体
Definition: PhysicBody.h:40
float GetAngularDamping() const
获取旋转阻尼
Definition: PhysicBody.h:475
float GetRotation() const
获取旋转角度
Definition: PhysicBody.h:406
float GetMass() const
获取质量 [kg]
Definition: PhysicBody.h:422
float GetGravityScale() const
获取物体受重力的比例
Definition: PhysicBody.h:451
void SetPosition(const Point &pos)
设置物体位置
Definition: PhysicBody.h:417
bool IsBullet() const
是否是子弹物体
Definition: PhysicBody.h:499
bool IsActive() const
是否启用
Definition: PhysicBody.h:535
void SetTransform(const Point &pos, float angle)
位置和旋转变换
Definition: PhysicBody.cpp:334
void SetAngularDamping(float damping)
设置旋转阻尼
Definition: PhysicBody.h:481
uint16_t GetCategoryBits() const
获取类别码
Definition: PhysicBody.h:391
bool IsAwake() const
是否处于唤醒状态
Definition: PhysicBody.h:511
void SetRotation(float angle)
设置旋转角度
Definition: PhysicBody.h:412
void SetActive(bool flag)
设置启用状态
Definition: PhysicBody.h:541
Type
物体类型
Definition: PhysicBody.h:47
PhysicWorld * GetWorld() const
获取物体所在物理世界
Definition: PhysicBody.h:552
FixtureList GetFixtureList() const
获取夹具列表
Definition: PhysicBody.h:372
void SetGravityScale(float scale)
设置物体受重力的比例
Definition: PhysicBody.h:457
void SetLinearDamping(float damping)
设置线性阻尼
Definition: PhysicBody.h:469
Type GetType() const
获取物体类型
Definition: PhysicBody.h:434
b2Body * GetB2Body() const
获取b2Body
Definition: PhysicBody.h:547
void SetType(Type type)
设置物体类型
Definition: PhysicBody.h:439
void SetBullet(bool flag)
设置物体是否是子弹物体
Definition: PhysicBody.h:505
uint16_t GetMaskBits() const
获取碰撞掩码
Definition: PhysicBody.h:396
void SetSleepingAllowed(bool flag)
设置是否允许休眠
Definition: PhysicBody.h:529
ContactEdgeList GetContactList() const
获取接触边列表
Definition: PhysicBody.h:383
bool IsSleepingAllowed() const
是否启用休眠
Definition: PhysicBody.h:523
int16_t GetGroupIndex() const
获取组索引
Definition: PhysicBody.h:401
Point GetPosition() const
获取物体位置
Definition: PhysicBody.cpp:328
void SetIgnoreRotation(bool flag)
设置是否固定旋转角度
Definition: PhysicBody.h:493
bool IsIgnoreRotation() const
旋转角度是否固定
Definition: PhysicBody.h:487
float GetInertia() const
获取惯性
Definition: PhysicBody.h:428
float GetLinearDamping() const
获取线性阻尼
Definition: PhysicBody.h:463
void SetAwake(bool flag)
设置唤醒状态
Definition: PhysicBody.h:517
物理世界
Definition: PhysicWorld.h:45