cubes.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = 0;
  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. s3l_scene.camera.transform.translation.x -= ROOM_SIZE / 4;
  28. s3l_scene.camera.transform.rotation.y = -1 * TPE_FRACTIONS_PER_UNIT / 16;
  29. for (int i = 0; i < 6; ++i)
  30. helper_addBox(CUBE_SIZE / 2,CUBE_SIZE / 2,CUBE_SIZE / 2,CUBE_SIZE / 4,100);
  31. #define move(i,x,y) \
  32. TPE_bodyMove(&tpe_world.bodies[i],TPE_vec3((CUBE_SIZE / 2 + 10) * x,10 + CUBE_SIZE / 2 + y * (CUBE_SIZE + 10),0));
  33. move(0,0,0)
  34. move(1,-2,0)
  35. move(2,2,0)
  36. move(3,-1,1)
  37. move(4,1,1)
  38. move(5,0,2)
  39. #undef move
  40. for (int i = 0; i < 6; ++i)
  41. {
  42. updateOrientPos(i);
  43. TPE_bodyDeactivate(&tpe_bodies[i]);
  44. }
  45. ballJoints[0] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2,0),0);
  46. ballJoints[1] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2 - 100,-600),0);
  47. ballJoints[2] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2 - 200,-1200),0);
  48. ballJoints[3] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2 - 300,-1800),400);
  49. ballJoints[3].velocity[1] = -100;
  50. ballConnections[0].joint1 = 0; ballConnections[0].joint2 = 1;
  51. ballConnections[1].joint1 = 1; ballConnections[1].joint2 = 2;
  52. ballConnections[2].joint1 = 2; ballConnections[2].joint2 = 3;
  53. TPE_bodyInit(&tpe_world.bodies[6],ballJoints,4,ballConnections,3,10000);
  54. tpe_world.bodyCount++;
  55. while (helper_running)
  56. {
  57. helper_frameStart();
  58. helper_cameraFreeMovement();
  59. if (helper_frame % 16 == 0)
  60. {
  61. helper_printCPU();
  62. if (sdl_keyboard[SDL_SCANCODE_P])
  63. debugDrawOn = !debugDrawOn;
  64. if (sdl_keyboard[SDL_SCANCODE_L])
  65. for (int i = 0; i < 6; ++i)
  66. {
  67. TPE_bodyActivate(&tpe_world.bodies[i]);
  68. TPE_bodyAccelerate(&tpe_world.bodies[i],
  69. TPE_vec3Plus(TPE_vec3(0,100,0),TPE_vec3Times(cubePositions[i],128)));
  70. }
  71. }
  72. TPE_jointPin(&tpe_world.bodies[6].joints[0],TPE_vec3(0,ROOM_SIZE / 2 - 10,0));
  73. tpe_world.bodies[6].deactivateCount = 0;
  74. TPE_worldStep(&tpe_world);
  75. helper_set3dColor(100,100,100);
  76. helper_draw3dCubeInside(TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE),TPE_vec3(0,0,0));
  77. helper_set3dColor(200,10,10);
  78. for (int i = 0; i < 6; ++i)
  79. {
  80. if (!(tpe_world.bodies[i].flags & TPE_BODY_FLAG_DEACTIVATED))
  81. {
  82. TPE_bodyAccelerate(&tpe_world.bodies[i],TPE_vec3(0,-5,0));
  83. updateOrientPos(i);
  84. }
  85. helper_draw3dCube(cubePositions[i],TPE_vec3(CUBE_SIZE,CUBE_SIZE,CUBE_SIZE),cubeOrientations[i]);
  86. }
  87. tpe_world.bodies[6].joints[3].velocity[1] -= 5;
  88. helper_draw3dSphere(tpe_world.bodies[6].joints[3].position,TPE_vec3(400,400,400),TPE_vec3(0,0,0) );
  89. for (int i = 0; i < 3; ++i)
  90. helper_drawLine3D(
  91. tpe_world.bodies[6].joints[tpe_world.bodies[6].connections[i].joint1].position,
  92. tpe_world.bodies[6].joints[tpe_world.bodies[6].connections[i].joint2].position,
  93. 255,0,0);
  94. if (debugDrawOn)
  95. helper_debugDraw();
  96. helper_frameEnd();
  97. }
  98. helper_end();
  99. return 0;
  100. }