0
votes

I'm trying to make a pinball-style game for a school project for the Android in the SDK in Eclipse.

There's a really weird and super frustrating problem in which objects are moving without any code telling them to. Basically, each Wall instance contains 4 Line objects which are used for collision detection with the Ball. These Lines work the first time, but as soon as the ball collides with them once, then that Line somehow moves to another position on the screen. I've been debugging it, and wouldn't ask if I hadn't already tried everything, but there is honestly no reason for the Line to shift anywhere. The way I handle a collision is by pushing the Ball to be 1px away from the wall, and then given new dx and dy (velocities) to move away. The code for checking for collisions is below, followed by the function that handles a collision to change the ball's position and velocity. Both are methods in the Ball class.

GameElement[] walls = currLevel.getWalls();
int i, j;
Line[] lines;
Line line;
RectF lineBounds;
boolean hadCollision = false;

for (i = 0; i < walls.length & !hadCollision; i++) {
    lines = walls[i].getLines();
    for (j = 0; j < lines.length & !hadCollision; j++) {
        lineBounds = lines[j].getBounds();
        if (lineBounds.intersect(point)) {
            paint.setColor(Color.BLUE); // Colour ball blue.
            reactToCollision3(lines[j]);
            // TEST RESET!!!
            //this.x = (float)(648+40);
            //this.y = (float)(900-30);
            hadCollision = true;
            //printWallsLines();
        }
    }
}

and the function to handle the collision is:

public void reactToCollision3 (Line line) {

float liney = line.sy;
float linex = line.sx;

if (line.rotation == 0.0) {     // HORIZONTAL EDGE
    if (this.y > liney) { // Ball moving upward hits the bottom of a wall.
        this.y = liney + this.radius + 1.0f;
    } else { // Ball moving downward hits the top of a wall.
        this.y = liney - this.radius - 1.0f;
    }
        this.dy *= -1.0f;
} else {                        // VERTICAL EDGE
    if (this.x > linex) { // Ball moving leftward hits right edge of a wall.
        this.x = linex + this.radius + 1.0f;
    } else { // Ball moving rightward hits left edge of a wall.
        this.x = linex - this.radius - 1.0f;
    }
    this.dx *= -1.0f;
}

So when I run this right now, the ball will bounce off a wall the first time it hits it, and then that line (edge of the wall) that it hit will be shifted elsewhere, but is not visible because the Wall is drawn as one unit so the Lines that comprise it don't affect the drawing. If I comment out the lines for "this.x = ..." and "this.y = ...", then this problem doesn't happen anymore. Also, if I uncomment the test RESET lines for setting the ball's position in the above function, then the line doesn't shift then either. But as soon as I run this, it happens again.

I'm going insane looking for why this would happen. Please give me suggestions. Thank you!

1

1 Answers

0
votes

Did you intend to use bitwise &? (See ** in code) Your test for "!hadCollision" will fail and you will also be masking your wall length. I believe you meant to use &&

for (i = 0; i < walls.length **&** !hadCollision; i++) {
    lines = walls[i].getLines();
    for (j = 0; j < lines.length **&** !hadCollision; j++) {
        lineBounds = lines[j].getBounds();
        if (lineBounds.intersect(point)) {
            paint.setColor(Color.BLUE); // Colour ball blue.
            reactToCollision3(lines[j]);
            // TEST RESET!!!
            //this.x = (float)(648+40);
            //this.y = (float)(900-30);
            hadCollision = true;
            //printWallsLines();
        }
    }
}