Mix some C#, GLEED2D and Farseer Physics and the desire for a simple game sample...

Monday we talked up a Physics Helper that is based on Farseer Physics, Getting physical Windows 8 Modern UI XAML with the Physics Helper XAML. In another recent post we highlighted GLEED2D, What we need is an updated GLEE, No... not that Glee, instead a GLEED2D for XNA 4. (That's besides all the other times we've mentioned Farseer)

So it kind of seems fitting to highlight a Game Sample that mashes all this up...

Farseer Physics 3.3.1 and Gleed2d XNA 4.0 Game Sample

Farseer Physics Engine 3.3.1 and Gleed2d for XNA 4.0 combined into playable VS10 solution

Motivation for this project was to create a platformer game on XNA 4.0 using Farseer Physics and Gleed2d level editor. Problem was that tutorials for using Farseer are usually for version 3.2. Also most of the tutorials for using the Gleed2d were outdated, since there's new version for XNA4.0. So I spent some time researching the Farseer 3.3.1 Game Samples and Gleed2d forum discussions to get a grip how to implement this on newest versions.

In a sense this is not actual game but only a skeleton and sample from which you can try and see how to get these two technologies work. There's a movable character for testing if the tileblocks and stuff really are where they should be.

To fully use this sample (create your own levels that are better than supplemental testlevel.gleed) you also need to download Gleed2d for XNA 4.0

Documentation

Fully functionable solution that has MyLevel.cs that can import Gleed2d level. Tile.cs that can create rectangle body and PathTile.cs that creates a path from gleed information. It also has an animated player character to show how the level physics work. Character can run and jump using arrow keys and spacebar, respectively. Character has some problem with movement: when you jump or drop down it begins to stutter like a car with broken wheels.

The character was made with this code. The original character code works fine but when I added it to my solution, something went wrong. But it actually isn't the most important thing of this project.

Some things you have to remember:

This project comes with a leveltest.gleed level, that might or might not work out of the box, if not, then you should make a new one with Gleed2d and add it to project.

Gleed2d saves the texture paths like "content/something/image.jpg" and as you know, passing this to content.load() methods doesn't work. That's why in MyLevel.cs LoadTiles() ...

Here's game sample Solution;

image

And some snaps of it running;

SNAGHTML5f3fc13

As you can see, it's a simple example, but again, as the author noted, that's the intent, to be a simple example of using GLEED and Farseer.

Here's a snip of code that does the level and tile loading;

/// <summary>
   /// Creates a new level, sets up Farseer world object and loads tiles
   /// </summary>
   /// <param name="serviceProvider"></param>
   public MyLevel(ContentManager serviceProvider)
   {
       content = serviceProvider;

       if (world == null)
       {
           world = new World(new Vector2(0f, 80f));
       }
       else
       {
           world.Clear();
       }

       LoadTiles();
   }

   /// <summary>
   /// Tile loading method, reads gleed2d xml-file and sorts out paths and textures
   /// </summary>
   protected void LoadTiles()
   {
       tex = content.Load<Texture2D>("Tiles/blockHat");

       using (Stream stream = TitleContainer.OpenStream("Content/Levels/leveltest.gleed")) //#1 change gere you level file path
       {
           XElement xml = XElement.Load(stream);
           level = LevelLoader.Load(xml);
       }

       tiles = new List<Tile>();
       pathTiles = new List<PathTile>();
       textures = new List<MyTexture>();

       foreach (Layer layer in level.Layers)
       {
           if (layer.Properties.Name.Equals("Collision")) //2# change here your layer's name
           {
               foreach (LayerItem item in layer.Items)
               {
                   if (item.Properties is PathItemProperties)
                   {
                       PathItemProperties pathProperties = item.Properties as PathItemProperties;
                       pathTile = new PathTile(pathProperties.LocalPoints, pathProperties.Position, world);
                       pathTiles.Add(pathTile);
                   }
                   if (item.Properties is RectangleItemProperties)
                   {
                       RectangleItemProperties pathProperties = item.Properties as RectangleItemProperties;
                       tile = new Tile(TileCollision.Impassable, pathProperties.Width, pathProperties.Height, pathProperties.Position, pathProperties.Rotation, world);
                       tiles.Add(tile);
                   }
               }
           }

           if (layer.Properties.Name.Equals("Textures"))
           {
               foreach (LayerItem item in layer.Items)
               {
                   if (item.Properties is TextureItemProperties)
                   {
                       TextureItemProperties textureProperties = item.Properties as TextureItemProperties;
                       string filename = "Tiles/" + Path.GetFileNameWithoutExtension(textureProperties.TexturePathRelativeToContentRoot); //3# change here your tile textures' file path
                       Texture2D texture = content.Load<Texture2D>(filename);
                       myTexture = new MyTexture(texture, textureProperties.Position, textureProperties.Rotation, textureProperties.Scale);
                       textures.Add(myTexture);
                   }
               }
           }
       }

       hero = new Hero(this, new Vector2(70, 0), 20f);
   }

So if you've seen GLEED and wondered how it could be used in a game, want to see a simple game sample that uses Farseer or just looking for something easily approachable, here you go...

Tags:

Follow the Discussion

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.