app.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. for(int i = 0; i < this->numberOfCorners; i++)
  28. {
  29. this->particles.push_back(new particle(graphics::width() / 2.0 + i * 5,
  30. graphics::height() / 2.0 + i * 5,
  31. 2, 10));
  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[0]->position - this->mouseCursor).unitVector();
  86. double impulseMagnitude = (particles[0]->position - this->mouseCursor).magnitude() * 5.0;
  87. particles[0]->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. for (auto part:this->particles)
  123. {
  124. part->addForce(pushForce);
  125. vec2 friction = force::generateFrictionForce(*part, 20);
  126. part->addForce(friction);
  127. vec2 drag = force::generateDragForce(*part, 0.002);
  128. part->addForce(drag);
  129. vec2 weight = vec2(0, part->mass * 9.81 * PIXELS_PER_METER);
  130. part->addForce(weight);
  131. }
  132. for(int i = 0; i < this->numberOfCorners; i++)
  133. {
  134. particle *a = this->particles[i];
  135. for(int j = i + 1; j < this->numberOfCorners; j++)
  136. {
  137. particle *b = this->particles[j];
  138. vec2 spring = force::generateSpringForce(*a, *b, this->restLenght, this->k);
  139. a->addForce(spring);
  140. b->addForce( -spring);
  141. }
  142. }
  143. for (auto part:this->particles)
  144. {
  145. part->integrate(deltaTime);
  146. }
  147. for (auto part:this->particles)
  148. {
  149. // check the particles position and keep the particle in the window.
  150. if ( ((part->position.y - part->radius) <= 0) ||
  151. ((part->position.y + part->radius) >= graphics::height()))
  152. {
  153. if ((part->position.y - part->radius) <= 0)
  154. {
  155. part->position.y = part->radius;
  156. }
  157. else
  158. {
  159. part->position.y = graphics::height() - part->radius;
  160. }
  161. part->velocity.y = -part->velocity.y;
  162. }
  163. if ( ((part->position.x - part->radius) <= 0) ||
  164. ((part->position.x + part->radius) >= graphics::width()))
  165. {
  166. if ((part->position.x - part->radius) <= 0)
  167. {
  168. part->position.x = part->radius;
  169. }
  170. else
  171. {
  172. part->position.x = graphics::width() - part->radius;
  173. }
  174. part->velocity.x = -part->velocity.x;
  175. }
  176. }
  177. updateTime.stop();
  178. }
  179. void application::render()
  180. {
  181. renderTime.start();
  182. graphics::clearScreen(0xFF0F0721);
  183. if (this->leftButtonPressed)
  184. {
  185. graphics::draw::line(this->particles[0]->position.x, this->particles[0]->position.y,
  186. this->mouseCursor.x, this->mouseCursor.y, 0xFFFFFFFF);
  187. }
  188. for(int i = 0; i < this->numberOfCorners; i++)
  189. {
  190. particle *a = this->particles[i];
  191. for(int j = i + 1; j < this->numberOfCorners; j++)
  192. {
  193. particle *b = this->particles[j];
  194. graphics::draw::line(a->position.x, a->position.y,
  195. b->position.x, b->position.y,
  196. 0xFF313131);
  197. }
  198. }
  199. for(auto part: this->particles)
  200. {
  201. graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFAA3300);
  202. }
  203. for(auto part: this->particles)
  204. {
  205. part->forceDebug(this->showVelocity, this->showForceSum, this->showAllForces);
  206. }
  207. graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
  208. graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
  209. graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
  210. graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f",
  211. 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
  212. graphics::draw::text(5, 53, 0x11FF22, "DBG: V:%c S:%c A:%c", this->showVelocity ? 'Y':'N',
  213. this->showForceSum ? 'Y':'N', this->showAllForces ? 'Y':'N');
  214. graphics::renderFrame();
  215. renderTime.stop();
  216. }
  217. void application::destroy()
  218. {
  219. // Nothing for now.
  220. for (auto part:this->particles)
  221. {
  222. delete part;
  223. }
  224. graphics::closeWindow();
  225. }