water.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //#define FPS 60
  2. #define CAMERA_STEP 200
  3. #include "helper.h"
  4. #define GRID_RESOLUTION 8
  5. #define GRID_STEP 1000
  6. #define JOINT_SIZE 100
  7. #define BALL_SIZE 700
  8. #define ROOM_SIZE (GRID_RESOLUTION * GRID_STEP + JOINT_SIZE)
  9. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  10. {
  11. return TPE_envAABoxInside(p,TPE_vec3(0,0,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE,ROOM_SIZE));
  12. }
  13. #define WATER_JOINTS (GRID_RESOLUTION * GRID_RESOLUTION)
  14. #define WATER_CONNECTIONS (2 * ((GRID_RESOLUTION - 1) * GRID_RESOLUTION))
  15. TPE_Joint joints[WATER_JOINTS + 1];
  16. TPE_Connection connections[WATER_CONNECTIONS];
  17. S3L_Unit vertices[WATER_JOINTS * 3];
  18. S3L_Index triangles[((GRID_RESOLUTION - 1) * (GRID_RESOLUTION - 1) * 2) * 3];
  19. S3L_Model3D model;
  20. TPE_Body bodies[2];
  21. TPE_Vec3 jointPlace(int index)
  22. {
  23. return TPE_vec3((-1 * GRID_RESOLUTION * GRID_STEP) / 2 + (index % GRID_RESOLUTION) * GRID_STEP + GRID_STEP / 2,0,
  24. (-1 * GRID_RESOLUTION * GRID_STEP) / 2 + (index / GRID_RESOLUTION) * GRID_STEP + GRID_STEP / 2);
  25. }
  26. int main(void)
  27. {
  28. helper_init();
  29. puts("WSAD, XC: move the ball");
  30. helper_debugDrawOn = 1;
  31. s3l_scene.camera.transform.translation.z = -3000;
  32. s3l_scene.camera.transform.translation.y = 2000;
  33. s3l_scene.camera.transform.translation.x = 0;
  34. s3l_scene.camera.transform.rotation.y = TPE_FRACTIONS_PER_UNIT / 16;
  35. // build the grid:
  36. int index = 0;
  37. for (int j = 0; j < GRID_RESOLUTION; ++j)
  38. for (int i = 0; i < GRID_RESOLUTION; ++i)
  39. {
  40. joints[j * GRID_RESOLUTION + i] = TPE_joint(jointPlace(index),JOINT_SIZE);
  41. index++;
  42. }
  43. index = 0;
  44. for (int j = 0; j < GRID_RESOLUTION; ++j)
  45. for (int i = 0; i < GRID_RESOLUTION - 1; ++i)
  46. {
  47. connections[index].joint1 = j * GRID_RESOLUTION + i;
  48. connections[index].joint2 = connections[index].joint1 + 1;
  49. index++;
  50. connections[index].joint1 = i * GRID_RESOLUTION + j;
  51. connections[index].joint2 = connections[index].joint1 + GRID_RESOLUTION;
  52. index++;
  53. }
  54. index = 0;
  55. for (int j = 0; j < GRID_RESOLUTION - 1; ++j)
  56. for (int i = 0; i < GRID_RESOLUTION - 1; ++i)
  57. {
  58. triangles[index] = j * GRID_RESOLUTION + i;
  59. triangles[index + 1] = triangles[index] + 1;
  60. triangles[index + 2] = triangles[index + 1] + GRID_RESOLUTION;
  61. triangles[index + 3] = triangles[index];
  62. triangles[index + 4] = triangles[index + 1] + GRID_RESOLUTION;
  63. triangles[index + 5] = triangles[index] + GRID_RESOLUTION;
  64. index += 6;
  65. }
  66. S3L_model3DInit(
  67. vertices,
  68. WATER_JOINTS * 3,
  69. triangles,
  70. ((GRID_RESOLUTION - 1) * (GRID_RESOLUTION - 1) * 2),
  71. &model);
  72. TPE_bodyInit(&bodies[0],joints,WATER_JOINTS,connections,WATER_CONNECTIONS,
  73. 1000);
  74. bodies[0].flags |= TPE_BODY_FLAG_SOFT;
  75. joints[WATER_JOINTS] = TPE_joint(TPE_vec3(0,0,ROOM_SIZE / 4),BALL_SIZE);
  76. TPE_bodyInit(&bodies[1],joints + WATER_JOINTS,1,connections,0,200);
  77. TPE_worldInit(&tpe_world,bodies,2,environmentDistance);
  78. while (helper_running)
  79. {
  80. helper_frameStart();
  81. helper_cameraFreeMovement();
  82. TPE_bodyActivate(&bodies[0]);
  83. TPE_bodyActivate(&bodies[1]);
  84. S3L_Unit *v = vertices;
  85. for (int i = 0; i < WATER_JOINTS; ++i)
  86. {
  87. *v = joints[i].position.x;
  88. v++;
  89. *v = joints[i].position.y;
  90. v++;
  91. *v = joints[i].position.z;
  92. v++;
  93. }
  94. for (int index = 0; index < WATER_JOINTS; ++index)
  95. if (index % GRID_RESOLUTION == 0 || index % GRID_RESOLUTION == GRID_RESOLUTION - 1 ||
  96. index / GRID_RESOLUTION == 0 || index / GRID_RESOLUTION == GRID_RESOLUTION - 1)
  97. TPE_jointPin(&joints[index],jointPlace(index));
  98. TPE_worldStep(&tpe_world);
  99. #define G ((5 * 30) / FPS)
  100. TPE_bodyApplyGravity(&tpe_world.bodies[1], bodies[1].joints[0].position.y > 0 ? G : (-2 * G));
  101. #define ACC ((25 * 30) / FPS )
  102. if (sdl_keyboard[SDL_SCANCODE_W])
  103. TPE_bodyAccelerate(&bodies[1],TPE_vec3(0,0,ACC));
  104. else if (sdl_keyboard[SDL_SCANCODE_S])
  105. TPE_bodyAccelerate(&bodies[1],TPE_vec3(0,0,-1 * ACC));
  106. else if (sdl_keyboard[SDL_SCANCODE_D])
  107. TPE_bodyAccelerate(&bodies[1],TPE_vec3(ACC,0,0));
  108. else if (sdl_keyboard[SDL_SCANCODE_A])
  109. TPE_bodyAccelerate(&bodies[1],TPE_vec3(-1 * ACC,0,0));
  110. else if (sdl_keyboard[SDL_SCANCODE_C])
  111. TPE_bodyAccelerate(&bodies[1],TPE_vec3(0,ACC,0));
  112. else if (sdl_keyboard[SDL_SCANCODE_X])
  113. TPE_bodyAccelerate(&bodies[1],TPE_vec3(0,-1 * ACC,0));
  114. helper_set3dColor(255,0,0);
  115. helper_draw3dSphere(bodies[1].joints[0].position,TPE_vec3(BALL_SIZE,BALL_SIZE,BALL_SIZE),TPE_vec3(0,0,0));
  116. helper_set3dColor(0,100,255);
  117. helper_drawModel(&model,TPE_vec3(0,0,0),TPE_vec3(512,512,512),TPE_vec3(0,0,0));
  118. if (helper_debugDrawOn)
  119. helper_debugDraw(1);
  120. helper_frameEnd();
  121. }
  122. helper_end();
  123. return 0;
  124. }