diff --git a/src/main.cpp b/src/main.cpp index 7e90d78..61f99b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,7 @@ #include +#include "Network.h" + #include #include #include @@ -7,7 +9,8 @@ #include #include -static Nz::Vector3f Get2DDirectionVectorFromRotation(float yaw) { +static Nz::Vector3f Get2DDirectionVectorFromRotation(float yaw) +{ return { -std::sin(yaw), 0, @@ -136,6 +139,7 @@ static void CreateModel(Nz::EnttWorld &world) } static Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f); +static Nz::MillisecondClock updateClock; static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Application &app) { @@ -151,7 +155,7 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Applicati entt::handle cameraEntity = world.CreateEntity(); auto &cameraNode = cameraEntity.emplace(); - cameraNode.SetPosition({0, 1, 0}); + cameraNode.SetPosition({0, 2.5, 0}); auto &cameraComponent = cameraEntity.emplace(std::make_shared(windowSwapchain), Nz::ProjectionType::Perspective); cameraComponent.UpdateClearColor(Nz::Color(0.3f, 0.8f, 1.0f)); @@ -166,42 +170,56 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Applicati window.GetEventHandler().OnMouseMoved.Connect([&](const Nz::WindowEventHandler * /*eventHandler*/, const Nz::WindowEvent::MouseMoveEvent &event) { - constexpr float sensitivity = 0.3f; + constexpr float sensitivity = 0.3f; - camAngles.yaw -= event.deltaX * sensitivity; - camAngles.yaw.Normalize(); + camAngles.yaw -= event.deltaX * sensitivity; + camAngles.yaw.Normalize(); - camAngles.pitch = Nz::Clamp(camAngles.pitch - event.deltaY * sensitivity, -89.f, 89.f); + camAngles.pitch = Nz::Clamp(camAngles.pitch - event.deltaY * sensitivity, -89.f, 89.f); - camAngles.roll = 0.0f; + camAngles.roll = 0.0f; - cameraNode.SetRotation(camAngles); - - }); + cameraNode.SetRotation(camAngles); + }); - std::shared_ptr boxCollider = std::make_shared(Nz::Vector3f(1, 2, 1)); + Nz::Vector3f playerSize{0.5, 2, 0.5}; - + { + + std::shared_ptr boxMesh = Nz::GraphicalMesh::Build(Nz::Primitive::Box(playerSize)); + + std::shared_ptr boxMaterial = Nz::MaterialInstance::Instantiate(Nz::MaterialType::Phong); + boxMaterial->SetValueProperty("BaseColor", Nz::Color::sRGBToLinear({1.0, 0.0, 0.0})); + + std::shared_ptr sphereModel = std::make_shared(boxMesh); + sphereModel->SetMaterial(0, std::move(boxMaterial)); + playerEntity.emplace(std::move(sphereModel)); + } + + std::shared_ptr boxCollider = std::make_shared(playerSize); static const int playerMass = 100000; Nz::RigidBody3D::DynamicSettings settings; settings.geom = boxCollider; settings.mass = playerMass; - auto &playerBody = playerEntity.emplace(settings); - app.AddUpdaterFunc([&](){ - static Nz::MillisecondClock updateClock; - if (std::optional deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60))) + playerEntity.emplace(settings); + + app.AddUpdaterFunc([playerEntity]() + { + if (std::optional deltaTime = updateClock.RestartIfOver(Nz::Time::Milliseconds(30))) { - camAngles.roll = 0; - cameraNode.SetRotation(camAngles); - Nz::Vector3f front = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians()); Nz::Vector3f left = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians() + 3.1415926535/2.0); + + auto& playerBody = playerEntity.get(); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z)) - playerBody.AddForce(front * 10.f * playerMass, Nz::CoordSys::Local); + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z)){ + std::cout << front << "\n"; + std::cout << "Pos : " << playerBody.GetPosition() << " \n"; + playerBody.AddForce(front * 10.f * playerMass, Nz::CoordSys::Global); + } if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S)) playerBody.AddForce(-front * 10.f * playerMass, Nz::CoordSys::Local); @@ -212,14 +230,21 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Applicati if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D)) playerBody.AddForce(-left * 10.f * playerMass, Nz::CoordSys::Local); - if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space)) - playerBody.AddForce(Nz::Vector3f::Up() * 15.f * playerMass, Nz::CoordSys::Local); - } - }); + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space)){ + playerBody.AddForce(Nz::Vector3f::Up() * 15.f * playerMass, Nz::CoordSys::Global); + } + + if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::R)) { + auto& playerNode = playerEntity.get(); + playerNode.SetPosition({0, 10, 0}); + playerBody.SetPosition({0, 10, 0}); + } + + //playerBody.SetRotation({}); + } }); } -int main(int argc, char **argv) -{ +int Video(int argc, char **argv) { Nz::Application app(argc, argv); auto &windowing = app.AddComponent(); @@ -233,7 +258,7 @@ int main(int argc, char **argv) auto &physSystem = world.AddSystem(); physSystem.GetPhysWorld().SetMaxStepCount(1); - physSystem.GetPhysWorld().SetStepSize(Nz::Time::TickDuration(50)); + physSystem.GetPhysWorld().SetStepSize(Nz::Time::Milliseconds(20)); physSystem.GetPhysWorld().SetGravity(Nz::Vector3f::Down() * 9.81f); CreateCamera(world, window, app); @@ -258,3 +283,8 @@ int main(int argc, char **argv) return app.Run(); } + +int main(int argc, char **argv) +{ + TestNetwork(); +}