# Problems with collisions and sdl\_rects

**URL:** <https://discourse.libsdl.org/t/problems-with-collisions-and-sdl-rects/19651>\
**Category:** SDL Development\
**Created:** [June 6, 2013, 5:30am UTC](https://discourse.libsdl.org/t/problems-with-collisions-and-sdl-rects/19651 "2013-06-06T05:30:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrNicholas](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/b5e925/32.png) [@DrNicholas](https://discourse.libsdl.org/u/DrNicholas)\
**Post date:** [June 6, 2013, 5:30am UTC](https://discourse.libsdl.org/t/problems-with-collisions-and-sdl-rects/19651/1 "2013-06-06T05:30:21Z")

</div>

source here: [http://www.mediafire.com/download/8d0osuuabogodb9/NickTest\_6-2-2013\_1741.zip](http://www.mediafire.com/download/8d0osuuabogodb9/NickTest_6-2-2013_1741.zip)

I am having a problem with collisions. Now I know the problem, I just cant find anywhere on a way to better optimize it. In player.cpp, you will see the code

Code:  
void player::move()  
{  
//Move the player left or right  
box.x += xVel;

```
//If the player went too far to the left or right or has collided with the wall
for( i = 0; i<= 0; i++)
{
	if( ( box.x < 0 ) || ( box.x + player_w > screenx ) || ( check_collision( box, wall[i] ) ) )
	{
		//Move back
		box.x -= xVel;
	}
}

//Move the player up or down
box.y += yVel;

//Falling
for( i = 0; i<= 0; i++)
{
	if( ( box.y < 0 ) || ( box.y + player_h > screeny ) || ( check_collision( box, wall[i] ) ) )
	{
		box.y -= yVel;
		grounded = true;
		//printf("player is on the ground\n");
	} 
	else //if there is nothing underneath the player
	{
		box.y += yVel;
		grounded = false;
		//printf("player is in air\n");
	}
}

```

}

I know that it is checking to collide with every instance of wall (ill rename is block later on). Wall[0] is a rectangle that represents the floor, Wall[1] is a rectangle that represents the wall; in the future, I want to make every wall a 16x16 or 32x32 block, and that will mean having a zillion different wall[i]'s.

Is my system on the right track or will it cause problems later? Can any of you show me with my code how to improve this? I have been stuck on this for three days because of this. Thanks------------------------  
I am making an awesome SDL platformer

---

<div class="post-metadata">

**Author:** ![Jared\_Maddox](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@Jared\_Maddox](https://discourse.libsdl.org/u/Jared_Maddox)\
**Post date:** [June 8, 2013, 6:38pm UTC](https://discourse.libsdl.org/t/problems-with-collisions-and-sdl-rects/19651/2 "2013-06-08T18:38:36Z")

</div>

> Message-ID: \<1370496621.m2f.37366 at [forums.libsdl.org](http://forums.libsdl.org)\>  
> Content-Type: text/plain; charset=“iso-8859-1”
> 
> source here:  
> [http://www.mediafire.com/download/8d0osuuabogodb9/NickTest\_6-2-2013\_1741.zip](http://www.mediafire.com/download/8d0osuuabogodb9/NickTest_6-2-2013_1741.zip)
> 
> I am having a problem with collisions. Now I know the problem, I just cant  
> find anywhere on a way to better optimize it. In player.cpp, you will see  
> the code

> I know that it is checking to collide with every instance of wall (ill  
> rename is block later on). Wall[0] is a rectangle that represents the floor,  
> Wall[1] is a rectangle that represents the wall; in the future, I want to  
> make every wall a 16x16 or 32x32 block, and that will mean having a zillion  
> different wall[i]'s.
> 
> Is my system on the right track or will it cause problems later? Can any of  
> you show me with my code how to improve this? I have been stuck on this for  
> three days because of this. Thanks
> 
> * * *
> 
> I am making an awesome SDL platformer

Do you have a working demo form of the program? If so, then build a  
large level, and attempt to play it in the game. If it’s too slow or  
takes too much CPU then you’ll want to do an optimization, otherwise  
you can leave it as-is.

Now, as far as optimizations go, what you want is a scene graph. A  
scene graph basically just divides an environment into pieces, so that  
you can deal with only the parts you’re close to, instead of dealing  
with all of it.

You’re building a platformer, right? If it’s an old-school  
side-scroller then you’ll probably be able to get away with dividing  
the levels up into “screens”, and only check the “screen” that you’re  
currently on, and the two beside it. If it has any meaningful height  
then you might want to add some “screens” in that direction as well,  
and if it’s a full 3d platformer then you’ll want “depth” screens in  
addition, but those are just extensions to the basic concept.\> Date: Wed, 05 Jun 2013 22:30:21 -0700

> From: “DrNicholas”   
> To: sdl at [lists.libsdl.org](http://lists.libsdl.org)  
> Subject: [SDL] problems with collisions and sdl\_rects

---

<div class="post-metadata">

**Author:** ![Sik\_the\_hedgehog](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@Sik\_the\_hedgehog](https://discourse.libsdl.org/u/Sik_the_hedgehog)\
**Post date:** [June 9, 2013, 1:00am UTC](https://discourse.libsdl.org/t/problems-with-collisions-and-sdl-rects/19651/3 "2013-06-09T01:00:35Z")

</div>

Given he wants to make every block the same size, why not just go with  
tiles? As a bonus, you will literally only ever check with the blocks  
that would get in the way of the player, and never any other. The only  
downside is that 1) it’s a 2D array (even old games used to do this  
though, so not much of an issue!) and 2) all blocks have to be aligned  
to a grid (that may look better anyway).

…just make sure to make the type of array with the map data the  
smallest you can (ideally byte-sized, that gives you room for 255  
unique blocks besides empty).

2013/6/8, Jared Maddox :\>\> Date: Wed, 05 Jun 2013 22:30:21 -0700

> > From: “DrNicholas”   
> > To: sdl at [lists.libsdl.org](http://lists.libsdl.org)  
> > Subject: [SDL] problems with collisions and sdl\_rects  
> > Message-ID: \<1370496621.m2f.37366 at [forums.libsdl.org](http://forums.libsdl.org)\>  
> > Content-Type: text/plain; charset=“iso-8859-1”
> > 
> > source here:  
> > [http://www.mediafire.com/download/8d0osuuabogodb9/NickTest\_6-2-2013\_1741.zip](http://www.mediafire.com/download/8d0osuuabogodb9/NickTest_6-2-2013_1741.zip)
> > 
> > I am having a problem with collisions. Now I know the problem, I just  
> > cant  
> > find anywhere on a way to better optimize it. In player.cpp, you will see  
> > the code
> 
> > I know that it is checking to collide with every instance of wall (ill  
> > rename is block later on). Wall[0] is a rectangle that represents the  
> > floor,  
> > Wall[1] is a rectangle that represents the wall; in the future, I want to  
> > make every wall a 16x16 or 32x32 block, and that will mean having a  
> > zillion  
> > different wall[i]'s.
> > 
> > Is my system on the right track or will it cause problems later? Can any  
> > of  
> > you show me with my code how to improve this? I have been stuck on this  
> > for  
> > three days because of this. Thanks
> > 
> > * * *
> > 
> > I am making an awesome SDL platformer
> 
> Do you have a working demo form of the program? If so, then build a  
> large level, and attempt to play it in the game. If it’s too slow or  
> takes too much CPU then you’ll want to do an optimization, otherwise  
> you can leave it as-is.
> 
> Now, as far as optimizations go, what you want is a scene graph. A  
> scene graph basically just divides an environment into pieces, so that  
> you can deal with only the parts you’re close to, instead of dealing  
> with all of it.
> 
> You’re building a platformer, right? If it’s an old-school  
> side-scroller then you’ll probably be able to get away with dividing  
> the levels up into “screens”, and only check the “screen” that you’re  
> currently on, and the two beside it. If it has any meaningful height  
> then you might want to add some “screens” in that direction as well,  
> and if it’s a full 3d platformer then you’ll want “depth” screens in  
> addition, but those are just extensions to the basic concept.
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
