water.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #define CAMERA_STEP (TPE_F / 2)
  2. #define JOINT_SIZE (TPE_F / 4)
  3. #define BALL_SIZE (5 * TPE_F / 4)
  4. #define HEIGHTMAP_3D_RESOLUTION 8
  5. #define HEIGHTMAP_3D_STEP (TPE_F * 2)
  6. #include "helper.h"
  7. #define ROOM_SIZE (HEIGHTMAP_3D_RESOLUTION * HEIGHTMAP_3D_STEP + JOINT_SIZE)
  8. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  9. {
  10. return TPE_envAABoxInside(p,TPE_vec3(0,0,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE,ROOM_SIZE));
  11. }
  12. #define WATER_JOINTS (HEIGHTMAP_3D_RESOLUTION * HEIGHTMAP_3D_RESOLUTION)
  13. #define WATER_CONNECTIONS (2 * ((HEIGHTMAP_3D_RESOLUTION - 1) * HEIGHTMAP_3D_RESOLUTION))
  14. TPE_Joint joints[WATER_JOINTS + 1];
  15. TPE_Connection connections[WATER_CONNECTIONS];
  16. TPE_Body bodies[2];
  17. int main(void)
  18. {
  19. helper_init();
  20. puts("WSAD, XC: move the ball");
  21. s3l_scene.camera.transform.translation.z = -6 * TPE_F;
  22. s3l_scene.camera.transform.translation.y = 4 * TPE_F;
  23. s3l_scene.camera.transform.translation.x = 0;
  24. s3l_scene.camera.transform.rotation.y = TPE_F / 16;
  25. // build the water body:
  26. for (int i = 0; i < HEIGHTMAP_3D_POINTS; ++i)
  27. joints[i] = TPE_joint(helper_heightmapPointLocation(i),JOINT_SIZE);
  28. int index = 0;
  29. for (int j = 0; j < HEIGHTMAP_3D_RESOLUTION; ++j)
  30. for (int i = 0; i < HEIGHTMAP_3D_RESOLUTION - 1; ++i)
  31. {
  32. connections[index].joint1 = j * HEIGHTMAP_3D_RESOLUTION + i;
  33. connections[index].joint2 = connections[index].joint1 + 1;
  34. index++;
  35. connections[index].joint1 = i * HEIGHTMAP_3D_RESOLUTION + j;
  36. connections[index].joint2 = connections[index].joint1 + HEIGHTMAP_3D_RESOLUTION;
  37. index++;
  38. }
  39. TPE_bodyInit(&bodies[0],joints,WATER_JOINTS,connections,WATER_CONNECTIONS,
  40. 2 * TPE_F);
  41. bodies[0].flags |= TPE_BODY_FLAG_SOFT;
  42. bodies[0].flags |= TPE_BODY_FLAG_ALWAYS_ACTIVE;
  43. // create the ball body:
  44. joints[WATER_JOINTS] = TPE_joint(TPE_vec3(0,0,ROOM_SIZE / 4),BALL_SIZE);
  45. TPE_bodyInit(&bodies[1],joints + WATER_JOINTS,1,connections,0,200);
  46. bodies[1].flags |= TPE_BODY_FLAG_ALWAYS_ACTIVE;
  47. TPE_worldInit(&tpe_world,bodies,2,environmentDistance);
  48. while (helper_running)
  49. {
  50. helper_frameStart();
  51. helper_cameraFreeMovement();
  52. // update the 3D model vertex positions:
  53. S3L_Unit *v = heightmapVertices;
  54. for (int i = 0; i < WATER_JOINTS; ++i)
  55. {
  56. *v = joints[i].position.x;
  57. v++;
  58. *v = joints[i].position.y;
  59. v++;
  60. *v = joints[i].position.z;
  61. v++;
  62. }
  63. // pin the joints at the edges of the grid:
  64. for (int index = 0; index < WATER_JOINTS; ++index)
  65. if (index % HEIGHTMAP_3D_RESOLUTION == 0 || index % HEIGHTMAP_3D_RESOLUTION == HEIGHTMAP_3D_RESOLUTION - 1 ||
  66. index / HEIGHTMAP_3D_RESOLUTION == 0 || index / HEIGHTMAP_3D_RESOLUTION == HEIGHTMAP_3D_RESOLUTION - 1)
  67. TPE_jointPin(&joints[index],helper_heightmapPointLocation(index));
  68. TPE_worldStep(&tpe_world);
  69. #define G ((5 * 30) / FPS)
  70. TPE_bodyApplyGravity(&tpe_world.bodies[1],
  71. bodies[1].joints[0].position.y > 0 ? G : (-2 * G));
  72. #define ACC ((25 * 30) / FPS )
  73. if (sdl_keyboard[SDL_SCANCODE_W])
  74. TPE_bodyAccelerate(&bodies[1],TPE_vec3(0,0,ACC));
  75. else if (sdl_keyboard[SDL_SCANCODE_S])
  76. TPE_bodyAccelerate(&bodies[1],TPE_vec3(0,0,-1 * ACC));
  77. else if (sdl_keyboard[SDL_SCANCODE_D])
  78. TPE_bodyAccelerate(&bodies[1],TPE_vec3(ACC,0,0));
  79. else if (sdl_keyboard[SDL_SCANCODE_A])
  80. TPE_bodyAccelerate(&bodies[1],TPE_vec3(-1 * ACC,0,0));
  81. else if (sdl_keyboard[SDL_SCANCODE_C])
  82. TPE_bodyAccelerate(&bodies[1],TPE_vec3(0,ACC,0));
  83. else if (sdl_keyboard[SDL_SCANCODE_X])
  84. TPE_bodyAccelerate(&bodies[1],TPE_vec3(0,-1 * ACC,0));
  85. helper_set3DColor(255,0,0);
  86. helper_draw3DSphere(bodies[1].joints[0].position,TPE_vec3(BALL_SIZE,BALL_SIZE,BALL_SIZE),TPE_vec3(0,0,0));
  87. helper_set3DColor(0,100,255);
  88. helper_drawModel(&heightmapModel,TPE_vec3(0,0,0),TPE_vec3(TPE_F,TPE_F,TPE_F),TPE_vec3(0,0,0));
  89. if (helper_debugDrawOn)
  90. helper_debugDraw(1);
  91. helper_frameEnd();
  92. }
  93. helper_end();
  94. return 0;
  95. }