main.c 2.5 KB

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