app.cpp 6.3 KB

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