textures to polygons-various movements

Sep 18, 2010 at 9:02 PM
hi friends, below the code loads 9 textures, converts them to polygon..All the objects fall down with the gravity..everything works perfect..
my question is : what if i want to apply to different objects different impacts ? For example i want 8th texture move upward as ignoring gravity.
(in fpe 2 it was very simple , coz every texture has its own body and geom) , how will i be able to do that in the below code. ? pls help..
thx in advance..

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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FarseerPhysics;
using FarseerPhysics.DebugViewXNA;
using FarseerGames.DrawingSystem;

using FarseerPhysics.Controllers;
using FarseerPhysics.Collision;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using FarseerPhysics.Common;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Common.ConvexHull;
using FarseerPhysics.Common.PolygonManipulation;

namespace arkanoid_farseer_3
{
  /// 
  /// This is the main type for your game
  /// 
  public class Game1 : Microsoft.Xna.Framework.Game
  {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    World _world = new World(new Vector2(0, -40));

    int image = 0;



    Matrix Projection;
    Matrix View;

    private Fixture fr2, fr3, fr4, fr5;
    private Texture2D tex2;


    Vector2[] origin = new Vector2[10];
    Vector2[] scale = new Vector2[10];

    List textlist = new List();
    Random rand = new Random();
    
    
    
    DebugViewXNA debugView;

    const float baseScreenHeightInWorldCoordinates = 30;

    public Game1()
    {
      graphics = new GraphicsDeviceManager(this);
      Content.RootDirectory = "Content";
      IsFixedTimeStep = false;
      debugView = new DebugViewXNA(_world);
    }

    
    protected override void Initialize()
    {
      // TODO: Add your initialization logic here
      graphics.PreferredBackBufferWidth = 1500;
      graphics.PreferredBackBufferHeight = 900;
      graphics.PreferMultiSampling = true;
      graphics.GraphicsDevice.PresentationParameters.MultiSampleQuality = 8;
      graphics.ApplyChanges();
      base.Initialize();
    }

    

    protected override void LoadContent()
    {

      // Create a new SpriteBatch, which can be used to draw textures.

      spriteBatch = new SpriteBatch(GraphicsDevice);
      DebugViewXNA.LoadContent(graphics.GraphicsDevice, Content);
      

      tex2 = DrawingHelper.CreateRectangletexture(graphics.GraphicsDevice, 1500, 10, Color.YellowGreen, Color.Red); //Content.Load("image3");
      
      
      for (int i = 0; i < 9; i++)
      {
        ana _ana = new ana();
        image += 1;
        _ana.kaplama = Content.Load("a" + image);

        _ana.data = new uint[_ana.kaplama.Width * _ana.kaplama.Height];
        _ana.kaplama.GetData(_ana.data);


        _ana.verts = PolygonTools.CreatePolygon(_ana.data, _ana.kaplama.Width, _ana.kaplama.Height);


        
        _ana.org.X = _ana.verts.GetCentroid().X - _ana.kaplama.Width / 2;
        _ana.org.Y = _ana.verts.GetCentroid().Y - _ana.kaplama.Height / 2;


        _ana.scale = new Vector2(1f / baseScreenHeightInWorldCoordinates, 1f / baseScreenHeightInWorldCoordinates);
        _ana.scale.Y = -_ana.scale.Y;
        _ana.verts.Scale(ref _ana.scale);
        
        
        

        _ana.list = EarclipDecomposer.ConvexPartition(_ana.verts);// BayazitDecomposer.ConvexPartition(_ana.verts);

        foreach (Vertices item in _ana.list)
        {
          if (item.Count == 0)
          {

            _ana.toRemove.Add(item);

          }
        }

        foreach (Vertices item in _ana.toRemove)
        {
          _ana.list.Remove(item);
        }


        _ana.fr = FixtureFactory.CreateCompundPolygon(_world, _ana.list, 1f);

        _ana.fr[0].Body.BodyType = BodyType.Dynamic;

        _ana.fr[0].Body.Position = new Vector2(-5*i+15  , 1);
        
        _ana.fr[0].Restitution = 1f;
        if (i == 8)
        {
          
          _ana.fr[0].Body.IgnoreGravity = true; ------>HERE LAST TEXTURE ACCEPTS IGNOREGRAVITY
          _ana.fr[0].Body.ApplyTorque(1000); // ------>HERE LAST TEXTURE DOES NOT ACCEPT APPLYTORQUE
        }
        _ana.pos = new Vector2(-5 + 15, 1);

        textlist.Add(_ana);

        
      }

      fr2 = FixtureFactory.CreateRectangle(_world, tex2.Width / 30f, tex2.Height / 30f, 1, new Vector2(0, -15f));
      fr3 = FixtureFactory.CreateRectangle(_world, tex2.Width / 30f, tex2.Height / 30f, 1, new Vector2(0, 15f));
      fr4 = FixtureFactory.CreateRectangle(_world, tex2.Height / 30f, tex2.Width / 30f, 1, new Vector2(-25, 0f));
      fr5 = FixtureFactory.CreateRectangle(_world, tex2.Height / 30f, tex2.Width / 30f, 1, new Vector2(25, 0f));

      fr2.Restitution = 1f; fr3.Restitution = 1f; fr4.Restitution = 1f; fr5.Restitution = 1f;

      // TODO: use this.Content to load your game content here
    }


    protected override void UnloadContent()
    {
      // TODO: Unload any non ContentManager content here
    }
    protected override void Update(GameTime gameTime)
    {
      // Allows the game to exit
     
      KeyboardState s = Keyboard.GetState();
      if (s.IsKeyDown(Keys.Escape)) this.Exit();
      
      _world.Step((float)(gameTime.ElapsedGameTime.Milliseconds * .001f));

      base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
      GraphicsDevice.Clear(Color.Black);

      Viewport viewPort = GraphicsDevice.Viewport;

      ////
      //// Create View and Projection matrices
      ////
      Projection =
        Matrix.CreateOrthographic(baseScreenHeightInWorldCoordinates * viewPort.AspectRatio,
                                  baseScreenHeightInWorldCoordinates,
                0, 1);

      //// move camera back and forth over time
      //// this could be anything - pan, zoom, rotate, etc..
      //View = Matrix.CreateTranslation((float)Math.Sin(gameTime.TotalGameTime.TotalSeconds), 0, 0);

      //// Create a transform matrix to send to spriteBatch
      View = Matrix.Identity;
      Matrix transform =
            View
          * Matrix.CreateScale(viewPort.Height / baseScreenHeightInWorldCoordinates)
          * Matrix.CreateScale(1f, -1f, 1f)
          * Matrix.CreateTranslation(viewPort.Width * .5f, viewPort.Height * .5f, 0f);



      spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, transform);

      Vector2 origin2 = new Vector2(tex2.Width / 2, tex2.Height / 2);



      Vector2 scale2 = new Vector2(1f / baseScreenHeightInWorldCoordinates,
                  1f / baseScreenHeightInWorldCoordinates);


      scale2.Y = -scale2.Y;

      foreach (var vvv in textlist)
      {
                
        spriteBatch.Draw(vvv.kaplama,vvv.fr[0].Body.Position, null, Color.White, vvv.fr[0].Body.Rotation, vvv.org, vvv.scale, SpriteEffects.None, 0);
        //Console.WriteLine(vvv.Body.Position);
        
      }


      spriteBatch.Draw(tex2, fr2.Body.Position, null, Color.White, 0, origin2,
        scale2, SpriteEffects.None, 0);
      spriteBatch.Draw(tex2, fr3.Body.Position, null, Color.White, 0, origin2,
          scale2, SpriteEffects.None, 0);
      spriteBatch.Draw(tex2, fr4.Body.Position, null, Color.White, MathHelper.PiOver4 * 90, origin2,
            scale2, SpriteEffects.None, 0);
      spriteBatch.Draw(tex2, fr5.Body.Position, null, Color.White, MathHelper.PiOver4 * 90, origin2,
            scale2, SpriteEffects.None, 0);


      spriteBatch.End();

      base.Update(gameTime);
    }

  }

}


public class ana
{
  public Vertices verts;
  public List toRemove = new List();
  
  public List fr = new List();
  public List list;
  public Texture2D kaplama;
  public Vector2 pos;
  public Vector2 org;
  public Vector2 scale;
  public uint[] data;

}
Coordinator
Sep 19, 2010 at 6:28 PM

You need to save your ana objects in an array or collection. If you want the 8th ana object to move up (like a ballon), you will have to apply an upwards force on each update to the 8th ana object in the collection.

I can demonstrate with a bit of code:

------ Code to create ana objects ------

List<ana> myObjects = new List<ana>(8);

for (int i = 0; i <9; i++)
{
ana myAna = new ana();
myObjects.Add(myAna);
}

---------------------------------------------

----------- Code inside Update -----------

 protected override void Update(GameTime gameTime)
{
     myObjects[8].fr[0].Body.ApplyForce(forceHere);
}

-----------------------------------------------

 

Sep 19, 2010 at 7:29 PM

thx so much genbox, you are great.

thank you very much.