• Create Account

### #ActualMoonkis

Posted 12 April 2013 - 01:21 PM

You should be able to treat the Rectangle object like a data member(C++)/instance variable(Java) of the Wall class.

In Java, it would look like this:

public class Wall{

private Rectangle rectangle;

}

In your case, it would look similar to this:

public class Tiles{

private Rectangle rectangle;

}

The idea I have is that you can test for collision when the character is close to object it is not suppose to be able to over-lap. That would make the code less expensive.

But I don't want to have a tile class, i.e I don't want to have 1'000'000 bounding boxes....

EDIT:

I came up with this:

void Player::collision(Tiles& tiles)
{
int startY = ( m_y - 32 ) / 32;
int startX = ( m_x - 32 ) / 32;
int endY = ( m_y + 64 ) / 32;
int endX = ( m_x + 64) / 32;

for( int sy = startY; sy < endY; sy++ )
{
for( int sx = startX; sx < endX; sx++ )
{
if( tiles[sx][sy] != 1 && tiles[sx][sy] != 5 )
{
sf::IntRect wall;
wall.top = sy * 32;
wall.left = sx * 32;
wall.width = 32;
wall.height = 32;
m_boundingbox.top = m_nextY+20;
m_boundingbox.left = m_nextX+8;
if(m_boundingbox.intersects(wall))
{
return;
}

}
}
}
m_x = m_nextX;
m_y = m_nextY;
}


It works BUT... I'm not sure how to make the character "slide" against a wall if the collision is only because of one axis. Example:
If you are moving both up and right and there is an collision because you are moving upwards you don't want to move up but towards the left instead, making the character slide on the x-axis ( to the right ).

### #1Moonkis

Posted 12 April 2013 - 01:13 PM

You should be able to treat the Rectangle object like a data member(C++)/instance variable(Java) of the Wall class.

In Java, it would look like this:

public class Wall{

private Rectangle rectangle;

}

In your case, it would look similar to this:

public class Tiles{

private Rectangle rectangle;

}

The idea I have is that you can test for collision when the character is close to object it is not suppose to be able to over-lap. That would make the code less expensive.

But I don't want to have a tile class, i.e I don't want to have 1'000'000 bounding boxes....

PARTNERS