Advertisement
Guest User

farseer 3.3.1

a guest
Apr 26th, 2012
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1.  
  2.         World physicsWorld = new World(new Vector2(0, 300));
  3.         Body box2, box;
  4.         Texture2D _bodyTexture;
  5.         Vector2 _bodyOrigin;
  6.  
  7.         protected override void LoadContent()
  8.         {
  9.             spriteBatch = new SpriteBatch(GraphicsDevice);
  10.  
  11.             box = RectangleBody(50, 50, BodyType.Dynamic, 0, 0);
  12.             box.Position = new Vector2(100, 100);
  13.             box.Mass = 7;
  14.  
  15.             box2 = RectangleBody(50, 50, BodyType.Dynamic, 0, 0);
  16.             box2.Position = new Vector2(125, 200);
  17.             box2.Mass = 7;
  18.             box2.ApplyLinearImpulse(new Vector2(100, 100));
  19.  
  20.             _bodyTexture = Content.Load<Texture2D>("box");
  21.             _bodyOrigin = new Vector2(_bodyTexture.Width / 2f, _bodyTexture.Height / 2f);
  22.  
  23.             Body floor = RectangleBody(1000, 100, BodyType.Static, 0.2f, 0);
  24.             floor.Position = new Vector2(500, 500);
  25.         }
  26.  
  27.         public Body RectangleBody(int width, int height, BodyType bodyType, float restitution, float friction)
  28.         {
  29.             Vertices rectVerts = PolygonTools.CreateRectangle(width / 2, height / 2);
  30.             PolygonShape shape = new PolygonShape(rectVerts, 1);
  31.  
  32.             Body body = BodyFactory.CreateBody(physicsWorld);
  33.             body.BodyType = bodyType;
  34.             body.CreateFixture(shape);
  35.             body.FixtureList[0].Friction = friction;
  36.             body.FixtureList[0].Restitution = restitution;
  37.  
  38.             return body;
  39.         }
  40.  
  41.         protected override void Update(GameTime gameTime)
  42.         {
  43.             physicsWorld.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds);
  44.  
  45.             base.Update(gameTime);
  46.         }
  47.         protected override void Draw(GameTime gameTime)
  48.         {
  49.             GraphicsDevice.Clear(Color.CornflowerBlue);
  50.  
  51.             spriteBatch.Begin();
  52.             spriteBatch.Draw(_bodyTexture, box.Position, null, Color.White, box.Rotation, _bodyOrigin, 1f, SpriteEffects.None, 1f);
  53.             spriteBatch.Draw(_bodyTexture, box2.Position, null, Color.White, box2.Rotation, _bodyOrigin, 1f, SpriteEffects.None, 1f);
  54.             spriteBatch.End();
  55.  
  56.             base.Draw(gameTime);
  57.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement