player.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #define SCALE_3D_RENDERING 1
  2. #include "helper.h"
  3. #define ROOM_SIZE 10000
  4. #define CUBE_SIZE 800
  5. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  6. {
  7. TPE_ENV_START( TPE_envAABoxInside(p,TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE)),p )
  8. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(4000,160,4000),TPE_vec3(1000,160,1000)),p )
  9. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(4000,80,2500),TPE_vec3(1000,80,500)),p )
  10. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-1000,270,4500),TPE_vec3(4000,270,250)),p )
  11. TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,-2000),TPE_vec3(0,255,255)),p )
  12. TPE_ENV_NEXT( TPE_envInfiniteCylinder(p,TPE_vec3(2000,0,-1100),TPE_vec3(0,255,0),400),p )
  13. TPE_ENV_END
  14. }
  15. int jumpCountdown = 0;
  16. TPE_Unit rotation = 0;
  17. int onGroundCount = 0;
  18. TPE_Vec3 ballRot, ballPreviousPos;
  19. TPE_Vec3 directionVec;
  20. void updateDirection(void)
  21. {
  22. directionVec.x = TPE_sin(rotation);
  23. directionVec.z = TPE_cos(rotation);
  24. directionVec.y = 0;
  25. }
  26. uint8_t collisionCallback(uint16_t b1, uint16_t j1, uint16_t b2, uint16_t j2,
  27. TPE_Vec3 p)
  28. {
  29. if (b1 == 0 && b1 == b2 && j1 == 0 &&
  30. p.y < tpe_world.bodies[0].joints[0].position.y -50)
  31. onGroundCount = 2;
  32. }
  33. int main(void)
  34. {
  35. helper_init();
  36. helper_debugDrawOn = 1;
  37. updateDirection();
  38. ballRot = TPE_vec3(0,0,0);
  39. tpe_world.environmentFunction = environmentDistance;
  40. tpe_world.collisionCallback = collisionCallback;
  41. s3l_scene.camera.transform.translation.z -= ROOM_SIZE / 2;
  42. s3l_scene.camera.transform.translation.y += ROOM_SIZE / 3;
  43. s3l_scene.camera.transform.translation.x -= ROOM_SIZE / 4;
  44. s3l_scene.camera.transform.rotation.y = -1 * TPE_FRACTIONS_PER_UNIT / 16;
  45. helper_add2Line(400,300,400);
  46. TPE_bodyMove(&tpe_world.bodies[0],TPE_vec3(0,1000,0));
  47. TPE_bodyRotateByAxis(&tpe_world.bodies[0],TPE_vec3(0,0,TPE_FRACTIONS_PER_UNIT / 4));
  48. tpe_world.bodies[0].elasticity = 0;
  49. tpe_world.bodies[0].friction = 0;
  50. tpe_world.bodies[0].flags |= TPE_BODY_FLAG_NONROTATING;
  51. helper_addBall(1000,100);
  52. TPE_bodyMove(&tpe_world.bodies[1],TPE_vec3(-1000,1000,0));
  53. ballPreviousPos = tpe_world.bodies[1].joints[0].position;
  54. tpe_world.bodies[1].elasticity = 400;
  55. tpe_world.bodies[1].friction = 100;
  56. //helper_addBox(400,400,400,200,300);
  57. helper_addCenterRect(600,600,400,50);
  58. //helper_addBox(500,500,500,250,300);
  59. TPE_bodyMove(&tpe_world.bodies[2],TPE_vec3(-3000,1000,2000));
  60. tpe_world.bodies[2].elasticity = 400;
  61. tpe_world.bodies[2].friction = 50;
  62. while (helper_running)
  63. {
  64. if (onGroundCount > 0)
  65. onGroundCount--;
  66. if (jumpCountdown > 0)
  67. jumpCountdown--;
  68. helper_frameStart();
  69. s3l_scene.camera.transform.translation.x = tpe_world.bodies[0].joints[0].position.x;
  70. s3l_scene.camera.transform.translation.y = TPE_keepInRange(
  71. s3l_scene.camera.transform.translation.y,
  72. tpe_world.bodies[0].joints[1].position.y,
  73. tpe_world.bodies[0].joints[1].position.y + 10);
  74. TPE_bodyMultiplyNetSpeed(&tpe_world.bodies[0],onGroundCount ? 300 : 505);
  75. s3l_scene.camera.transform.translation.z = tpe_world.bodies[0].joints[0].position.z;
  76. s3l_scene.camera.transform.rotation.y = -1 * rotation;
  77. TPE_worldStep(&tpe_world);
  78. TPE_Vec3 ballRoll = TPE_fakeSphereRotation
  79. (ballPreviousPos,tpe_world.bodies[1].joints[0].position,1000);
  80. ballRot = TPE_rotationRotateByAxis(ballRot,ballRoll);
  81. ballPreviousPos = tpe_world.bodies[1].joints[0].position;
  82. TPE_bodyActivate(&tpe_world.bodies[0]);
  83. if (onGroundCount)
  84. {
  85. if (sdl_keyboard[SDL_SCANCODE_SPACE] && jumpCountdown == 0 && onGroundCount)
  86. {
  87. tpe_world.bodies[0].joints[0].velocity[1] = 80;
  88. jumpCountdown = 8;
  89. onGroundCount = 0;
  90. }
  91. #define AAA 16
  92. if (sdl_keyboard[SDL_SCANCODE_UP] || sdl_keyboard[SDL_SCANCODE_W])
  93. {
  94. tpe_world.bodies[0].joints[0].velocity[0] += directionVec.x / AAA;
  95. tpe_world.bodies[0].joints[0].velocity[2] += directionVec.z / AAA;
  96. }
  97. else if (sdl_keyboard[SDL_SCANCODE_DOWN] || sdl_keyboard[SDL_SCANCODE_S])
  98. {
  99. tpe_world.bodies[0].joints[0].velocity[0] -= directionVec.x / AAA;
  100. tpe_world.bodies[0].joints[0].velocity[2] -= directionVec.z / AAA;
  101. }
  102. if (sdl_keyboard[SDL_SCANCODE_A])
  103. {
  104. tpe_world.bodies[0].joints[0].velocity[2] += directionVec.x / AAA;
  105. tpe_world.bodies[0].joints[0].velocity[0] -= directionVec.z / AAA;
  106. }
  107. else if (sdl_keyboard[SDL_SCANCODE_D])
  108. {
  109. tpe_world.bodies[0].joints[0].velocity[2] -= directionVec.x / AAA;
  110. tpe_world.bodies[0].joints[0].velocity[0] += directionVec.z / AAA;
  111. }
  112. #undef AAA
  113. }
  114. if (sdl_keyboard[SDL_SCANCODE_LEFT])
  115. rotation -= 8;
  116. else if (sdl_keyboard[SDL_SCANCODE_RIGHT])
  117. rotation += 8;
  118. updateDirection();
  119. helper_set3dColor(100,100,100);
  120. helper_draw3dBoxInside(TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE),TPE_vec3(0,0,0));
  121. helper_draw3dBox(TPE_vec3(4000,160,4000),TPE_vec3(2000,320,2000),TPE_vec3(0,0,0));
  122. helper_draw3dBox(TPE_vec3(4000,80,2500),TPE_vec3(2000,160,1000),TPE_vec3(0,0,0));
  123. helper_draw3dBox(TPE_vec3(-1000,270,4500),TPE_vec3(8000,540,500),TPE_vec3(0,0,0));
  124. helper_draw3dCylinder(
  125. TPE_vec3(2000,
  126. 5000,-1100),TPE_vec3(400,10000,400),TPE_vec3(0,0,0));
  127. helper_draw3dPlane(
  128. TPE_vec3(0,1500,-3500),
  129. TPE_vec3(10000,512,4000),
  130. TPE_vec3(-64,0,0));
  131. helper_set3dColor(200,50,0);
  132. helper_draw3dBox(
  133. TPE_bodyGetCenterOfMass(&tpe_world.bodies[2]),
  134. TPE_vec3(1200,800,1200),
  135. TPE_bodyGetRotation(&tpe_world.bodies[2],0,2,1)
  136. );
  137. helper_draw3dSphere(
  138. tpe_world.bodies[1].joints[0].position
  139. ,TPE_vec3(1000,1000,1000),ballRot );
  140. helper_set3dColor(200,10,10);
  141. for (int i = 0; i < tpe_world.bodyCount; ++i)
  142. TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
  143. if (helper_debugDrawOn)
  144. helper_debugDraw();
  145. helper_frameEnd();
  146. }
  147. helper_end();
  148. return 0;
  149. }