2d.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /** Demo showing how 2D physics can be implemented. */
  2. #define DEBUG_DRAW_DIVIDE 8
  3. #include "helper.h"
  4. #define ROOM_W (TPE_F * 10)
  5. #define ROOM_H ((RES_Y * ROOM_W) / RES_X)
  6. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  7. {
  8. return TPE_envAABoxInside(p,TPE_vec3(0,0,0),TPE_vec3(ROOM_W,ROOM_H,ROOM_W));
  9. }
  10. int inactiveCount = 0;
  11. int main(void)
  12. {
  13. helper_init();
  14. tpe_world.environmentFunction = environmentDistance;
  15. s3l_scene.camera.transform.translation.z -= ROOM_W / 2;
  16. s3l_scene.camera.focalLength = 0; // set orthographic projection
  17. for (int i = 0; i < 4; ++i) // add bodies
  18. {
  19. if (i != 2)
  20. {
  21. helper_addCenterRectFull(TPE_F,TPE_F,TPE_F / 5,TPE_F / 5);
  22. TPE_bodyRotateByAxis(&helper_lastBody,TPE_vec3(TPE_F / 4,0,0));
  23. helper_lastBody.joints[4].sizeDivided *= 3; // make center point bigger
  24. }
  25. else
  26. helper_addBall(6 * TPE_F / 5,TPE_F / 5);
  27. helper_lastBody.friction = 4 * TPE_F / 5;
  28. helper_lastBody.elasticity = TPE_F / 5;
  29. TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(-2 * TPE_F + i * 2 * TPE_F,0,0));
  30. }
  31. while (helper_running)
  32. {
  33. helper_frameStart();
  34. #define ACCELERATION (TPE_F / 25)
  35. if (sdl_keyboard[SDL_SCANCODE_LEFT])
  36. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(-1 * ACCELERATION,0,0));
  37. else if (sdl_keyboard[SDL_SCANCODE_RIGHT])
  38. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(ACCELERATION,0,0));
  39. if (sdl_keyboard[SDL_SCANCODE_UP])
  40. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(0,ACCELERATION,0));
  41. #undef ACCELERATION
  42. TPE_worldStep(&tpe_world);
  43. for (int i = 0; i < tpe_world.bodyCount; ++i)
  44. TPE_bodyApplyGravity(&tpe_world.bodies[i],TPE_F / 100);
  45. /* Here we implement our own improvement of deactivation; after some time of
  46. all bodies having low speed we disable them all at once. */
  47. TPE_Unit speed, speedMax = 0;
  48. int anyActive = 0;
  49. for (int i = 0; i < tpe_world.bodyCount; ++i)
  50. {
  51. // as we're in 2D we'll keep all joint Z positions and velocities at 0
  52. for (int j = 0; j < tpe_world.bodies[i].jointCount; ++j)
  53. {
  54. tpe_world.bodies[i].joints[j].position.z = 0;
  55. tpe_world.bodies[i].joints[j].velocity[2] = 0;
  56. }
  57. if (!(tpe_world.bodies[i].flags & TPE_BODY_FLAG_DEACTIVATED))
  58. anyActive = 1;
  59. speed = TPE_bodyGetAverageSpeed(&tpe_world.bodies[i]);
  60. if (speed > speedMax)
  61. speedMax = speed;
  62. }
  63. if (anyActive && speedMax < TPE_F / 10)
  64. inactiveCount++;
  65. else
  66. inactiveCount = 0;
  67. if (inactiveCount > 100)
  68. {
  69. TPE_worldDeactivateAll(&tpe_world);
  70. inactiveCount = 0;
  71. }
  72. helper_debugDraw(0);
  73. helper_frameEnd();
  74. }
  75. helper_end();
  76. return 0;
  77. }