Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- World physicsWorld = new World(new Vector2(0, 300));
- Body box2, box;
- Texture2D _bodyTexture;
- Vector2 _bodyOrigin;
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- box = RectangleBody(50, 50, BodyType.Dynamic, 0, 0);
- box.Position = new Vector2(100, 100);
- box.Mass = 7;
- box2 = RectangleBody(50, 50, BodyType.Dynamic, 0, 0);
- box2.Position = new Vector2(125, 200);
- box2.Mass = 7;
- box2.ApplyLinearImpulse(new Vector2(100, 100));
- _bodyTexture = Content.Load<Texture2D>("box");
- _bodyOrigin = new Vector2(_bodyTexture.Width / 2f, _bodyTexture.Height / 2f);
- Body floor = RectangleBody(1000, 100, BodyType.Static, 0.2f, 0);
- floor.Position = new Vector2(500, 500);
- }
- public Body RectangleBody(int width, int height, BodyType bodyType, float restitution, float friction)
- {
- Vertices rectVerts = PolygonTools.CreateRectangle(width / 2, height / 2);
- PolygonShape shape = new PolygonShape(rectVerts, 1);
- Body body = BodyFactory.CreateBody(physicsWorld);
- body.BodyType = bodyType;
- body.CreateFixture(shape);
- body.FixtureList[0].Friction = friction;
- body.FixtureList[0].Restitution = restitution;
- return body;
- }
- protected override void Update(GameTime gameTime)
- {
- physicsWorld.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds);
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- spriteBatch.Draw(_bodyTexture, box.Position, null, Color.White, box.Rotation, _bodyOrigin, 1f, SpriteEffects.None, 1f);
- spriteBatch.Draw(_bodyTexture, box2.Position, null, Color.White, box2.Rotation, _bodyOrigin, 1f, SpriteEffects.None, 1f);
- spriteBatch.End();
- base.Draw(gameTime);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement