From b7063963b85598f1dc876fb328c1948fa90f6cb9 Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Sat, 25 Jun 2016 19:12:25 +1000 Subject: [PATCH] Add basic AABB collision detection --- src/Renderer.c | 6 ++--- src/WorldTraveller.c | 44 ++++++++++++++++++++++++++++++++---- src/include/Dengine/Entity.h | 13 +++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/Renderer.c b/src/Renderer.c index b43baa4..a5fbf24 100644 --- a/src/Renderer.c +++ b/src/Renderer.c @@ -21,16 +21,14 @@ void renderer_entity(Renderer *renderer, Entity *entity, f32 rotate, v3 color) // this->shader->uniformSetVec3f("spriteColor", color); glActiveTexture(GL_TEXTURE0); - glCheckError(); glBindTexture(GL_TEXTURE_2D, entity->tex->id); - glCheckError(); shader_uniformSet1i(renderer->shader, "tex", 0); glCheckError(); glBindVertexArray(renderer->vao); - glCheckError(); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - glCheckError(); glBindVertexArray(0); + + glBindTexture(GL_TEXTURE_2D, 0); glCheckError(); } diff --git a/src/WorldTraveller.c b/src/WorldTraveller.c index 1596afe..405517f 100644 --- a/src/WorldTraveller.c +++ b/src/WorldTraveller.c @@ -44,6 +44,7 @@ void worldTraveller_gameInit(GameState *state) V2(58.0f, 98.0f), direction_east, asset_getTexture(texlist_hero), + TRUE, 0, 0, 0}; @@ -89,6 +90,7 @@ void worldTraveller_gameInit(GameState *state) hero->size, direction_null, hero->tex, + TRUE, 0, 0, 0}; @@ -193,7 +195,8 @@ INTERNAL void parseInput(GameState *state, const f32 dt) currAnim->currRectIndex = 0; hero->currAnimIndex = 0; } - } else if (hero->currAnimIndex == 0) + } + else if (hero->currAnimIndex == 0) { SpriteAnim *currAnim = &hero->anim[hero->currAnimIndex]; currAnim->currDuration = currAnim->duration; @@ -221,9 +224,42 @@ INTERNAL void parseInput(GameState *state, const f32 dt) v2 dPos = v2_scale(hero->dPos, dt); v2 newHeroP = v2_add(v2_add(ddPosNew, dPos), hero->pos); - // f'(t) = curr velocity = a*t + v, where v is old velocity - hero->dPos = v2_add(hero->dPos, v2_scale(ddPos, dt)); - hero->pos = newHeroP; + b32 heroCollided = FALSE; + if (hero->collides == TRUE) + { + for (i32 i = 0; i < ARRAY_COUNT(state->entityList); i++) + { + if (i == state->heroIndex) continue; + Entity entity = state->entityList[i]; + if (entity.collides) + { + v4 heroRect = + V4(newHeroP.x, newHeroP.y, (newHeroP.x + hero->size.x), + (newHeroP.y + hero->size.y)); + v4 entityRect = getEntityScreenRect(entity); + + if (((heroRect.z >= entityRect.x && heroRect.z <= entityRect.z) || + (heroRect.x >= entityRect.x && heroRect.x <= entityRect.z)) && + ((heroRect.w <= entityRect.y && heroRect.w >= entityRect.w) || + (heroRect.y <= entityRect.y && heroRect.y >= entityRect.w))) + { + heroCollided = TRUE; + break; + } + } + } + } + + if (heroCollided) + { + hero->dPos = V2(0.0f, 0.0f); + } + else + { + // f'(t) = curr velocity = a*t + v, where v is old velocity + hero->dPos = v2_add(hero->dPos, v2_scale(ddPos, dt)); + hero->pos = newHeroP; + } } void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt) diff --git a/src/include/Dengine/Entity.h b/src/include/Dengine/Entity.h index 75aa878..0923d56 100644 --- a/src/include/Dengine/Entity.h +++ b/src/include/Dengine/Entity.h @@ -31,11 +31,24 @@ typedef struct Entity v2 size; enum Direction direction; Texture *tex; + b32 collides; // TODO(doyle): String based access SpriteAnim anim[16]; i32 freeAnimIndex; i32 currAnimIndex; + } Entity; +INTERNAL inline v4 getEntityScreenRect(Entity entity) +{ + v2 entityUpperLeftBound = v2_add(entity.pos, V2(0.0f, entity.size.y)); + v2 entityLowerRightBound = v2_add(entity.pos, V2(entity.size.x, 0.0f)); + + v4 result = V4(entityUpperLeftBound.x, entityUpperLeftBound.y, + entityLowerRightBound.x, entityLowerRightBound.y); + + return result; +} + #endif