2d tile collision advice

Started by
3 comments, last by marvel_magnum 12 years, 2 months ago
Hi I'm new to game programming and this forum. I've been searching all over the net for tutorials on how to do 2d tile collision between a sprite and tile using a 2d array. I just can't wrap my head around the logic to make it work properly. I've used primitive drawing routines to create a small map and sprite represented my boxes. Currently the sprite box will get stuck whenever I touch a solid tile and try to move.

red box = solid tile represented by a 1.
green = passibile tile reprented by a 0.

I'm using Visual C++ and the allegro sdk.

Thank you for your time :)


#include <allegro.h>
using namespace std;
#define ScreenWidth 800
#define ScreenHeight 600
volatile long timer = 0;
void Increment(){timer++;}
END_OF_FUNCTION(Increment);
int map[10][10] = {{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,1,1,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,1,1,1,1,0,0,1},
{1,0,0,1,0,0,1,0,0,1},
{1,0,0,1,1,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
};

int main()
{
allegro_init();
install_keyboard();
install_timer();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
set_window_title("Collision detection");
LOCK_FUNCTION(Increment);
LOCK_VARIABLE(counter);
install_int_ex(Increment, BPS_TO_TIMER(60));
BITMAP *Buffer = create_bitmap(800, 600);
bool done = false;
int x = 64, y = 64;
//Game Loop
while(!done)
{
int px = x / 32;
int py = y / 32;
int px2 = (x+32)/ 32;
int py2 = (y+32)/ 32;
//Update Loop
while(timer > 0)
{
if(key[KEY_ESC])
done = true;
if(key[KEY_UP])
{
if(map[px][py]==0 && map[px2][py]==0)
y -= 1;
}
if(key[KEY_RIGHT])
{
if(map[px2][py]==0 && map[px2][py2]==0)
x += 1;
}
if(key[KEY_LEFT])
{
if(map[px][py]==0 && map[px][py2]==0)
x -= 1;
}
if(key[KEY_DOWN])
{
if(map[px][py2]==0 && map[px2][py2]==0)
y += 1;
}

timer--;
}


//Draw tiles
for(int a = 0; a < 10; a++)
{
for(int b = 0; b < 10; b++)
{
if(map[a]== 0)
rectfill(Buffer, a*32,b*32, a*32+32, b*32+32, makecol(0,255,0));
else
rectfill(Buffer, a*32,b*32, a*32+32, b*32+32, makecol(200,0,0));

}
}

textprintf(Buffer, font, 400, 530, 255, "X: %i Y: %i", x, y);
textprintf(Buffer, font, 400, 550, 255, "Integer: %i, %i", px, py);
textprintf(Buffer, font, 400, 570, 255, "Map tile: %i", map[px][py]);
rect(Buffer, x,y,x+32,y+32, 255);
blit(Buffer, screen, 0,0,0,0,800, 600);
clear_bitmap(Buffer);
}
destroy_bitmap(Buffer);
allegro_exit();
return 0;
}
END_OF_MAIN()

Advertisement
Programming questions don't belong in the Game Design forum. Moving this to For Beginners (a technical programming forum).

-- Tom Sloper -- sloperama.com

How do you want exactly the collision to be? And what is colliding with what?
If the objects are all rectanges you could try AABB Collision Detection.
I wrote this article on tile collision detection which should give you some pointers:

http://www.wildbunny.co.uk/blog/2011/12/14/how-to-make-a-2d-platform-game-part-2-collision-detection/

:)

Cheers, Paul.

I wrote this article on tile collision detection which should give you some pointers:

http://www.wildbunny...sion-detection/


That is a amazing resource. Thanks man.

This topic is closed to new replies.

Advertisement