player.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //#define SCALE_3D_RENDERING 1
  2. #define S3L_NEAR_CROSS_STRATEGY 2
  3. #define S3L_PERSPECTIVE_CORRECTION 2
  4. #include "helper.h"
  5. #define ROOM_SIZE 10000
  6. #define CUBE_SIZE 800
  7. TPE_Unit elevatorHeight;
  8. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  9. {
  10. TPE_ENV_START( TPE_envAABoxInside(p,TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE)),p )
  11. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(4000,160,4000),TPE_vec3(1000,160,1000)),p )
  12. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(4000,80,2500),TPE_vec3(1000,80,500)),p )
  13. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-1000,270,4500),TPE_vec3(4000,270,250)),p )
  14. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-4000,elevatorHeight,0),TPE_vec3(1000,elevatorHeight,1000)),p )
  15. TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,-2000),TPE_vec3(0,255,255)),p )
  16. TPE_ENV_NEXT( TPE_envInfiniteCylinder(p,TPE_vec3(2000,0,-1100),TPE_vec3(0,255,0),400),p )
  17. TPE_ENV_END
  18. }
  19. int jumpCountdown = 0, onGround = 0;
  20. TPE_Unit playerRotation = 0, groundDist;
  21. TPE_Vec3 ballRot, ballPreviousPos, playerDirectionVec;
  22. TPE_Body *playerBody = 0;
  23. void updateDirection(void) // updates player direction vector
  24. {
  25. playerDirectionVec.x = TPE_sin(playerRotation);
  26. playerDirectionVec.z = TPE_cos(playerRotation);
  27. playerDirectionVec.y = 0;
  28. }
  29. int main(void)
  30. {
  31. helper_init();
  32. updateDirection();
  33. ballRot = TPE_vec3(0,0,0);
  34. tpe_world.environmentFunction = environmentDistance;
  35. helper_add2Line(400,300,400);
  36. playerBody = &(tpe_world.bodies[0]);
  37. TPE_bodyMoveBy(&tpe_world.bodies[0],TPE_vec3(0,1000,0));
  38. TPE_bodyRotateByAxis(&tpe_world.bodies[0],TPE_vec3(0,0,TPE_FRACTIONS_PER_UNIT / 4));
  39. playerBody->elasticity = 0;
  40. playerBody->friction = 0;
  41. playerBody->flags |= TPE_BODY_FLAG_NONROTATING;
  42. groundDist = TPE_JOINT_SIZE(playerBody->joints[0]) + 30;
  43. helper_addBall(1000,100);
  44. TPE_bodyMoveBy(&tpe_world.bodies[1],TPE_vec3(-1000,1000,0));
  45. tpe_world.bodies[1].elasticity = 400;
  46. tpe_world.bodies[1].friction = 100;
  47. ballPreviousPos = tpe_world.bodies[1].joints[0].position;
  48. helper_addCenterRect(600,600,400,50);
  49. TPE_bodyMoveBy(&tpe_world.bodies[2],TPE_vec3(-3000,1000,2000));
  50. tpe_world.bodies[2].elasticity = 100;
  51. tpe_world.bodies[2].friction = 50;
  52. while (helper_running)
  53. {
  54. helper_frameStart();
  55. TPE_worldStep(&tpe_world);
  56. if (jumpCountdown > 0)
  57. jumpCountdown--;
  58. /* Check whether on ground. This can be done in several ways, e.g. by
  59. checking recent collisions. We'll do it by casting a downwards ray: */
  60. onGround = (playerBody->flags & TPE_BODY_FLAG_DEACTIVATED) ||
  61. TPE_DISTANCE( playerBody->joints[0].position,
  62. TPE_castEnvironmentRay(playerBody->joints[0].position,
  63. TPE_vec3(0,-1 * TPE_FRACTIONS_PER_UNIT,0),tpe_world.environmentFunction,
  64. 128,512,512)) <= groundDist;
  65. elevatorHeight = TPE_sin(helper_frame * 4) + TPE_FRACTIONS_PER_UNIT;
  66. s3l_scene.camera.transform.translation.x = playerBody->joints[0].position.x;
  67. s3l_scene.camera.transform.translation.z = playerBody->joints[0].position.z;
  68. s3l_scene.camera.transform.translation.y = TPE_keepInRange(
  69. s3l_scene.camera.transform.translation.y,
  70. playerBody->joints[1].position.y,
  71. playerBody->joints[1].position.y + 10);
  72. TPE_bodyMultiplyNetSpeed(&tpe_world.bodies[0],onGround ? 300 : 505);
  73. s3l_scene.camera.transform.rotation.y = -1 * playerRotation;
  74. TPE_Vec3 ballRoll = TPE_fakeSphereRotation(ballPreviousPos,
  75. tpe_world.bodies[1].joints[0].position,1000);
  76. ballRot = TPE_rotationRotateByAxis(ballRot,ballRoll);
  77. ballPreviousPos = tpe_world.bodies[1].joints[0].position;
  78. for (int i = 0; i < tpe_world.bodyCount; ++i)
  79. TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
  80. if (onGround)
  81. {
  82. TPE_bodyActivate(&tpe_world.bodies[0]);
  83. if (sdl_keyboard[SDL_SCANCODE_SPACE] && jumpCountdown == 0)
  84. {
  85. playerBody->joints[0].velocity[1] = 90;
  86. jumpCountdown = 8;
  87. }
  88. #define D 16 // just some vector divisor to make the speed slower
  89. if (sdl_keyboard[SDL_SCANCODE_UP] || sdl_keyboard[SDL_SCANCODE_W])
  90. {
  91. playerBody->joints[0].velocity[0] += playerDirectionVec.x / D;
  92. playerBody->joints[0].velocity[2] += playerDirectionVec.z / D;
  93. }
  94. else if (sdl_keyboard[SDL_SCANCODE_DOWN] || sdl_keyboard[SDL_SCANCODE_S])
  95. {
  96. playerBody->joints[0].velocity[0] -= playerDirectionVec.x / D;
  97. playerBody->joints[0].velocity[2] -= playerDirectionVec.z / D;
  98. }
  99. if (sdl_keyboard[SDL_SCANCODE_A])
  100. {
  101. playerBody->joints[0].velocity[2] += playerDirectionVec.x / D;
  102. playerBody->joints[0].velocity[0] -= playerDirectionVec.z / D;
  103. }
  104. else if (sdl_keyboard[SDL_SCANCODE_D])
  105. {
  106. playerBody->joints[0].velocity[2] -= playerDirectionVec.x / D;
  107. playerBody->joints[0].velocity[0] += playerDirectionVec.z / D;
  108. }
  109. #undef D
  110. }
  111. updateDirection();
  112. if (sdl_keyboard[SDL_SCANCODE_LEFT])
  113. playerRotation -= 8;
  114. else if (sdl_keyboard[SDL_SCANCODE_RIGHT])
  115. playerRotation += 8;
  116. if (helper_frame % 64 == 0)
  117. helper_printCPU();
  118. // draw the 3D environment
  119. helper_set3dColor(180,180,180);
  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_set3dColor(100,200,180);
  122. helper_draw3dBox(TPE_vec3(4000,160,4000),TPE_vec3(2000,320,2000),TPE_vec3(0,0,0));
  123. helper_draw3dBox(TPE_vec3(4000,80,2500),TPE_vec3(2000,160,1000),TPE_vec3(0,0,0));
  124. helper_draw3dBox(TPE_vec3(-1000,270,4500),TPE_vec3(8000,540,500),TPE_vec3(0,0,0));
  125. helper_draw3dBox(TPE_vec3(-4000,elevatorHeight,0),TPE_vec3(2000,2 * elevatorHeight,2000),TPE_vec3(0,0,0));
  126. helper_draw3dCylinder(TPE_vec3(2000,5000,-1100),TPE_vec3(400,10000,400),TPE_vec3(0,0,0));
  127. helper_draw3dPlane(TPE_vec3(0,1500,-3500),TPE_vec3(10000,512,4000),TPE_vec3(-64,0,0));
  128. helper_set3dColor(200,50,0);
  129. helper_draw3dBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[2]),
  130. TPE_vec3(1200,800,1200),TPE_bodyGetRotation(&tpe_world.bodies[2],0,2,1));
  131. helper_draw3dSphere(tpe_world.bodies[1].joints[0].position,
  132. TPE_vec3(1000,1000,1000),ballRot);
  133. if (helper_debugDrawOn)
  134. helper_debugDraw(1);
  135. helper_frameEnd();
  136. }
  137. helper_end();
  138. return 0;
  139. }