[SDL]Colision with multiple objects. How?

Started by
7 comments, last by ZadrraS 20 years ago
I have a player on screen and the map is drawn with this function:

void DrawMap() {
	for (int x=0; x<20; x++) {
		for (int y=0; y<15; y++) {
			if (map[y][x]==OPEN) {
				Draw(Etile, x*40, y*40);
			}
			else if (map[y][x]==BLUE) {
				Draw(Btile, x*40, y*40);
			}
			else if (map[y][x]==GREEN) {
				Draw(Gtile, x*40, y*40);
			}
		}
	}
}
(Every tile is 40x40 size). The player moves around the screen, but how do i check collision,i could do it if there would be only few objects drawn separetly (like in ping-pong) But how do i check it if lots of tiles are drawn this way? Thanks!
Advertisement
are your tiles a seperate class with their own attributes or are they just simply drawn?
You could divide your world into sectors. Then only test the objects that are in the same sector for collisions.
quote:Original post by vaneger
are your tiles a seperate class with their own attributes or are they just simply drawn?

Theyre simply drawn. If they shouldn't be, just say it


[edited by - ZadrraS on April 6, 2004 2:06:45 AM]
i think he means, do you store any data for your tile? as in, do you have a class called Tile which has attributes such as SDL_Surface* or bool is_drawn
FTA, my 2D futuristic action MMORPG
Like ive said, i don''t have any classes for keeping info about each tile. I have 5 surface, the screen, the player, Empty Tile(ETile), Blue Tile(BTile) and Green Tile (GTile). I have an array called map which stores either OPEN, BLUE or GREEN. My functions loops through each of the maps integers and if the current one is OPEN, it draws Etile (maps[y][x]) on current y*40 and current x*40 (each tile is 40 pixels) and etc.
Ok, suppose your player is at location (230.0f, 112.0f).

Divide those values by your tilesize and you can determine which tile the player is inside at that moment. Then you can ensure you only collision check the ones surrounding it.

230 / 40 = 5.75
112 / 40 = 2.8

Round them down and you''ve got map[5][2].

hope that helps.
Hmmmm.. ok, butt what to do after that?, i need to push the player out of the tile in the direction he came from. How to do that?
i`ve written tutorials on tilebased collision detection.

you can read them at http://jnrdev.kbs-design.net/

and you can check out the source to my jumpnrun super mario war here: http://jnrdev.kbs-design.net/smw

[Edited by - no_skill on February 10, 2005 1:28:59 AM]

This topic is closed to new replies.

Advertisement