Add basic AABB collision detection

This commit is contained in:
Doyle Thai 2016-06-25 19:12:25 +10:00
parent 7acf36cd59
commit b7063963b8
3 changed files with 55 additions and 8 deletions

View File

@ -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();
}

View File

@ -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)

View File

@ -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