stack.c 3.9 KB

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