I'v been working on this game for "OneGameAMonth". So far it's been going pretty good, with only minor hiccups until just recently. I'm trying to do the collision and I just can't figure out what to do!
My world is built on a grid ( 2D array ) and uses 32x32 tiles, my Player and Monsters however are free moving and not bound by the grid, this has caused some problems when trying to do collision.
Here is an image:
Frankly I don't know how to do it, I tried converting my character coordinates to tile coordinates which works kinda... until you try to move through the small passages or "doorways", then it goes wild, suddenly you can walk through walls and what not.
The code looks something like this:
void Player::update(float dt, const Tiles& tiles)
{
float x = m_x;
float y = m_y;
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) )
{
y -= m_speed;
}
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) )
{
y += m_speed;
}
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) )
{
x -= m_speed;
}
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) )
{
x += m_speed;
}
int topY = y / 32;
int leftX = x / 32;
int bottomY = ( 32 + y )/32;
int rightX = ( 32 + x )/32;
if( tiles[leftX][topY] == 1 && tiles[rightX][topY] == 1)
{
m_x = x;
}
if( tiles[leftX][bottomY] == 1 && tiles[rightX][bottomY] == 1)
{
m_y = y;
}
m_sprite.setPosition(m_x, m_y);
}
I'm out of ideas and don't know what to do!
EDIT:
I'm using C++ and SFML