From 553476bb59527084662dd73a621dfe0e6e091ecc Mon Sep 17 00:00:00 2001 From: Armin <armin.co@hs-bochum.de> Date: Sat, 23 Jan 2021 20:16:04 +0100 Subject: [PATCH] Added Simulation mode. --- App/CMakeLists.txt | 1 - App/GameLayer.cpp | 25 ++++++++++++++++++++----- App/GameLayer.hpp | 1 + CMakeLists.txt | 2 +- QPong/CMakeLists.txt | 1 + QPong/Particle.cpp | 13 +++++++++++-- QPong/Particle.hpp | 1 + {App => QPong}/Utils.cpp | 9 +++++++++ {App => QPong}/Utils.hpp | 1 + 9 files changed, 45 insertions(+), 9 deletions(-) rename {App => QPong}/Utils.cpp (91%) rename {App => QPong}/Utils.hpp (82%) diff --git a/App/CMakeLists.txt b/App/CMakeLists.txt index e42fbe0..af3f7e2 100644 --- a/App/CMakeLists.txt +++ b/App/CMakeLists.txt @@ -5,7 +5,6 @@ add_executable( QPongGame MainMenuLayer.cpp QPongApp.cpp Window.cpp - Utils.cpp ) target_include_directories( QPong PUBLIC diff --git a/App/GameLayer.cpp b/App/GameLayer.cpp index a01442d..c7a8677 100644 --- a/App/GameLayer.cpp +++ b/App/GameLayer.cpp @@ -17,7 +17,7 @@ #include "GameLayer.hpp" #include "Log.hpp" -#include "Utils.hpp" +#include <QPong/Utils.hpp> #include <string> #include <thread> @@ -50,6 +50,7 @@ QPong::GameLayer::GameLayer(int resolution, int threads) , m_properties {initialParticleSettings} , m_speedScale{5.0f} , m_randomMomentum {true} + , m_gaming {true} { m_properties.size = resolution; m_properties.threads = threads; @@ -122,7 +123,6 @@ void QPong::GameLayer::onAttach() m_viewProjectionLocation = glGetUniformLocation(m_shader->getRendererID(), "u_viewProjection"); m_showPotentialsLocation = glGetUniformLocation(m_shader->getRendererID(), "u_showPotential"); m_isMomentumEnabled = glGetUniformLocation(m_shader->getRendererID(), "u_isMomentumEnabled"); - reset(); } @@ -154,6 +154,7 @@ void QPong::GameLayer::reset() positive = rand() % 2; m_properties.momentum.y = (1.0f - (2.0f * positive)) * (1.0 + (float)yRand / 120.0f); } + m_options.game = m_gaming; m_particle.reset(new Particle(m_properties, m_options, m_playerOne, m_playerTwo, m_leftBat, m_rightBat, m_staticBorderPotential, m_dynamicPotential)); m_restart = false; @@ -225,9 +226,12 @@ void QPong::GameLayer::onUpdate(GuiCore::Timestep ts) glDrawElements(GL_TRIANGLES, m_numberOfIndices, GL_UNSIGNED_INT, nullptr); } m_openGlTimer = openGlTimer.stop(); - std::stringstream totalScore; - totalScore << m_playerOne.getTotalScore() << " : " << m_playerTwo.getTotalScore(); - GuiCore::Glyphs::renderText(totalScore.str(), -0.32f, 1.45f, 0.0075f, {1.0, 1.0, 1.0}); + if (m_gaming) + { + std::stringstream totalScore; + totalScore << m_playerOne.getTotalScore() << " : " << m_playerTwo.getTotalScore(); + GuiCore::Glyphs::renderText(totalScore.str(), -0.32f, 1.45f, 0.0075f, {1.0, 1.0, 1.0}); + } } void QPong::GameLayer::onGuiRender() @@ -235,6 +239,8 @@ void QPong::GameLayer::onGuiRender() // GUI settings for the particle ImGui::Begin("Particle Settings"); + ImGui::DragFloat2("Start Position", glm::value_ptr(m_properties.position), 0.01f, -1.0f, 1.0f); + // Select initial momentum for particle at restart ImGui::DragFloat2("Momentum [x,y]", glm::value_ptr(m_properties.momentum), 0.01f, -3.0f, 3.0f); ImGui::Checkbox("Random Momentum", &m_randomMomentum); @@ -294,5 +300,14 @@ void QPong::GameLayer::onGuiRender() ImGui::Text("FFT took %0.1lfms", m_particle->getTimeFFT()); ImGui::Text("OpenGL took %.1lfms", m_openGlTimer); + if (ImGui::Checkbox("Gaming", &m_gaming)) + { + m_propagate = false; + m_randomMomentum = false; + m_options.absorbtionEnabled = false; + m_options.momentumEnabled = true; + m_restart = true; + } + ImGui::End(); } \ No newline at end of file diff --git a/App/GameLayer.hpp b/App/GameLayer.hpp index d92521f..a7d3ada 100644 --- a/App/GameLayer.hpp +++ b/App/GameLayer.hpp @@ -71,6 +71,7 @@ namespace QPong float m_speedScale{1.0f}; double m_openGlTimer; + bool m_gaming; }; } #endif \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 80f2246..1112d31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project(QPong VERSION 0.0.1 LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_BUILD_TYPE Release) -set(CMAKE_CXX_FLAGS "-Ofast -flto") +set(CMAKE_CXX_FLAGS "-Ofast") option(LOG_DEBUG_INFO "Enable logging t console" OFF) if(LOG_DEBUG_INFO) diff --git a/QPong/CMakeLists.txt b/QPong/CMakeLists.txt index 2dcd81e..eb417d3 100644 --- a/QPong/CMakeLists.txt +++ b/QPong/CMakeLists.txt @@ -2,6 +2,7 @@ add_library(QPong Bat.cpp Particle.cpp Workers.cpp + Utils.cpp ) target_include_directories(QPong PUBLIC diff --git a/QPong/Particle.cpp b/QPong/Particle.cpp index ccf96e1..71bb4a3 100644 --- a/QPong/Particle.cpp +++ b/QPong/Particle.cpp @@ -30,7 +30,7 @@ double QPong::yAtIndex(int i, int arraySize) return posAt(i / arraySize, arraySize); } -double QPong::potentialAt(int pos, int arraySize, double hbar) +double QPong::potentialAt(int pos, const int arraySize, const double hbar) { if (pos > arraySize / 2) { @@ -76,7 +76,16 @@ Particle::Particle(Properties properties, GameOptions &options, Player &playerOn m_psi = static_cast<std::complex<double> *>(fftw_malloc(memorySize)); m_momentum = static_cast<std::complex<double> *>(fftw_malloc(memorySize)); initialiseParticleMomentum(); - calculateBorderPotential(m_staticBorders, m_yAt, m_properties.elements()); + + if (options.game == true) + { + calculateBorderPotential(m_staticBorders, m_yAt, m_properties.elements()); + } + else + { + // "Simulation" + calculateSchroedingerPotential(m_staticBorders, m_xAt, m_yAt, m_properties.elements()); + } fftw_init_threads(); fftw_plan_with_nthreads(m_properties.threads); diff --git a/QPong/Particle.hpp b/QPong/Particle.hpp index f5340ea..9c7c8c8 100644 --- a/QPong/Particle.hpp +++ b/QPong/Particle.hpp @@ -45,6 +45,7 @@ namespace QPong bool potentialsEnabled; bool absorbtionEnabled; bool momentumEnabled; + bool game; }; Particle(Properties properties, GameOptions &options, Player &m_playerOne, Player &m_playerTwo ,Bat &leftBat, Bat &rightBat, float *staticBorders, float *dynamicPotential); diff --git a/App/Utils.cpp b/QPong/Utils.cpp similarity index 91% rename from App/Utils.cpp rename to QPong/Utils.cpp index 139ad7a..2586bcf 100644 --- a/App/Utils.cpp +++ b/QPong/Utils.cpp @@ -3,6 +3,7 @@ /// #include "Utils.hpp" +#include <cmath> void calculatePointPositions(float *positions, int arraySize) { @@ -82,4 +83,12 @@ void calculateBorderPotential(float *potential, double *yAt, int arrayElements) float pot = pow2(y) * pow2(y) * pow2(y) * pow2(y) * pow2(y) * pow2(y) * pow2(y) * 0.02; potential[i] = pot; } +} + +void calculateSchroedingerPotential(float *potential, double *xAt, double *yAt, int arrayElements) +{ + for (int i {0}; i < arrayElements; ++i) + { + potential[i] = ( pow2(pow2(xAt[i]) + pow2(yAt[i])) ) / 200.0; + } } \ No newline at end of file diff --git a/App/Utils.hpp b/QPong/Utils.hpp similarity index 82% rename from App/Utils.hpp rename to QPong/Utils.hpp index 93767da..35a069c 100644 --- a/App/Utils.hpp +++ b/QPong/Utils.hpp @@ -17,5 +17,6 @@ void calculatePointPositions(float *points, int arraySize); void calculateMomentumViewPositions(float *points, int arraySize); void calculateTriangleIndices(uint32_t *indices, int arraySize); void calculateBorderPotential(float *potential, double *yAt, int arrayElements); +void calculateSchroedingerPotential(float *potential, double *xAt, double *yAt, int arrayElements); #endif \ No newline at end of file -- GitLab