Hi !
I'm here because I can't resolve my problem :
I'm releazing a 2D game with Allegro 5 and I'm not able to make collision with my character. Actually, I made a tile map and a table with the tiles number.
My problem is that I can't know the position of my character on the map. So I can't control collision with it.
So if you can give me some answers, it will be great !
Here the zip file.
Thanks.
PS: I'm french and sorry if there are mistakes in my language.
You always track your player position.
for example:
float player_x; float player_y; bool map[32][32]; //1 indicates solid, 0 is empty const int TILE_WIDTH = 16; const int TILE_HEIGHT = TILE_WIDTH; if(map[player_x / TILE_WIDTH][player_y / HEIGHT) == 1) //convert from position to index in the map by dividing by the tile size { //collision has occurred }
You can use the player_x and player_y to represent the "center" of your character, then draw the character with:
my_draw_function(player_bitmap, player_x / PLAYER_WIDTH, player_y / PLAYER_HEIGHT)
This would center it at your player point. Or for a platformer, you want to center horizontally, but not vertically. You want to be checking your players feet, not his chest. (Whereas the exact center could work for a space ship.) So you'd do:
my_draw_function(player_bitmap, player_x / PLAYER_WIDTH, player_y - PLAYER_HEIGHT).
That is, centered horizontally, but not vertically. And you subtract the height to move the sprite higher on the screen so now the bottom of the graphic (typically his / her feet) will be the contact point.
You can also have more contact points, such as top/bottom/left/right.
Can you translate your answer with my code please ?
Allegro.h
main.cpp
Thanks.