main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "raylib.h"
  2. #include "raymath.h"
  3. #include <math.h>
  4. #include <stdio.h>
  5. #define DRAG -0.001f
  6. typedef struct ship_t {
  7. Vector2 position;
  8. Vector2 velocity;
  9. Vector2 accel;
  10. Vector2 size;
  11. Vector2 direction;
  12. float angle;
  13. } ship;
  14. typedef struct game_g {
  15. ship s;
  16. float dt;
  17. } game;
  18. game g;
  19. void ship_draw(const ship *s) { DrawRectangleV(s->position, s->size, BLACK); }
  20. void ship_up(ship *s) {
  21. Vector2 min = {.x = -1000.0f, .y = -1000.0f};
  22. Vector2 max = {.x = 1000.0f, .y = 1000.0f};
  23. Vector2 diff = Vector2Subtract(s->direction, s->position);
  24. Vector2 accellv = CLITERAL(Vector2){.x = diff.x, .y = diff.y};
  25. Vector2Normalize(accellv);
  26. s->accel = Vector2Add(accellv, s->accel);
  27. s->accel = Vector2Clamp(s->accel, min, max);
  28. }
  29. void ship_stop(ship *s) {
  30. Vector2 min = {.x = 1.0, .y = 1.0};
  31. Vector2 max = {.x = 0.0, .y = 0.0};
  32. s->accel.y -= 0.0001f;
  33. s->accel.x -= 0.0001f;
  34. s->accel = Vector2Clamp(s->accel, min, max);
  35. }
  36. void ship_update(ship *s) {
  37. Vector2 drag = {.x = DRAG * s->velocity.x, .y = DRAG * s->velocity.y};
  38. float x = cosf(s->angle);
  39. float y = sinf(s->angle);
  40. Vector2 polar = CLITERAL(Vector2){.x = x, .y = y};
  41. Vector2 pos = Vector2Add(s->position, polar);
  42. float x1 = 100 * cosf(s->angle);
  43. float y1 = 100 * sinf(s->angle);
  44. Vector2 polar1 = CLITERAL(Vector2){.x = x1, .y = y1};
  45. Vector2 pos1 = Vector2Add(s->position, polar1);
  46. s->direction.x = pos.x;
  47. s->direction.y = pos.y;
  48. s->velocity.x += s->accel.x * g.dt;
  49. s->velocity.y += s->accel.y * g.dt;
  50. s->velocity = Vector2Add(s->velocity, drag);
  51. s->position.x += s->velocity.x * g.dt;
  52. s->position.y += s->velocity.y * g.dt;
  53. DrawLine(s->position.x, s->position.y, pos1.x, pos1.y, RED);
  54. }
  55. void process_inputs(game *g) {
  56. if (IsKeyDown(KEY_W)) {
  57. ship_up(&g->s);
  58. } else {
  59. ship_stop(&g->s);
  60. }
  61. if (IsKeyDown(KEY_D)) {
  62. g->s.angle += 1.0f * g->dt;
  63. }
  64. if (IsKeyDown(KEY_A)) {
  65. g->s.angle -= 1.0f * g->dt;
  66. }
  67. }
  68. void text(const game *g) {
  69. char ind[256] = {0};
  70. sprintf(ind,
  71. "ship position %fx%f\nship speed %fx%f\nship accel %fx%f\nangle: "
  72. "%f\ndirection %fx%f",
  73. g->s.position.x, g->s.position.y, g->s.velocity.x, g->s.velocity.y,
  74. g->s.accel.x, g->s.accel.y, g->s.angle, g->s.direction.x,
  75. g->s.direction.y);
  76. DrawText(ind, 10, 10, 20, BLACK);
  77. }
  78. int main(void) {
  79. InitWindow(1820, 900, "Hello World");
  80. ship s1 = {0};
  81. s1.position.x = 910.0f;
  82. s1.position.y = 450.0f;
  83. s1.size.x = 10.0f;
  84. s1.size.y = 10.0f;
  85. s1.angle = 3 * PI / 2;
  86. g.s = s1;
  87. while (!WindowShouldClose()) {
  88. BeginDrawing();
  89. ClearBackground(WHITE);
  90. process_inputs(&g);
  91. ship_draw(&g.s);
  92. ship_update(&g.s);
  93. text(&g);
  94. EndDrawing();
  95. g.dt = GetFrameTime();
  96. }
  97. return (0);
  98. }