commit c1fc73a188a95355ee47a30feccbe5bcfd8c92c4
parent 5ae8724e047c415f75d1c615b3bbb3f27b121b47
Author: amin <dev@aminmesbah.com>
Date: Fri, 19 Apr 2019 23:17:57 +0000
Do hilarious inverted AABB collision detection
FossilOrigin-Name: 95afb325c226d1199f8fc758ee429b7b2d197423d8cd71d2380d06b3407fb740
Diffstat:
3 files changed, 35 insertions(+), 28 deletions(-)
diff --git a/src/game.c b/src/game.c
@@ -14,7 +14,11 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer)
struct GameState *game_state = game_memory->game_state;
// init player
- game_state->player.pos = (v2) { 1.0f, 1.0f };
+ game_state->player.pos = (v2) {1.0f, 1.0f};
+
+ // In Knytt, the player is 9 by 14 texels and a tile is 24 by 24 texels.
+ // These dimensions are relative to a square 'meter', one tile
+ game_state->player.dimensions = (v2) {0.375f, 0.583f};
// set up and load tiles
{
@@ -56,7 +60,6 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer)
game_state->tiles.vao = vao;
game_state->tiles.vbo = vbo;
game_state->tiles.ebo = vbo;
- game_state->tiles.count = 1;
}
{
@@ -165,13 +168,6 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
player->pos = glmth_v2_a(player->pos, glmth_v2f_m(player->velocity, dt));
glmth_clamp(&(player->pos.x), 0, ROOM_TILE_DIM_X);
glmth_clamp(&(player->pos.y), 0, ROOM_TILE_DIM_Y);
-
- printf("A: ");
- glmth_v2_print(player->acceleration);
- printf("V: ");
- glmth_v2_print(player->velocity);
- printf("P: ");
- glmth_v2_print(player->pos);
}
// render tiles
@@ -224,20 +220,25 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
color = (v3) {0.3f, 0.3f, 0.3f};
}
- v2 player_pos = game_state->player.pos;
- bool player_is_in_tile = (
- player_pos.x >= tile_pos.x
- && player_pos.x <= tile_pos.x + tile_size
- && player_pos.y >= tile_pos.y
- && player_pos.y <= tile_pos.y + tile_size
- );
- if (player_is_in_tile)
- {
- color = (v3) {0.4f, 0.8f, 0.4f};
- }
- if (player_is_in_tile && tile_id > 0)
{
- color = (v3) {0.8f, 0.4f, 0.4f};
+ struct Entity player = game_state->player;
+ rect player_aabb = {
+ .min = {player.pos.x - (0.5f * player.dimensions.width), player.pos.y - (0.5f * player.dimensions.height)},
+ .max = {player.pos.x + (0.5f * player.dimensions.width), player.pos.y + (0.5f * player.dimensions.height)},
+ };
+ rect tile_aabb = {
+ .min = tile_pos,
+ .max = {tile_pos.x + tile_size, tile_pos.y + tile_size},
+ };
+ bool player_is_in_tile = glmth_aabb_intersect(player_aabb, tile_aabb);
+ if (player_is_in_tile)
+ {
+ color = (v3) {0.4f, 0.8f, 0.4f};
+ }
+ if (player_is_in_tile && tile_id > 0)
+ {
+ color = (v3) {0.8f, 0.4f, 0.4f};
+ }
}
shader_setv3(&game_state->tiles.shader, "color", &color);
@@ -254,16 +255,13 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
// render player
{
shader_use(&game_state->player.shader);
- struct Entity player = game_state->player;
glBindVertexArray(game_state->tiles.vao);
+ struct Entity player = game_state->player;
- // In Knytt, the player is 9 by 14 texels and a tile is 24 by 24 texels
- // these dimensions are relative to a square 'meter', one tile
- v2 player_dim = { 0.375f, 0.583f };
m4 model = glmth_m4_init_id();
model = glmth_translate(model, (v3) {player.pos.x, player.pos.y, 0.0f});
- model = glmth_scale(model, (v3) {player_dim.x, player_dim.y, 1.0f});
+ model = glmth_scale(model, (v3) {player.dimensions.x, player.dimensions.y, 1.0f});
v3 color = (v3) { 1.0f, 0.0f, 1.0f };
diff --git a/src/game.h b/src/game.h
@@ -21,7 +21,6 @@ struct Tiles
GLuint vao;
GLuint vbo;
GLuint ebo;
- u32 count;
struct Shader shader;
};
@@ -30,6 +29,7 @@ struct Entity
v2 acceleration;
v2 velocity;
v2 pos;
+ v2 dimensions;
struct Shader shader;
};
diff --git a/src/glmth.h b/src/glmth.h
@@ -5,6 +5,15 @@
// TODO: make sure these functions are inlineable and that the compiler does
// indeed inline them.
+internal inline bool glmth_aabb_intersect(rect r1, rect r2)
+{
+ bool aabbs_do_indeed_intersect = (
+ (r1.min.x >= r2.max.x && r1.min.y >= r2.max.y)
+ || (r2.min.x >= r1.max.x && r2.min.y >= r1.max.y)
+ );
+ return aabbs_do_indeed_intersect;
+}
+
internal inline f32 glmth_wrap(f32 n, f32 min, f32 max)
{
if (n > max)