Jump to content
  • Advertisement
Sign in to follow this  
Dospro

Solid tiles

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Im going to start a new plataform game. But before i can start doing it i need to know whats the best technich to make solid tiles, i mean, tiles where you can step on, or better said, "walls". I have recentrly made a pacman which uses an array as a map an all the collition detections ware based in this array. Is there a better way? Please help me with this

Share this post


Link to post
Share on other sites
Advertisement
You've got the right idea with the array.
In tile based games the array that determines collisions is called a "Tile Map".

There might be better ways, but I don't know of them. This is certainly the fastest at least.

Share this post


Link to post
Share on other sites
Ok, yea, but dont you think that if i plan to make a map as big as the megaman 1-5+ games or like mario bros 1 and 3 it would be really HUGE arrays i mean an array that can iven use 1Mb of memory or more. What do you think or what are other ideas, becuase in pacman i used a 30*30 bytes map and in the screen i have to amplify it 16 times to make it look big enough to play(480*480).

Now, as concecuence of this amplify, the colision detection is not perfect, it works well, but sometimes it can get through walls, so thats why i need something better.

Share this post


Link to post
Share on other sites
Well, another way (I don't know if it is used or not...) could be using objects as the walls, and then checking collision agains any object type "wall", that way, if there is a collision you would make you're player/s stop walking...

I don't know..., just an idea....

Good luck!

Haora

Share this post


Link to post
Share on other sites
Oldschool games used tilemaps.
Take mario for example. The tiles are 32x32px. Level 1-2 is 192x15 tiles.
192 x 15 x 1 byte = 2880 bytes. 2.88k for a level of that size is very small.

Now, the NES only had 2k of ram- so they probably didn't load the entire map at once. However, nowadays, even if your levels are 3x the size of Mario 1's, I'm sure you'll have plenty of space leftover.

I don't quite know what you're talking about when you say that you "amplified" your map. You don't need to change the size of your tilemap when you rescale things. If this is the case, then maybe you're a bit fuzzy about the concept of a tilemap?

Here's a rough example:

Map = 10X5 tiles. Tiles = 50x50px. 0 = Open tile(draw sky), 1 = Closed tile (draw block)
0000000000
0001000000
0001000000
0001111110
1111111111

Given this, the lower left corner of the map is Coordinate (0, 0)
The upper right corner of the map is (500, 250) - 10*50, 5*50
Upper left corner: (0, 250)
Lower right corner: (500, 0)


class Game{


byte[][] tilemap = new byte[10][5];
byte[][] world = new byte[10 * TILE_WIDTH][5 * TILE_HEIGHT];

...

boolean canPlayerMoveHere(int desiredX, int desiredY){
int gridX = desiredX / TILE_WIDTH; // TILE_WIDTH = 50px
int gridY = desiredY / TILE_HEIGHT; // TILE_HEIGHT = 50px

//NOTE: Check that the array index is in bounds!

if(tilemap[gridX][gridY] != 1){ // 1 = Block
return true;
else
return false;
}
}

void render(){
for(int y = 0; y < 5; y++)
for(int x = 0; x < 10; x++)
drawTile(tilemap[x][y]);

drawPlayer(playerX, playerY);
}
}

Share this post


Link to post
Share on other sites
Well, i see i was in the correct way.
By saying amplify i mean the drawed map, not the array, i mean an array of 30*30 where each numer is a pixel is really small, so i scale it 16 times so it could fit perfectly in the 640*580 resolution im using.

Anyway, thanks for the help, i was searching for something better than map tiles but i think this is the best method, then the only thing ill have to search for is scrolling, i still dont have a good concept of how to scroll.

I mean with this array, how can i scroll it right, left, down, up.

Share this post


Link to post
Share on other sites
To expand just a little on what Funkapotamus said:

You ussually want to create a struct or class for for tiles, rather than just a standard type (byte, int, etc). This way you can not only store whether the tile is 'solid', but also any other information that might be helpfull: texture, switches, properties (a mud tile might slow u down), and anything else you can think of.

Tile maps are one of the best 2D level architectures out there! Really easy to use, and relativly small.


class CTile
{
private:
// Is the tile solid
bool bSolid;

// ID for its texture
int iTextureID;

// ...And anything else u can think of

// ...You'll also want to add methods like render( ), and checkCollision( )
}

// Elsewhere...

CTile tileMap[MAPSIZE_X][MAPSIZE_Y];



Best of luck!
Matt Hughson

Share this post


Link to post
Share on other sites
Quote:
Original post by Dospro
...the only thing ill have to search for is scrolling, i still dont have a good concept of how to scroll.

I mean with this array, how can i scroll it right, left, down, up.


It's actually not to hard. But first we need to talk about how you render the tilemap normally. Say you had a 10 tile by 10 tile map. And each tile is 16x16 pixels.

To render a tile you would simple multiply its index in the array by the dimensions of the tiles. For example:

Tile index 0,0 would be rendered at 0,0 since 0 * 16 = 0

Tile index 1,1 would be rendered at 16,16 since 1 * 16 = 0

Tile index 8, 3 would be rendered at 128,48 since 8 * 16 = 128 and 3 * 16 = 48

// Formula
Tile index x, y would be rendered at x*tileWidth, y*tileHeight

So back to the question at hand. To scroll we simply add an offset to those calculated positions. I find it's best to visuallize it as a camera. So you store this camera position in a variable (or better yet a class so you can add to it later on with stuff like shakey cam, better movement code, etc).

// Start the camera at 0, 0
float fCameraPosition[2] = { 0, 0 };

Now when you want to scroll left you just move the camera:

fCameraPosition[0] -= 1.0f; // Left
fCameraPosition[0] += 1.0f; // Right
fCameraPosition[1] -= 1.0f; // Down
fCameraPosition[1] += 1.0f; // Up

And finally we update that formula from earlier to reflect the addition of a camera:

// Formula
Tile index x, y would be rendered at x*tileWidth-fCameraPosition[0], y*tileHeight-fCameraPosition[1]

The reason we subtract the camera position is because if the camera moves forward, visually the world should seem to move backwards.

Good luck!
Matt Hughson

Share this post


Link to post
Share on other sites
MMM vry very interesting.
Actually i had wroked with tiles in the past, but never do something like scroll, and analizing the formula, you say this will only work in painting so if you want to scroll the map, i mean that you can move in where we are readyng from the map.

Its kinda weird but i think i more less have the idea, and by the way, when i move the main character, i have a little doubt.

Lets say the character is at (0, 320) which is in the middle of the left "wall" when i move left, first i make the sprite(main character move) until i reach the center, after reaching it, what? i stop the sprite moving and start moving the background instead(scrolling)? and in the same case when we have no more background at the right to scroll we let the sprite got to the right wall?

I think it must be like that, but i prefer to ask.

Share this post


Link to post
Share on other sites
Typically your camera will have a bounding box to which it will stop scrolling. It should coincide with the edges of your map.

As far as moving the camera- you're right in how it should be done.

As far as when things start scrolling, that's up to you. You could center the scene around the player, or give the player a few tiles left/right off center to which the camera will begin chasing the player.

Just to give an idea: in Mario games, this "leeway" area was about 1/3rd the side of the screen. In Sonic games, it mostly remained in the center unless the character was moving fast- at which point the "leeway" area would get larger depending on how fast you were going.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!