Wednesday, April 17, 2013

[android help] ArrayIndexOutOfBoundsException on collision detection


I have a sprite and I have a maze which is randomly generated so I have a method here which checks if a wall is there and return true if there is.


I have a maze which is drawn and checks if there is a wall:



public boolean isWall(int oldX, int oldY, int newX, int newY) {
boolean wallPresent;
if ((oldX == newX) && (oldY == newY)) { wallPresent = false; }
else if (newX == oldX - 1) {
wallPresent = west[oldX][oldY];
} else if (newX == oldX + 1) {
wallPresent = east[oldX][oldY];
} else if (newY == oldY - 1) {
wallPresent = north[oldX][oldY];
} else if (newY == oldY + 1) {
wallPresent = south[oldX][oldY];
} else { wallPresent = false; }

if ((oldX != newX) && (oldY != newY)) {
if ((newX == oldX + 1) && (newY == oldY + 1) &&
(north[newX][newY] || west[newX][newY]) ) {
wallPresent = true;
} else if ((newX == oldX + 1) && (newY == oldY - 1) &&
(south[newX][newY] || west[newX][newY]) ) {
wallPresent = true;
} else if ((newX == oldX - 1) && (newY == oldY + 1) &&
(north[newX][newY] || east[newX][newY]) ) {
wallPresent = true;
} else if ((newX == oldX - 1) && (newY == oldY - 1) &&
(south[newX][newY] || east[newX][newY]) ) {
wallPresent = true;
}
}

return wallPresent;
}


I have also created a sprite in the form of a bitmap image and this is my movement and collision detection:


public void onDraw(Canvas canvas) {



while (Game.pressedUp) {
movementGo();
}

// update();
// SOURCE EDU4JAVA - www.edu4java.com
// using the rows and columns of the sprite field, separate the required
// sprite
int srcX = currentFrame * width;
int srcY = direction * height;

src = new Rect(srcX, srcY, srcX + width, srcY + height);
dst = new Rect(x, y, x + width, y + height);
//System.out.println(x);
//System.out.println(y);
// SOURCE END
canvas.drawBitmap(bmp, src, dst, null);
}
//x+width
private boolean canBallMove(int x, int y) {
int cellWidth = 64;
int newMidX = x + x/2;
int newMidY = y + y/2;

// Find out which cell ball's middle was in.
// Note that this division will round down, which is correct
int oldCellX = x + xSpeed/cellWidth +1;
int oldCellY = y + xSpeed/cellWidth +1;

// Find out which cell ball's middle would be in now
int newCellX = newMidX/cellWidth + 1 ;
int newCellY = newMidY/cellWidth + 1;
System.out.println(newCellX);
System.out.println(newCellY);
if ((newMidX % cellWidth) + x >= cellWidth) {
newCellX++;
}
if ((newMidX % cellWidth) - x <= 0) {
newCellX--;
}
if ((newMidY % cellWidth) + y>= cellWidth) {
newCellY++;
}
if ((newMidY % cellWidth) - y <= 0) {
newCellY--;
}


return !(Game.gameV.isWall(oldCellX, oldCellY, newCellX, newCellY));
}


}


Now, the else statement triggers, meaning that it knows there is a wall nearby but I still think its broken and now I keep getting these ArrayIndexOutOfBoundsException crashes? Any clue anyone?



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...