Page 1 of 1

Who wants a challenge?! <SDL/C++>

Posted: Wed Apr 28, 2010 5:39 am
by PaperDuckyFTW
Hiyah its that noob thats annoying and cant really use english good. That was a fail attempt at being funny. But yes, i am a noob and to some maybe annoying and sometimes i type inunderstandable things.

The challenge to you if you chose to take it is tile cillision! Yep, its pretty much the same as my last post! See how im prolly annoying!!!!!!

This is the drawng function:

Code: Select all

void draw_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect clip = NULL ) //same as Lazy Foo's function
{
   SDL_Rect pos
   {
      pos.x = x;
      pos.y = y;
   }

   SDL_BlitSurface( source, clip, destination, &pos );
}
This is the collision function:

Code: Select all

bool check_collision( SDL_Rect A, SDL_Rect B )  //yup again, same as Lazy Foo's bounding box collision
{
    //The sides of the rectangles
    int leftA, leftB;
    int rightA, rightB;
    int topA, topB;
    int bottomA, bottomB;

    //Calculate the sides of rect A
    leftA = A.x;
    rightA = A.x + A.w;
    topA = A.y;
    bottomA = A.y + A.h;
        
    //Calculate the sides of rect B
    leftB = B.x;
    rightB = B.x + B.w;
    topB = B.y;
    bottomB = B.y + B.h;

    //If any of the sides from A are outside of B
    if( bottomA <= topB )
    {
        return false;
    }
    
    if( topA >= bottomB )
    {
        return false;
    }
    
    if( rightA <= leftB )
    {
        return false;
    }
    
    if( leftA >= rightB )
    {
        return false;
    }
    
    //If none of the sides from A are outside B
    return true;
}
And here's the tile drawing method:

Code: Select all

int map[5][5] = {
{ 0, 0, 0, 1, 0 }
{ 0, 1, 2, 1, 1 }
{ 0, 0, 0, 0, 0 }
{ 0, 1, 1, 1, 1 }
{ 0, 0, 0, 2, 0  }
}

for( int x = 0; x < 5; x++ )
{
	for( int y = 0; y < 5; y++ )
	{
		if( map[x][y] == 0 )
		{
			draw_surface( x * 32, y * 32, grass, screen );
		}
		else if( map[x][y] == 1 )
		{
			draw_surface( x * 32, y * 32, water, screen );
		}
	}
}
//same for each number in array
Youre challenge is not to think im a idiot and to see if you can make a tile collision system, using these things or adding stuff. Sorry ifim a pest though. I have an idea, and if it works ill celebrate abuot it for a long time on this topic and then yerr...ask for t to be deleted. Havefun and take care.

Re: Who wants a challenge?! <SDL/C++>

Posted: Wed Apr 28, 2010 5:47 am
by PaperDuckyFTW
Sorry, i meant i have an idea on how to make the map have collision.

Re: Who wants a challenge?! <SDL/C++>

Posted: Thu Apr 29, 2010 9:03 pm
by captjack
This doesn't answer your question directly, but I'll let you know what I did just for the sake of offering other points of view...

My maps are a multi-dimenstional one, like yours. I allow maps of any size, up to a #defined WORLD_MAX_X and WORLD_MAX_Y.

The tile maps, look like:

Code: Select all

3 3 3 3 3 3 3 3 3 3 3
3 1 1 1 1 1 1 1 1 1 3
3 1 1 1 2 2 2 1 1 1 3
3 1 1 1 2 4 2 1 1 1 3
3 1 1 1 2 2 2 1 1 1 3
3 1 1 1 1 1 1 1 1 1 3
3 3 3 3 3 1 3 3 3 3 3
A grass tile has an ID of 1. A dirt tile has an ID of 2. Mountain tiles are ID 3. A boulder tile has an ID of 4.
So you can see that I have a green grassy field with a dirt patch in the middle. A boulder is right in the middle of that dirt patch. The whole map is bordered by mountains with a wee pathway at the very bottom middle.

Collisions are managed in a collision layer. This is loaded after the map.

Code: Select all

1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 1 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 1
1 1 1 1 1 0 1 1 1 1 1
A 1 indicates a collidable tile. A 0 is freely passable.

When the player hits a movement key, say down, I check the next collision layer tile down (x, y+1) to see if it has a 1 or not.

This works for me since my player moves one tile at a time. I don't implement smooth scrolling ala Final Fantasy. Mine is like Ultima IV. :)