# Sprite, collision with tiles

Hi guys,
I need a function thet understands when a sprite collides with a tile.

I made something that uses the position of the sprite in relationship
with the tile (ex: NumTileInMap = SpriteXPixel + (SpriteYPixel *
MAP_WIDTH))

That works fine when the sprite is in the bottom or in the left of the
tile but it dosen’t work fine when the sprite is at the top and in the
right of the tile.

Besides I need to check if the sprite collides with a tile when it’s
in the middle of two tiles.

Thanx a lot

Hello !

In my game i use this function
to detect collisions with the map :

bool Map::Collide_Map (CRect *rect)
{
Sint32 lx = 0, ly = 0, rx = 0, ry = 0;
Uint32 index_layer = 0, index_tile_x = 0, index_tile_y = 0;
Uint32 start_tile_x = 0, start_tile_y = 0;
Uint32 end_tile_x = 0, end_tile_y = 0;

``````Check_CRect (rect);

lx = rect -> lx;
ly = rect -> ly;
rx = rect -> rx;
ry = rect -> ry;

if (lx < 0 || ly < 0) return true;
``````

// FIXME: This check is not working correct ?!?!?!
/*
if ( ( lx > Map_Ptr -> Max_Map_Offset_X ) ||
( ly > Map_Ptr -> Max_Map_Offset_Y ) ) return false;
*/

``````if ( ( rx < 0			    ) ||
( ry < 0			    )	    )	return false;

if (lx < 0)
start_tile_x = 0;
else
start_tile_x = (Uint32) ( lx / Map_Ptr -> Tile_Size_X );

if (ly < 0)
start_tile_y = 0;
else
start_tile_y = (Uint32) ( ly / Map_Ptr -> Tile_Size_Y );

if (rx < Map_Ptr -> Max_Map_Offset_X)
end_tile_x = (Uint32) ( rx / Map_Ptr -> Tile_Size_X );
else
end_tile_x = Map_Ptr -> Nr_Tiles_X - 1;

if (ry < Map_Ptr -> Max_Map_Offset_Y)
end_tile_y = (Uint32) ( ry / Map_Ptr -> Tile_Size_Y );
else
end_tile_y = Map_Ptr -> Nr_Tiles_Y - 1;

for (index_layer = 0; index_layer < Map_Ptr -> Nr_Layers; index_layer ++)
{
for (index_tile_y = start_tile_y;
index_tile_y <= end_tile_y;
index_tile_y ++)
{
for (index_tile_x = start_tile_x;
index_tile_x <= end_tile_x;
index_tile_x ++)
{
if (Map_Ptr -> Layers [index_layer].
Tiles [index_tile_x][index_tile_y]
.Type == TYPE_WALL)
{
return true;
}
}
}
}

return false;
``````

}

CU

Maybe you should use a bounding box for your sprite and, for each tile,
check if any of the 4 box corners are within the tile area. But this would
work only if you keep track of your tiles as structs, or at least their
position.

Hope this helps! =)

&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&
Carolina SimAues Gomes

[Game Programmer - C/C++, J2ME]
[Game Designer]
[Developer - Ailandra Project(http:\www.ailandra.com)]

Computer Engineering 2005 - UNICAMP
Cell phone : (11)9715-0347
E-mail : @Carolina_Simoes_Gome
&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&----------------------------------------------------------------------

From: "Salvatore Di Fazio"
Reply-To: "A list for developers using the SDL library.
(includesSDL-announce)"
To: "A list for developers using the SDL library. (includes
SDL-announce)"
Subject: [SDL] Sprite, collision with tiles
Date: Sat, 2 Sep 2006 21:45:39 +0200

Hi guys,
I need a function thet understands when a sprite collides with a tile.

I made something that uses the position of the sprite in relationship
with the tile (ex: NumTileInMap = SpriteXPixel + (SpriteYPixel *
MAP_WIDTH))

That works fine when the sprite is in the bottom or in the left of the
tile but it dosen’t work fine when the sprite is at the top and in the
right of the tile.

Besides I need to check if the sprite collides with a tile when it’s
in the middle of two tiles.

Thanx a lot

SDL mailing list
SDL@libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Chegou o Windows Live spaces com rede social! Confira aqui!

Please do not use HTML in email.On %A %d %B %Y %H:%M:%S %p, Carolina Sim?es Gomes wrote:

Maybe you should use a bounding box for your sprite and, for each tile, check if any of the 4 box corners are within the tile area. But this would work only if you keep track of your tiles as structs, or at least their position.

Hope this helps! =)

&&&&&&&&&&&&&& &&&&&& &&&&&&&&&&&&&&& &&&&&&
Carolina Sim?es Gomes

[Game Programmer - C/C++, J2ME]
[Game Designer]
[Developer - Ailandra Project(http:\\www.ailandra.com)]

Computer Engineering 2005 - UNICAMP
Cell phone : (11)9715-0347
&&&&&&&&&&&&&&& amp;&&&&& &&&&&&&&&&&&&&& &&&&&&

From: "Salvatore Di Fazio" <salvatore.difazio at gmail.com>
Reply-To: "A list for developers using the SDL library. (includesSDL-announce)" <sdl at libsdl.org>
To: "A list for developers using the SDL library. (includes SDL-announce)"<sdl at libsdl.org>
Subject: [SDL] Sprite, collision with tiles
Date: Sat, 2 Sep 2006 21:45:39 +0200
>Hi guys,
>I need a function thet understands when a sprite collides with a tile.
>
>I made something that uses the position of the sprite in relationship
>with the tile (ex: NumTileInMap = SpriteXPixel + (SpriteYPixel *
>MAP_WIDTH))
>
>That works fine when the sprite is in the bottom or in the left of the
>tile but it dosen't work fine when the sprite is at the top and in the
>right of the tile.
>
>Besides I need to check if the sprite collides with a tile when it's
>in the middle of two tiles.
>
>Thanx a lot
>
>_______________________________________________
>S DL mailing list
>SDL at libsdl.org
>http://www.libsdl.org/mailman/listinfo/sd l

Chegou o Windows Live spaces com rede social! Confira aqui!

2006/9/2, Torsten Giebl :

Hello !

In my game i use this function
to detect collisions with the map :

But your code check if the sprite is in the middle of two tiles, where
one is unwalkable and another is walkable?

Tnx

2006/9/2, Carolina Sim?es Gomes <alma_horadrim at hotmail.com>:

Maybe you should use a bounding box for your sprite and, for each tile,
check if any of the 4 box corners are within the tile area. But this would
work only if you keep track of your tiles as structs, or at least their
position.

Yes, I would and I keep track of my tiles position in an array.

Anyway maybe I found a solution…