1
SFML projects / Re: 'Tiled' Tile-map Loader
« on: April 05, 2015, 07:18:39 am »
Hi guys! I've been using sfml for a while and just discovered the tmx loader. I'm using it in a college project but i'm having a particular issue. When using a layer to perform collisions my player collides well for a while but then pass througth the solid objects. This is my collision code:
for (auto layer = layers.begin(); layer != layers.end(); ++layer)
{
if (layer->name == "Coll")
{
for (auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
{
if (object->Visible())
{
if (object->GetAABB().intersects(getGlobalBounds(), area))
{
if (area.width > area.height)
{
if (area.contains({ area.left, getPosition().y }))
{
// Up side crash
setPosition({ getPosition().x, getPosition().y + area.height });
std::cout << "Up side crash" << std::endl;
}
else
{
// Down side crash
onGround = true;
inAir = 0.f;
setPosition({ getPosition().x, getPosition().y - area.height});
std::cout << "Down side crash" << std::endl;
}
}
else if (area.width < area.height)
{
if (area.contains({ getPosition().x + getGlobalBounds().width - 1.f, area.top + 1.f }))
{
//Right side crash
setPosition({ getPosition().x - area.width, getPosition().y });
std::cout << "Right side crash" << std::endl;
}
else
{
//Left side crash
setPosition({ getPosition().x + area.width, getPosition().y });
std::cout << "Left side crash" << std::endl;
}
}
}
}
}
}
The code works ok if i just collide againts a simple sprite but when i use it with the .tmx map it fails. Any help?
for (auto layer = layers.begin(); layer != layers.end(); ++layer)
{
if (layer->name == "Coll")
{
for (auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
{
if (object->Visible())
{
if (object->GetAABB().intersects(getGlobalBounds(), area))
{
if (area.width > area.height)
{
if (area.contains({ area.left, getPosition().y }))
{
// Up side crash
setPosition({ getPosition().x, getPosition().y + area.height });
std::cout << "Up side crash" << std::endl;
}
else
{
// Down side crash
onGround = true;
inAir = 0.f;
setPosition({ getPosition().x, getPosition().y - area.height});
std::cout << "Down side crash" << std::endl;
}
}
else if (area.width < area.height)
{
if (area.contains({ getPosition().x + getGlobalBounds().width - 1.f, area.top + 1.f }))
{
//Right side crash
setPosition({ getPosition().x - area.width, getPosition().y });
std::cout << "Right side crash" << std::endl;
}
else
{
//Left side crash
setPosition({ getPosition().x + area.width, getPosition().y });
std::cout << "Left side crash" << std::endl;
}
}
}
}
}
}
The code works ok if i just collide againts a simple sprite but when i use it with the .tmx map it fails. Any help?