Getting started [Solved]

Topics: User Forum
Dec 13, 2010 at 8:38 PM

I read http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=219643 and the getting started documentation. But there is no PhysicsSimulator or PhysicsSimulatorView...

This is what I did so far:

1: I created a new solution.

2: I added the DemoBaseXNA.dll and the Farseer Physics 3.0 XNA.dll and the DebugViewXNA.dll to the project as a reference.

3: I created a new instance of FarseerPhysics.Dynamics.World with a gravity.

4: I added a sample body:

        protected override void Initialize()
        {
            myBody = Engine.World.CreateBody();
            myBody.BodyType = BodyType.Dynamic;
            CircleShape circleShape = new CircleShape(0.5f);
            Fixture fixture = myBody.CreateFixture(circleShape);
            base.Initialize();
        }
        Body myBody;

Problem: I can't call update/draw as it used to be. Where is the PhysicsSimulator.Draw()/Update() ? I'm using XNA 4.0 and VS 2010. I can't find it in the documentation nor in Google and I'm sorry if I overlooked it somewhere.

 

Thank you.

Dec 14, 2010 at 12:05 PM

I Solved it. Solution:

References added: DemoBaseXNA.dll, the Farseer Physics 3.0 XNA.dll and the DebugViewXNA.dll

 

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.Dynamics;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Factories;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.DebugViewXNA;

namespace Test
{ /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Body myBody; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; Engine.Game = this; Engine.Graphics = graphics; } protected override void Initialize() { Engine.DebugViewXNA = new DebugViewXNA(Engine.World); myBody = Engine.World.CreateBody(); myBody.BodyType = BodyType.Dynamic; CircleShape circleShape = new CircleShape(50f); Fixture fixture = myBody.CreateFixture(circleShape); myBody.Position = new Vector2(100, 100); base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); DebugViewXNA.LoadContent(graphics.GraphicsDevice, Content); Engine.DebugViewXNA.AppendFlags(DebugViewFlags.AABB); Engine.DebugViewXNA.AppendFlags(DebugViewFlags.CenterOfMass); Engine.DebugViewXNA.AppendFlags(DebugViewFlags.ContactPoints); Engine.DebugViewXNA.AppendFlags(DebugViewFlags.Shape); Engine.DebugViewXNA.AppendFlags(DebugViewFlags.DebugPanel); Fixture myFixture = FixtureFactory.CreateCircle(Engine.World, 0.5f, 1); myFixture.OnCollision += MyOnCollision; } public bool MyOnCollision(Fixture f1, Fixture f2, Contact contact) { return true; } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { Engine.World.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f, (1f / 30f))); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); float aspect = (float)Engine.Width / Engine.Height; Engine.Projection = Matrix.CreateOrthographic(Engine.Width * aspect, Engine.Height, 0, 1); spriteBatch.Begin(); Engine.DebugViewXNA.RenderDebugData(ref Engine.Projection, ref Engine.View); spriteBatch.End(); Window.Title = Engine.World.BodyList[0].Position.ToString(); base.Draw(gameTime); } } }
and for the engine class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNALib;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.DebugViewXNA;

namespace Test
{
    public static class Engine
    {
        private static World m_World = new World(new Vector2(0,-20));
        public static World World { get { return m_World; } }

        public static Game1 Game {get;set;}
        public static DebugViewXNA DebugViewXNA { get; set; }
        public static Matrix Projection;
        public static Matrix View = Matrix.Identity;
        public static GraphicsDeviceManager Graphics {get;set;}
        public static int Width { get { return Graphics.PreferredBackBufferWidth; } }
        public static int Height { get { return Graphics.PreferredBackBufferHeight; } }
    }
}