Game.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #include "Game.h"
  2. #include "Engine.h"
  3. #include "input/Controller.h"
  4. #include "utils/List.h"
  5. #include "utils/Utils.h"
  6. struct Line final {
  7. Vector2 a;
  8. Vector2 b;
  9. Color4 color;
  10. float friction;
  11. Line(const Vector2& a, const Vector2& b, Color4 color, float friction)
  12. : a(a), b(b), color(color), friction(friction) {
  13. }
  14. Line(const Vector2& a, const Vector2& b)
  15. : a(a), b(b), color(Color4(0xFF, 0xFF, 0xFF, 0xFF)), friction(0.0f) {
  16. }
  17. };
  18. static List<Line> lines;
  19. static constexpr float SPEED = 2.5f;
  20. static constexpr float GRAVITY = 0.5f;
  21. static constexpr float COLLISION_STEP = 0.005f;
  22. static Vector2 windowSize;
  23. static Vector2 lastPosition;
  24. static Vector2 position{150.0f, 50.0f};
  25. static Vector2 size{50.0f, 50.0f};
  26. static Vector2 velocity;
  27. static Vector2 acceleration;
  28. static Vector2 drag;
  29. static float mass = 5.0f;
  30. static bool onGround = false;
  31. static float jumpPower = 0.0f;
  32. static float steepness = 0.0f;
  33. static float friction = 0.0f;
  34. static bool physicsToggle = true;
  35. static Vector2 waterPosition;
  36. static Vector2 waterSize;
  37. static Vector2 projectileLastPosition;
  38. static Vector2 projectilePosition;
  39. static Vector2 projectileVelocity;
  40. static Vector2 projectileSize{20.0f, 20.0f};
  41. static void addForce(const Vector2& force) {
  42. acceleration += force / mass;
  43. }
  44. static void addMovement() {
  45. physicsToggle = physicsToggle ^ Controller::start.wasReleased();
  46. float movement = Controller::right.isDown() - Controller::left.isDown();
  47. float actualSpeed = SPEED * (1.0f - steepness * onGround);
  48. if(physicsToggle) {
  49. addForce(Vector2(actualSpeed, 0.0f) * movement);
  50. } else {
  51. // movement by velocity should have the same terminal velocity as
  52. // movement by force
  53. float terminalFactor = 1.0f / ((1.0 - drag[0]) * drag[0]);
  54. velocity[0] = (actualSpeed / mass) * terminalFactor * movement;
  55. }
  56. }
  57. static bool isIn(const Vector2& posA, const Vector2& sizeA, const Vector2& posB,
  58. const Vector2& sizeB) {
  59. Vector2 maxA = posA + sizeA;
  60. Vector2 maxB = posB + sizeB;
  61. return posA[0] < maxB[0] && maxA[0] > posB[0] && posA[1] < maxB[1] &&
  62. maxA[1] > posB[1];
  63. }
  64. float ERROR = 1.0f / 512.0f;
  65. static bool isBetween(float y, float y1, float y2) {
  66. return y >= std::min(y1, y2) && y <= std::max(y1, y2);
  67. }
  68. static bool compareFloats(float a, float b) {
  69. return std::abs(a - b) < ERROR;
  70. }
  71. static bool intersect(float x11, float y11, float x12, float y12, float x21,
  72. float y21, float x22, float y22) {
  73. if(compareFloats(x11, x12)) {
  74. if(compareFloats(x21, x22)) {
  75. return false;
  76. } else {
  77. if(!isBetween(x11, x21, x22)) {
  78. return false;
  79. }
  80. float k = (y21 - y22) / (x21 - x22);
  81. float d = y22 - k * x22;
  82. float y = d + x11 * k;
  83. return isBetween(y, y11, y12) && isBetween(y, y21, y22);
  84. }
  85. } else {
  86. if(compareFloats(x21, x22)) {
  87. if(!isBetween(x21, x11, x12)) {
  88. return false;
  89. }
  90. float k = (y11 - y12) / (x11 - x12);
  91. float d = y12 - k * x12;
  92. float y = d + x21 * k;
  93. return isBetween(y, y11, y12) && isBetween(y, y21, y22);
  94. } else {
  95. float k1 = (y11 - y12) / (x11 - x12);
  96. float k2 = (y21 - y22) / (x21 - x22);
  97. if(compareFloats(k1, k2)) {
  98. return false;
  99. }
  100. float d1 = y12 - k1 * x12;
  101. float d2 = y22 - k2 * x22;
  102. float x = (d1 - d2) / (k2 - k1);
  103. if(!isBetween(x, x11, x12) || !isBetween(x, x21, x22)) {
  104. return false;
  105. }
  106. float y = k1 * x + d1;
  107. return isBetween(y, y11, y12) && isBetween(y, y21, y22);
  108. }
  109. }
  110. }
  111. static bool areColliding(const Line& a, const Line& b) {
  112. return intersect(a.a[0], a.a[1], a.b[0], a.b[1], b.a[0], b.a[1], b.b[0],
  113. b.b[1]);
  114. }
  115. static bool doesPlayerCollide() {
  116. Vector2 posX = position + Vector2(size[0], 0.0f);
  117. Vector2 posY = position + Vector2(0.0f, size[1]);
  118. Vector2 posXY = position + size;
  119. for(const Line& line : lines) {
  120. if(areColliding(line, {position, posX}) ||
  121. areColliding(line, {position, posY}) ||
  122. areColliding(line, {posX, posXY}) ||
  123. areColliding(line, {posY, posXY})) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. static void slopeSteepnessAndFriction() {
  130. Line left{position - Vector2(0.0f, 10.0f * COLLISION_STEP),
  131. position + Vector2(0.0f, size[1])};
  132. Line right{position + Vector2(size[0], -10.0f * COLLISION_STEP),
  133. position + size};
  134. float min = 100.0f;
  135. float value = 100.0f;
  136. for(const Line& line : lines) {
  137. if(areColliding(line, left) || areColliding(line, right)) {
  138. float f = (line.a[1] - line.b[1]) / (line.a[0] - line.b[0]);
  139. if(std::abs(f) < min) {
  140. min = std::abs(f);
  141. value = f;
  142. friction = line.friction;
  143. }
  144. }
  145. }
  146. steepness = value;
  147. }
  148. static void applyDrag() {
  149. waterPosition = windowSize * Vector2(0.5f, 0.0f);
  150. waterSize = windowSize * Vector2(0.5f, 0.125f);
  151. drag = Vector2(0.1f, 0.1f);
  152. if(isIn(position, size, waterPosition, waterSize)) {
  153. drag *= 4.0f;
  154. }
  155. acceleration -= velocity * drag;
  156. }
  157. static void applyGravity() {
  158. acceleration -= Vector2(0.0f, GRAVITY);
  159. }
  160. static void handleJump() {
  161. bool justJumped = false;
  162. if(Controller::a.isDown() && onGround) {
  163. jumpPower = 15.0f;
  164. onGround = false;
  165. justJumped = true;
  166. }
  167. addForce(Vector2(0.0f, jumpPower));
  168. jumpPower *= 0.9f;
  169. jumpPower *= Controller::a.isDown() && (velocity[1] > 0.0f || justJumped);
  170. }
  171. static void relaunchProjectile() {
  172. if(projectilePosition[1] >= 0.0f &&
  173. !isIn(position, size, projectilePosition, projectileSize)) {
  174. return;
  175. }
  176. projectilePosition = windowSize * Vector2(0.0f, 0.5f);
  177. projectileLastPosition = projectilePosition;
  178. Vector2 start = projectilePosition;
  179. Vector2 end = position + size * 0.5f;
  180. constexpr float strengh = 25.0f;
  181. float best = 100000000.0f;
  182. float bestRad = 0.0f;
  183. for(float angle = -30.0f; angle < 90.0f; angle += 0.25f) {
  184. float rad = angle * 0.017453293f;
  185. Vector2 v(strengh * cosf(rad), strengh * sinf(rad));
  186. float t = (end[0] - start[0]) / v[0];
  187. float y = v[1] * t - GRAVITY * t * t * 0.5f + start[1];
  188. float diff = std::abs(y - end[1]);
  189. if(diff < best) {
  190. bestRad = rad;
  191. best = diff;
  192. }
  193. }
  194. projectileVelocity =
  195. Vector2(strengh * cosf(bestRad), strengh * sinf(bestRad));
  196. }
  197. static float sign(float f) {
  198. return (f > 0.0f) - (f < 0.0f);
  199. }
  200. static void applyFriction() {
  201. if(!onGround) {
  202. return;
  203. }
  204. float rad = atanf(steepness);
  205. float c = cosf(rad);
  206. float fnLength = mass * GRAVITY * c;
  207. Vector2 fn(fnLength * c, fnLength * sinf(rad)); // already swapt
  208. Vector2 fullFriction = fn * friction;
  209. fullFriction =
  210. Vector2(std::abs(fullFriction[0]), std::abs(fullFriction[1]));
  211. float signX = sign(acceleration[0]);
  212. float signY = sign(acceleration[1]);
  213. fullFriction *= -Vector2(signX, signY);
  214. addForce(fullFriction);
  215. if(sign(acceleration[0]) != signX) {
  216. acceleration[0] = 0.0f;
  217. }
  218. if(sign(acceleration[1]) != signY) {
  219. acceleration[1] = 0.0f;
  220. }
  221. }
  222. void Game::tick() {
  223. windowSize = Vector2(static_cast<float>(Engine::getSize().width),
  224. static_cast<float>(Engine::getSize().height));
  225. lastPosition = position;
  226. acceleration = Vector2();
  227. applyGravity();
  228. addMovement();
  229. handleJump();
  230. applyFriction();
  231. applyDrag();
  232. if(steepness != 100.0f) {
  233. if(steepness < -1.0f) {
  234. acceleration[0] += 0.1f;
  235. } else if(steepness > 1.0f) {
  236. acceleration[0] -= 0.1f;
  237. }
  238. }
  239. lines.clear();
  240. lines.add({{0.0f, 0.0f}, windowSize * Vector2(1.0f, 0.0f)});
  241. lines.add({{0.0f, 0.0f}, windowSize * Vector2(0.0f, 1.0f)});
  242. lines.add({windowSize, windowSize * Vector2(1.0f, 0.0f)});
  243. lines.add(
  244. {windowSize * Vector2(0.75f, 0.0f), windowSize * Vector2(1.0f, 0.5f)});
  245. lines.add({{0.0f, 30.0f}, {400.0f, 0.0f}});
  246. lines.add({{0.0f, 60.0f}, {300.0f, 0.0f}});
  247. lines.add({{0.0f, 90.0f}, {200.0f, 0.0f}});
  248. lines.add({{0.0f, 120.0f}, {100.0f, 0.0f}});
  249. lines.add({{100.0f, 180.0f}, {200.0f, 180.0f}});
  250. lines.add({{200.0f, 180.0f},
  251. {300.0f, 230.0f},
  252. Color4(0xF0, 0x00, 0x00, 0xFF),
  253. 0.2f});
  254. lines.add({{300.0f, 230.0f},
  255. {400.0f, 280.0f},
  256. Color4(0xC0, 0x00, 0x00, 0xFF),
  257. 0.4f});
  258. lines.add({{400.0f, 280.0f},
  259. {500.0f, 330.0f},
  260. Color4(0x90, 0x00, 0x00, 0xFF),
  261. 0.6f});
  262. lines.add({{500.0f, 330.0f},
  263. {600.0f, 380.0f},
  264. Color4(0x60, 0x00, 0x00, 0xFF),
  265. 0.8f});
  266. lines.add({{300.0f, 140.0f},
  267. {400.0f, 200.0f},
  268. Color4(0xF0, 0x00, 0x00, 0xFF),
  269. 0.2f});
  270. lines.add({{400.0f, 200.0f},
  271. {500.0f, 220.0f},
  272. Color4(0xC0, 0x00, 0x00, 0xFF),
  273. 0.4f});
  274. lines.add({{500.0f, 220.0f},
  275. {600.0f, 240.0f},
  276. Color4(0x90, 0x00, 0x00, 0xFF),
  277. 0.6f});
  278. lines.add({{600.0f, 240.0f},
  279. {700.0f, 260.0f},
  280. Color4(0x60, 0x00, 0x00, 0xFF),
  281. 0.8f});
  282. velocity += acceleration;
  283. onGround = false;
  284. Vector2 energy = velocity;
  285. while(energy[0] != 0.0f || energy[1] != 0.0f) {
  286. for(int i = 0; i < 2; i++) {
  287. if(energy[i] != 0.0f) {
  288. float old = position[i];
  289. if(energy[i] > COLLISION_STEP) {
  290. position[i] += COLLISION_STEP;
  291. energy[i] -= COLLISION_STEP;
  292. } else if(energy[i] < -COLLISION_STEP) {
  293. position[i] -= COLLISION_STEP;
  294. energy[i] += COLLISION_STEP;
  295. } else {
  296. position[i] += energy[i];
  297. energy[i] = 0.0f;
  298. }
  299. if(doesPlayerCollide()) {
  300. if(i == 0) {
  301. float oldY = position[1];
  302. position[1] += COLLISION_STEP;
  303. if(!doesPlayerCollide()) {
  304. continue;
  305. }
  306. position[1] = oldY;
  307. }
  308. energy[i] = 0.0f;
  309. velocity[i] = 0.0f;
  310. position[i] = old;
  311. }
  312. }
  313. }
  314. }
  315. float oldSteepness = steepness;
  316. slopeSteepnessAndFriction();
  317. onGround = std::abs(steepness) < 1.0f;
  318. if(onGround) {
  319. velocity[1] = 0.0f;
  320. }
  321. if(steepness == 100.0f) {
  322. steepness = oldSteepness;
  323. }
  324. projectileLastPosition = projectilePosition;
  325. projectileVelocity -= Vector2(0.0f, GRAVITY);
  326. projectilePosition += projectileVelocity;
  327. relaunchProjectile();
  328. }
  329. void Game::render(float lag, Renderer& r) {
  330. r.translateTo(0.0f, 0.0f)
  331. .scale(1.0f, -1.0f)
  332. .translateY(Engine::getSize().height)
  333. .update();
  334. r.drawRectangle(waterPosition, waterSize, Color4(0x00, 0x00, 0xFF, 0xFF));
  335. r.push();
  336. r.translateTo(0.0f, 0.0f)
  337. .translate(-size * 0.5f)
  338. .rotate(atanf(steepness) * 180.0f / M_PI)
  339. .translate(Utils::interpolate(lastPosition, position, lag))
  340. .translate(size * 0.5f)
  341. .scale(1.0f, -1.0f)
  342. .translateY(Engine::getSize().height)
  343. .update();
  344. r.drawRectangle(Vector2(), size, Color4(0xFF, 0x00, 0x00, 0xFF));
  345. r.pop();
  346. r.update();
  347. r.drawRectangle(
  348. Utils::interpolate(projectileLastPosition, projectilePosition, lag),
  349. projectileSize, Color4(0xFF, 0xFF, 0x00, 0xFF));
  350. for(const Line& line : lines) {
  351. Vector2 dir = line.b - line.a;
  352. Vector2 normal(dir[1], -dir[0]);
  353. normal.normalize();
  354. normal *= 2.0f;
  355. Vector2 ap = line.a + normal;
  356. Vector2 bp = line.b + normal;
  357. Vector2 an = line.a - normal;
  358. Vector2 bn = line.b - normal;
  359. r.drawTriangle(ap, bp, an, line.color);
  360. r.drawTriangle(bp, an, bn, line.color);
  361. }
  362. r.translateTo(0.0f, 0.0f).update();
  363. r.setStringSize(2);
  364. float y = 10.0f;
  365. y = r.drawString(10.0f, y,
  366. physicsToggle ? "Force Physics" : "Velocity Physics");
  367. StringBuffer<100> s("a = ");
  368. s.append(acceleration).append(" before collision");
  369. y = r.drawString(10.0f, y, s);
  370. s.clear().append("v = ").append(velocity).append(" after collision");
  371. y = r.drawString(10.0f, y, s);
  372. s.clear().append("Ground = ").append(onGround);
  373. y = r.drawString(10.0f, y, s);
  374. s.clear().append("Angle = ").append(atanf(steepness) * 57.295779513f);
  375. y = r.drawString(10.0f, y, s);
  376. s.clear().append("Friction = ").append(friction);
  377. y = r.drawString(10.0f, y, s);
  378. }
  379. bool Game::isRunning() {
  380. return !Controller::select.isDown();
  381. }