conservation.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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,1100); 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. /* We don't want any energy losses due to friction and non-elastic collision,
  33. so we'll turn them off. This alone won't be enough though as numeric errors
  34. will still change the total energy. */
  35. b->friction = 0;
  36. b->elasticity = TPE_FRACTIONS_PER_UNIT;
  37. TPE_bodyAccelerate(b, // give some initial velocity
  38. TPE_vec3Plus(TPE_vec3Times(TPE_bodyGetCenterOfMass(b),55),TPE_vec3(0,8,0)));
  39. }
  40. TPE_Unit sTotal = TPE_worldGetNetSpeed(&tpe_world);
  41. // ^ keep the record of total speed
  42. while (helper_running)
  43. {
  44. helper_frameStart();
  45. helper_cameraFreeMovement();
  46. TPE_worldStep(&tpe_world);
  47. TPE_Unit s = TPE_worldGetNetSpeed(&tpe_world);
  48. TPE_Unit ratio = (sTotal * TPE_FRACTIONS_PER_UNIT) / TPE_nonZero(s);
  49. if (ratio < (4 * TPE_FRACTIONS_PER_UNIT) / 5 ||
  50. ratio > (6 * TPE_FRACTIONS_PER_UNIT) / 5)
  51. {
  52. // if total speed changed by more than 1/5, we adjust it
  53. printf("net speed is now %d but needs to be %d, correcting!\n",s,sTotal);
  54. for (int i = 0; i < tpe_world.bodyCount; ++i)
  55. TPE_bodyMultiplyNetSpeed(&tpe_world.bodies[i],ratio);
  56. }
  57. helper_set3dColor(200,10,10);
  58. for (int i = 0; i < tpe_world.bodyCount; ++i)
  59. {
  60. TPE_Joint *joints = tpe_world.bodies[i].joints;
  61. TPE_Vec3 pos = TPE_bodyGetCenterOfMass(&tpe_world.bodies[i]);
  62. TPE_Vec3 right = TPE_vec3(512,0,0);
  63. TPE_Vec3 forw = TPE_vec3(0,0,512);
  64. TPE_bodyActivate(&tpe_world.bodies[i]); // don't let bodies deactivate
  65. if (i != 1) // ugly code to get the correct orientation :)
  66. {
  67. if (i != 3)
  68. {
  69. forw = TPE_vec3Minus(joints[2].position,joints[0].position);
  70. right = TPE_vec3Minus(joints[1].position,joints[0].position);
  71. }
  72. else
  73. forw = TPE_vec3Minus(joints[1].position,joints[0].position);
  74. }
  75. TPE_Vec3 orient = TPE_rotationFromVecs(forw,right);
  76. switch (i % 5) // and draw the correct shape
  77. {
  78. case 0: helper_draw3dBox(pos,TPE_vec3(1200,1200,1200),orient); break;
  79. case 1: helper_draw3dSphere(pos,TPE_vec3(500,500,500),orient); break;
  80. case 2: helper_draw3dBox(pos,TPE_vec3(1200,400,1200),orient); break;
  81. case 3: helper_draw3dBox(pos,TPE_vec3(200,200,1200),orient); break;
  82. default: break;
  83. }
  84. }
  85. helper_set3dColor(200,200,200);
  86. helper_draw3dSphereInside(TPE_vec3(0,0,0),
  87. TPE_vec3(SPHERE_R,SPHERE_R,SPHERE_R),TPE_vec3(0,0,0));
  88. if (helper_debugDrawOn)
  89. helper_debugDraw(1);
  90. helper_frameEnd();
  91. }
  92. helper_end();
  93. return 0;
  94. }