app.cpp 8.0 KB

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