2d.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_Unit sss[6] =
  8. {
  9. 500,0,
  10. 0,2000,
  11. -500,0
  12. };
  13. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  14. {
  15. /*
  16. TPE_ENV_START( TPE_envAABoxInside(p,TPE_vec3(0,0,0),TPE_vec3(ROOM_W,ROOM_H,ROOM_W)), p )
  17. TPE_ENV_NEXT(TPE_envAATriPrism(p,TPE_vec3(-ROOM_W / 4,-ROOM_H / 2,0),sss,1000,0),p)
  18. TPE_ENV_END
  19. */
  20. return TPE_envAABoxInside(p,TPE_vec3(0,0,0),TPE_vec3(ROOM_W,ROOM_H,ROOM_W));
  21. }
  22. inactiveCount = 0;
  23. int main(void)
  24. {
  25. helper_init();
  26. helper_debugDrawOn = 1;
  27. tpe_world.environmentFunction = environmentDistance;
  28. s3l_scene.camera.transform.translation.z -= ROOM_W / 2;
  29. s3l_scene.camera.focalLength = 0; // set orthographic projection
  30. for (int i = 0; i < 4; ++i)
  31. {
  32. if (i != 2)
  33. {
  34. helper_addCenterRectFull(SQUARE_SIZE,SQUARE_SIZE,100,100);
  35. TPE_bodyRotateByAxis(&tpe_world.bodies[i],TPE_vec3(TPE_FRACTIONS_PER_UNIT / 4,0,0));
  36. tpe_world.bodies[i].joints[4].sizeDivided = 300 / TPE_JOINT_SIZE_MULTIPLIER;
  37. /*
  38. helper_addRect(SQUARE_SIZE,SQUARE_SIZE,100,100);
  39. TPE_bodyRotateByAxis(&tpe_world.bodies[i],TPE_vec3(TPE_FRACTIONS_PER_UNIT / 4,0,0));
  40. */
  41. }
  42. else
  43. helper_addBall(600,100);
  44. tpe_world.bodies[i].friction = 400;
  45. //tpe_world.bodies[i].friction = 0;
  46. //tpe_world.bodies[i].flags |= TPE_BODY_FLAG_SOFT; // comment or uncomment
  47. tpe_world.bodies[i].elasticity = 100;
  48. TPE_bodyMoveBy(&tpe_world.bodies[i],TPE_vec3(-1000 + i * 800,0,0));
  49. }
  50. while (helper_running)
  51. {
  52. helper_frameStart();
  53. if (sdl_keyboard[SDL_SCANCODE_LEFT])
  54. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(-10,0,0));
  55. else if (sdl_keyboard[SDL_SCANCODE_RIGHT])
  56. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(10,0,0));
  57. if (sdl_keyboard[SDL_SCANCODE_UP])
  58. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(0,10,0));
  59. TPE_worldStep(&tpe_world);
  60. helper_set3dColor(100,100,100);
  61. helper_set3dColor(200,10,10);
  62. for (int i = 0; i < tpe_world.bodyCount; ++i)
  63. TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
  64. TPE_Unit speed, speedMax = 0;
  65. int anyActive = 0;
  66. for (int i = 0; i < tpe_world.bodyCount; ++i)
  67. {
  68. for (int j = 0; j < tpe_world.bodies[i].jointCount; ++j)
  69. {
  70. tpe_world.bodies[i].joints[j].position.z = 0;
  71. tpe_world.bodies[i].joints[j].velocity[2] = 0;
  72. }
  73. if (!(tpe_world.bodies[i].flags & TPE_BODY_FLAG_DEACTIVATED))
  74. anyActive = 1;
  75. speed = TPE_bodyGetAverageSpeed(&tpe_world.bodies[i]);
  76. if (speed > speedMax)
  77. speedMax = speed;
  78. }
  79. if (anyActive && speedMax < 50)
  80. inactiveCount++;
  81. else
  82. inactiveCount = 0;
  83. if (inactiveCount > 100)
  84. {
  85. TPE_worldDeactivateAll(&tpe_world);
  86. inactiveCount = 0;
  87. }
  88. // if (helper_debugDrawOn)
  89. helper_debugDraw(0);
  90. helper_frameEnd();
  91. }
  92. helper_end();
  93. return 0;
  94. }