texture to polygon in 101907 pls help

Topics: User Forum
Mar 21, 2013 at 11:22 AM
hi, i could be able to run my codes about texture to polygon in my previous farseer 3.3.1 projects.
but i need to implement it in source 101907.
below code i can't see debugview lines and texture "1" does not collide the ground and i couldn't find out where i am wrong.
pls can someone help me to run it correctly ?
thanks



using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using FarseerPhysics;
using FarseerPhysics.Collision;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Common.TextureTools;
using FarseerPhysics.Common.PhysicsLogic;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Common.ConvexHull;
using FarseerPhysics.Common.PolygonManipulation;
using FarseerPhysics.Content;
using FarseerPhysics.Controllers;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using FarseerPhysics.DebugView;


namespace WindowsGame3
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch _batch;

    Vector2 _origin;
    private Vertices ter;
    private Body _ground;
    private World _world;
    private Matrix _view;
    private Vector2 _cameraPosition;
    private Vector2 _screenCenter;
    private Body body;
    private Texture2D tex;
    private Vertices vert = new Vertices();
    private const float mip = 64f;
    DebugViewXNA _debugView;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        graphics.PreferredBackBufferWidth = 800;
        graphics.PreferredBackBufferHeight = 480;
        _world = new World(new Vector2(0, 1));

    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        _view = Matrix.Identity;
        _cameraPosition = Vector2.Zero;
        _screenCenter = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2f,
                                           graphics.GraphicsDevice.Viewport.Height / 2f);

        // Create a new SpriteBatch, which can be used to draw textures.
        _batch = new SpriteBatch(GraphicsDevice);
        tex = Content.Load<Texture2D>("1");
        //List<Vertices> tex = Content.Load<List<Vertices>>("1");
        uint[] data = new uint[tex.Width * tex.Height];
        tex.GetData(data);
        vert = PolygonTools.CreatePolygon(data, tex.Width,false);
        Vector2 scale = vert.GetCentroid();

        vert.Translate(ref scale);
        _origin = -scale;

        body = BodyFactory.CreateCompoundPolygon(_world, Triangulate.ConvexPartition(
            vert, TriangulationAlgorithm.Bayazit),1f,BodyType.Dynamic);
        body.Position =  new Vector2(5, 0);
        body.BodyType = BodyType.Dynamic;


        _ground = new Body(_world);
        {
            ter = new Vertices();
            ter.Add(new Vector2(0f, 5f));
            ter.Add(new Vector2(15f, 5f));

        }
        for (int i = 0; i < ter.Count - 1; ++i)
        {
            FixtureFactory.AttachEdge(ter[i], ter[i + 1], _ground);
        }


        _debugView = new DebugViewXNA(_world);
        _debugView.AppendFlags(DebugViewFlags.DebugPanel);
        _debugView.DefaultShapeColor = Color.AliceBlue;
        _debugView.SleepingShapeColor = Color.Aquamarine;
        _debugView.LoadContent(GraphicsDevice, Content);

    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        _world.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        _batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, _view);
        _batch.Draw(tex, body.Position * mip, null, Color.White, body.Rotation,_origin, 1f, SpriteEffects.None, 0f);
        _batch.End();

        Matrix projection = Matrix.CreateOrthographicOffCenter(0f, graphics.GraphicsDevice.Viewport.Width/mip ,
                                                         graphics.GraphicsDevice.Viewport.Height/mip, 0f, 0f,
                                                         1f);
        Matrix view = Matrix.CreateTranslation(new Vector3((_cameraPosition / mip) - (_screenCenter / mip), 0f)) * Matrix.CreateTranslation(new Vector3((_screenCenter / mip), 0f));

        // draw the debug view
        _debugView.RenderDebugData(ref projection, ref view);

        base.Draw(gameTime);
    }
}
}
Developer
Mar 21, 2013 at 11:40 AM
myriac wrote:
hi, i could be able to run my codes about texture to polygon in my previous farseer 3.3.1 projects.
but i need to implement it in source 101907.
Why? What's wrong with 3.3.1?
Farseer is currently under active development and a lot of the new and old features are still broken in the latest build. There aren't any new features that are fully working yet. Once there are, there will be a new release. So there is absolutely no reason to not stick with 3.3.1 until then.
Mar 21, 2013 at 12:49 PM
Elsch,

i really didn't know those old and new features are still broken..
i needed to implement it because i thought and was excited i could make a project especially when i saw fully working demos/samples of 101907. They were impressive.
I only wanted to implement a very basic texture to polygon code using the latest FarseerPhysics XNA+debugview XNA after those samples.
thanks for your reply anyway

kind regards
Developer
Mar 21, 2013 at 1:14 PM
There are some "breaking" changes, like LoopShape has been renamed to ChainShape and maybe fixed joints are going to be removed. But these decisions may change again until the release.
There are no new features in the Farseer Samples yet, which won't work with 3.3.1. Most of the stuff is just fancy boilerplate code which isn't part of Farseer anyway. With a few renames here and there you should be able to use all the sample stuff with 3.3.1

Most of the "impressive" stuff stems from the content pipeline extensions (I guess), which make it easy to define your shapes in inkscape or any other svg capable editor. They are far from finished though: still a lot of missing features and bugs in there.
The texture to polygon pipeline just wraps the old texture to polygon sample code i.e. it does the same thing at compile time vs. runtime. The svg path parser should also work with 3.3.1 after renaming chain to loop shape ;)

If you want to try those and need some help I could probably point you in the right direction here and there. Basically you could just add the extension projects to your current one... and link to the "old" Farseer version.

If I ever find the time, I need to write some documentation for those anyway :/
Mar 21, 2013 at 1:56 PM
Elsch wrote:
There are some "breaking" changes, like LoopShape has been renamed to ChainShape and maybe fixed joints are going to be removed. But these decisions may change again until the release.
There are no new features in the Farseer Samples yet, which won't work with 3.3.1. Most of the stuff is just fancy boilerplate code which isn't part of Farseer anyway. With a few renames here and there you should be able to use all the sample stuff with 3.3.1

Most of the "impressive" stuff stems from the content pipeline extensions (I guess), which make it easy to define your shapes in inkscape or any other svg capable editor. They are far from finished though: still a lot of missing features and bugs in there.
The texture to polygon pipeline just wraps the old texture to polygon sample code i.e. it does the same thing at compile time vs. runtime. The svg path parser should also work with 3.3.1 after renaming chain to loop shape ;)

If you want to try those and need some help I could probably point you in the right direction here and there. Basically you could just add the extension projects to your current one... and link to the "old" Farseer version.

If I ever find the time, I need to write some documentation for those anyway :/
thanks four your help Elsch

i always thought that an up-to-date documentation would be very helpfull for all.
on the other hand for the moment it would be better waiting for a complete and detailed (i hope) doc when FP 3.5 released.
till then stick with my implementation.

best.