shoot.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /** Tiny catapult Angry Birds like shooter demo. */
  2. #include "helper.h"
  3. #define CATAPULT_HEIGHT (TPE_F * 2)
  4. #define CATAPULT_WIDTH (3 * TPE_F / 2)
  5. #define BALL_RADIUS (2 * TPE_F / 5)
  6. uint8_t collisionCallback(uint16_t b1, uint16_t j1, uint16_t b2, uint16_t j2,
  7. TPE_Vec3 p)
  8. {
  9. // disable collision between the catapult and ball
  10. return !((b1 == 0 && b2 == 1) || (b2 == 1 && b1 == 0));
  11. }
  12. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  13. {
  14. return TPE_envGround(p,0);
  15. }
  16. TPE_Joint joints[3];
  17. TPE_Connection connections[2];
  18. TPE_Body catapult;
  19. uint8_t released = 0;
  20. uint8_t anythingHit = 0;
  21. int main(void)
  22. {
  23. helper_init();
  24. tpe_world.collisionCallback = collisionCallback;
  25. tpe_world.environmentFunction = environmentDistance;
  26. s3l_scene.camera.transform.translation.x = -2032;
  27. s3l_scene.camera.transform.translation.y = 1838;
  28. s3l_scene.camera.transform.translation.z = -1705;
  29. s3l_scene.camera.transform.rotation.x = -5;
  30. s3l_scene.camera.transform.rotation.y = -102;
  31. joints[0] = TPE_joint(TPE_vec3(0,CATAPULT_HEIGHT,CATAPULT_WIDTH / 2),10);
  32. joints[1] = TPE_joint(TPE_vec3(0,CATAPULT_HEIGHT,0),10);
  33. joints[2] = TPE_joint(TPE_vec3(0,CATAPULT_HEIGHT,-1 * CATAPULT_WIDTH / 2),10);
  34. connections[0].joint1 = 0;
  35. connections[0].joint2 = 1;
  36. connections[1].joint1 = 1;
  37. connections[1].joint2 = 2;
  38. TPE_bodyInit(&tpe_bodies[0],joints,3,connections,2,10);
  39. joints[1].position.x -= 2 * TPE_F;
  40. joints[1].position.y -= TPE_F / 2;
  41. tpe_bodies[0].flags |= TPE_BODY_FLAG_SOFT;
  42. tpe_bodies[0].flags |= TPE_BODY_FLAG_SIMPLE_CONN; // this makes the string faster
  43. tpe_world.bodyCount++;
  44. helper_addBall(BALL_RADIUS,2 * TPE_F);
  45. helper_addCenterBox(TPE_F,TPE_F,3 * TPE_F / 2,2 * TPE_F / 5,TPE_F);
  46. helper_lastBody.joints[8].sizeDivided *= 2;
  47. TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(8 * TPE_F,6 * TPE_F / 5,-6 * TPE_F / 5));
  48. TPE_bodyDeactivate(&helper_lastBody);
  49. helper_addCenterBox(TPE_F,TPE_F,3 * TPE_F / 2,2 * TPE_F / 5,TPE_F);
  50. helper_lastBody.joints[8].sizeDivided *= 2;
  51. TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(8 * TPE_F,6 * TPE_F / 5,6 * TPE_F / 5));
  52. TPE_bodyDeactivate(&helper_lastBody);
  53. helper_addCenterRect(TPE_F,3 * TPE_F,TPE_F / 2,TPE_F);
  54. helper_lastBody.joints[4].sizeDivided *= 3;
  55. helper_lastBody.joints[4].sizeDivided /= 2;
  56. TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(8 * TPE_F,3 * TPE_F - TPE_F / 4,0));
  57. TPE_bodyDeactivate(&helper_lastBody);
  58. helper_addCenterBox(6 * TPE_F / 5,6 * TPE_F / 5,2 * TPE_F - TPE_F / 6,TPE_F / 3,TPE_F);
  59. helper_lastBody.joints[8].sizeDivided *= 2;
  60. TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(8 * TPE_F,4 * TPE_F + TPE_F / 3,0));
  61. TPE_bodyDeactivate(&helper_lastBody);
  62. while (helper_running)
  63. {
  64. helper_frameStart();
  65. helper_cameraFreeMovement();
  66. if (released)
  67. {
  68. TPE_worldStep(&tpe_world);
  69. TPE_jointPin(&joints[0],TPE_vec3(0,CATAPULT_HEIGHT,CATAPULT_WIDTH / 2));
  70. TPE_jointPin(&joints[2],TPE_vec3(0,CATAPULT_HEIGHT,-1 * CATAPULT_WIDTH / 2));
  71. for (int i = 1; i < tpe_world.bodyCount; ++i)
  72. TPE_bodyApplyGravity(&tpe_world.bodies[i],6);
  73. // make the string lose energy over time:
  74. TPE_bodyMultiplyNetSpeed(&tpe_world.bodies[0],(19 * TPE_F) / 20);
  75. }
  76. if (!released || tpe_world.bodies[1].joints[0].position.x < 0)
  77. {
  78. tpe_world.bodies[1].joints[0].position = joints[1].position;
  79. for (int i = 0; i < 3; ++i)
  80. tpe_world.bodies[1].joints[0].velocity[i] = joints[1].velocity[i];
  81. }
  82. if (sdl_keyboard[SDL_SCANCODE_SPACE])
  83. released = 1;
  84. if (!released)
  85. {
  86. #define OFFSET (TPE_F / 20)
  87. if (sdl_keyboard[SDL_SCANCODE_W])
  88. joints[1].position.y += OFFSET;
  89. else if (sdl_keyboard[SDL_SCANCODE_S] && joints[1].position.y > 0)
  90. joints[1].position.y -= OFFSET;
  91. if (sdl_keyboard[SDL_SCANCODE_F])
  92. joints[1].position.x -= OFFSET;
  93. else if (sdl_keyboard[SDL_SCANCODE_G] && joints[1].position.x < 5)
  94. joints[1].position.x += OFFSET;
  95. if (sdl_keyboard[SDL_SCANCODE_D])
  96. joints[1].position.z -= OFFSET;
  97. else if (sdl_keyboard[SDL_SCANCODE_A])
  98. joints[1].position.z += OFFSET;
  99. #undef OFFSET
  100. }
  101. else if (!anythingHit)
  102. {
  103. /* here we check if any of the tower bodies is active which means it has
  104. been hit in which case we activate all the bodies so that they do their
  105. thing and none stays hanging in the air :) */
  106. for (int i = 2; i < tpe_world.bodyCount; ++i)
  107. {
  108. if (TPE_bodyIsActive(&tpe_world.bodies[i]))
  109. {
  110. puts("someting was hit, activating all bodies");
  111. TPE_worldActivateAll(&tpe_world);
  112. anythingHit = 1;
  113. break;
  114. }
  115. }
  116. }
  117. helper_set3DColor(200,200,255);
  118. helper_draw3DSphere(tpe_world.bodies[1].joints[0].position,TPE_vec3(BALL_RADIUS,BALL_RADIUS,BALL_RADIUS),TPE_vec3(0,0,0));
  119. helper_set3DColor(200,255,200);
  120. helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[2]),TPE_vec3(700,1000,700),
  121. TPE_bodyGetRotation(&tpe_world.bodies[2],0,1,2));
  122. helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[3]),TPE_vec3(700,1000,700),
  123. TPE_bodyGetRotation(&tpe_world.bodies[3],0,1,2));
  124. helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[4]),TPE_vec3(2000,500,700),
  125. TPE_bodyGetRotation(&tpe_world.bodies[4],0,1,2));
  126. helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[5]),TPE_vec3(800,1100,800),
  127. TPE_bodyGetRotation(&tpe_world.bodies[5],0,1,2));
  128. helper_drawLine3D(TPE_vec3(0,0,0),TPE_vec3(0,CATAPULT_HEIGHT / 2,0),255,0,0 );
  129. helper_drawLine3D(TPE_vec3(0,CATAPULT_HEIGHT / 2,0),TPE_vec3(0,CATAPULT_HEIGHT,-1 * CATAPULT_WIDTH / 2),255,0,0);
  130. helper_drawLine3D(TPE_vec3(0,CATAPULT_HEIGHT / 2,0),TPE_vec3(0,CATAPULT_HEIGHT,CATAPULT_WIDTH / 2),255,0,0);
  131. helper_drawLine3D(TPE_vec3(0,CATAPULT_HEIGHT,-1 * CATAPULT_WIDTH / 2),joints[1].position,0,255,0);
  132. helper_drawLine3D(TPE_vec3(0,CATAPULT_HEIGHT,CATAPULT_WIDTH / 2),joints[1].position,0,255,0);
  133. if (helper_debugDrawOn)
  134. helper_debugDraw(1);
  135. helper_frameEnd();
  136. }
  137. helper_end();
  138. return 0;
  139. }