Texture too complex for TextureConverter?

Topics: Developer Forum
Jan 18, 2012 at 11:56 PM
Edited Jan 19, 2012 at 12:02 AM

EDIT: I'm using Farseer 3.3.1.

Hey guys, I'm trying to use the texture converter to create my level with Farseer.

The texture: http://dl.dropbox.com/u/19198187/collisionbgleft.zip

The code:

 

private void InitLevelPolygon(Game game)
        {
            _tex_collisionbgleft = game.Content.Load<Texture2D>("Textures/collisionbgleft");
            _tex_collisionbgright = game.Content.Load<Texture2D>("Textures/collisionbgright");

            uint[] _bgdataleft = new uint[_tex_collisionbgleft.Width * _tex_collisionbgleft.Height];
            _tex_collisionbgleft.GetData(_bgdataleft);
            uint[] _bgdataright = new uint[_tex_collisionbgright.Width * _tex_collisionbgright.Height];
            _tex_collisionbgright.GetData(_bgdataright);

            List<Vertices> _collisionvertsleft = PolygonTools.CreatePolygon(_bgdataleft, _tex_collisionbgleft.Width, 1.0f, 1, true, true);
            List<Vertices> _collisionvertsright = PolygonTools.CreatePolygon(_bgdataright, _tex_collisionbgright.Width, 1.0f, 1, true, true);

            foreach (Vertices verts in _collisionvertsleft)
            {
                Body body = BodyFactory.CreateCompoundPolygon(_world, BayazitDecomposer.ConvexPartition(verts), 1.0f);
                body.BodyType = BodyType.Static;
            }

            foreach (Vertices verts in _collisionvertsright)
            {
                Body body = BodyFactory.CreateCompoundPolygon(_world, BayazitDecomposer.ConvexPartition(verts), 1.0f);
                body.SetTransform(new Vector2(_tex_collisionbgleft.Width, 0.0f), 0.0f);
                body.BodyType = BodyType.Static;
            }
        }

 

The problem I run into is if I try to scale the texture as the examples demonstrate, collision just doesn't work, period, and if I don't scale it, I get really weird and wonky results (the collision is totally not accurate at all). Is this texture simply too complex for the texture converter to accurately create a polygon out of, or am I doing something wrong? For reference, here is the code I'm using for the player:

 

private void InitPlayer(Game game)
        {
            Texture2D _tex_player = game.Content.Load<Texture2D>("Textures/player");

            uint[] _playertexdata = new uint[_tex_player.Width * _tex_player.Height];
            _tex_player.GetData(_playertexdata);

            Vertices _playerverts = PolygonTools.CreatePolygon(_playertexdata, _tex_player.Width);

            Body _playerbody = BodyFactory.CreateCompoundPolygon(_world, BayazitDecomposer.ConvexPartition(_playerverts), 1.0f);
            _playerbody.BodyType = BodyType.Dynamic;

            _player = new Player(ref _playerbody, Vector2.Zero, _tex_player);
        }

Before anyone says anything, yes, I do understand why the vertices need to be scaled in the physics engine. As I said before, when I do that, collision just doesn't work, period.

Jan 30, 2012 at 6:06 PM

I bumped into a very similar issue using very similar code. (my post is here).

 Did you find what was wrong?

Jan 30, 2012 at 8:16 PM

Nope, but I'm pretty sure the texture is just too complex. MSTerrain seems to be the class I wanted to use, but it hasn't worked for me, so.. I might go back to making terrain the hard way. :D

Feb 3, 2012 at 11:21 PM
Edited Feb 3, 2012 at 11:23 PM

I think the problem are your islands.

The texture to polygon composer barely works with simple holes in a shape, so even less an island in a hole in a shape.

 

Divide it and make the islands independent (other texture, other body), and just place them over the main shape.