so I’m making a really simple program in which I want one sprite to collide with another.
I made a ‘level’ by making a grid with images, using a two-dimensional array.
How would I implement simple collision? I would appreciate coding examples, so I can actually see
what’s going on. Thanks. Here’s the code:
int pacmanPosX = 32;
int pacmanPosY = 32;
Sprite pacman (new Surface( "assets/tiles/pacman.png"), 1);
Surface* tileSet[2];
int landTile[14][16] = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1,},
{1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1,},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1,},
{1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,},
{1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,},
{1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1,},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}};
void Game::Init()
{
// put your initialization code here; will be executed once
tileSet[0] = new Surface( "assets/tiles/void.bmp" ); // Sets up data for tileSet
tileSet[1] = new Surface( "assets/tiles/wall.bmp" );
}
void Game::Tick( float a_DT )
{
m_Screen->Clear( 0 );
for (int indexX = 0; indexX <= 14; indexX++)
{
for (int indexY = 0; indexY <= 16; indexY++)
{
int tile = landTile[indexY][indexX];
tileSet[tile]->CopyTo( m_Screen, indexX * 32, indexY * 32 );
}
}
pacman.Draw(pacmanPosX, pacmanPosY, m_Screen);
Sleep( 10 );
int colPosX = pacmanPosX / 32;
int colPosY = (pacmanPosY-1) / 32;
bool collision = (bool)landTile[colPosX][colPosY];
int direction;
if(collision = false)
{
if (GetAsyncKeyState( VK_UP )) pacmanPosY -=2;
if (GetAsyncKeyState( VK_DOWN )) pacmanPosY += 2;
if (GetAsyncKeyState( VK_RIGHT )) pacmanPosX += 2;
if (GetAsyncKeyState( VK_LEFT )) pacmanPosX -= 2;
}
}
That doesn’t work, the character can’t move at all now. For your information, every sprite is
32×32, including the character.
if you calculate the position of the pacman to correspond to the landTitle array by dividing the pacmanPosX and pacmanPosY with the size of each square (32) and then cast it to an int. then you could simply check if the position in front of you is set to 1 or 0 in the landTile and in that way you can get a very simple and fast collision.
Tell me if you get into any hindrance
EDIT
What I mean is that you an array that explains where all the walls should be in your map. Because of that you don’t have to do any sprite collision, instead you can do a simple collision check based on where you are supposed to be in the array. Lets consider the case where you are supposed to move up on the map:
if collision is true then you have a collision one pixel upwards from your position, thats what the -1 in the calculation of colPosY is for.
Edit 2
If i should edit your code then I would do it more like this