School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,444 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,522 people online right now. Registration is fast and FREE... Join Now!




2D map collision detection

 

2D map collision detection, see title

jyth

29 Jun, 2009 - 04:36 PM
Post #1

New D.I.C Head
Group Icon

Joined: 29 Jun, 2009
Posts: 23


Dream Kudos: 50
My Contributions
First post here, seems like a large community. fun fun.
Anyways, the dealio here is i'm working on this game in my free time just to kind of learn some basic stuff of how games work, collisions, graphics, what have you. I had absolutely no idea how games were actually programmed and structured a month ago and am heading into a CS major in the fall.
So, i'm working with a 2D map, essentially the same as one from a 2D zelda game. Cliffs, trees, water, most of you probobly know what I mean, and I'm having trouble getting my head around how I will do collision detection between a moving entity/sprite and the map.
As of now the basic map is just one big image, while anything that is animated or changes is to be created as an object on top of it. How would I do collisions in this context? I thought of breaking down the map into tons of sprites and constructing the map out of objects using a file for locations of things, which would make collisions easy, but I'm thinking that would be wildly inefficent.
One other idea i've had is to make a second imaginary map of rectangles constructed much like the above idea, except it would just be a layer of unshown rectangles used for collisions. Is this a good idea?

Image is an example area.

Anyways, questions/comments appreciated, even if they are to let me know i'm starting too big biggrin.gif

EDIT: forgot to mention i'm working in java, although I don't think this will really matter.

This post has been edited by jyth: 29 Jun, 2009 - 04:40 PM


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post


bobjob

RE: 2D Map Collision Detection

29 Jun, 2009 - 04:45 PM
Post #2

D.I.C Head
**

Joined: 29 Mar, 2008
Posts: 115



Thanked: 8 times
My Contributions
I also tend to program in Java.

if the map is top down. you can make a 2 dimension array of blocks. and each block contains a boolean state (to show if its occupied). So when the character/sprite moves out of the block it occupies: it will check to see if the next block is occupied before entering. And if it enters then it will un-occupy its last block position.


If you are doing a platform game. I find the best way is to do line collision tests.
User is offlineProfile CardPM
+Quote Post

bobjob

RE: 2D Map Collision Detection

29 Jun, 2009 - 04:51 PM
Post #3

D.I.C Head
**

Joined: 29 Mar, 2008
Posts: 115



Thanked: 8 times
My Contributions
If you want you could always have the two dimension block array independant of graphics and set each block manually.

But I personally think its best to have images grouped with the blocks (TileMap).
If you decide to use a TileMap Layout, You can save alot of space, as all images will be relatively small and re-usable.
Also you can have images with alpha channels for trees/rocks/sign_post/characters on top of the tile, stored in the same tile block object.

if you want a Java example of an efficient Top View Editor Check out:
Tile Map Editor

You could even use this to edit your own custom maps for your game.


This post has been edited by bobjob: 29 Jun, 2009 - 08:03 PM
User is offlineProfile CardPM
+Quote Post

BetaWar

RE: 2D Map Collision Detection

29 Jun, 2009 - 05:00 PM
Post #4

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
QUOTE
How would I do collisions in this context? I thought of breaking down the map into tons of sprites and constructing the map out of objects using a file for locations of things, which would make collisions easy, but I'm thinking that would be wildly inefficent.

I have no idea about effeciency here, but I would think you are correct in it being innefficient. One other thing you could do is create another image which has only 2 colors on it, blak and white and place black squares over everything you don't want to allow movement on. For instace, a collision map for your current image may look like so:
Attached Image

Then you ignore all the white, and use the black as a collision zone.

HTH
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: 2D Map Collision Detection

29 Jun, 2009 - 11:11 PM
Post #5

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
How are you actually drawing the map? Did you make one large bitmap and you are scrolling that or did you make a tile engine? If you are using a tile engine, the normal way to do this is with a collisoin layer. Like what bobjob was saying. Tile engines are typically far more efficient than just loading and scrolling one large bitmap because the bitmap takes up huge amounts of memory. You can design your tile engine so that it only draws the visible part of the screen, plus a few extra tiles around the edges to keep it from disappearing off the screen. As I said, using tiles reduces the amount of memory needed for the map as if you have one large field of grass you can use 1 tile to make up the field of grass. You can also add what chuckb refrences as 'splatter tiles' which would break up the monotany of a large field of grass. A 'splatter tile' is a tile that would be mostly trasparent that would add to the image.

Typically tile maps are made up of 2D arrays of integers where each integer represents a tile. You could make a class to represent a tile though. In the class, you would have the tile and other information about the tile. Such as the player can't pass through it. The player takes damage passing through. The player walks more slowly. The sky is the limit here.

There are even isomertic tile engines. Games like Civilization, Age of Empires, Star Craft, Warcraft(not WoW mind you) all use isometric tile engines. Isometric tile engines give the illusion of depth.
User is offlineProfile CardPM
+Quote Post

Fib

RE: 2D Map Collision Detection

30 Jun, 2009 - 06:50 AM
Post #6

D.I.C Head
**

Joined: 12 Mar, 2009
Posts: 71



Thanked: 6 times
My Contributions
Hi Jyth,

Did you create that map yourself? If you did then with what software? I'm just curious because it is a very well drawn background. I usually have trouble creating backgrounds/sprites because I don't have much artistic talent.

If you used a certain kind of software then maybe I can try it out!

biggrin.gif
User is offlineProfile CardPM
+Quote Post

jyth

RE: 2D Map Collision Detection

30 Jun, 2009 - 03:07 PM
Post #7

New D.I.C Head
Group Icon

Joined: 29 Jun, 2009
Posts: 23


Dream Kudos: 50
My Contributions
Some interesting suggestions. Wouldn't the tile map have to be 800x600 because Im thinking an object can have a (x, y) position in any pixel. Maybe that is unneeded detail though. I'll look into the isometric tile system suggested, although i'm not looking for 3D.

QUOTE(Fib @ 30 Jun, 2009 - 06:50 AM) *

Hi Jyth,

Did you create that map yourself? If you did then with what software? I'm just curious because it is a very well drawn background. I usually have trouble creating backgrounds/sprites because I don't have much artistic talent.

If you used a certain kind of software then maybe I can try it out!

biggrin.gif


As to your question, GOD NO! I wish I could =P
It's an image of an area in LoZ: Minish Cap cool.gif
Essentially i'm looking for the way nintendo may have handled the map in the game.
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: 2D Map Collision Detection

30 Jun, 2009 - 08:03 PM
Post #8

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
QUOTE

As to your question, GOD NO! I wish I could =P
It's an image of an area in LoZ: Minish Cap cool.gif
Essentially i'm looking for the way nintendo may have handled the map in the game.


Like I and others said, Nintendo would definitely have done that using a tile engine. Try a google search for Java tile engines as I think I remember you saying you were doing this with Java. There are plenty of source code examples around in other languages so I'm sure you will find one in Java.

User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: 2D Map Collision Detection

1 Jul, 2009 - 06:32 AM
Post #9

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,379



Thanked: 53 times
Dream Kudos: 300
My Contributions
SixofEleven is right they would have used a tile engine, a map like that could be done using a simple solid or not solid tile structure. Adding depth could be achieved by the order you draw objects in, for example when you walk under a tree, you can draw the tree after you draw your player.

I wrote a basic tile engine that used a pair of ints to signify a tile, like this

CODE


// a 5x5 tile map
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1


The first number is the bool to check whether a tile is solid or not and the second is for the graphic to draw.

Something like this might suit your needs quite well icon_up.gif

This post has been edited by stayscrisp: 1 Jul, 2009 - 06:33 AM
User is offlineProfile CardPM
+Quote Post

jyth

RE: 2D Map Collision Detection

1 Jul, 2009 - 01:59 PM
Post #10

New D.I.C Head
Group Icon

Joined: 29 Jun, 2009
Posts: 23


Dream Kudos: 50
My Contributions
QUOTE(stayscrisp @ 1 Jul, 2009 - 06:32 AM) *

SixofEleven is right they would have used a tile engine, a map like that could be done using a simple solid or not solid tile structure. Adding depth could be achieved by the order you draw objects in, for example when you walk under a tree, you can draw the tree after you draw your player.

I wrote a basic tile engine that used a pair of ints to signify a tile, like this

CODE


// a 5x5 tile map
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1


The first number is the bool to check whether a tile is solid or not and the second is for the graphic to draw.

Something like this might suit your needs quite well icon_up.gif


Would it be inefficent to make a class for a tile, as i can think of some more booleans that would have to be made, swim/walk, new area entrance, i dunno.

Thanks for all the tips and ideas so far.

User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: 2D Map Collision Detection

1 Jul, 2009 - 02:22 PM
Post #11

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,379



Thanked: 53 times
Dream Kudos: 300
My Contributions
No I wouldn't think that to be inefficient, in fact thats exactly what I did. You could also create a struct to hold your tiles in your map class.

I created an enumeration to hold types of tiles

CODE

TILE_TYPE_SOLID = 0,
TILE_TYPE_PASSABLE,
TILE_TYPE_SLIPPY,
TILE_TYPE_WATER,
TILE_TYPE_EXIT


Then when you move your character and check for whether a tile is valid or not

CODE

bool GameObject::ValidateTile(Tile* Tile)
{
   if(Tile == NULL){
     return false;
   }
   if(Tile->TypeID == TILE_TYPE_SOLID)
   {
      return false;
   }

   if(Tile->TypeID == TILE_TYPE_WATER)
   {
         player->swimming = true;
         return true;
   }
   return false;
}


This is just an example but I hope you get my meaning.


User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: 2D Map Collision Detection

1 Jul, 2009 - 02:24 PM
Post #12

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
QUOTE(jyth @ 1 Jul, 2009 - 03:59 PM) *

QUOTE(stayscrisp @ 1 Jul, 2009 - 06:32 AM) *

SixofEleven is right they would have used a tile engine, a map like that could be done using a simple solid or not solid tile structure. Adding depth could be achieved by the order you draw objects in, for example when you walk under a tree, you can draw the tree after you draw your player.

I wrote a basic tile engine that used a pair of ints to signify a tile, like this

CODE


// a 5x5 tile map
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1


The first number is the bool to check whether a tile is solid or not and the second is for the graphic to draw.

Something like this might suit your needs quite well icon_up.gif


Would it be inefficent to make a class for a tile, as i can think of some more booleans that would have to be made, swim/walk, new area entrance, i dunno.

Thanks for all the tips and ideas so far.


No, making a class for the tiles would not be inefficient. It would actually be a good idea. The efficiency is in the rendering process. That is where your program will be bogged down the most. Transferring data to the screen will probably be your biggest bottle neck for a 2D game. You can get around this by only drawing a portion of the map or splitting the map into smaller sections. You could use a portal system for splitting the map. When the player reaches a portal, you could switch to a new map. For the other part, you can try and only draw what part of the map the player is on and one or two tiles around it to keep the map from scrolling off the screen.
User is offlineProfile CardPM
+Quote Post

mono15591

RE: 2D Map Collision Detection

10 Jul, 2009 - 10:20 PM
Post #13

D.I.C Regular
***

Joined: 5 Nov, 2008
Posts: 350



Thanked: 11 times
My Contributions
QUOTE(SixOfEleven @ 1 Jul, 2009 - 02:24 PM) *

No, making a class for the tiles would not be inefficient. It would actually be a good idea. The efficiency is in the rendering process. That is where your program will be bogged down the most. Transferring data to the screen will probably be your biggest bottle neck for a 2D game. You can get around this by only drawing a portion of the map or splitting the map into smaller sections. You could use a portal system for splitting the map. When the player reaches a portal, you could switch to a new map. For the other part, you can try and only draw what part of the map the player is on and one or two tiles around it to keep the map from scrolling off the screen.


For loading the map couldn't you just have the player always be in the middle of the screen and just have it draw tile so far out from the center of the screen. I've never made a 2d game so I don't know how efficient this actually is.

XXXXXXXXXXX
XXXXXXXXXXX
XXXXXOXXXXX
XXXXXXXXXXX
XXXXXXXXXXX<- like that.

just have the surroundings drawn and when he moves it erases the stuff off screen and draws the new things coming onto screen. I would say make the area where things are drawn to go a tile or two off screen so you don't see anything when the map is loading. idk my idea

This post has been edited by mono15591: 10 Jul, 2009 - 10:25 PM
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: 2D Map Collision Detection

11 Jul, 2009 - 02:53 AM
Post #14

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,379



Thanked: 53 times
Dream Kudos: 300
My Contributions
Well that is basically what you would do if you where to draw a large map, you would have to write a camera that would follow you and keep you centered on the screen.

This would only work on an RPG exploration type of game, I found this out when making a platform/side scroller, remaining center meant if you jumped the whole map followed you, it looked terrible. laugh.gif
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: 2D Map Collision Detection

11 Jul, 2009 - 08:18 AM
Post #15

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
QUOTE(mono15591 @ 11 Jul, 2009 - 12:20 AM) *

For loading the map couldn't you just have the player always be in the middle of the screen and just have it draw tile so far out from the center of the screen. I've never made a 2d game so I don't know how efficient this actually is.

XXXXXXXXXXX
XXXXXXXXXXX
XXXXXOXXXXX
XXXXXXXXXXX
XXXXXXXXXXX<- like that.

just have the surroundings drawn and when he moves it erases the stuff off screen and draws the new things coming onto screen. I would say make the area where things are drawn to go a tile or two off screen so you don't see anything when the map is loading. idk my idea


Well, like staycrisp said, that doesn't always work so well in all cases. You can pad the map so the player will never be able to walk off the side of the map, using tiles that the player can't walk through. It was done like in several games. Using the camera to control scrolling it will also work for other types of tiling, like isometric where the map is a diamond, not a square, like in the first Age of Empires. Also, if you wanted to have the player be able to scroll the map using the mouse by having the mouse at the edge of the screen, like in popular RTS games, centering the map on the player wouldn't work all that well.
User is offlineProfile CardPM
+Quote Post

LostPunisher

RE: 2D Map Collision Detection

30 Jul, 2009 - 09:30 AM
Post #16

D.I.C Head
Group Icon

Joined: 19 Feb, 2008
Posts: 57


Dream Kudos: 50
My Contributions
Oh yeah, the Tile maps are boss.
User is offlineProfile CardPM
+Quote Post

SwiftStriker00

RE: 2D Map Collision Detection

31 Jul, 2009 - 06:54 AM
Post #17

D.I.C Regular
Group Icon

Joined: 25 Dec, 2008
Posts: 295



Thanked: 18 times
Dream Kudos: 125
My Contributions
Well if you were going to make something similar to Zelda where the screen is stationary. It would be fairly easy to load. because you could define a ScreenObj that would hold a 10x10 of TileObjects. Just make sure you have exitScreen() and exitTile() methods, in order to handle when the screen should change.

If you wanted to keep your player in the center all the time. have a FocusTile that holds a reference to whatever the current tile your player is on. then you can have a method DrawSurroundings( Tile focus ), which will draw a 10x10 around the focus tile's current cordinates. I like doing this because if you implement a scrolling feature with mouse near the edge you can unlock the FocusTile from your character, and for every 1/2 second the mouse is on a particular edge, you can move the focus tile 1 unit in that direction, and then call the DrawSurroundings( Tile focus ) again. Then have something like space-bar set the FocusTile equal to the characters current tile ( and call the draw again).
User is offlineProfile CardPM
+Quote Post

NeoTifa

RE: 2D Map Collision Detection

31 Jul, 2009 - 08:03 AM
Post #18

Yay caek! ZOMG!!!
Group Icon

Joined: 24 Sep, 2008
Posts: 6,292



Thanked: 79 times
Dream Kudos: 150
My Contributions
.... I'm hijacking this for a second. What language did you say you were doing this in again? Java? How exactly would you split the tilesheet into individual tiles? I have a theory, but I wanna know what you guys did.
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: 2D Map Collision Detection

31 Jul, 2009 - 03:31 PM
Post #19

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,379



Thanked: 53 times
Dream Kudos: 300
My Contributions
QUOTE(NeoTifa @ 31 Jul, 2009 - 08:03 AM) *

.... I'm hijacking this for a second. What language did you say you were doing this in again? Java? How exactly would you split the tilesheet into individual tiles? I have a theory, but I wanna know what you guys did.


What do you mean? How do you differentiate between which tiles to draw?


User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: 2D Map Collision Detection

31 Jul, 2009 - 03:44 PM
Post #20

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
@NeoTifa

I don't know how graphics work in Java but for my tile sets in C#, I have a List(basically an array that I don't need to know the length of) of source rectangles for the tiles in the tile set. When I want to draw a tile I can specify the source rectangle in the tile set and just draw that portion of the tile set.

Say I had a tileset with four tiles horizontally each tile 128*128 pixels. The rectangles would look like this: (X, Y, Width, Height):

(0, 0, 128, 128) (128, 0, 128, 128) (256, 0, 128, 128) (384, 0, 128, 128)

Then to select the tile I would use it's index and find the source rectangle from the above.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:36AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month