Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (3 posts)

2d worms style game - How to make sprite move on terrain

Last post 3/7/2010 11:40 AM by maurilio di cicco. 2 replies.
  • 3/1/2010 7:33 PM

    2d worms style game - How to make sprite move on terrain

    Hi,

    I've started to make a worms style game with destroyable terrain.
    I have one problem, I aint got a clue on how to make the sprite follow the shape of the terrain. I can move the sprite left / right but i need to make it go up and down depending on the terrain.

    I create a random heightmap for the terrain but I don’t know how to make the sprite stick to the ground.

    I have looked on the xna forum and used google to find an answer but no such tutorial exists.

    Any help would be much appreciated.

    Thanks
    Shazan

  • 3/1/2010 9:16 PM In reply to

    Re: 2d worms style game - How to make sprite move on terrain

    Here's one possible way to do this:

    For the sake of the example, assume that your background terrain texture is a full screen 720p texture (1280 x 720) and has only 2 colors: Blue for sky and brown for ground, in a side view. You want your sprite to "walk" on the ground. So what you do is get the color data of the texture into an array of Color[]:

    Declare and initialize an array member in your class:
        Color[] MyTextureColor = new Color[1280 * 720];

    Then after loading the texture, copy its data into the array:
        MyTexture.GetData(MyTextureColor)

    You can now check the color of every single pixel by looking into this array: Color at pos x,y is = MyTextureColor[x + y * 1280]. So you can program a constraint in such a way, that your sprite is only allowed to move to a position where the color at pos x,y is blue (=sky). If the color is brown (=ground) the sprite is not allowed to move there.

    To "destroy" some terrain, modify the values in the MyTextureColor array, i.e. set the brown color at those pixel positions which are being blown up to become blue color instead. After changing all the pixels that are affected by the explosion, write this change back into the texture:
        MyTexture.SetData<Color>(MyTextureColor);

    Of course this is very much oversimplified, but it should give you something to start off from...

    Doc
  • 3/7/2010 11:40 AM In reply to

    Re: 2d worms style game - How to make sprite move on terrain

    it's really a nice example!!
    Is there an "advanced" method to get the same result or the logic is always the same? (modify the texture data) 


Page 1 of 1 (3 posts) Previous Discussion Next Discussion