4
\$\begingroup\$

I'm using C# and Visual Studios 2010 with XNA 4.0+lua, and my teacher has asked us to make a 2D RPG game similar to zelda(top down). I was aiming to use just one image for each map region, instead of using the tile approach, however i'm stuck on the aspect of movement limitation.

My teacher mentioned being able to mask another image on top of the map's current image, using only back and white colors and using those to be able control where the avatar can move(move where black is and restrict movement where white is). He did not, however, instruct us on how to do this, and I have been unable to find any tutorials anywhere.

I'm fairly new to C# in general, so if anyone can point me in the direction of a tutorial that could help me accomplish this, or add their own advice I would really, greatly appreciate it.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You can use the Texture2D's GetData method to access the colour of a pixel on a texture: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.texture2d.getdata.aspx

So... using your method, you would load 2 textures, one that contains the colour image you want, and a matching black and white (2 colours, not grey-scale) texture that will be your "collision map" for each region. You can use the GetData method to determine whether a particular (x,y) location is black or white (walkable or not) on the collision map.

You will want to test one pixel on each edge of your character. Good choices for these test pixels would be the centres of each edge, or the corners. Depends on how you want it to look and feel. Testing these 4 pixels every time you move should tell you if you will be walking into a wall. This same set of tests will need to happen for any projectiles or bad guys you create.

One problem you may come across is called "Tunnelling". Tunnelling occurs when an object is moving fast enough to "tunnel" through a wall without noticing the wall. To prevent this, make sure your walls are never really thin, and avoid fast-moving objects. There are ways to properly handle this, but they are more complex and unnecessary for a project of this magnitude. You can research more about tunnelling if you come across this problem, or want to learn more about how to prevent it properly.

\$\endgroup\$
5
  • \$\begingroup\$ Awesome, I'll play around with it. It's nothing too complex i'm trying to do, no tunnles or rooms within the map. More-less just avoiding random room obstacles like boxes, etc. I'll definitely give it a shot though. One question I did have, was when I load my collision map, I don't draw it, correct, I just load it and use it to feed to GetData method to determine where to walk, correct? \$\endgroup\$
    – John
    Commented Sep 2, 2011 at 4:24
  • \$\begingroup\$ I wouldn't think you'd need to load it in as a texture. Any kind of 2D array data structure will work just fine (2D array of bools). Just set up a few loops to look at all the pixels, call getpixel on each, store true for black, false for white or whatever makes sense to you. \$\endgroup\$
    – House
    Commented Sep 2, 2011 at 6:49
  • \$\begingroup\$ @Byte56: Using a graphic does make sense for the purpose of actually generating the data though. You can just overlay one over the top in your graphics program of choice and work with that visually. Whether you use a texture in memory though, or load the texture, then translate that to the bool array though is something else to decide. \$\endgroup\$ Commented Sep 2, 2011 at 13:06
  • 1
    \$\begingroup\$ Yes, good point. It would be easier to debug if you had a visual representation of the "solid map". I was just thinking about how slow the GetData function is, trying to optimize too soon! \$\endgroup\$
    – House
    Commented Sep 2, 2011 at 14:40
  • \$\begingroup\$ @John: Correct, you do not draw the black and white image, you only use GetData on this image to determine where you can walk. These other guys seem to think it's best to convert the image to some other data-structure, it will work, but I think it's easiest to use the image directly. Also, my paragraph on "Tunnelling" has nothing to do tunnels in your map, make sure to read it over again and make sure you understand what I'm trying to get at. It's fairly easy to avoid, and it will help you out in the long run. \$\endgroup\$ Commented Sep 2, 2011 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .