app.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * 2D Physic Engine
  3. * app.cpp: basic application handling.
  4. * Based on pikuma.com Learn Game Physics Engine Programming course.
  5. * Copyright (c) 2022 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 07/06/2022.
  8. */
  9. #include <app.h>
  10. #include <graphics.h>
  11. #include <imgui.h>
  12. #include <imgui_sdl.h>
  13. #include <imgui_impl_sdl.h>
  14. #include <physics/constants.h>
  15. #include <physics/particle.h>
  16. #include <physics/vec2.h>
  17. #include <physics/force.h>
  18. #include <SDL.h>
  19. void application::parseParameters(int argc, char *argv[])
  20. {
  21. // Nothing to do for now
  22. }
  23. void application::setup()
  24. {
  25. this->running = graphics::openWindow();
  26. this->millisecsPreviousFrame = SDL_GetTicks();
  27. this->particles.push_back(new particle(200, 200, 1, 6));
  28. this->particles.push_back(new particle(500, 500, 20.0, 20));
  29. }
  30. void application::input()
  31. {
  32. SDL_Event event;
  33. while(SDL_PollEvent(&event))
  34. {
  35. //ImGui_ImplSDL2_ProcessEvent(&event);
  36. //ImGuiIO &io = ImGui::GetIO();
  37. switch(event.type)
  38. {
  39. case SDL_QUIT:
  40. this->running = false;
  41. break;
  42. case SDL_KEYDOWN:
  43. if (event.key.keysym.sym == SDLK_ESCAPE)
  44. {
  45. this->running = false;
  46. }
  47. if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = -50 * PIXELS_PER_METER;
  48. if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 50 * PIXELS_PER_METER;
  49. if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 50 * PIXELS_PER_METER;
  50. if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = -50 * PIXELS_PER_METER;
  51. break;
  52. case SDL_KEYUP:
  53. if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = 0;
  54. if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 0;
  55. if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 0;
  56. if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = 0;
  57. break;
  58. case SDL_MOUSEBUTTONDOWN:
  59. if (event.button.button == SDL_BUTTON_LEFT)
  60. {
  61. this->leftButtonPressed = true;
  62. }
  63. break;
  64. case SDL_MOUSEBUTTONUP:
  65. if (event.button.button == SDL_BUTTON_LEFT)
  66. {
  67. if (this->leftButtonPressed)
  68. {
  69. vec2 impulseDirection = (particles[0]->position - this->mouseCursor).unitVector();
  70. double impulseMagnitude = (particles[0]->position - this->mouseCursor).magnitude() * 5.0;
  71. particles[0]->velocity = impulseDirection * impulseMagnitude;
  72. }
  73. this->leftButtonPressed = false;
  74. }
  75. break;
  76. case SDL_MOUSEMOTION:
  77. this->mouseCursor.x = event.motion.x;
  78. this->mouseCursor.y = event.motion.y;
  79. break;
  80. }
  81. }
  82. }
  83. void application::syncAndDeltatime()
  84. {
  85. waitTime.start();
  86. /* Let's make sure we are running at the expected framerate */
  87. uint32_t now = SDL_GetTicks();
  88. int32_t timeToWait = MILLISECS_PER_FRAME - (now - this->millisecsPreviousFrame);
  89. if ((timeToWait > 0) && (timeToWait <= MILLISECS_PER_FRAME))
  90. {
  91. SDL_Delay(timeToWait);
  92. }
  93. this->deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
  94. /* clamp maximum deltatime to avoid weird behaviours */
  95. if (this->deltaTime > 0.016)
  96. {
  97. this->deltaTime = 0.016;
  98. }
  99. this->millisecsPreviousFrame = SDL_GetTicks();
  100. waitTime.stop();
  101. }
  102. void application::update()
  103. {
  104. this->syncAndDeltatime();
  105. updateTime.start();
  106. for (auto part:this->particles)
  107. {
  108. vec2 friction = force::generateFrictionForce(*part, 20);
  109. part->addForce(friction);
  110. }
  111. vec2 attraction = force::generateGravitationalForce(*this->particles[0], *this->particles[1],
  112. 1000, 5, 100);
  113. this->particles[0]->addForce(attraction);
  114. this->particles[1]->addForce(-attraction);
  115. for (auto part:this->particles)
  116. {
  117. part->integrate(deltaTime);
  118. }
  119. for (auto part:this->particles)
  120. {
  121. // check the particles position and keep the particle in the window.
  122. if ( ((part->position.y - part->radius) <= 0) ||
  123. ((part->position.y + part->radius) >= graphics::height()))
  124. {
  125. if ((part->position.y - part->radius) <= 0)
  126. {
  127. part->position.y = part->radius;
  128. }
  129. else
  130. {
  131. part->position.y = graphics::height() - part->radius;
  132. }
  133. part->velocity.y = -part->velocity.y;
  134. }
  135. if ( ((part->position.x - part->radius) <= 0) ||
  136. ((part->position.x + part->radius) >= graphics::width()))
  137. {
  138. if ((part->position.x - part->radius) <= 0)
  139. {
  140. part->position.x = part->radius;
  141. }
  142. else
  143. {
  144. part->position.x = graphics::width() - part->radius;
  145. }
  146. part->velocity.x = -part->velocity.x;
  147. }
  148. }
  149. updateTime.stop();
  150. }
  151. void application::render()
  152. {
  153. renderTime.start();
  154. graphics::clearScreen(0xFF0F0721);
  155. if (this->leftButtonPressed)
  156. {
  157. graphics::draw::line(this->particles[0]->position.x, this->particles[0]->position.y,
  158. this->mouseCursor.x, this->mouseCursor.y, 0xFFFFFFFF);
  159. }
  160. graphics::draw::fillCircle(this->particles[0]->position.x, this->particles[0]->position.y,
  161. this->particles[0]->radius, 0xFFAA3300);
  162. graphics::draw::fillCircle(this->particles[1]->position.x, this->particles[1]->position.y,
  163. this->particles[1]->radius, 0xFF00FFFF);
  164. graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
  165. graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
  166. graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
  167. graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f", 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
  168. graphics::renderFrame();
  169. renderTime.stop();
  170. }
  171. void application::destroy()
  172. {
  173. // Nothing for now.
  174. for (auto part:this->particles)
  175. {
  176. delete part;
  177. }
  178. graphics::closeWindow();
  179. }