cubes.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "helper.h"
  2. #define ROOM_SIZE 7000
  3. #define CUBE_SIZE 800
  4. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  5. {
  6. return TPE_envAABoxInside(p,TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE));
  7. }
  8. TPE_Vec3 cubeOrientations[6];
  9. TPE_Vec3 cubePositions[6];
  10. TPE_Joint ballJoints[4];
  11. TPE_Connection ballConnections[3];
  12. uint8_t debugDrawOn = 1;
  13. void updateOrientPos(int i)
  14. {
  15. TPE_Joint *joints = tpe_world.bodies[i].joints;
  16. cubeOrientations[i] = TPE_orientationFromVecs(
  17. TPE_vec3Minus(joints[2].position,joints[0].position),
  18. TPE_vec3Minus(joints[1].position,joints[0].position));
  19. cubePositions[i] = TPE_bodyGetCenter(&tpe_world.bodies[i]);
  20. }
  21. int main(void)
  22. {
  23. helper_init();
  24. tpe_world.environmentFunction = environmentDistance;
  25. s3l_scene.camera.transform.translation.z -= ROOM_SIZE / 2;
  26. s3l_scene.camera.transform.translation.y += ROOM_SIZE / 3;
  27. for (int i = 0; i < 6; ++i)
  28. helper_addBox(CUBE_SIZE / 2,CUBE_SIZE / 2,CUBE_SIZE / 2,CUBE_SIZE / 4,100);
  29. #define move(i,x,y) \
  30. TPE_bodyMove(&tpe_world.bodies[i],TPE_vec3((CUBE_SIZE / 2 + 10) * x,10 + CUBE_SIZE / 2 + y * (CUBE_SIZE + 10),0));
  31. move(0,0,0)
  32. move(1,-2,0)
  33. move(2,2,0)
  34. move(3,-1,1)
  35. move(4,1,1)
  36. move(5,0,2)
  37. #undef move
  38. for (int i = 0; i < 6; ++i)
  39. {
  40. updateOrientPos(i);
  41. TPE_bodyDeactivate(&tpe_bodies[i]);
  42. }
  43. ballJoints[0] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2,0),0);
  44. ballJoints[1] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2 - 100,-600),0);
  45. ballJoints[2] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2 - 200,-1200),0);
  46. ballJoints[3] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2 - 300,-1800),400);
  47. ballJoints[3].velocity[1] = -30;
  48. ballConnections[0].joint1 = 0; ballConnections[0].joint2 = 1;
  49. ballConnections[1].joint1 = 1; ballConnections[1].joint2 = 2;
  50. ballConnections[2].joint1 = 2; ballConnections[2].joint2 = 3;
  51. TPE_bodyInit(&tpe_world.bodies[6],ballJoints,4,ballConnections,3,
  52. 1000);
  53. tpe_world.bodyCount++;
  54. while (helper_running)
  55. {
  56. helper_frameStart();
  57. helper_cameraFreeMovement();
  58. if (helper_frame % 16 == 0)
  59. {
  60. helper_printCPU();
  61. if (sdl_keyboard[SDL_SCANCODE_P])
  62. debugDrawOn = !debugDrawOn;
  63. if (sdl_keyboard[SDL_SCANCODE_L])
  64. for (int i = 0; i < 6; ++i)
  65. {
  66. TPE_bodyActivate(&tpe_world.bodies[i]);
  67. TPE_bodyAccelerate(&tpe_world.bodies[i],
  68. TPE_vec3Plus(TPE_vec3(0,100,0),TPE_vec3Times(cubePositions[i],128)));
  69. }
  70. }
  71. tpe_world.bodies[6].joints[0].position = TPE_vec3(0,ROOM_SIZE / 2 - 10,0);
  72. tpe_world.bodies[6].joints[0].velocity[0] = 0;
  73. tpe_world.bodies[6].joints[0].velocity[1] = 0;
  74. tpe_world.bodies[6].joints[0].velocity[2] = 0;
  75. tpe_world.bodies[6].deactivateCount = 0;
  76. TPE_worldStep(&tpe_world);
  77. helper_set3dColor(100,100,100);
  78. helper_draw3dCubeInside(TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE),TPE_vec3(0,0,0));
  79. helper_set3dColor(200,10,10);
  80. for (int i = 0; i < 6; ++i)
  81. {
  82. if (!(tpe_world.bodies[i].flags & TPE_BODY_FLAG_DEACTIVATED))
  83. {
  84. TPE_bodyAccelerate(&tpe_world.bodies[i],TPE_vec3(0,-5,0));
  85. updateOrientPos(i);
  86. }
  87. helper_draw3dCube(cubePositions[i],TPE_vec3(CUBE_SIZE,CUBE_SIZE,CUBE_SIZE),cubeOrientations[i]);
  88. }
  89. tpe_world.bodies[6].joints[3].velocity[1] -= 5;
  90. helper_draw3dSphere(tpe_world.bodies[6].joints[3].position,TPE_vec3(400,400,400),TPE_vec3(0,0,0) );
  91. if (debugDrawOn)
  92. helper_debugDraw();
  93. helper_frameEnd();
  94. }
  95. helper_end();
  96. return 0;
  97. }