3

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

Leptoflux
  • 43
  • 1
  • 8
  • 1
    `for (int j=0; j – virolino Oct 21 '19 at 06:13
  • 1
    When asking a question about a run-time problem, as this question is doing, post a [mcve] so we can reproduce the problem and help you debug it. As it is, the posted code does not even begin to compile. – user3629249 Oct 21 '19 at 06:30
  • @virolino has something. You also don't use `j` in those for loops – Swedgin Oct 21 '19 at 06:37
  • Thanks it has fixed a few problems I had that was breaking the code by fixing up the j and i but its still not colliding with the walls @virolino. I can't provide enough code to test it as it needs both a custom library and this controller setup but I will add some more info – Leptoflux Oct 21 '19 at 06:54
  • If you are enforcing that `x2 > x1` by swapping, you don't need all the tests for `(dx > 0)`. Your line from (58, 25) to (72, 30) doesn't Bresenham correctly. – M Oehm Oct 21 '19 at 08:08
  • 1
    What is the purpose of function `wall_collide()`? Is it a command to perform a collision (under which circumstances?), or it is a test to detect a possible collision? – virolino Oct 21 '19 at 08:25
  • its a test to detect possible collision between the character and the wall coordinates. I have managed to get it working after fixing some of the things pointed out here and a whole lot of attempts at fixing it myself. – Leptoflux Oct 21 '19 at 08:53
  • 1
    Aha. It is better practice then to name the function something like `isWallCollision()` or `hasUserCollideIntoWall()` or similar, just to make the purpose of the function obvious. – virolino Oct 21 '19 at 09:27
  • @virolino yeah sorry still getting a hang of naming conventions and code quality stuff so my work tends to be a bit messy – Leptoflux Oct 21 '19 at 10:01

0 Answers0