Tile maps collision detection on top-down view. C++/Allegro.
Moderator: Coders of Rage
- fryz
- Chaos Rift Newbie
- Posts: 18
- Joined: Thu Jul 22, 2010 8:27 am
- Current Project: Ping Pong.
- Favorite Gaming Platforms: PC.
- Programming Language of Choice: C++, Allegro.
- Location: Lithuania
Tile maps collision detection on top-down view. C++/Allegro.
Hi everybody! I am again confused about some things. I don't actually know how to make tile maps detect, if you are entering in a forbidden tile! I think about it this way: You go trough road tiles (let's say array image number 1). If you enter array image number two, i are stopped, as it detects collision. But i really cannot manage to do it. Can anyone help me with that? This time i mean, that i don't even have an idea on how to do it.
P.S. By the way, someone has posted Allegro forums earlier, but i cannot enter them, anybody know why? I have tried googling them, but still cannot.
P.S. By the way, someone has posted Allegro forums earlier, but i cannot enter them, anybody know why? I have tried googling them, but still cannot.
Learning Allegro. Hope i will learn it and make my ideas live. I have a lot of them!
Your help is greatly appreciated!
Your help is greatly appreciated!
- isamgray
- Chaos Rift Newbie
- Posts: 11
- Joined: Mon Jul 19, 2010 11:24 am
- Current Project: Boxy Engine
- Favorite Gaming Platforms: Xbox360
- Programming Language of Choice: C/++ and Objective-C
- Location: Dallas, TX
- Contact:
Re: Tile maps collision detection on top-down view. C++/Allegro.
Quick question(so I can give you the easiest answer to your question)...Are you doing a pixel, or tile movement? Like, each time the player presses the up arrow, does he move a certain number of pixels, or just one tile up?
My blog: http://sam.boxysource.com/
- fryz
- Chaos Rift Newbie
- Posts: 18
- Joined: Thu Jul 22, 2010 8:27 am
- Current Project: Ping Pong.
- Favorite Gaming Platforms: PC.
- Programming Language of Choice: C++, Allegro.
- Location: Lithuania
Re: Tile maps collision detection on top-down view. C++/Allegro.
Hi. Sorry, i was very busy with other things past few days, looks like it's almost done (not programming), so it looks like i can be back to programming stuff.
I'm doing pixel movement. But actually tile movement is quite a good idea for such a game, haven't even thought of "tile movement"... Could you please help me with both, different versions?
As for tile movement it's like if(key[UP]){char_y + 32;}, right? According, that one tile is 32x32px.
I'm doing pixel movement. But actually tile movement is quite a good idea for such a game, haven't even thought of "tile movement"... Could you please help me with both, different versions?
As for tile movement it's like if(key[UP]){char_y + 32;}, right? According, that one tile is 32x32px.
Learning Allegro. Hope i will learn it and make my ideas live. I have a lot of them!
Your help is greatly appreciated!
Your help is greatly appreciated!
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Tile maps collision detection on top-down view. C++/Allegro.
Yup. And isamgray's question was very important, because collision in a tile-based world is going to be drastically simpler. Why?fryz wrote: As for tile movement it's like if(key[UP]){char_y + 32;}, right? According, that one tile is 32x32px.
If movement is based on pixels you'll likely be using a bounding box (an invisible rectangle representing the collidable area of an object) collision system. If these invisible boxes overlap, a collision has occurred and must be resolved appropriately.
In a tile-based world collision it is as simple as giving each tile a "solid" attribute and a simple check before moving the player. If the tile you will be moving onto is solid, leave the player where he stands. Otherwise, move the player to that tile. Think of it like a very simple game of chess where you control a single King and every one of your enemy's pieces is a solid brick wall who's square you may not walk on.
If you decide that pixel-based movement is still a must, post back here and perhaps we can example the slightly more advanced collision methods involving bounding boxes (or perhaps google "AABB collision" or "rectangle bounding box").
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- fryz
- Chaos Rift Newbie
- Posts: 18
- Joined: Thu Jul 22, 2010 8:27 am
- Current Project: Ping Pong.
- Favorite Gaming Platforms: PC.
- Programming Language of Choice: C++, Allegro.
- Location: Lithuania
Re: Tile maps collision detection on top-down view. C++/Allegro.
Thanks for your explanation.
So basically i understand the principle of tile collision yea, but how to do it i'm not sure:
Check on what number in the map array player steps, and if it is (let's say the normal walkable ground is 0 and the wall is 1 for simplicity) 1, then just do nothing. Leave an empty if statement. And else it is 0, then the position advances by 32 pixes. Right?
Okay, now for pixel collision... Well, my next game is gonna be a snake clone, so i'm going to need the pixel collision. For this game and learning purposes, i can use the tile collision. So yea, i would like to receive some help about that kind of collision on a tile map.
So basically i understand the principle of tile collision yea, but how to do it i'm not sure:
Check on what number in the map array player steps, and if it is (let's say the normal walkable ground is 0 and the wall is 1 for simplicity) 1, then just do nothing. Leave an empty if statement. And else it is 0, then the position advances by 32 pixes. Right?
Okay, now for pixel collision... Well, my next game is gonna be a snake clone, so i'm going to need the pixel collision. For this game and learning purposes, i can use the tile collision. So yea, i would like to receive some help about that kind of collision on a tile map.
Learning Allegro. Hope i will learn it and make my ideas live. I have a lot of them!
Your help is greatly appreciated!
Your help is greatly appreciated!
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Tile maps collision detection on top-down view. C++/Allegro.
Well there's never any point to an empty if statement.fryz wrote: Check on what number in the map array player steps, and if it is (let's say the normal walkable ground is 0 and the wall is 1 for simplicity) 1, then just do nothing. Leave an empty if statement. And else it is 0, then the position advances by 32 pixes. Right?
Code: Select all
if( tileWalkingOnto.walkable == true ) {
player.Move();
}
Basic AABB collision. If the two rectangles representing the snake and the food are colliding, call the EatFood() function.fryz wrote: Okay, now for pixel collision... Well, my next game is gonna be a snake clone, so i'm going to need the pixel collision. For this game and learning purposes, i can use the tile collision. So yea, i would like to receive some help about that kind of collision on a tile map.
Psuedo-code (this will actually crash due to uninitialized variables):
Code: Select all
struct Snake {
int x;
int y;
int w;
int h;
void EatFood(){ return; }
};
struct Food {
int x;
int y;
int w;
int h;
};
//Simplest rectangle collision test.
//Provides only a boolean result (true or false).
//If you need more information (ie. which sides collided) this method will not do.
bool checkCollision(Snake *player, Food *apple) {
if((player.x < apple.x + apple.w) && (player.x + player.w > apple.x) && (player.y < apple.y + apple.h) && (player.y + player.h > apple.y)) {
return true;
}
return false;
}
int main() {
Snake *player = new Snake();
Food *apple = new Food();
while( !quit ) {
if( checkCollision(player, apple) ) {
Snake.EatFood();
quit = true;
}
}
return 0;
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- fryz
- Chaos Rift Newbie
- Posts: 18
- Joined: Thu Jul 22, 2010 8:27 am
- Current Project: Ping Pong.
- Favorite Gaming Platforms: PC.
- Programming Language of Choice: C++, Allegro.
- Location: Lithuania
Re: Tile maps collision detection on top-down view. C++/Allegro.
Okay, thanks for the explanations, very nicely done, explained a lot, give me a few nice ideas how to do other, related stuff. But now i have tried to do that thing i was talking in the last post, tile movement:
My tile size is 32px. When i write the code like this:
Well, it jumps almost trough all the screen... I have tried lowering the time counter to 5 and 10, just for testing purposes. The character's moved very edgy and still incredibly fast. Looks like i fail something on the tile movement, don't i?
Here is my logic thinking:
If the player presses a key, the guy moves a tile to the side the player pressed the button. It is by 32 pixels, since the tile size is 32px. But then again, logically if player HOLDS they key, the guy keeps moving. Also, if i slow down the movement in some way (use wait()? Or sleep(), i don't quite remmeber, need to check in the tutorial of Allegro basics, haven't used this thing.), the movement looks edgy. So to avoid this, i should do a game, which is based on this movement. OR limit the keypress like that:
Am i right somewhere?
Okay, going to try tht ball accelleration thing, you gave me tips about, rised me some ideas how to make it work.
Thanks for your answers, really helps a lot.
My tile size is 32px. When i write the code like this:
Code: Select all
if(key[KEY_DOWN])
{
player_y = player_y + 32;
keypress = 2;
}
Here is my logic thinking:
If the player presses a key, the guy moves a tile to the side the player pressed the button. It is by 32 pixels, since the tile size is 32px. But then again, logically if player HOLDS they key, the guy keeps moving. Also, if i slow down the movement in some way (use wait()? Or sleep(), i don't quite remmeber, need to check in the tutorial of Allegro basics, haven't used this thing.), the movement looks edgy. So to avoid this, i should do a game, which is based on this movement. OR limit the keypress like that:
Code: Select all
int keypress = 0;
while(!quit)
{
if(key[KEY_UP])
key_press = 1;
keypress = 0;
if(keypress == 1)
player_y = player_y - 32;
}
Okay, going to try tht ball accelleration thing, you gave me tips about, rised me some ideas how to make it work.
Thanks for your answers, really helps a lot.
Learning Allegro. Hope i will learn it and make my ideas live. I have a lot of them!
Your help is greatly appreciated!
Your help is greatly appreciated!
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Tile maps collision detection on top-down view. C++/Allegro.
In SDL there's an SDL_EnableKeyRepeat() function which can be used to set the delay between repeated keys to a higher value. I'd imagine there's something similar in Allegro, check the docs.fryz wrote: Also, if i slow down the movement in some way (use wait()? Or sleep(), i don't quite remmeber, need to check in the tutorial of Allegro basics, haven't used this thing.), the movement looks edgy. So to avoid this, i should do a game, which is based on this movement. OR limit the keypress like that:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- fryz
- Chaos Rift Newbie
- Posts: 18
- Joined: Thu Jul 22, 2010 8:27 am
- Current Project: Ping Pong.
- Favorite Gaming Platforms: PC.
- Programming Language of Choice: C++, Allegro.
- Location: Lithuania
Re: Tile maps collision detection on top-down view. C++/Allegro.
I think this might be the thing:
http://alleg.sourceforge.net/stabledocs ... board_rate
But there aren't even a single explanation or example how to use it. Tried using it like a function, might define something, but no effect, also like a variable... Anyway, going to try the collision thing, with the fast, edgy movement.
http://alleg.sourceforge.net/stabledocs ... board_rate
But there aren't even a single explanation or example how to use it. Tried using it like a function, might define something, but no effect, also like a variable... Anyway, going to try the collision thing, with the fast, edgy movement.
Learning Allegro. Hope i will learn it and make my ideas live. I have a lot of them!
Your help is greatly appreciated!
Your help is greatly appreciated!
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Tile maps collision detection on top-down view. C++/Allegro.
The function definition IS the explanation. You call the set_keyboard_rate() function with two integer parameters: the first being the delay after the initial key press to start repeating, the second being the delay between repeats. Both are in milliseconds.void set_keyboard_rate(int delay, int repeat);
BTW, that's a function reference, not proper documentation. Hence the lack of any in-depth explanation.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!