cubes.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. void updateOrientPos(int i)
  13. {
  14. cubeOrientations[i] = TPE_bodyGetOrientation(&tpe_world.bodies[i],0,2,1);
  15. }
  16. int main(void)
  17. {
  18. helper_init();
  19. tpe_world.environmentFunction = environmentDistance;
  20. s3l_scene.camera.transform.translation.z -= ROOM_SIZE / 2;
  21. s3l_scene.camera.transform.translation.y += ROOM_SIZE / 3;
  22. s3l_scene.camera.transform.translation.x -= ROOM_SIZE / 4;
  23. s3l_scene.camera.transform.rotation.y = -1 * TPE_FRACTIONS_PER_UNIT / 16;
  24. for (int i = 0; i < 6; ++i)
  25. {
  26. helper_addBox(CUBE_SIZE / 2,CUBE_SIZE / 2,CUBE_SIZE / 2,CUBE_SIZE / 4,100);
  27. tpe_world.bodies[tpe_world.bodyCount - 1].elasticity = 256;
  28. tpe_world.bodies[tpe_world.bodyCount - 1].friction = 256;
  29. //if (i % 2)
  30. //tpe_world.bodies[tpe_world.bodyCount - 1].flags |= TPE_BODY_FLAG_NONROTATING;
  31. }
  32. #define move(i,x,y) \
  33. TPE_bodyMove(&tpe_world.bodies[i],TPE_vec3((CUBE_SIZE / 2 + 10) * x,10 + CUBE_SIZE / 2 + y * (CUBE_SIZE + 10),0));
  34. move(0,0,0)
  35. move(1,-2,0)
  36. move(2,2,0)
  37. move(3,-1,1)
  38. move(4,1,1)
  39. move(5,0,2)
  40. #undef move
  41. for (int i = 0; i < 6; ++i)
  42. {
  43. updateOrientPos(i);
  44. TPE_bodyDeactivate(&tpe_bodies[i]);
  45. }
  46. ballJoints[0] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2,0),0);
  47. ballJoints[1] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2 - 100,-600),0);
  48. ballJoints[2] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2 - 200,-1200),0);
  49. ballJoints[3] = TPE_joint(TPE_vec3(0,ROOM_SIZE / 2 - 300,-1800),400);
  50. ballJoints[3].velocity[1] = -200;
  51. ballConnections[0].joint1 = 0; ballConnections[0].joint2 = 1;
  52. ballConnections[1].joint1 = 1; ballConnections[1].joint2 = 2;
  53. ballConnections[2].joint1 = 2; ballConnections[2].joint2 = 3;
  54. TPE_bodyInit(&tpe_world.bodies[6],ballJoints,4,ballConnections,3,10000);
  55. tpe_world.bodyCount++;
  56. while (helper_running)
  57. {
  58. helper_frameStart();
  59. helper_cameraFreeMovement();
  60. if (helper_frame % 16 == 0)
  61. {
  62. helper_printCPU();
  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_jointPin(&tpe_world.bodies[6].joints[0],TPE_vec3(0,ROOM_SIZE / 2 - 10,0));
  72. tpe_world.bodies[6].deactivateCount = 0;
  73. TPE_worldStep(&tpe_world);
  74. helper_set3dColor(100,100,100);
  75. helper_draw3dCubeInside(TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE),TPE_vec3(0,0,0));
  76. helper_set3dColor(200,10,10);
  77. for (int i = 0; i < 6; ++i)
  78. {
  79. TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
  80. if (!(tpe_world.bodies[i].flags & TPE_BODY_FLAG_DEACTIVATED))
  81. updateOrientPos(i);
  82. helper_draw3dCube(cubePositions[i],TPE_vec3(CUBE_SIZE,CUBE_SIZE,CUBE_SIZE),cubeOrientations[i]);
  83. }
  84. tpe_world.bodies[6].joints[3].velocity[1] -= 5;
  85. helper_draw3dSphere(tpe_world.bodies[6].joints[3].position,TPE_vec3(400,400,400),TPE_vec3(0,0,0) );
  86. for (int i = 0; i < 3; ++i)
  87. helper_drawLine3D(
  88. tpe_world.bodies[6].joints[tpe_world.bodies[6].connections[i].joint1].position,
  89. tpe_world.bodies[6].joints[tpe_world.bodies[6].connections[i].joint2].position,
  90. 255,0,0);
  91. if (helper_debugDrawOn)
  92. helper_debugDraw();
  93. helper_frameEnd();
  94. }
  95. helper_end();
  96. return 0;
  97. }