conservation.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. Example that fakes the momentum/energy conservation. We simply keep the record
  3. of total speed in the system (as an approximation of momentum/energy/whatever)
  4. and if it changes too much, we multiply them accordingly to get them back :)
  5. */
  6. #include "helper.h"
  7. #define SPHERE_R 3500
  8. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  9. {
  10. TPE_ENV_START( TPE_envSphereInside(p,TPE_vec3(0,0,0),SPHERE_R),p )
  11. TPE_ENV_END
  12. }
  13. uint8_t debugDrawOn = 1;
  14. unsigned long timeMeasure = 0;
  15. int main(void)
  16. {
  17. helper_init();
  18. tpe_world.environmentFunction = environmentDistance;
  19. s3l_scene.camera.transform.translation.z = -1 * SPHERE_R - 1000;
  20. for (int i = 0; i < 4; ++i)
  21. {
  22. switch (i)
  23. {
  24. case 0: helper_addBox(800,800,800,400,700); break;
  25. case 1: helper_addBall(500,700); break;
  26. case 2: helper_addRect(800,800,400,800); break;
  27. case 3: helper_add2Line(900,200,600); break;
  28. default: break;
  29. }
  30. TPE_Body *b = &tpe_world.bodies[tpe_world.bodyCount - 1];
  31. TPE_bodyMove(b,TPE_vec3((i - 2) * 1200,0,0));
  32. b->friction = 0;
  33. b->elasticity = TPE_FRACTIONS_PER_UNIT;
  34. TPE_bodyAccelerate(b,
  35. TPE_vec3Plus(
  36. TPE_vec3Times(TPE_bodyGetCenterOfMass(b),50),
  37. TPE_vec3(0,10,0))
  38. );
  39. }
  40. TPE_Unit sTotal = TPE_worldGetNetSpeed(&tpe_world);
  41. while (helper_running)
  42. {
  43. helper_frameStart();
  44. helper_cameraFreeMovement();
  45. if (helper_frame % 16 == 0)
  46. {
  47. //helper_printCPU();
  48. //helper_printCamera();
  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. timeMeasure = 0;
  57. }
  58. unsigned long t1 = helper_getMicroSecs();
  59. TPE_worldStep(&tpe_world);
  60. timeMeasure += helper_getMicroSecs() - t1;
  61. helper_set3dColor(180,10,10);
  62. TPE_Unit s = TPE_worldGetNetSpeed(&tpe_world);
  63. TPE_Unit ratio = (sTotal * TPE_FRACTIONS_PER_UNIT) / TPE_nonZero(s);
  64. if (ratio < (4 * TPE_FRACTIONS_PER_UNIT) / 5 ||
  65. ratio > (6 * TPE_FRACTIONS_PER_UNIT) / 5)
  66. {
  67. printf("net speed is now %d but needs to be %d, correcting!\n",s,sTotal);
  68. for (int i = 0; i < tpe_world.bodyCount; ++i)
  69. TPE_bodyMultiplyNetSpeed(&tpe_world.bodies[i],ratio);
  70. }
  71. for (int i = 0; i < tpe_world.bodyCount; ++i)
  72. {
  73. TPE_Joint *joints = tpe_world.bodies[i].joints;
  74. TPE_Vec3 pos = TPE_bodyGetCenterOfMass(&tpe_world.bodies[i]);
  75. TPE_Vec3 right = TPE_vec3(512,0,0);
  76. TPE_Vec3 forw = TPE_vec3(0,0,512);
  77. TPE_bodyActivate(&tpe_world.bodies[i]);
  78. if (i != 1)
  79. {
  80. if (i != 3)
  81. {
  82. forw = TPE_vec3Minus(joints[2].position,joints[0].position);
  83. right = TPE_vec3Minus(joints[1].position,joints[0].position);
  84. }
  85. else
  86. forw = TPE_vec3Minus(joints[1].position,joints[0].position);
  87. }
  88. TPE_Vec3 orient = TPE_rotationFromVecs(forw,right);
  89. switch (i % 5)
  90. {
  91. case 0: helper_draw3dBox(pos,TPE_vec3(1200,1200,1200),orient); break;
  92. case 1: helper_draw3dSphere(pos,TPE_vec3(500,500,500),orient); break;
  93. case 2: helper_draw3dBox(pos,TPE_vec3(1200,400,1200),orient); break;
  94. case 3: helper_draw3dBox(pos,TPE_vec3(200,200,1200),orient); break;
  95. default: break;
  96. }
  97. }
  98. helper_set3dColor(200,200,200);
  99. helper_draw3dSphereInside(TPE_vec3(0,0,0),TPE_vec3(SPHERE_R,SPHERE_R,SPHERE_R),
  100. TPE_vec3(0,0,0) );
  101. if (helper_debugDrawOn)
  102. helper_debugDraw(1);
  103. helper_frameEnd();
  104. }
  105. helper_end();
  106. return 0;
  107. }