Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've hit a little snag in my game. It's a 2D platform game and I need to check for collisions against the level. I'm using GLEED2D to create the map.

I'd like to do per-pixel collision checking, but I don't know how to get the texture color values out of the Level.cs file provided by GLEED2D.

If any one can point me in the right direction that be great -- or if they're are any better level mapping programs with similar features as GLEED2D that would be better to use for this type of game, that would be helpful as well.

share|improve this question
    
FYI, Gleed2D is now hosted on GitHub: github.com/SteveDunn/Gleed2D –  John McDonald Mar 21 '12 at 17:14
add comment

1 Answer 1

up vote 1 down vote accepted

Step 1 - Making the Textures accessible from Level.cs

The texture for each item is being stored inside the TextureItem class inside Level.cs. You should be able find something like this on your file:

public partial class TextureItem : Item
{
    Texture2D texture;

    public override void load(ContentManager cm)
    {
        // Your load implementation
    }
}

Now, start by making it public:

public partial class TextureItem : Item
{
    public Texture2D Texture;

    public override void load(ContentManager cm)
    {
        // Your load implementation
    }
}

And then it's simply a matter of acessing the texture directly. Example:

Level level = Level.FromFile("map.xml", Content);

foreach(Layer layer in level.Layers)
{
    foreach(Item item in layer.Items)
    {
        ImageItem imageItem = item as ImageItem;
        if(imageItem != null)
        {
            Texture2D texture = imageItem.Texture;
            // Do something with texture, such as per pixel collision detection
        }
    }
}

Step 2 - Handling per-pixel collision detection in XNA

In case you're not familiar with this process, here's two good references on the topic:

  1. Collision Series 2: 2D Per-Pixel Collision to learn how to implement the basics in XNA. The core feature needed is pretty much the Texture.GetData() method which allows you to get back pixel color information from any texture.
  2. Sonic Physics Guide which describes physics in Sonic games. They also used per-pixel collision detection with the terrain so you might be able to get some tips out of it.
share|improve this answer
    
it is already set to public under TextureItem : Item. any other ideas ? –  Dusty Jan 15 '12 at 20:50
    
It's not set to public on the copy that you download from the GLEED2D website (I checked), but... so? If it's already set to public, just use it. I demonstrated how in my answer... –  David Gouveia Jan 15 '12 at 20:53
    
the problem that's occurring is that I already set it to public, when I want to use it, I get 'GLEED2D.Item' does not contain a definition for 'texture' and no extension method 'texture' accepting a first argument of type 'GLEED2D.Item' could be found –  Dusty Jan 15 '12 at 23:10
1  
And the compiler is right because there is really no texture member on the Item class. It only exists on the ImageItem class. So you need to downcast your object to ImageItem, just like I showed you in my example: ImageItem imageItem = item as ImageItem; if(imageItem != null) { Do something with imageItem.texture } –  David Gouveia Jan 15 '12 at 23:39
    
thanks, I'm still a bit new to this type of programming –  Dusty Jan 16 '12 at 21:31
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.