]> git.lizzy.rs Git - nothing.git/commitdiff
Introduce squishing force into platform collision detection
authorrexim <reximkut@gmail.com>
Thu, 21 Dec 2017 21:05:13 +0000 (04:05 +0700)
committerrexim <reximkut@gmail.com>
Thu, 21 Dec 2017 21:05:13 +0000 (04:05 +0700)
levels/platforms.svg
levels/platforms.txt
src/game.c
src/platforms.c
src/platforms.h
src/player.c

index fc7b44eb2d88fb7ef35c39f16546214a66f68a68..316fbb691bf2a042f04ca2cb097b63474bb7f29a 100644 (file)
@@ -22,8 +22,8 @@
      guidetolerance="10"
      inkscape:pageopacity="0"
      inkscape:pageshadow="2"
-     inkscape:window-width="1916"
-     inkscape:window-height="1041"
+     inkscape:window-width="956"
+     inkscape:window-height="1023"
      id="namedview13"
      showgrid="false"
      inkscape:snap-bbox="true"
      inkscape:snap-bbox-midpoints="true"
      inkscape:snap-bbox-edge-midpoints="true"
      inkscape:zoom="2"
-     inkscape:cx="282.86631"
-     inkscape:cy="332.49893"
-     inkscape:window-x="0"
-     inkscape:window-y="18"
+     inkscape:cx="439.36631"
+     inkscape:cy="336.49893"
+     inkscape:window-x="960"
+     inkscape:window-y="36"
      inkscape:window-maximized="0"
      inkscape:current-layer="svg16" />
   <metadata
      width="50"
      y="200"
      x="350" />
-  <rect
-     id="rect4-7"
-     style="fill:#0000ff;stroke-width:1"
-     height="25"
-     width="50"
-     y="200"
-     x="400" />
 </svg>
index 144ee9e45ec7298ff1d8034db90a1f164afbc0db..ea30c54ee893c1425680dd903a680f0ce27775ea 100644 (file)
@@ -1,4 +1,4 @@
-12
+11
 0.000000 250.000000 50.000000 50.000000
 300 200 50 25
 150.000000 250.000000 650.000000 50.000000
@@ -10,4 +10,3 @@
 100 250 50 50
 300 100 50 50
 350 200 50 25
-400 200 50 25
index 718cc7e7a2c40ab5e2ccc2681bb3fc2f9c14434c..0db44c624011eb8ba3f84dc82ea1aa765a9e6c96 100644 (file)
@@ -221,7 +221,6 @@ int game_input(game_t *game,
 {
     assert(game);
     assert(keyboard_state);
-    assert(the_stick_of_joy);
 
     if (game->state == GAME_STATE_QUIT || game->state == GAME_STATE_PAUSE) {
         return 0;
index 0aa47a5a8d60c8b11c707837a6777e67714360f3..273a2e91711829e2871c16ec4400661bf09c7a45 100644 (file)
@@ -16,13 +16,6 @@ struct platforms_t {
     size_t rects_size;
 };
 
-static const vec_t opposing_rect_side_forces[RECT_SIDE_N] = {
-    { .x = 1.0f,  .y =  0.0f  },  /* RECT_SIDE_LEFT = 0, */
-    { .x = -1.0f, .y =  0.0f  },  /* RECT_SIDE_RIGHT, */
-    { .x = 0.0f,  .y =  1.0f, },  /* RECT_SIDE_TOP, */
-    { .x = 0.0f,  .y = -1.0f,  }    /* RECT_SIDE_BOTTOM, */
-};
-
 platforms_t *create_platforms(const rect_t *rects, size_t rects_size)
 {
     assert(rects);
@@ -156,30 +149,15 @@ int render_platforms(const platforms_t *platforms,
     return 0;
 }
 
-vec_t platforms_rect_object_collide(const platforms_t *platforms,
-                                    rect_t object)
+void platforms_rect_object_collide(const platforms_t *platforms,
+                                   rect_t object,
+                                   int sides[RECT_SIDE_N])
 {
     assert(platforms);
 
-    int sides[4];
-    memset(sides, 0, sizeof(sides));
+    memset(sides, 0, sizeof(int) * RECT_SIDE_N);
 
     for (size_t i = 0; i < platforms->rects_size; ++i) {
         rect_object_impact(&object, &platforms->rects[i], sides);
     }
-
-    vec_t opposing_force = {
-        .x = 0.0f,
-        .y = 0.0f
-    };
-
-    for (rect_side_t side = 0; side < RECT_SIDE_N; ++side) {
-        if (sides[side]) {
-            vec_add(
-                &opposing_force,
-                opposing_rect_side_forces[side]);
-        }
-    }
-
-    return opposing_force;
 }
index d25eb75811ef33039cea3bc7396fa2aaff3986c7..89de725c7600ff2b490234d75e46f0e04bc550dd 100644 (file)
@@ -18,7 +18,8 @@ int render_platforms(const platforms_t *platforms,
                      SDL_Renderer *renderer,
                      const camera_t *camera);
 
-vec_t platforms_rect_object_collide(const platforms_t *platforms,
-                                    rect_t object);
+void platforms_rect_object_collide(const platforms_t *platforms,
+                                   rect_t object,
+                                   int sides[RECT_SIDE_N]);
 
 #endif  // PLATFORMS_H_
index 8a88b351a154d5812be055678776fd7cb8db44b0..7e1a1a7717f4b1e6d4d4d39f733f4d0aa86b03b8 100644 (file)
@@ -22,6 +22,41 @@ struct player_t {
     float width;
 };
 
+static const vec_t opposing_rect_side_forces[RECT_SIDE_N] = {
+    { .x = 1.0f,  .y =  0.0f  },  /* RECT_SIDE_LEFT = 0, */
+    { .x = -1.0f, .y =  0.0f  },  /* RECT_SIDE_RIGHT, */
+    { .x = 0.0f,  .y =  1.0f, },  /* RECT_SIDE_TOP, */
+    { .x = 0.0f,  .y = -1.0f, }   /* RECT_SIDE_BOTTOM, */
+};
+
+static vec_t opposing_force_by_sides(int sides[RECT_SIDE_N])
+{
+    vec_t opposing_force = {
+        .x = 0.0f,
+        .y = 0.0f
+    };
+
+    for (rect_side_t side = 0; side < RECT_SIDE_N; ++side) {
+        if (sides[side]) {
+            vec_add(
+                &opposing_force,
+                opposing_rect_side_forces[side]);
+        }
+    }
+
+    return opposing_force;
+}
+
+static int squishing_horizontal_force(int sides[RECT_SIDE_N])
+{
+    return sides[RECT_SIDE_LEFT] && sides[RECT_SIDE_RIGHT];
+}
+
+static int squishing_vertical_force(int sides[RECT_SIDE_N])
+{
+    return sides[RECT_SIDE_TOP] && sides[RECT_SIDE_BOTTOM];
+}
+
 player_t *create_player(float x, float y)
 {
     player_t *player = malloc(sizeof(player_t));
@@ -100,15 +135,20 @@ void update_player(player_t *player,
     player->height = fminf(player->height + PLAYER_INFLATION * d, PLAYER_HEIGHT);
     player->width = (PLAYER_WIDTH * PLAYER_HEIGHT) / player->height;
 
-    vec_t opposing_force = platforms_rect_object_collide(
-        platforms,
-        player_hitbox(player));
+
+    int sides[RECT_SIDE_N] = { 0, 0, 0, 0 };
+
+    platforms_rect_object_collide(platforms, player_hitbox(player), sides);
+    vec_t opposing_force = opposing_force_by_sides(sides);
 
     if (opposing_force.y < 0.0f && (player->velocity.y + player->movement.y) > 800.0f) {
         player->height = PLAYER_HEIGHT / 2;
     }
 
-    for (int i = 0; i < 1000 && vec_length(opposing_force) > 1e-6; ++i) {
+    for (int i = 0; i < 1000
+                    && (vec_length(opposing_force) > 1e-6
+                        || squishing_vertical_force(sides)
+                        || squishing_horizontal_force(sides)); ++i) {
         player->position = vec_sum(
             player->position,
             vec_scala_mult(
@@ -125,9 +165,21 @@ void update_player(player_t *player,
             player->movement.y = 0.0f;
         }
 
-        opposing_force = platforms_rect_object_collide(
+        if (squishing_vertical_force(sides)) {
+            player->height -= 1e-2f;
+            /* player->width = (PLAYER_WIDTH * PLAYER_HEIGHT) / player->height; */
+        }
+
+        if (squishing_horizontal_force(sides)) {
+           player->width -= 1e-2f;
+           /* player->height = (PLAYER_WIDTH * PLAYER_HEIGHT) / player->width; */
+        }
+
+        platforms_rect_object_collide(
             platforms,
-            player_hitbox(player));
+            player_hitbox(player),
+            sides);
+        opposing_force = opposing_force_by_sides(sides);
     }
 }