tile collission in MIDP 2.0 and scrolling?

Hi all!

I have the following problem - I want my airplane to be able to collide with a tile.

this are the methods I’m now using but they doesn’t seem to work as I want them to…

In the GameCanvas class:

/method to check for collission/

boolean containsImpassableArea(int x, int y, int widths, int heights) {
int rowMin = y / tHeight;
int rowMax = (y + heights - 1) / tHeight;
int columnMin = x / tWidth;
int columnMax = (x + widths - 1) / tWidth;

for (int row = rowMin; row <= rowMax; ++row) {
for (int column = columnMin; column <= columnMax; ++column) {
int cell = tBakgrund.getCell(column, row);
if (cell == STOP_TILE) {
return true;
}
}
}
return false;
}

/*method to check for keystates and invokes containsImpassableArea */

private void checkKeys() throws Exception {

int keyStates = getKeyStates();

if ((keyStates & LEFT_PRESSED) != 0) {
if (sprit.getX() > 0
&& !containsImpassableArea(sprit.getX() - 4, sprit.getY(),
sprit.getWidth(), sprit.getHeight())) {
sprit.move(-4, 0);
sprit.setFrame(8);
} else {
sprit.setFrame(9);
}
}
/the same on RIGHT_PRESSED, UP and DOWN/

/* if no key is pressed */
if ((keyStates == 0)) {
sprit.setFrame(9);

if(containsImpassableArea(sprit.getX(), sprit.getY() - 4,
sprit.getWidth(), sprit.getHeight())) { scrolling = false; }
else{
scrolling = true;
}

}

/* the method that handles the scolling */

if (terrainScroll < 0 && scrolling == true ) {
terrainScroll += 2;
tBackground.setPosition(0,terrainScroll);
}

}//end of method checkkeys…

my tiles are defined in a two-dimentional array/matrice

ok… why doesn’t it work …is it some kind of layerproblem (sprite and tiles not in the same layers)?

Perhaps someone has any code samples?

Thanks in advance!

/arnold

Is there nobody who has written a vertical scrolling game and knows how to make the spaceship collide with a tile?

Please help… anyone who has the code or knows where to get it / other forums?

/Arnold

This isn’t an issue specific to MIDP, so any collision code you find will do. (hint, hint) The code you posted looks more or less ok, with a few possible caveats:

  1. Are you sure you want to be scrolling only when the user presses a key?

  2. ++row != row++. It has been a while since I’ve figured out how that affects loops, but you may want to verify the start and end points you’re getting.

Beyond that, I’d have to see your rendering code. I have a sneaky suspicion that you’re mixing coordinate systems and getting into trouble. Keep in mind that if your sprites are in the screen space, then they won’t properly align to the scrolling background. The “correct” way to handle this situation is to use the background coordinates for the space ship, then render the space ship minus the scrolling offset. i.e.:


//Move the background
terrainScroll += 2;
tBackground.setPosition(0,terrainScroll);

//Move the sprite relative to the background
sprite.move(0, -2);

//render background
background.render();

//render sprite at sprite.x, sprite.y - terrainScroll
//the values passed are offsets
sprite.render(0, -terrainScroll);

A few other notes:

  1. Please use the [ code ] tag when posting code. That makes it more readable and more likely you’ll get a response.

  2. Feel free to spell out your variable names. You can always use an obfuscator to shrink your class file for you. As for typing, a good IDE should help with auto-complete.

Good luck, StarFox!