2d.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #define TPE_RESHAPE_ITERATIONS 5
  2. #define DEBUG_DRAW_DIVIDE 8
  3. #include "helper.h"
  4. #define ROOM_W 5100
  5. #define ROOM_H ((RES_Y * ROOM_W) / RES_X)
  6. #define SQUARE_SIZE 500
  7. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  8. {
  9. return TPE_envAABoxInside(p,TPE_vec3(0,0,0),TPE_vec3(ROOM_W,ROOM_H,ROOM_W));
  10. }
  11. inactiveCount = 0;
  12. int main(void)
  13. {
  14. helper_init();
  15. helper_debugDrawOn = 1;
  16. tpe_world.environmentFunction = environmentDistance;
  17. s3l_scene.camera.transform.translation.z -= ROOM_W / 2;
  18. s3l_scene.camera.focalLength = 0; // set orthographic projection
  19. for (int i = 0; i < 4; ++i)
  20. {
  21. if (i != 2)
  22. {
  23. helper_addCenterRectFull(SQUARE_SIZE,SQUARE_SIZE,100,100);
  24. TPE_bodyRotateByAxis(&tpe_world.bodies[i],TPE_vec3(TPE_FRACTIONS_PER_UNIT / 4,0,0));
  25. tpe_world.bodies[i].joints[4].sizeDivided = 300 / TPE_JOINT_SIZE_MULTIPLIER;
  26. }
  27. else
  28. helper_addBall(600,100);
  29. tpe_world.bodies[i].friction = 400;
  30. TPE_bodyMove(&tpe_world.bodies[i],TPE_vec3(-1000 + i * 800,ROOM_H / 4,0));
  31. }
  32. while (helper_running)
  33. {
  34. helper_frameStart();
  35. if (sdl_keyboard[SDL_SCANCODE_LEFT])
  36. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(-10,0,0));
  37. else if (sdl_keyboard[SDL_SCANCODE_RIGHT])
  38. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(10,0,0));
  39. if (sdl_keyboard[SDL_SCANCODE_UP])
  40. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(0,10,0));
  41. TPE_worldStep(&tpe_world);
  42. helper_set3dColor(100,100,100);
  43. helper_set3dColor(200,10,10);
  44. for (int i = 0; i < tpe_world.bodyCount; ++i)
  45. TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
  46. TPE_Unit speed, speedMax = 0;
  47. int anyActive = 0;
  48. for (int i = 0; i < tpe_world.bodyCount; ++i)
  49. {
  50. for (int j = 0; j < tpe_world.bodies[i].jointCount; ++j)
  51. {
  52. tpe_world.bodies[i].joints[j].position.z = 0;
  53. tpe_world.bodies[i].joints[j].velocity[2] = 0;
  54. }
  55. if (!(tpe_world.bodies[i].flags & TPE_BODY_FLAG_DEACTIVATED))
  56. anyActive = 1;
  57. speed = TPE_bodyGetAverageSpeed(&tpe_world.bodies[i]);
  58. if (speed > speedMax)
  59. speedMax = speed;
  60. }
  61. if (anyActive && speedMax < 50)
  62. inactiveCount++;
  63. else
  64. inactiveCount = 0;
  65. if (inactiveCount > 100)
  66. {
  67. TPE_worldDeactivateAll(&tpe_world);
  68. inactiveCount = 0;
  69. }
  70. // if (helper_debugDrawOn)
  71. helper_debugDraw(0);
  72. helper_frameEnd();
  73. }
  74. helper_end();
  75. return 0;
  76. }