Friday, March 16, 2012

Farseer Physis Engine-Tutorial Basic-1

繼上一篇

Farseer Physis Engine-Installation Note

開始Farseer 的第一個練習
XNA-Farsser Physis Engine Test

Coding
Using FarseerPhysics.Dynamics;
Using FarseerPhysics.Factories;
World world;
Body floor;
Body Block;
Body Object;
Texture2D blockTexture;
Texture2D floorTexture;
Texture2D objectTexture;

At Initialize Method add code below:
public override void Initialize()
{
   //world is the manager all of it,And set value
   world = new World(new Vector2(0,20));

   //Setting you object BodyType and Position....
   //BodyType Static:object that it don't need to be movable something like wall...
   //         Dynamic: object that it need to be movable.
   //Position: your object initialize Position.
   floor = BodyFactory.CreateRectangle(world,floorTexture.Width,floorTexture.Height,1.0f);
   floor.BodyType = BodyType.Static;
   floor.Position = new Vector2(640,650);
   
   Block = BodyFactory.CreateRectangle(world,blockTexture.Width,blockTexture.Height,1.0f);
   Block.BodyType = BodyType.Static;
   Block.Position = new Vector2(500,200);
   
   object = BodyFactory.CreateRectangle(world,objectTexture.Width,objectTexture.Height,1.0f);
   object.BodyType = BodyType;
   object.Position = new Vector2(640,-10);
}

Initial all of value we need.



At LoadContent Method  Loading all of texture from our content
public override void LoadContent()
{
  blockTexture = Game.Content.Load<Texture2D>("Your folder");
  //And all of your Texture you need.....
  ......
}



Update World and your physis world will process.
public override void Update()
{
   //Update our world
   world.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);
}



Draw your Textures.
public override void Draw()
{
  ...
  //Draw Your Texture!! 
  spriteBatch.Begin();
  spriteBatch.Draw(BlockTexture ,new Vector2(Block.Position.X,Block.Position.Y) , null , Color.White , Block.Rotation , new Vector2(blockTexture.Width / 2.0f , blockTexture.Height / 2.0f) , 1.0f , SpriteEffects.None , 0f);
  spriteBatch.Draw(floorTexture , new Vector2(floor.Position.X , floor.Position.Y) , null , Color.White , floor.Rotation , new Vector2(floorTexture.Width / 2.0f , floorTexture.Height / 2.0f) , 1.0f , SpriteEffects.None , 0);
  spriteBatch.Draw(objectTexture , new Vector2(Object.Position.X , Object.Position.Y) , null , Color.White , Object.Rotation , new Vector2(objectTexture.Width / 2.0f , objectTexture.Height / 2.0f) ,1.0f, SpriteEffects.None , 0);
  spriteBatch.End();
  ...
}



And Done!!!!!

There are Documentation:http://farseerphysics.codeplex.com/documentation from farrsser.



also you can add GameStateManager to your project. http://create.msdn.com/en-US/education/catalog/sample/game_state_management



Next Article:Farseer Physis Engine- Tutorial:Basic.2 – DebugView

No comments:

Post a Comment