Kiwano Engine v1.2.x
PhysicWorld.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/PhysicBody.h>
23#include <kiwano-physics/Joint.h>
24
25namespace kiwano
26{
27namespace physics
28{
29
44class KGE_API PhysicWorld : public Component
45{
46 friend class PhysicBody;
47 friend class Joint;
48
49public:
51
55 PhysicWorld(const Vec2& gravity);
56
57 virtual ~PhysicWorld();
58
61 void AddBody(PhysicBodyPtr body);
62
65 void RemoveBody(PhysicBodyPtr body);
66
69 void RemoveAllBodies();
70
73 const List<PhysicBodyPtr>& GetAllBodies() const;
74
77 void AddJoint(JointPtr joint);
78
81 void RemoveJoint(JointPtr joint);
82
85 void RemoveAllJoints();
86
89 const List<JointPtr>& GetAllJoints() const;
90
93 Vec2 GetGravity() const;
94
97 void SetGravity(Vec2 gravity);
98
101 ContactList GetContactList();
102
105 void SetVelocityIterations(int vel_iter);
106
109 void SetPositionIterations(int pos_iter);
110
113 void ShowDebugInfo(bool show);
114
117 b2World* GetB2World();
118
121 const b2World* GetB2World() const;
122
123protected:
126 void InitComponent(Actor* actor) override;
127
130 void OnUpdate(Duration dt) override;
131
134 void OnRender(RenderContext& ctx) override;
135
138 void DispatchEvent(Event* evt);
139
142 void JointRemoved(b2Joint* b2joint);
143
146 void BeforeSimulation(Actor* parent, const Matrix3x2& parent_to_world, float parent_rotation);
147
150 void AfterSimulation(Actor* parent, const Matrix3x2& parent_to_world, float parent_rotation);
151
152private:
153 bool debug_;
154 int vel_iter_;
155 int pos_iter_;
156 b2World world_;
157
158 class DebugDrawer;
159 std::unique_ptr<DebugDrawer> drawer_;
160
161 List<PhysicBodyPtr> bodies_;
162 List<JointPtr> joints_;
163
164 std::unique_ptr<b2DestructionListener> destroy_listener_;
165 std::unique_ptr<b2ContactListener> contact_listener_;
166};
167
170inline void PhysicWorld::SetVelocityIterations(int vel_iter)
171{
172 vel_iter_ = vel_iter;
173}
174
175inline void PhysicWorld::SetPositionIterations(int pos_iter)
176{
177 pos_iter_ = pos_iter;
178}
179
180} // namespace physics
181} // namespace kiwano
角色
Definition: Actor.h:70
组件
Definition: Component.h:51
事件
Definition: Event.h:44
渲染上下文
Definition: RenderContext.h:64
物理接触列表
Definition: Contact.h:119
关节
Definition: Joint.h:49
物体
Definition: PhysicBody.h:40
物理世界
Definition: PhysicWorld.h:45
void SetPositionIterations(int pos_iter)
设置位置迭代次数, 默认为 2
Definition: PhysicWorld.h:175
void SetVelocityIterations(int vel_iter)
设置速度迭代次数, 默认为 6
Definition: PhysicWorld.h:170
时间段
Definition: Duration.h:48