Browse Source

feat: steering behaviour for the ship

Douglas A 1 year ago
parent
commit
8290392ba3
3 changed files with 103 additions and 95 deletions
  1. 3 0
      .gitignore
  2. 7 0
      CMakeLists.txt
  3. 93 95
      main.c

+ 3 - 0
.gitignore

@@ -1,2 +1,5 @@
 .vscode/
 build/
+.cache/
+.kdev4/
+*.kdev4

+ 7 - 0
CMakeLists.txt

@@ -6,6 +6,13 @@ add_subdirectory(vendored/raylib)
 
 add_executable(test-raylib main.c)
 
+
 target_include_directories(test-raylib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendored/raylib/src)
+
+IF(WIN32)
 target_link_libraries(test-raylib PUBLIC raylib WinMM)
+ENDIF()
 
+IF(UNIX)
+target_link_libraries(test-raylib PUBLIC raylib)
+ENDIF()

+ 93 - 95
main.c

@@ -1,131 +1,129 @@
-#include <stdio.h>
-#include <math.h>
 #include "raylib.h"
 #include "raymath.h"
+#include <math.h>
+#include <stdio.h>
 
 #define DRAG -0.001f
 
-typedef struct ship_t
-{
-    Vector2 position;
-    Vector2 velocity;
-    Vector2 accel;
-    Vector2 size;
-    float angle;
+typedef struct ship_t {
+  Vector2 position;
+  Vector2 velocity;
+  Vector2 accel;
+  Vector2 size;
+  Vector2 direction;
+  float angle;
 } ship;
 
-typedef struct game_g
-{
-    ship s;
-    float dt;
+typedef struct game_g {
+  ship s;
+  float dt;
 } game;
 
 game g;
 
-void ship_draw(const ship *s)
-{
-    DrawRectangleV(s->position, s->size, BLACK);
-}
+void ship_draw(const ship *s) { DrawRectangleV(s->position, s->size, BLACK); }
 
-void ship_up(ship *s)
-{
+void ship_up(ship *s) {
 
-    Vector2 min = {.x = 0.0f, .y = 1.0f};
-    Vector2 max = {.x = 0.0f, .y = -1.0f};
+  Vector2 min = {.x = -1000.0f, .y = -1000.0f};
+  Vector2 max = {.x = 1000.0f, .y = 1000.0f};
 
-    float x = cosf(s->angle);
-    float y = sinf(s->angle);
-    Vector2 polar = CLITERAL(Vector2){.x = x, .y = y};
+  Vector2 diff = Vector2Subtract(s->direction, s->position);
 
-    s->accel.y -= 0.3f; // * pos.y;
-    s->accel = Vector2Clamp(s->accel, min, max);
-    
-}
+  Vector2 accellv = CLITERAL(Vector2){.x = diff.x, .y = diff.y};
+  Vector2Normalize(accellv);
+  s->accel = Vector2Add(accellv, s->accel);
 
-void ship_stop(ship *s)
-{
+  s->accel = Vector2Clamp(s->accel, min, max);
+}
 
-    Vector2 min = {.x = 1.0, .y = 1.0};
-    Vector2 max = {.x = 0.0, .y = 0.0};
-    s->accel.y -= 0.0001f;
-    s->accel = Vector2Clamp(s->accel, min, max);
+void ship_stop(ship *s) {
 
+  Vector2 min = {.x = 1.0, .y = 1.0};
+  Vector2 max = {.x = 0.0, .y = 0.0};
+  s->accel.y -= 0.0001f;
+  s->accel.x -= 0.0001f;
+  s->accel = Vector2Clamp(s->accel, min, max);
 }
 
-void ship_update(ship *s)
-{
-    Vector2 drag = {.x = DRAG * s->velocity.x, .y = DRAG * s->velocity.y};
+void ship_update(ship *s) {
+  Vector2 drag = {.x = DRAG * s->velocity.x, .y = DRAG * s->velocity.y};
 
-    s->velocity = Vector2Add(s->velocity, s->accel);
-    s->velocity = Vector2Add(s->velocity, drag);
+  float x = cosf(s->angle);
+  float y = sinf(s->angle);
 
-    s->position.x += s->velocity.x * g.dt;
-    s->position.y += s->velocity.y * g.dt;
+  Vector2 polar = CLITERAL(Vector2){.x = x, .y = y};
+  Vector2 pos = Vector2Add(s->position, polar);
 
-    float x = 100 * cosf(s->angle);
-    float y = 100 * sinf(s->angle);
-    Vector2 polar = CLITERAL(Vector2){.x = x, .y = y};
-    // polar = Vector2Normalize(polar);
-    Vector2 pos = Vector2Add(s->position, polar);
-    // pos = Vector2Normalize(pos);
-    // printf("polar (%f,%f)\n", pos.x, pos.y);
-    DrawLine(s->position.x, s->position.y, pos.x, pos.y, RED);
+  float x1 = 100 * cosf(s->angle);
+  float y1 = 100 * sinf(s->angle);
+
+  Vector2 polar1 = CLITERAL(Vector2){.x = x1, .y = y1};
+  Vector2 pos1 = Vector2Add(s->position, polar1);
+
+  s->direction.x = pos.x;
+  s->direction.y = pos.y;
+
+  s->velocity.x += s->accel.x * g.dt;
+  s->velocity.y += s->accel.y * g.dt;
+  s->velocity = Vector2Add(s->velocity, drag);
+
+  s->position.x += s->velocity.x * g.dt;
+  s->position.y += s->velocity.y * g.dt;
+
+  DrawLine(s->position.x, s->position.y, pos1.x, pos1.y, RED);
 }
 
-void process_inputs(game *g)
-{
-    if (IsKeyDown(KEY_W))
-    {
-        ship_up(&g->s);
-    }
-    else
-    {
-        ship_stop(&g->s);
-    }
-    if (IsKeyDown(KEY_D))
-    {
-        g->s.angle += 1.0f * g->dt;
-    }
-    if (IsKeyDown(KEY_A))
-    {
-        g->s.angle -= 1.0f * g->dt;
-    }
+void process_inputs(game *g) {
+  if (IsKeyDown(KEY_W)) {
+    ship_up(&g->s);
+  } else {
+    ship_stop(&g->s);
+  }
+  if (IsKeyDown(KEY_D)) {
+    g->s.angle += 1.0f * g->dt;
+  }
+  if (IsKeyDown(KEY_A)) {
+    g->s.angle -= 1.0f * g->dt;
+  }
 }
 
-void text(const game *g)
-{
-    char ind[256] = {0};
-    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);
-    DrawText(ind, 10, 10, 20, BLACK);
+void text(const game *g) {
+  char ind[256] = {0};
+  sprintf(ind,
+          "ship position %fx%f\nship speed %fx%f\nship accel %fx%f\nangle: "
+          "%f\ndirection %fx%f",
+          g->s.position.x, g->s.position.y, g->s.velocity.x, g->s.velocity.y,
+          g->s.accel.x, g->s.accel.y, g->s.angle, g->s.direction.x,
+          g->s.direction.y);
+  DrawText(ind, 10, 10, 20, BLACK);
 }
 
-int main(void)
-{
+int main(void) {
 
-    InitWindow(1820, 900, "Hello World");
+  InitWindow(1820, 900, "Hello World");
 
-    ship s1 = {0};
-    s1.position.x = 910.0f;
-    s1.position.y = 450.0f;
+  ship s1 = {0};
+  s1.position.x = 910.0f;
+  s1.position.y = 450.0f;
 
-    s1.size.x = 10.0f;
-    s1.size.y = 10.0f;
-    s1.angle = 3 * PI / 2;
+  s1.size.x = 10.0f;
+  s1.size.y = 10.0f;
+  s1.angle = 3 * PI / 2;
 
-    g.s = s1;
+  g.s = s1;
 
-    while (!WindowShouldClose())
-    {
-        BeginDrawing();
-        ClearBackground(WHITE);
-        process_inputs(&g);
-        ship_draw(&g.s);
-        ship_update(&g.s);
-        text(&g);
-        EndDrawing();
+  while (!WindowShouldClose()) {
+    BeginDrawing();
+    ClearBackground(WHITE);
+    process_inputs(&g);
+    ship_draw(&g.s);
+    ship_update(&g.s);
+    text(&g);
+    EndDrawing();
 
-        g.dt = GetFrameTime();
-    }
+    g.dt = GetFrameTime();
+  }
 
-    return (0);
-}
+  return (0);
+}