I'm coding a 3D game for a project using OpenGL and I'm trying to do tile collision on a surface.
The surface plane is split into a grid of 64x64 pixels and I can simply check if the (x,y) tile is empty or not.
Besides having a grid for collision, there's still free movement inside a tile.
For each entity, in the end of the update function I simply increase the position by the velocity:
pos.x += v.x;
pos.y += v.y;
I already have a collision grid created but my collide function is not great, i'm not sure how to handle it. I can check if the collision occurs but the way I handle is terrible.
int leftTile = repelBox.x / grid->cellSize;
int topTile = repelBox.y / grid->cellSize;
int rightTile = (repelBox.x + repelBox.w) / grid->cellSize;
int bottomTile = (repelBox.y + repelBox.h) / grid->cellSize;
for (int y = topTile; y <= bottomTile; ++y)
{
for (int x = leftTile; x <= rightTile; ++x)
{
if (grid->getCell(x, y) == BLOCKED){
Rect colBox = grid->getCellRectXY(x, y);
Rect xAxis = Rect(pos.x - 20 / 2.0f, pos.y - 20 / 4.0f, 20, 10);
Rect yAxis = Rect(pos.x - 20 / 4.0f, pos.y - 20 / 2.0f, 10, 20);
if (colBox.Intersects(xAxis))
v.x *= -1;
if (colBox.Intersects(yAxis))
v.y *= -1;
}
}
}
If instead of reversing the direction I set it to false then when the entity tries to get away from the wall it's still intersecting the tile and gets stuck on that position.
EDIT: I've worked with Flashpunk and it has a great function for movement and collision called moveBy. Are there any simplified implementations out there so I can check them out?