stack.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //#define FPS 10
  2. #include "helper.h"
  3. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  4. {
  5. TPE_ENV_START( TPE_envHalfPlane(p,TPE_vec3(0,0,0),TPE_vec3(256,256,0)),p )
  6. TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,0),TPE_vec3(-256,256,-256)),p )
  7. TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,0),TPE_vec3(-256,256,256)),p )
  8. TPE_ENV_END
  9. }
  10. unsigned long timeMeasure = 0;
  11. TPE_Vec3 bodyPositions[16];
  12. TPE_Vec3 bodyOrientations[16];
  13. int main(void)
  14. {
  15. helper_init();
  16. tpe_world.environmentFunction = environmentDistance;
  17. s3l_scene.camera.transform.translation.y = 6000;
  18. s3l_scene.camera.transform.translation.z = -2000;
  19. s3l_scene.camera.transform.rotation.x = -70;
  20. for (int i = 0; i < 16; ++i)
  21. {
  22. switch (i % 5)
  23. {
  24. case 0: helper_addBox(800,800,800,400,700); break;
  25. case 1: helper_addTriangle(1100,200,600); break;
  26. case 2: helper_addBall(500,700); break;
  27. case 3: helper_addRect(800,800,400,800); break;
  28. case 4: helper_add2Line(900,200,600); break;
  29. default: break;
  30. }
  31. TPE_bodyMoveBy(&tpe_world.bodies[tpe_world.bodyCount - 1],TPE_vec3((1 - (i % 4)) * 1200,8000,(2 - (i / 4)) * 1200));
  32. }
  33. while (helper_running)
  34. {
  35. helper_frameStart();
  36. helper_cameraFreeMovement();
  37. if (helper_frame % 16 == 0)
  38. {
  39. helper_printCPU();
  40. if (sdl_keyboard[SDL_SCANCODE_L])
  41. for (int i = 0; i < tpe_world.bodyCount; ++i)
  42. {
  43. TPE_bodyActivate(&tpe_world.bodies[i]);
  44. TPE_bodyAccelerate(&tpe_world.bodies[i],
  45. TPE_vec3(0,(500 * 30) / FPS,0));
  46. }
  47. printf("world update (us): %lu\n",timeMeasure / 16);
  48. timeMeasure = 0;
  49. }
  50. unsigned long t1 = helper_getMicroSecs();
  51. TPE_worldStep(&tpe_world);
  52. timeMeasure += helper_getMicroSecs() - t1;
  53. if (helper_frame == 200)
  54. printf("hash: %lu\n",TPE_worldHash(&tpe_world));
  55. for (int i = 0; i < tpe_world.bodyCount; ++i)
  56. {
  57. TPE_bodyApplyGravity(&tpe_world.bodies[i],(5 * 30) / FPS);
  58. TPE_Joint *joints = tpe_world.bodies[i].joints;
  59. TPE_Vec3 pos = TPE_bodyGetCenterOfMass(&tpe_world.bodies[i]);
  60. TPE_Vec3 right = TPE_vec3(512,0,0);
  61. TPE_Vec3 forw = TPE_vec3(0,0,512);
  62. if (i % 5 != 2 && i % 5 != 1) // don't mind the ugly code
  63. {
  64. if (i % 5 != 4)
  65. {
  66. forw = TPE_vec3Minus(joints[2].position,joints[0].position);
  67. right = TPE_vec3Minus(joints[1].position,joints[0].position);
  68. }
  69. else
  70. forw = TPE_vec3Minus(joints[1].position,joints[0].position);
  71. }
  72. TPE_Vec3 orient = TPE_rotationFromVecs(forw,right);
  73. helper_set3DColor(100 + i * 5,16 - i,100 - i * 5);
  74. bodyPositions[i] =
  75. TPE_vec3KeepWithinBox(
  76. bodyPositions[i],
  77. TPE_bodyGetCenterOfMass(&tpe_world.bodies[i]),
  78. TPE_vec3(20,20,20)
  79. );
  80. bodyOrientations[i].x =
  81. (TPE_abs(bodyOrientations[i].x - orient.x) < 20) ?
  82. ((bodyOrientations[i].x + orient.x) / 2) : orient.x;
  83. bodyOrientations[i].y =
  84. (TPE_abs(bodyOrientations[i].y - orient.y) < 20) ?
  85. ((bodyOrientations[i].y + orient.y) / 2) : orient.y;
  86. bodyOrientations[i].z =
  87. (TPE_abs(bodyOrientations[i].z - orient.z) < 20) ?
  88. ((bodyOrientations[i].z + orient.z) / 2) : orient.z;
  89. switch (i % 5)
  90. {
  91. case 0: helper_draw3DBox(bodyPositions[i],TPE_vec3(1200,1200,1200),bodyOrientations[i]); break;
  92. case 1: helper_draw3DTriangle(bodyPositions[i],joints[1].position,joints[2].position); break;
  93. case 2: helper_draw3DSphere(bodyPositions[i],TPE_vec3(500,500,500),bodyOrientations[i]); break;
  94. case 3: helper_draw3DBox(bodyPositions[i],TPE_vec3(1200,400,1200),bodyOrientations[i]); break;
  95. case 4: helper_draw3DBox(bodyPositions[i],TPE_vec3(200,200,1200),bodyOrientations[i]); break;
  96. default: break;
  97. }
  98. }
  99. helper_set3DColor(100,100,100);
  100. helper_draw3DTriangle(TPE_vec3(0,0,0),TPE_vec3(-5000,5000,-10000),TPE_vec3(-5000,5000,10000));
  101. helper_set3DColor(140,140,140);
  102. helper_draw3DTriangle(TPE_vec3(0,0,0),TPE_vec3(-5000,5000,10000),TPE_vec3(5000,5000,0));
  103. helper_set3DColor(80,80,80);
  104. helper_draw3DTriangle(TPE_vec3(0,0,0),TPE_vec3(-5000,5000,-10000),TPE_vec3(5000,5000,0));
  105. if (helper_debugDrawOn)
  106. helper_debugDraw(1);
  107. helper_frameEnd();
  108. }
  109. helper_end();
  110. return 0;
  111. }