tile based collision detection question

So im making a tile based game but the colision detection with the tiles doenst work right(the collision rect goes thru the corners of the blocked tile). although i know why it doesnt work , i cant think of a way around it. here is the method where the problem is at:

//moves the sprite, acording to the velx, vely, if it is possible to do so
//this method moves sprites using the hitsquare collision method with the map tiles
//calls the sprites mapCollision() method if it collides with a blocked tile
public void moveSquareSprite(Sprite s){
	int sl = s.getHitSquareLength();
	int newx = (int) (s.getPosX()+s.getVelX());
	int newy = (int) (s.getPosY()+s.getVelY());
	//calc row/col of tiles that each side is on...	
	int top 	= (newy-(sl/2)) / tileHeight ;
	int bottom 	= (newy+(sl/2)) / tileHeight ;
	int left 	= (newx-(sl/2)) / tileWidth  ;
	int right 	= (newx+(sl/2)) / tileWidth  ;
	
	boolean topleft = tiles[left][top].blocked;
	boolean bottomleft = tiles[left][bottom].blocked;
	boolean topright = tiles[right][top].blocked;
	boolean bottomright = tiles[right][bottom].blocked;
	
	boolean collided = false;
	
	//test if moving upward
	if(s.getVelY() < 0){
		if(topright && topleft){
			s.setPosY( (tileHeight*(top+1)) + (sl/2));
			s.setVelY(0);
			collided=true;
		}else{
			s.setPosY( s.getPosY()+s.getVelY() );
		}
	}
	
	//test for moving downward
	else if(s.getVelY() > 0){
		if(bottomright && bottomleft){
			s.setPosY( tileHeight*bottom - (sl/2));
			s.setVelY(0);
			collided=true;
		}else{
			s.setPosY( s.getPosY()+s.getVelY() );
		}	
	}
	
	//test for moving left
	if(s.getVelX() < 0){
		if(topleft && bottomleft){
			s.setPosX( (tileWidth*(left+1)) + (sl/2));
			s.setVelX(0);
			collided=true;
		}else{
			s.setPosX( s.getPosX()+s.getVelX() );
		}
	}
	
	//test for moving right
	else if(s.getVelX() > 0){
		if(bottomright && topright){
			s.setPosX( tileWidth*right - (sl/2));
			s.setVelX(0);
			collided=true;
		}else{
			s.setPosX( s.getPosX()+s.getVelX() );
		}	
	}
	
	if(collided)
		s.mapCollision();
}

I have attached the whole game in a zip file renamed to a *.txt file if you want to look at it.

thanks, please help me :wink:

PS.in the renamed zip file is also the server for my game, if you want you could look at that and inform me if it is efficent or not?

I need to understand this problem better.

Most tile based games use very simple tile based blocking. Characters move from tile center to tile center. If a tile is marked as blocked in the file map, the character is not allowed to move there.

Is there a game design reason you need pixel-accurate positioning?

I need accurate collision detection as i wan it to have a good feel. i just dont think it would be right if the ship could only move like 20px at a time, lots of games use this kind of collision detection eg mario, zelda - (well i think they do, you are not limited to the center of each tile)

he only said that they move from tile center to tile center, not that there is no animation between these steps :slight_smile: if you know you are only moving between these locations you can make collision really simple (in the way jeff mentioned). Just check if the next tile is empty before making the transition from one to the next, and don’t do any detection at all during the transition (you already know the character is moving to a safe location at that point).

Edit: realized later that you probably understand the above, I think I misread your post.

No,I got the same thing from his psot that you did.

Its jnto that the ship jumps from center to center, but that you check your collision a tile at a time and dont start a move into a tile if its blocked.

This is how games al lthe way up through things like the SSI Dark Sun engine worked. Its a lot simpler and a lot lower load.

The only limtiation is that the character most move in the 8 cardoinal directions and that the final stop position
must be centered on a tile,

I believe this is how most of the games you mentioned (eg Zelda) function.

i really think you have made a mistake, games like zelda dont limit you to the center of each tile, they allow you everywhere in the map, becuase it makes the player feel more in control. play zelda, you will see what i mean. :slight_smile:

its true that zelda doesn’t do this, but many other similarly styled games do (check out the final fantasy games for snes for example). You can still use some of the basic idea though, before transitioning to a new tile, check if it is solid or not, if it isn’t than transition and don’t check again until you are going to a new tile.

Now this works for most things, but there will be tiles where it seems like only one corner (or half the corners) should be blocked or maybe only one side should be blocked and you should be able to walk on the rest of the tile. You’ll have to come up with a way to handle these cases (or ignore them and only have solid/not-solid tiles).

I have now managed to fix it so that it basically works, but i still get the problem of “sticking” to the tiles that i didnt get before. IE you cant slide along the tile, you just stop.

Well, pixel positioning really is a minor varioation on what I already stated, if thats what you really want.

You still have a “hot point”, which is typically the middle of the base. For up and down, you check the tile map when the y of that point is abotu to cross over into that tile.

For X, youi do the same but add (or subtract going the other way) an offset first thats hal;f the width

Follow?

I never thought of doing it that way. sounds good. :slight_smile: