car.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #define SCALE_3D_RENDERING 1
  2. #define S3L_NEAR_CROSS_STRATEGY 2
  3. #define S3L_PERSPECTIVE_CORRECTION 2
  4. #include "helper.h"
  5. #include "carArenaModel.h"
  6. #include "carModel.h"
  7. #define ACCELERATION 40
  8. #define TURN_RADIUS 5000
  9. TPE_Unit rampPoits[6] =
  10. {
  11. 0,0,
  12. -2400,1400,
  13. -2400,0
  14. };
  15. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  16. {
  17. TPE_ENV_START( TPE_envHalfPlane(p,TPE_vec3(0,0,0),TPE_vec3(0,512,0)),p )
  18. // TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,-2000),TPE_vec3(0,255,255)),p )
  19. TPE_ENV_NEXT( TPE_envSphereInside(p,TPE_vec3(0,10000,0),20000),p )
  20. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-8700,100,-800),TPE_vec3(2200,1000,800)),p )
  21. TPE_ENV_NEXT( TPE_envAATriPrism(p,TPE_vec3(8700,0,0),rampPoits,5000,2),p)
  22. TPE_ENV_NEXT( TPE_envSphere(p,TPE_vec3(0,-200,0),1700),p )
  23. TPE_ENV_END
  24. }
  25. TPE_Unit averageRot(TPE_Unit a1, TPE_Unit a2)
  26. {
  27. TPE_Unit d = a2 - a1;
  28. if (d > 100 || d < -100)
  29. return a2;
  30. return a1 + d / 2;
  31. }
  32. uint8_t steering = 0;
  33. TPE_Vec3 carRot;
  34. uint8_t jointCollisions;
  35. TPE_Vec3 jointPreviousPositions[4];
  36. uint8_t collisionCallback(uint16_t b1, uint16_t j1, uint16_t b2, uint16_t j2,
  37. TPE_Vec3 p)
  38. {
  39. if (b1 == 0 && b1 == b2 && j1 < 4)
  40. {
  41. jointCollisions |= 0x01 << j1;
  42. jointPreviousPositions[j1] = tpe_world.bodies[0].joints[j1].position;
  43. }
  44. }
  45. TPE_Vec3 ballRot, ballPreviousPos;
  46. TPE_Vec3 directionVec;
  47. TPE_Body *carBody;
  48. TPE_Vec3 carForw, carSide, carUp, carPos;
  49. TPE_Vec3 carVelocity;
  50. int backOnGround, frontOnGround;
  51. TPE_Unit wheelSize;
  52. int main(void)
  53. {
  54. arenaModelInit();
  55. carModelInit();
  56. /*
  57. TPE_Vec3 aaa = TPE_vec3(512,0,0);
  58. TPE_Vec3 mmm = TPE_vec3(10,0,0);
  59. TPE_Vec3 ffff = wheelFriction(aaa,mmm,512);
  60. TPE_PRINTF_VEC3(ffff)
  61. putchar('\n');
  62. return 0;
  63. */
  64. helper_init();
  65. ballRot = TPE_vec3(0,0,0);
  66. carPos = TPE_vec3(0,0,0);
  67. helper_debugDrawOn = 1;
  68. tpe_world.environmentFunction = environmentDistance;
  69. tpe_world.collisionCallback = collisionCallback;
  70. helper_addCenterRectFull(1000,1800,400,2000);
  71. tpe_world.bodies[0].joints[4].position.y += 600;
  72. tpe_world.bodies[0].joints[4].sizeDivided *= 3;
  73. tpe_world.bodies[0].joints[4].sizeDivided /= 2;
  74. carBody = &tpe_world.bodies[0];
  75. TPE_bodyInit(&tpe_world.bodies[0],
  76. tpe_world.bodies[0].joints,
  77. tpe_world.bodies[0].jointCount,
  78. tpe_world.bodies[0].connections,
  79. tpe_world.bodies[0].connectionCount,
  80. 300
  81. );
  82. wheelSize = TPE_JOINT_SIZE(tpe_world.bodies[0].joints[0]) + 30;
  83. TPE_bodyMoveBy(carBody,TPE_vec3(3000,1000,0));
  84. tpe_world.bodies[0].elasticity = 64;
  85. tpe_world.bodies[0].friction = 64;
  86. while (helper_running)
  87. {
  88. helper_frameStart();
  89. jointCollisions = 0;
  90. TPE_worldStep(&tpe_world);
  91. for (int i = 0; i < 4; ++i)
  92. {
  93. if (jointCollisions & (0x01 << i))
  94. {
  95. TPE_Vec3 axis = carSide;
  96. if (i >= 2 && steering)
  97. {
  98. if (steering == 1)
  99. axis = TPE_vec3Plus(carSide,carForw);
  100. else
  101. axis = TPE_vec3Minus(carSide,carForw);
  102. axis = TPE_vec3Normalized(axis);
  103. }
  104. }
  105. }
  106. S3L_Vec4 toCar;
  107. backOnGround =
  108. TPE_DISTANCE( tpe_world.environmentFunction(
  109. carBody->joints[0].position,wheelSize),
  110. carBody->joints[0].position ) <= wheelSize ||
  111. TPE_DISTANCE( tpe_world.environmentFunction(
  112. carBody->joints[1].position,wheelSize),
  113. carBody->joints[1].position ) <= wheelSize;
  114. frontOnGround =
  115. TPE_DISTANCE( tpe_world.environmentFunction(
  116. carBody->joints[2].position,wheelSize),
  117. carBody->joints[2].position ) <= wheelSize ||
  118. TPE_DISTANCE( tpe_world.environmentFunction(
  119. carBody->joints[3].position,wheelSize),
  120. carBody->joints[3].position ) <= wheelSize;
  121. s3l_scene.camera.transform.translation.y = carPos.y + 800;
  122. TPE_Vec3 cPos = TPE_vec3KeepWithinDistanceBand(
  123. TPE_vec3(
  124. s3l_scene.camera.transform.translation.x,
  125. s3l_scene.camera.transform.translation.y,
  126. s3l_scene.camera.transform.translation.z
  127. ),carBody->joints[4].position,2000,3000);
  128. s3l_scene.camera.transform.translation.x = cPos.x;
  129. s3l_scene.camera.transform.translation.y = cPos.y;
  130. s3l_scene.camera.transform.translation.z = cPos.z;
  131. toCar.x = carPos.x - s3l_scene.camera.transform.translation.x;
  132. toCar.y = carPos.y - s3l_scene.camera.transform.translation.y;
  133. toCar.z = carPos.z - s3l_scene.camera.transform.translation.z;
  134. toCar.w = 0;
  135. TPE_Unit angleDiff =
  136. s3l_scene.camera.transform.rotation.y -
  137. (TPE_vec2Angle(toCar.x,toCar.z) - 128);
  138. if (angleDiff < 100 && angleDiff > -100)
  139. s3l_scene.camera.transform.rotation.y -= angleDiff / 2;
  140. else
  141. s3l_scene.camera.transform.rotation.y -= angleDiff;
  142. //s3l_scene.camera.transform.rotation.y =
  143. // angle;
  144. s3l_scene.camera.transform.rotation.x = - 35;
  145. carPos = TPE_vec3KeepWithinBox(carPos,
  146. carBody->joints[4].position,TPE_vec3(20,20,20));
  147. //S3L_lookAt(toCar,&s3l_scene.camera.transform);
  148. carForw =
  149. TPE_vec3Plus(
  150. TPE_vec3Normalized(
  151. TPE_vec3Minus(carBody->joints[2].position,
  152. carBody->joints[0].position)),
  153. TPE_vec3Normalized(
  154. TPE_vec3Minus(carBody->joints[3].position,
  155. carBody->joints[1].position)) );
  156. carForw.x /= 2;
  157. carForw.y /= 2;
  158. carForw.z /= 2;
  159. carSide = TPE_vec3Normalized(
  160. TPE_vec3Minus(carBody->joints[1].position,
  161. carBody->joints[0].position));
  162. carUp = TPE_vec3Cross(carForw,carSide);
  163. #define AAA 8
  164. #define BBB 16
  165. #define CCC 60
  166. //helper_cameraFreeMovement();
  167. carVelocity.x = carBody->joints[4].velocity[0];
  168. carVelocity.y = carBody->joints[4].velocity[1];
  169. carVelocity.z = carBody->joints[4].velocity[2];
  170. if (sdl_keyboard[SDL_SCANCODE_D])
  171. steering = 1;
  172. else if (sdl_keyboard[SDL_SCANCODE_A])
  173. steering = 2;
  174. else
  175. steering = 0;
  176. TPE_Unit speed = TPE_vec3Dot(carVelocity,carForw);
  177. if (speed > CCC)
  178. speed = CCC;
  179. else if (speed < -1 * CCC)
  180. speed = -1 * CCC;
  181. speed /= 4;
  182. if (frontOnGround && steering)
  183. {
  184. TPE_Vec3 ccc = TPE_vec3Minus(
  185. carBody->joints[4].position,
  186. TPE_vec3Times(carForw,TURN_RADIUS)
  187. );
  188. TPE_bodySpinWithCenter(carBody,TPE_vec3Times(carUp,
  189. (AAA * speed * (steering == 1 ? -1 : 1) ) / CCC),ccc);
  190. }
  191. TPE_bodyActivate(carBody);
  192. if (backOnGround)
  193. {
  194. TPE_Unit acc = ACCELERATION + speed;
  195. #define AAAA 16
  196. if (sdl_keyboard[SDL_SCANCODE_W])
  197. {
  198. carBody->joints[0].velocity[0] += (carForw.x * acc) / TPE_FRACTIONS_PER_UNIT;
  199. carBody->joints[0].velocity[1] += (carForw.y * acc) / TPE_FRACTIONS_PER_UNIT;
  200. carBody->joints[0].velocity[2] += (carForw.z * acc) / TPE_FRACTIONS_PER_UNIT;
  201. carBody->joints[1].velocity[0] += (carForw.x * acc) / TPE_FRACTIONS_PER_UNIT;
  202. carBody->joints[1].velocity[1] += (carForw.y * acc) / TPE_FRACTIONS_PER_UNIT;
  203. carBody->joints[1].velocity[2] += (carForw.z * acc) / TPE_FRACTIONS_PER_UNIT;
  204. }
  205. else if (sdl_keyboard[SDL_SCANCODE_S])
  206. {
  207. carBody->joints[0].velocity[0] -= (carForw.x * acc) / TPE_FRACTIONS_PER_UNIT;
  208. carBody->joints[0].velocity[1] -= (carForw.y * acc) / TPE_FRACTIONS_PER_UNIT;
  209. carBody->joints[0].velocity[2] -= (carForw.z * acc) / TPE_FRACTIONS_PER_UNIT;
  210. carBody->joints[1].velocity[0] -= (carForw.x * acc) / TPE_FRACTIONS_PER_UNIT;
  211. carBody->joints[1].velocity[1] -= (carForw.y * acc) / TPE_FRACTIONS_PER_UNIT;
  212. carBody->joints[1].velocity[2] -= (carForw.z * acc) / TPE_FRACTIONS_PER_UNIT;
  213. }
  214. //TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3Times(carForw,BBB) );
  215. // else if (sdl_keyboard[SDL_SCANCODE_S])
  216. //TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3Times(carForw,-1 * BBB) );
  217. }
  218. #undef AAA
  219. helper_set3dColor(180,180,180);
  220. // helper_draw3dBoxInside(TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE),TPE_vec3(0,0,0));
  221. /*
  222. helper_draw3dPlane(
  223. TPE_vec3(0,0,0),
  224. TPE_vec3(30000,30000,30000),
  225. TPE_vec3(0,0,0));
  226. */
  227. /*
  228. helper_draw3dPlane(
  229. TPE_vec3(0,1500,-3500),
  230. TPE_vec3(10000,512,4000),
  231. TPE_vec3(-64,0,0));
  232. */
  233. helper_set3dColor(20,150,150);
  234. helper_drawModel(&arenaModel,TPE_vec3(0,0,0),TPE_vec3(512 * 32,512 * 32,512 * 32),
  235. TPE_vec3(0,0,0));
  236. /*
  237. helper_draw3dSphereInside(
  238. TPE_vec3(0,20000,0),TPE_vec3(30000,30000,30000),TPE_vec3(0,0,0));
  239. */
  240. S3L_zBufferClear();
  241. helper_set3dColor(200,200,200);
  242. TPE_Vec3 newRot =
  243. TPE_bodyGetRotation(&tpe_world.bodies[0],0,2,1);
  244. carRot.x = (TPE_abs(carRot.x - newRot.x) < 50) ?
  245. (carRot.x + newRot.x) / 2 : newRot.x;
  246. carRot.x = averageRot(carRot.x,newRot.x);
  247. carRot.y = averageRot(carRot.y,newRot.y);
  248. carRot.z = averageRot(carRot.z,newRot.z);
  249. helper_drawModel(&carModel,
  250. TPE_vec3Minus(carPos,TPE_vec3Times(carUp,400)),
  251. TPE_vec3(600,600,600),
  252. carRot
  253. );
  254. /*
  255. helper_draw3dBox(
  256. carPos,
  257. TPE_vec3(1200,800,1200),
  258. TPE_bodyGetRotation(&tpe_world.bodies[0],0,2,1)
  259. );
  260. */
  261. if (TPE_vec3Dot(carUp,TPE_vec3Minus(carBody->joints[4].position,carBody->joints[0].position)
  262. ) < 0)
  263. {
  264. carBody->joints[4].position =
  265. TPE_vec3Plus(TPE_vec3Times(carUp,300),carBody->joints[0].position);
  266. printf("car geometry flipped over, fixing...\n");
  267. }
  268. helper_set3dColor(200,10,10);
  269. for (int i = 0; i < tpe_world.bodyCount; ++i)
  270. TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
  271. if (helper_debugDrawOn)
  272. helper_debugDraw(1);
  273. helper_frameEnd();
  274. }
  275. helper_end();
  276. return 0;
  277. }