stack.c 4.6 KB

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