app.cpp 7.3 KB

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