Kiwano Engine v1.3.x
World.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/Body.h>
23#include <kiwano-physics/Contact.h>
24
25#define KGE_COMP_PHYSIC_WORLD "__KGE_PHYSIC_WORLD__"
26
27namespace kiwano
28{
29namespace physics
30{
31
46class KGE_API World : public Component
47{
48 friend class Body;
49 friend class Joint;
50
51public:
55 World(const b2Vec2& gravity);
56
57 virtual ~World();
58
61 RefPtr<Body> AddBody(b2BodyDef* def);
62
65 b2Joint* AddJoint(b2JointDef* def);
66
69 ContactList GetContactList();
70
73 void SetVelocityIterations(int vel_iter);
74
77 void SetPositionIterations(int pos_iter);
78
81 void ShowDebugInfo(bool show);
82
85 b2World* GetB2World();
86
89 const b2World* GetB2World() const;
90
91protected:
94 void InitComponent(Actor* actor) override;
95
98 void OnUpdate(Duration dt) override;
99
102 void OnRender(RenderContext& ctx) override;
103
106 void DispatchEvent(Event* evt);
107
110 void BeforeSimulation(Actor* parent, const Matrix3x2& parent_to_world, float parent_rotation);
111
114 void AfterSimulation(Actor* parent, const Matrix3x2& parent_to_world, float parent_rotation);
115
116private:
117 int vel_iter_;
118 int pos_iter_;
119 float fixed_acc_;
120 b2World world_;
121
122 class DebugDrawer;
123 std::unique_ptr<DebugDrawer> drawer_;
124
125 std::unique_ptr<b2ContactListener> contact_listener_;
126};
127
130inline void World::SetVelocityIterations(int vel_iter)
131{
132 vel_iter_ = vel_iter;
133}
134
135inline void World::SetPositionIterations(int pos_iter)
136{
137 pos_iter_ = pos_iter;
138}
139
140} // namespace physics
141} // namespace kiwano
角色
Definition: Actor.h:63
组件
Definition: Component.h:48
事件
Definition: Event.h:43
引用计数智能指针
Definition: RefBasePtr.hpp:35
渲染上下文
Definition: RenderContext.h:62
物体
Definition: Body.h:41
物理接触列表
Definition: Contact.h:75
物理世界
Definition: World.h:47
void SetVelocityIterations(int vel_iter)
设置速度迭代次数, 默认为 6
Definition: World.h:130
void SetPositionIterations(int pos_iter)
设置位置迭代次数, 默认为 2
Definition: World.h:135
时间段
Definition: Duration.h:48