Page 1 of 1

PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Sat Apr 03, 2010 6:54 am
by PaperDuckyFTW
heeeeyyyyya ^.^ ,\/,, *waves retardedly*

i am working on a paltform game while i learned SDL and game programming in general. I already had snimation, gravity, jumping, map scrolling and collision on my old computer, and decided to restart the coding because it was messy and it would have been an hassle to get the code to my new computer. So far i have animation, gravity and collision in the game, but before i do anyhting else i wanted to nail tile maps. So far i can draw individual tiles and set a SDL_Rect to them as collision, but i am stumped with collision with a array tile map.

Befoe i post some wiked awesome code =] Ill tell what i thought might have worked, in case it COULD work, but i was doing something wrong or forgetting something. On the build on my old computer i could draw tiles and a single Rect assigneed to each tile, but the Rects were the same. If it doesnt make sence, this is an example :

Code: Select all

drawthings( 50, 35, wall, &block );
drawthings( 20, 50, invisiblewall, &block );
So i thought i coul draw the same block rect that the player collides with with every tile drawn from an array. Can anyone help? I have looked at Lazy Foo's tutorials, in fact like almost everyone esle, thats how i learned SDL. Except my tile system is perfectly fine and i'm happy with it, so i cbf learning some 100+ line crap when my simple version is sufficient. Anyway, all i have is an array liek this -> map[15][20] and then a loop checking it and drawing the wanted tile. hereis the super awesome rather crappy code =]

Code: Select all

for( int x = 0; x < 15; x++ )
	{
		for( int y = 0; y < 20; y++ )
		{
			if( map[y][x] == 1 )
			{	
				
			     draw_surface( x * 32, y * 32, wall, screen );
			}
		}
	}
yeah idk when i type, the whole text area jump up heard to explain. I cant see what im typing right now...Anyway, that just draws the wanted tile but i cant think of how to make collision. If anyone can help it would be most appreciated. IF you can supply code it would be most appreciated and easier. Thanks for the help and hope you all have an aweosme day/week/year/millenium. Especially mellenium. Cause idk if peopl can live that long. I think so...

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Sat Apr 03, 2010 8:20 am
by K-Bal
Could you please be more specific? What has the drawing of tiles to do with collision? I don't get what your problem is.

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Sat Apr 03, 2010 8:58 am
by MrDeathNote
K-Bal wrote:Could you please be more specific? What has the drawing of tiles to do with collision? I don't get what your problem is.
^This, do you use a separate collision layer, or are certain tiles solid, etc. We need more info!

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Sat Apr 03, 2010 9:36 am
by PaperDuckyFTW
oh sorry bout that, its a one dimensional array something like this: for example

map[5][5] = {
{ 0, 0, 0, 0, 0 }
{ 0, 0, 1, 1, 0 }
{ 0, 1, 1, 1, 0 }
{ 1, 0, 1, 0, 1 }
{ 0, 1, 1, 1, 0 }
};

And the loop just checks to see if there is a '0' in it, draw sky and it shere is a '1' in it, draw a brick. Can the same thing be done with SDL_Rects? Making a look and if there is a '1' in the array, place a Rect there but if theres a '0', dont place one? There isnt any collision layer abecause i would have a clue how to do that. Sorry if im making as much sense as a giraffe on a pogo stick, or im as much help as a retarded seal, but any help on how to make the damn thing collidable would be most helpful.

If theres anymore infomation you need just ask

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Sat Apr 03, 2010 10:27 am
by GroundUpEngine
You could do collision similar to how you do your rendering ;)

Code: Select all

Player fella; // lol
void Update() {
    for(int x = 0; x < 5; x++)
    {
        for(int y = 0; y < 5; y++)
        {
            if(map[y][x] == 1)
            {
                fella.IsCollision(x * 32, y * 32);
            }
        }
    }
}

Code: Select all

bool Player::IsCollision(int tile_x, int tile_y) {
    if(x < tile_x) return false;
    if(x+w < tile_x+32) return false;
    if(y > tile_y+32) return false;    
    if(y+h < tile_y) return false;
    return true;
}

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Sat Apr 03, 2010 11:03 am
by eatcomics
GroundUpEngine wrote:You could do collision similar to how you do your rendering ;)

Code: Select all

Player fella; // lol
void Update() {
    for(int x = 0; x < 5; x++)
    {
        for(int y = 0; y < 5; y++)
        {
            if(map[y][x] == 1)
            {
                fella.IsCollision(x * 32, y * 32);
            }
        }
    }
}

Code: Select all

bool Player::IsCollision(int tile_x, int tile_y) {
    if(x < tile_x) return false;
    if(x+w < tile_x+32) return false;
    if(y > tile_y+32) return false;    
    if(y+h < tile_y) return false;
    return true;
}
This is what I would suggest, its how I usually do collision...

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Sat Apr 03, 2010 11:40 am
by PaperDuckyFTW
okay thenks for your help =D

i was born with a rare disease call dumbassretard disease and need some help deciphering the code. I understand the concept but i just dont understand the values.

Code: Select all


bool Player::IsCollision(int tile_x, int tile_y) {
    if(x < tile_x) return false;
    if(x+w < tile_x+32) return false;
    if(y > tile_y+32) return false;    
    if(y+h < tile_y) return false;
    return true;
}
from what i can assume, the x, y, w, h are all the player's respective attrubutes? sorry if this is a nooby question or it may seem a waste of time but i dont want to waste any more time over this map cillision.

Thanks ^.- b

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Sat Apr 03, 2010 11:42 am
by GroundUpEngine
Yes ;)

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Mon Apr 05, 2010 2:23 am
by PaperDuckyFTW
hi again

i dont know what im donig wrong but i impliment both but the collision didnt work, i can still just walk through them. I have no clue as what to do to make the tiles collidable im completely stumped. I place your IsCollidable code before drawing the tiles, after drawing the tiles, after and before drawing the player but stil i dont know whats up with it.

Please, this thing is pissing me off like a...yeah i cant think of a metaphor to go with it. If anyone can help to get this to work i could cry in happiness ='D
I cant really post anymore information as i did exactly as suggested but i dont know why it doesnt work. Thanks

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Mon Apr 05, 2010 5:11 am
by MrDeathNote
PaperDuckyFTW wrote:hi again

i dont know what im donig wrong but i impliment both but the collision didnt work, i can still just walk through them. I have no clue as what to do to make the tiles collidable im completely stumped. I place your IsCollidable code before drawing the tiles, after drawing the tiles, after and before drawing the player but stil i dont know whats up with it.

Please, this thing is pissing me off like a...yeah i cant think of a metaphor to go with it. If anyone can help to get this to work i could cry in happiness ='D
I cant really post anymore information as i did exactly as suggested but i dont know why it doesnt work. Thanks
You know the code that he gave you only detects a collision right? It doesn't resolve it, your not telling it what to do once that collision has been detected. At the min your walking on a collidable tile, it's goin hey this is a collidable tile, i haven't been told what to do when this happens so ill just keep doin what im doin. There is a good post about AABB collision already, check this out http://elysianshadows.com/phpBB3/viewto ... art=999999.

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Fri Apr 16, 2010 4:12 am
by PaperDuckyFTW
hi again. Sorry if this is annoying ot irritating or tme-consuming but i was wondering if anyone could post a link to a tile map tutorial site, or file with tile map code or somehitng discussing the conceptss beind it. thanks if anyone can =-]

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Fri Apr 16, 2010 2:09 pm
by xiphirx
Look at lazyfoo's tutorials. Google "lazyfoo".

Re: PLEASE!! Tile map collision help!!! ( C++/SDL )

Posted: Sat Apr 24, 2010 3:07 am
by Zer0XoL
you are passing references which meybe is the same object that you are just changing...
try using new like this:
CreateTile(new tile);
i hope it helps :)