TrackingBody Camera2D

Topics: Developer Forum
Feb 8, 2011 at 11:01 PM

Hello,

I currently working on XNA4 and I need to add farseer :)

I started to play with the sources of farseer.

And I just found a TrackingBody into you camera.

But it still impossible for me to track a body with the camera.

There is how I use it.

 

 this.debug.LoadContent(GraphicsDevice, Content);
this.cam = new Camera2D(GraphicsDevice);

  this.cam.EnableTargetTracking = true;

Vector2 gameWorld = Camera2D.ConvertScreenToWorld(new Vector2(graphics.PreferredBackBufferWidth,
graphics.PreferredBackBufferHeight));

this.debug.Flags = DebugViewFlags.DebugPanel | DebugViewFlags.CenterOfMass | DebugViewFlags.TexturedShape | DebugViewFlags.ContactNormals | DebugViewFlags.PerformanceGraph;


Vertices box = PolygonTools.CreateRectangle(0.5f, 0.5f);
PolygonShape shape = new PolygonShape(box, 1);

body = BodyFactory.CreateBody(world);
body.Position = new Vector2(5.0f, 5.0f);
body.BodyType = BodyType.Dynamic;

Fixture fixture = body.CreateFixture(shape);
fixture.Friction = 0.3f;
float I = body.Inertia;
float mass = body.Mass;

float radius = (float)Math.Sqrt(2.0 * (I / mass));


this.cam.TrackingBody = body;

this is the Update Part:

  protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            this.debug.Update(gameTime);

            float timeStep = Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f, (1f / 30f));
            world.Step(timeStep);
            cam.Update();
            base.Update(gameTime);
        }

The body is falling \o/ but the camera doesn't want to fallow it :/ but the the Camera2D.currentPosition is changing :/

Did I forget something really stupide ?

 

Developer
Feb 9, 2011 at 12:50 PM

Nope the camera is just a bit broken at the moment. But there will be a new version soonish. Have a look at this thread: http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=244049

There is one solution how you could handle tracking...

Feb 9, 2011 at 7:21 PM

Cheers :)

I'm gonna try it right now :)

Feb 9, 2011 at 7:21 PM
Edited Feb 9, 2011 at 7:28 PM

Cheers :)

I'm gonna try it right now :)

 

Sorry :/ multipost

Feb 10, 2011 at 11:10 AM

Hello,

Thanx for your answer but it doesn't solve my problem.

I think I'm not using the same camera as you. (maybe another version)

for informations I'm using the farseerphysics-84452 sources. ( http://pix.delation.me/farseerphysics-84452.zip )

Did you fix your camera on the sources ?

I'm looking forward to reading you.

Coordinator
Feb 10, 2011 at 1:22 PM
Edited Feb 10, 2011 at 1:22 PM

You might have better luck with the camera from change-set 84001

Feb 10, 2011 at 1:23 PM

I am gonna try and give you my feed back :)

Cheers

Feb 10, 2011 at 2:07 PM

The camea position is changing but nothing append during the game.

what what did a forget ? :/

I set the

TrackingBody
 protected override void Initialize()
{
// TODO: Add your initialization logic here this.debug.LoadContent(GraphicsDevice, Content);
this.cam = new Camera2D(GraphicsDevice);
//Vector2 gameWorld = // Camera2D.ConvertScreenToWorld(new Vector2(graphics.PreferredBackBufferWidth, // graphics.PreferredBackBufferHeight)); this.debug.Flags = DebugViewFlags.DebugPanel | DebugViewFlags.CenterOfMass | DebugViewFlags.AABB | DebugViewFlags.TexturedShape | DebugViewFlags.ContactNormals | DebugViewFlags.PerformanceGraph;
DebugMaterial material = new DebugMaterial(MaterialType.Circles)
{
Color = Color.Gold,
Scale = 2.5f
};
_rectangle = FixtureFactory.CreateRectangle(this.world, 15, 5, 1, material);
_rectangle.Body.BodyType = BodyType.Dynamic;
_ground = FixtureFactory.CreateRectangle(this.world, 30, 3, 0, material);
_ground.Body.BodyType = BodyType.Static;
this._ground.Body.Position = new Vector2(this._ground.Body.Position.X, this._ground.Body.Position.Y - 25);
// this.cam.EnableTargetTracking = true; this.cam.TrackingBody = _rectangle.Body;
base.Initialize();
}


this.cam.EnableTargetTracking = true; <-- this line was used with
farseerphysics-84452 Camera now I'm using the farseerphysics-84001\Samples\DemoBaseXNA\ScreenSystem\Camera2D.cs

I'm updating the camera
 protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            float timeStep = Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f, (1f / 30f));
            this.debug.Update(gameTime);
            this.cam.Update();
            world.Step(timeStep);
            base.Update(gameTime);
        }

and there is my draw.

 /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            this.debug.RenderDebugData(ref Camera2D.Projection);
            base.Draw(gameTime);
        }

I don't understand :/

Coordinator
Feb 10, 2011 at 2:50 PM

If you take a closer look at the samples, you will notice that we use both the view and projection matrices:

DebugView.RenderDebugData(ref Camera2D.Projection, ref Camera2D.View);

Feb 10, 2011 at 5:12 PM

ok it works.

I Feel so stupide :/

 

Cheers.