Farseer Debug and XNA sprite problems. (Puzzled)

Topics: User Forum
Nov 9, 2010 at 8:57 PM

So heres my problem:

I am working on a game and we have set up a physics body for the character based on an article we found here: http://amazingretardo.simiansoftwerks.com/2010/02/17/platformer-character-control-farseer-physics-engine/

I used the Debug view to get the bodies set up and working together right. and got the movement working well enough. all that worked fine.

The next two things on my list of to do's was putting sprites on top of where the physics bodies are located and getting them to move with the bodies.  AND setting up a "2D camera" system to allow for some more complex character/mouse tracking in game.

The Problems: (there are many)

The Sprites dont seem to be drawing in the correct places, ( understand that farseer uses a coordinate system where 0,0 is in the center while XNA has 0,0 in the top left corner. I need to know if there is a good way to convert one system to the other. or if you just have to brute force the numbers)

I have created a sprite that renders "at the location" of the physics bodies. but it does not move with it as it should. (the movement is much less effective.)

Adding the 2D camera class (found here: http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/) seems to fix the problems with the coordinate system but the sprites still dont move with the physics bodies.

I will include the main game.cs file here, but there are several other classes that are associated with the program. If someone needs to see it I have posted a rar'd copy of the whole program (so far its small and simple) here:

http://www.filesavr.com/M1PYP3TANF2QMH3

 

 

Here is the main file for the program:

 

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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using FarseerPhysics.DebugViewXNA;

namespace Gunslingers
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
       private GraphicsDeviceManager graphics;
       private SpriteBatch spriteBatch;
       private World world = new World(new Vector2(0, -60));
       private DebugViewXNA debugView;


       // OBJECTS maps, characters, etc.
       private Map map = new Map();
       private Character player = new Character();
       private Camera2d _camera;


       

       public Game1()
        {
           graphics = new GraphicsDeviceManager(this);
           Content.RootDirectory = "Content";
           debugView = new DebugViewXNA(world);
        }


        protected override void Initialize()
        {

            // Make mouse visible
            this.IsMouseVisible = true;
            _camera = new Camera2d();

            base.Initialize();
        }


        protected override void LoadContent()
        {
           // Create a new SpriteBatch, which can be used to draw textures.
           spriteBatch = new SpriteBatch(GraphicsDevice);

           DebugViewXNA.LoadContent(graphics.GraphicsDevice, Content);

           map.loadContent(Content, world);
           player.loadContent(Content, world);
          

        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

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

            player.KeyInput();
            player.Move(graphics);

            _camera.Pos = player.characterPosition();

            world.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            /*spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
                              SpriteSortMode.BackToFront,
                              SaveStateMode.SaveState,
                              _camera.get_transformation(GraphicsDevice));*/
            spriteBatch.Begin();
          

            map.Draw(spriteBatch, graphics);
            player.Draw(spriteBatch, graphics);
          
            Matrix proj = Matrix.CreateOrthographic(50 * graphics.GraphicsDevice.Viewport.AspectRatio, 50, 0, 1);
            Matrix view = Matrix.Identity;

            debugView.RenderDebugData(ref proj, ref view);


            spriteBatch.End();
            base.Draw(gameTime);
        }


    }
}