I am trying to check if my joystick controlled character is colliding with a walls pixel stored in an array. The character has a coordinate relating to the top left of its position and a 2D array representing the 'bitmap' that draws it. However I either get no collision or the character can't move because the code thinks its colliding.
I've done multiple variations of calling the collision test - trying it in both the collision function and when calling it in the move function. I recently switched to 2 for loops because 1 only compares the pixel at the same position in their respective arrays and not against all of them. I made some changes as recommended in the comments below and now I've narrowed my problem down to the collision function always returning 0. I found this by doing collision with the enemy character and when I had if temp == 0 the player died without moving but when it was set to if (temp == 1) the player doesn't die even if they touch the enemy character.
int wall_collide(int x1, int y1, int i_count, int j_count, int array1[i_count][2], int array2[j_count][2]) {
for (int i = 0; i < i_count; ++i)
{
for (int j = 0; j < j_count; ++j)
{
if ((x1 + array1[i][0] == array2[j][0]) && (y1 + array1[i][1] == array2[j][1]))
{
return 1;
}
}
}
return 0;
}
void p_move() {
int temp = 0;
if (LBpressed == 1 && !(c_x == 0))
{
temp = wall_collide(c_x, c_y, 22, 64, c_xy, pixelbank);
if (temp == 0) c_x -= 1;
}
if (RBpressed == 1 && !(c_x == (LCD_X - 5)))
{
temp = wall_collide(c_x, c_y, 22, 64, c_xy, pixelbank);
if (temp == 0) c_x -= 1;
}
if (UBpressed == 1 && !(c_y == 8))
{
temp = wall_collide(c_x, c_y, 22, 64, c_xy, pixelbank);
if (temp == 0) c_y -= 1;
}
if (DBpressed == 1 && !(c_y == (LCD_Y - 7)))
{
temp = wall_collide(c_x, c_y, 22, 64, c_xy, pixelbank);
if (temp == 0) c_y += 1;
}
}
int walls[4][4] = { {18,15, 13,25}, {25,35, 25,45}, {45,10, 60,10}, {58,25, 72,30} };
This is how I'm storing the pixels for the walls when I draw them
void pixelbank(int x1, int y1, int x2, int y2) {
if ( x1 == x2 ) {
// Draw vertical line
for ( int i = y1; (y2 > y1) ? i <= y2 : i >= y2; (y2 > y1) ? i++ : i-- ) {
inputoarray(x1, i);
}
}
else if ( y1 == y2 ) {
// Draw horizontal line
for ( int i = x1; (x2 > x1) ? i <= x2 : i >= x2; (x2 > x1) ? i++ : i-- ) {
inputoarray(i, y1);
}
}
else {
// Always draw from left to right, regardless of the order the endpoints are
// presented.
if ( x1 > x2 ) {
int t = x1;
x1 = x2;
x2 = t;
t = y1;
y1 = y2;
y2 = t;
}
// Get Bresenhaming...
float dx = x2 - x1;
float dy = y2 - y1;
float err = 0.0;
float derr = ABS(dy / dx);
for ( int x = x1, y = y1; (dx > 0) ? x <= x2 : x >= x2; (dx > 0) ? x++ : x-- ) {
inputoarray(x, y);
err += derr;
while ( err >= 0.5 && ((dy > 0) ? y <= y2 : y >= y2) ) {
inputoarray(x, y);
y += (dy > 0) - (dy < 0);
err -= 1.0;
}
}
}
}
pixelbank is where the pixels of the walls are stored (int pixelbank[64][2]) c_x and c_y are the starting point for drawing the character and c_xy contains the offsets e.g. c_xy[0][0] = 1 & c_xy[0][1] = 0 as there is a pixel drawn at c_x+1, c_y+0.
I expect my character to not be able to move through walls but he either is or he can't move at all but my code doesn't provide any errors in compiling.
Also if it helps I am using a 48x84 LCD screen for my code