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