test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /** General automatic test for tinyphysicsengine, it should always pass. */
  2. #include "../tinyphysicsengine.h"
  3. #include <stdio.h>
  4. #define ass(cond,text) { printf(text ": "); if (!(cond)) { puts("ERROR"); return 1; } else puts("OK"); }
  5. TPE_Unit rampPoits[6] =
  6. {
  7. 0,0,
  8. -2400,1400,
  9. -2400,0
  10. };
  11. TPE_Vec3 envFunc(TPE_Vec3 p, TPE_Unit maxD)
  12. {
  13. TPE_ENV_START( TPE_envAABoxInside(p,TPE_vec3(0,1000,0),TPE_vec3(3000,2500,3000)),p )
  14. TPE_ENV_NEXT( TPE_envAATriPrism(p,TPE_vec3(100,200,-10),rampPoits,3000,2),p)
  15. //TPE_ENV_NEXT( TPE_envBox(p,TPE_vec3(-100,300,200),TPE_vec3(500,600,700),TPE_vec3(10,30,50)), p)
  16. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-100,300,200),TPE_vec3(500,600,700)), p)
  17. TPE_ENV_END
  18. }
  19. TPE_Vec3 envFuncBad(TPE_Vec3 p, TPE_Unit maxD)
  20. {
  21. p.x += 200;
  22. return p;
  23. }
  24. TPE_Vec3 envFuncBad2(TPE_Vec3 p, TPE_Unit maxD)
  25. {
  26. if (p.y > p.x)
  27. p.x = p.y;
  28. return p;
  29. }
  30. int main(void)
  31. {
  32. puts("== testing tinyphysicsengine ==");
  33. {
  34. ass(TPE_vec2Angle(-100,0) == TPE_FRACTIONS_PER_UNIT / 2,"vec2 angle")
  35. TPE_Unit l;
  36. l = TPE_LENGTH(TPE_vec3Normalized(TPE_vec3(100,0,0)));
  37. ass(TPE_abs(l - TPE_FRACTIONS_PER_UNIT) < 5,"vec3 normalize")
  38. l = TPE_LENGTH(TPE_vec3Normalized(TPE_vec3(0,0,0)));
  39. ass(TPE_abs(l - TPE_FRACTIONS_PER_UNIT) < 5,"zero vec3 normalize")
  40. l = TPE_LENGTH(TPE_vec3Normalized(TPE_vec3(0,-1,0)));
  41. ass(TPE_abs(l - TPE_FRACTIONS_PER_UNIT) < 5,"small vec3 normalize")
  42. l = TPE_LENGTH(TPE_vec3Normalized(TPE_vec3(500000,300000,-700000)));
  43. ass(TPE_abs(l - TPE_FRACTIONS_PER_UNIT) < 5,"big vec3 normalize")
  44. }
  45. {
  46. TPE_Joint joints[16];
  47. TPE_Connection cons[16];
  48. joints[0] = TPE_joint(TPE_vec3(200,100,-400),300);
  49. joints[1] = TPE_joint(TPE_vec3(100,200,-400),300);
  50. joints[2] = TPE_joint(TPE_vec3(200,-400,200),300);
  51. joints[3] = TPE_joint(TPE_vec3(200,100,-400),400);
  52. cons[0].joint1 = 0; cons[0].joint2 = 1; cons[0].length = 1000;
  53. cons[1].joint1 = 1; cons[1].joint2 = 0; cons[1].length = 1000;
  54. cons[2].joint1 = 0; cons[2].joint2 = 1; cons[2].length = 1100;
  55. cons[3].joint1 = 2; cons[3].joint2 = 3; cons[3].length = 100;
  56. uint32_t jHashes[4], cHashes[4];
  57. for (int i = 0; i < 4; ++i)
  58. {
  59. jHashes[i] = TPE_jointHash(&joints[i]);
  60. cHashes[i] = TPE_connectionHash(&cons[i]);
  61. }
  62. for (int i = 0; i < 4; ++i)
  63. for (int j = i + 1; j < 4; ++j)
  64. ass(jHashes[i] != jHashes[j] && cHashes[i] != cHashes[j],"joints/connection hash");
  65. TPE_Body bodies[8];
  66. uint32_t bHashes[4];
  67. TPE_bodyInit(&bodies[0],joints,4,cons,4,300);
  68. TPE_bodyInit(&bodies[1],joints + 1,3,cons,4,300);
  69. TPE_bodyInit(&bodies[2],joints,4,cons,4,300);
  70. bodies[2].flags |= TPE_BODY_FLAG_SOFT | TPE_BODY_FLAG_NONROTATING;
  71. TPE_bodyInit(&bodies[3],joints,4,cons,4,200);
  72. for (int i = 0; i < 4; ++i)
  73. bHashes[i] = TPE_bodyHash(&bodies[i]);
  74. for (int i = 0; i < 4; ++i)
  75. for (int j = i + 1; j < 4; ++j)
  76. ass(bHashes[i] != bHashes[j],"body hash");
  77. TPE_World world;
  78. uint32_t wHashes[4];
  79. TPE_worldInit(&world,bodies,4,0);
  80. wHashes[0] = TPE_worldHash(&world);
  81. bodies[0].jointCount--;
  82. wHashes[1] = TPE_worldHash(&world);
  83. bodies[4] = bodies[0]; bodies[0] = bodies[3]; bodies[3] = bodies[4];
  84. wHashes[2] = TPE_worldHash(&world);
  85. world.bodyCount--;
  86. wHashes[3] = TPE_worldHash(&world);
  87. for (int i = 0; i < 4; ++i)
  88. for (int j = i + 1; j < 4; ++j)
  89. ass(wHashes[i] != wHashes[j],"world hash");
  90. }
  91. {
  92. puts("-- environment functions --");
  93. ass(TPE_testClosestPointFunction(envFunc,TPE_vec3(-3000,-3000,-3000),
  94. TPE_vec3(3000,3000,3000),32,10,0),"env function");
  95. ass(!TPE_testClosestPointFunction(envFuncBad,TPE_vec3(-1000,-1000,-1000),
  96. TPE_vec3(2000,3000,100),32,10,0),"env function bad");
  97. ass(!TPE_testClosestPointFunction(envFuncBad2,TPE_vec3(-1000,-2000,-200),
  98. TPE_vec3(1000,1000,2000),32,10,0),"env function bad");
  99. }
  100. return 0;
  101. }