If there are errors in the code, terminology, etc. in the text, please correct me
113 knots
There are no new features in this section, just some changes in teaching videos and engine planning
-
engine planning
Complete engine to create and distribute 2D games like Hollow Knight
-
teaching video
Instead of live-streaming new features and then compressing short videos, they will directly post what they wrote during the live-streaming.
-
other
- The engine corresponding to this teaching video should not support both OpenGL and Vulkan, the workload is very heavy
- Project version of the engine switched to 2022
114 knots
If there are errors in the code, terminology, etc. in the text, please correct me
Article directory
foreword
-
of this program
-
Added a physics simulation run mode
To run the physics effects added to the object, the camera uses the camera in edit mode .
-
Distinguishing the physics of Play mode
The camera in this mode will become the main camera in the scene , not the current editing camera .
-
-
About Physics Simulation Mode
-
Rendered parts of the scene
In physics simulation mode: the rendered scene is the same as in edit mode
-
Physical simulation part
In physics simulation mode: the physics simulation is the same as in Play mode
Therefore, at the intersection of the editing mode of the physical simulation mode and the Play mode , the physical simulation mode should also copy the current editing scene.
-
-
How to achieve
Add an icon, a state to control
-
implementation details
- There should be no bugs when clicking Play mode while the physics simulation mode is running
- There should be no bugs when switching Scenes during physics simulation or Play mode
- There should be no bugs in the empty scene click Play mode and physics simulation mode
Key code + code flow
code flow
-
Click on the icon for physics simulation mode
ImGui::SameLine(); { Ref<Texture2D> icon = ( m_SceneState == SceneState::Edit || m_SceneState == SceneState::Play)? m_IconSimulate : m_IconStop; if (ImGui::ImageButton((ImTextureID)icon->GetRendererID(), ImVec2(size, size), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0.0f, 0.0f, 0.0f, 0.0f), tintColor) && toolbarEnabled) { // 图标可以换,但是下面的代码不会执行 if (m_SceneState == SceneState::Edit || m_SceneState == SceneState::Play) { OnSceneSimulate(); } else if (m_SceneState == SceneState::Simulate) { OnSceneStop(); } } }
-
Start physics simulation mode + scene to perform physics simulation
void EditorLayer::OnSceneSimulate()// 开始物理模拟模式 { if (m_SceneState == SceneState::Play) { OnSceneStop(); // 停止物理 } m_SceneState = SceneState::Simulate; // 复制新场景给活动场景 m_ActiveScene = Scene::Copy(m_EditorScene); m_ActiveScene->OnSimulationStart(); // 复制完新场景就开始运行。场景执行物理模拟 // 当前上下文是新场景---------------------------- m_SceneHierarchyPanel.SetContext(m_ActiveScene); } void EditorLayer::OnUpdate(Timestep ts) { // Scene更新 switch (m_SceneState) { case SceneState::Simulate: { // 摄像机要更新 m_EditorCamera.OnUpdate(ts); m_ActiveScene->OnUpdateSimulation(ts, m_EditorCamera); break; } }
-
Perform physics calculations + render the scene
void Scene::OnUpdateSimulation(Timestep ts, EditorCamera& camera) { // Physics { // 先script脚本影响Physics变化再当前帧渲染出来 // 迭代速度:使用更少的迭代可以提高性能,但准确性会受到影响。使用更多迭代会降低性能但会提高模拟质量 // 有点不董。。。。说啥:时间步长和迭代次数完全无关。迭代不是子步骤 // Cherno说迭代速度,多久进行一次计算模拟。好奇这个6,是多少毫秒计算6次吗? const int32_t velocityIterations = 6;// 这些参数应该移到编辑器 const int32_t positionIterations = 2; m_PhysicsWorld->Step(ts, velocityIterations, positionIterations); auto view = m_Registry.view<Rigidbody2DComponent>(); for (auto e : view) { Entity entity = { e, this }; auto& transform = entity.GetComponent<TransformComponent>(); auto& rb2d = entity.GetComponent<Rigidbody2DComponent>(); // 获取物理模拟计算后的主体 b2Body* body = (b2Body*)rb2d.RuntimeBody; // 将计算后的值赋予实体 const auto& position = body->GetPosition(); transform.Translation.x = position.x; transform.Translation.y = position.y; transform.Rotation.z = body->GetAngle();// 获取z轴角度 } // 脚本影响Pyhsics再下面渲染出来 } // Render 渲染场景 RenderScene(camera); }
code details
-
There should be no bugs in the empty scene click Play mode and physics simulation mode
- Switch these two states, nothing is rendered, and the physical world will only increase the consumption.
- Click the Play button to switch to the main camera in Play mode, but the current scene is empty and there is no main camera, so it needs to be judged as empty and not perform rendering , otherwise an error will be reported.
void EditorLayer::OnOverlayRender() { // 两个不同摄像机 if (m_SceneState == SceneState::Play) { // Play模式下找主摄像机 Entity camera = m_ActiveScene->GetPrimaryCameraEntity(); if (!camera) { return; // 找不到就退出 } Renderer2D::BeginScene(camera.GetComponent<CameraComponent>().camera, camera.GetComponent<TransformComponent>().GetTransform()); } else { Renderer2D::BeginScene(m_EditorCamera); }