app.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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(50), 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 bod:this->bodies)
  135. {
  136. bod->integrate(deltaTime);
  137. }
  138. for (auto bod:this->bodies)
  139. {
  140. if (bod->shp->getType() == SHAPE_CIRCLE) {
  141. // UGLY HACK
  142. circleShape *shp = (circleShape *) bod->shp;
  143. if (((bod->position.y - shp->radius) <= 0) ||
  144. ((bod->position.y + shp->radius) >= graphics::height()))
  145. {
  146. if ((bod->position.y - shp->radius) <= 0)
  147. {
  148. bod->position.y = shp->radius;
  149. }
  150. else
  151. {
  152. bod->position.y = graphics::height() - shp->radius;
  153. }
  154. bod->velocity.y = -bod->velocity.y;
  155. }
  156. if (((bod->position.x - shp->radius) <= 0) ||
  157. ((bod->position.x + shp->radius) >= graphics::width()))
  158. {
  159. if ((bod->position.x - shp->radius) <= 0)
  160. {
  161. bod->position.x = shp->radius;
  162. }
  163. else
  164. {
  165. bod->position.x = graphics::width() - shp->radius;
  166. }
  167. bod->velocity.x = -bod->velocity.x;
  168. }
  169. }
  170. }
  171. updateTime.stop();
  172. }
  173. void application::render()
  174. {
  175. renderTime.start();
  176. graphics::clearScreen(0xFF0F0721);
  177. if (this->leftButtonPressed)
  178. {
  179. graphics::draw::line(this->bodies[0]->position.x, this->bodies[0]->position.y,
  180. this->mouseCursor.x, this->mouseCursor.y, 0xFFFFFFFF);
  181. }
  182. graphics::draw::line(this->anchor.x, this->anchor.y,
  183. this->bodies[0]->position.x, this->bodies[0]->position.y,
  184. 0xFF313131);
  185. graphics::draw::fillCircle(this->anchor.x, this->anchor.y, 5, 0xFF001155);
  186. for(auto bod: this->bodies)
  187. {
  188. bod->rotation += 0.01;
  189. bod->shp->draw(*bod);
  190. }
  191. for(auto bod: this->bodies)
  192. {
  193. bod->forceDebug(this->showVelocity, this->showForceSum, this->showAllForces);
  194. }
  195. graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
  196. graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
  197. graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
  198. graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f",
  199. 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
  200. graphics::draw::text(5, 53, 0x11FF22, "DBG: V:%c S:%c A:%c", this->showVelocity ? 'Y':'N',
  201. this->showForceSum ? 'Y':'N', this->showAllForces ? 'Y':'N');
  202. graphics::renderFrame();
  203. renderTime.stop();
  204. }
  205. void application::destroy()
  206. {
  207. // Nothing for now.
  208. for (auto part:this->bodies)
  209. {
  210. delete part;
  211. }
  212. graphics::closeWindow();
  213. }