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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

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

Post 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...
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

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

Post 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.
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

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

Post 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!
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

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

Post 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
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

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

Post 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;
}
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

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

Post 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...
Image
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

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

Post 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
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

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

Post by GroundUpEngine »

Yes ;)
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

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

Post 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
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

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

Post 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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

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

Post 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 =-]
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

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

Post by xiphirx »

Look at lazyfoo's tutorials. Google "lazyfoo".
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

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

Post 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 :)
Image
Im Blue
Post Reply