app.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_1)
  44. {
  45. this->showVelocity ^= true;
  46. }
  47. if (event.key.keysym.sym == SDLK_2)
  48. {
  49. this->showForceSum ^= true;
  50. }
  51. if (event.key.keysym.sym == SDLK_3)
  52. {
  53. this->showAllForces ^= true;
  54. }
  55. if (event.key.keysym.sym == SDLK_ESCAPE)
  56. {
  57. this->running = false;
  58. }
  59. if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = -50 * PIXELS_PER_METER;
  60. if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 50 * PIXELS_PER_METER;
  61. if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 50 * PIXELS_PER_METER;
  62. if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = -50 * PIXELS_PER_METER;
  63. break;
  64. case SDL_KEYUP:
  65. if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = 0;
  66. if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 0;
  67. if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 0;
  68. if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = 0;
  69. break;
  70. case SDL_MOUSEBUTTONDOWN:
  71. if (event.button.button == SDL_BUTTON_LEFT)
  72. {
  73. this->leftButtonPressed = true;
  74. }
  75. break;
  76. case SDL_MOUSEBUTTONUP:
  77. if (event.button.button == SDL_BUTTON_LEFT)
  78. {
  79. if (this->leftButtonPressed)
  80. {
  81. vec2 impulseDirection = (particles[0]->position - this->mouseCursor).unitVector();
  82. double impulseMagnitude = (particles[0]->position - this->mouseCursor).magnitude() * 5.0;
  83. particles[0]->velocity = impulseDirection * impulseMagnitude;
  84. }
  85. this->leftButtonPressed = false;
  86. }
  87. break;
  88. case SDL_MOUSEMOTION:
  89. this->mouseCursor.x = event.motion.x;
  90. this->mouseCursor.y = event.motion.y;
  91. break;
  92. }
  93. }
  94. }
  95. void application::syncAndDeltatime()
  96. {
  97. waitTime.start();
  98. /* Let's make sure we are running at the expected framerate */
  99. uint32_t now = SDL_GetTicks();
  100. int32_t timeToWait = MILLISECS_PER_FRAME - (now - this->millisecsPreviousFrame);
  101. if ((timeToWait > 0) && (timeToWait <= MILLISECS_PER_FRAME))
  102. {
  103. SDL_Delay(timeToWait);
  104. }
  105. this->deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
  106. /* clamp maximum deltatime to avoid weird behaviours */
  107. if (this->deltaTime > 0.016)
  108. {
  109. this->deltaTime = 0.016;
  110. }
  111. this->millisecsPreviousFrame = SDL_GetTicks();
  112. waitTime.stop();
  113. }
  114. void application::update()
  115. {
  116. this->syncAndDeltatime();
  117. updateTime.start();
  118. for (auto part:this->particles)
  119. {
  120. vec2 friction = force::generateFrictionForce(*part, 20);
  121. part->addForce(friction);
  122. }
  123. vec2 attraction = force::generateGravitationalForce(*this->particles[0], *this->particles[1],
  124. 1000, 5, 100);
  125. this->particles[0]->addForce(attraction);
  126. this->particles[1]->addForce(-attraction);
  127. for (auto part:this->particles)
  128. {
  129. part->integrate(deltaTime);
  130. }
  131. for (auto part:this->particles)
  132. {
  133. // check the particles position and keep the particle in the window.
  134. if ( ((part->position.y - part->radius) <= 0) ||
  135. ((part->position.y + part->radius) >= graphics::height()))
  136. {
  137. if ((part->position.y - part->radius) <= 0)
  138. {
  139. part->position.y = part->radius;
  140. }
  141. else
  142. {
  143. part->position.y = graphics::height() - part->radius;
  144. }
  145. part->velocity.y = -part->velocity.y;
  146. }
  147. if ( ((part->position.x - part->radius) <= 0) ||
  148. ((part->position.x + part->radius) >= graphics::width()))
  149. {
  150. if ((part->position.x - part->radius) <= 0)
  151. {
  152. part->position.x = part->radius;
  153. }
  154. else
  155. {
  156. part->position.x = graphics::width() - part->radius;
  157. }
  158. part->velocity.x = -part->velocity.x;
  159. }
  160. }
  161. updateTime.stop();
  162. }
  163. void application::render()
  164. {
  165. renderTime.start();
  166. graphics::clearScreen(0xFF0F0721);
  167. if (this->leftButtonPressed)
  168. {
  169. graphics::draw::line(this->particles[0]->position.x, this->particles[0]->position.y,
  170. this->mouseCursor.x, this->mouseCursor.y, 0xFFFFFFFF);
  171. }
  172. graphics::draw::fillCircle(this->particles[0]->position.x, this->particles[0]->position.y,
  173. this->particles[0]->radius, 0xFFAA3300);
  174. graphics::draw::fillCircle(this->particles[1]->position.x, this->particles[1]->position.y,
  175. this->particles[1]->radius, 0xFF00FFFF);
  176. if (this->showForceSum || this->showVelocity || this->showAllForces)
  177. {
  178. for(auto part: this->particles)
  179. {
  180. part->forceDebug(this->showVelocity, this->showForceSum, this->showAllForces);
  181. }
  182. }
  183. graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
  184. graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
  185. graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
  186. graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f",
  187. 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
  188. graphics::draw::text(5, 53, 0x11FF22, "DBG: V:%c S:%c A:%c", this->showVelocity ? 'Y':'N',
  189. this->showForceSum ? 'Y':'N', this->showAllForces ? 'Y':'N');
  190. graphics::renderFrame();
  191. renderTime.stop();
  192. }
  193. void application::destroy()
  194. {
  195. // Nothing for now.
  196. for (auto part:this->particles)
  197. {
  198. delete part;
  199. }
  200. graphics::closeWindow();
  201. }