A few beginner and intermediate questions.

Mar 14, 2009 at 3:34 AM
Hey guys,
I just found your engine last week and am rebuilding my game around it because I loved what I saw in the simple and advanced samples. Great job and thank you very much for the engine.

I just started to program with any programming language just 2 months ago, so please forgive me if me questions tend to overlap a bit. I will try to keep them engine specific as I can though. My first, most simple question is:

1) I learned using Joran's (XNATutorial videos and Nick's Tile Engine videos) for most of my understanding foundation. However, in Joran's 9th pack of videos, he demonstrates how to create a Farseer singleton class. When creating bodies and geoms, typing Farseer.Physics, in the method overload seems to make everything run well enough. But I've noticed several different places genbox stresses you must add a body to PhysicsSimulator. My geoms, especially circle geoms, are really inaccurate at times and I'm wondering if this could be the cause?

2) I am making a platformer hybrid kind type game and was wondering how to handle animations. It looks silly just knocking the enemy's physics body over but I can't get the animation to run on either the player or enemy with any bodies/ geoms attatched. Any advice?

3) With projectiles, how would I add the geom without having gravity pull the projectile to the ground?

There are a question or two more that have slipped my mind at the moment, but if I recall I will post them. Thank you for the awesome support and even better engine guys!
Mar 14, 2009 at 5:55 AM
Welcome! About your questions:
I don't understand 1 and 2, so could you please clarify?
for 3. you can turn off gravity on the body that the geom is attached to (via a variable like GravityEnabled or something)
Mar 14, 2009 at 6:54 AM
Edited Mar 14, 2009 at 6:56 AM
Okay, I'm sorry. I'll state the questions in a different way.

1) First, here's my code; I took fireball out of it's class for this example:


public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Box boxSprite;
        Background background;
        Wizard wizardSprite;

       Texture2D fireballTexture;

        Vector2 fireballOrigin;
        Vector2 fireballPosition = new Vector2(200, 100);

         Body fireballBody;
        Geom fireballGeom;

        public Game1()
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
            boxSprite = new Box();
            wizardSprite = new Wizard();
            background = new Background();

            PhysicsSimulator physicsSimulator = new PhysicsSimulator() ;


        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            fireballTexture = Content.Load<Texture2D>("Fireball");

            fireballOrigin = new Vector2(fireballTexture.Width / 2f, fireballTexture.Height / 2f);

            fireballBody = BodyFactory.Instance.CreateCircleBody(Farseer.Physics, fireballTexture.Width / 2f, 100f);

            fireballBody.Position = fireballPosition;

            fireballGeom = GeomFactory.Instance.CreateCircleGeom(Farseer.Physics, fireballBody,
                fireballTexture.Width / 2f, 25);



        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
            // TODO: Unload any non ContentManager content here

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)


            Farseer.Physics.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);


        /// <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)


            spriteBatch.Draw(fireballTexture, fireballBody.Position, null, Color.White, fireballGeom.Rotation, fireballOrigin, 1,
                SpriteEffects.None, 0f);






                                           FARSEER SINGLETON FROM TUTORIAL VIDEO

    /// <summary>Farseer is a singleton, so we can access the physics engine from anywhere.</summary>
    public sealed class Farseer
        // Make a static field, so it is instantiated when needed.
        static readonly PhysicsSimulator physics = new PhysicsSimulator(new Vector2(0f, 300f));

        // Make empty constructors, so nobody can accidentally instantiate Farseer.
        static Farseer() { }
        Farseer() { }

        // Make the static field publicly available through a property.
        public static PhysicsSimulator Physics
            get { return physics; }

Ok, so see in the load content method, I use Farseer.Physics instead of physicsSimulator? That's because when I try physicsSimulator it gives me a runtime error highlighting the physicsSimulator.Add(body) line in the BodyFactory class but doesn't for some reason with Farseer.Physics. But my geoms and bodies are sometimes off and I was wondering if this was the cause and how to correct the runtime error.

2) As for my second question, I'm basically saying for example in a game like metal slug or contra, when you shoot the enemy, they fall and have some kind of flashing type death animation; that's what I'm trying to do, but knocking over the textures body looks silly and I can't seem to add a spritesheet animation with the physics. Same for when the characters are moving and I'm needing to animate those as well.
Mar 14, 2009 at 8:44 AM
Wel for your first question, i've never seen nor heard of some one using Farseer.Physics as a variable so i cant help there. The second one i don't really have an answer either im sorry. Genbox or mattbetcher or Shpaidaman (he should be of great help) ought to be around soon.
Mar 14, 2009 at 11:11 AM
Saw the Farseer Physics logo in the sky and hurried online. (Think Batman ;) )

First of all, welcome JinJohn to our forums and thanks for using our engine.
As for the questions:

1) A singleton is a class that creates one and only one instance of a class. This is great in some cases because you always have access to that one instance (You don't need to pass reference everywhere) and since you only need one physics engine anyway... well, it's great.
I see that you already have a good singleton and you access it using Farseer.Physics, so it's all good.

Your circles are not inaccurate because you have not added a body. The body is the object that controls speed, rotation, forces and the like. A geometry is what gives the body it's "outline" (geometry...) and makes it possible for a body to collide with other objects. It's inaccurate because circles are approximations of circles, and not real circles. They are essentially based on a list of points and that makes them bounce funny sometimes.

2) First off you create a class that contains the body, the geometry and the animation sprite. You can call this class AnimatedCharacter.
Then you create your drawing and updating logic inside that class. This way you have a clean structure and it's easy to handle. Your updating logic should be something like this:

//Inside Update() inside AnimatedCharacter:
if (characterState == CharacterState.Walking)
   //Change the geometry to a geometry that fits the characters outline. Makes more realistic collisions.
  characterGeom = walkingCharacterGeom;

   //Start the animation if the character walking

And you do this for every state the character can have. (running, standing, jumping, shooting...)
Oh, and another thing. Body.Tag and Geom.Tag should also be mentioned. They are "object holders" that keeps a reference to an object you choose. Search forums for more info if you like.

3) Simply use body.IgnoreGravity. If you set it to true, it will not be affected by gravity at all.
Mar 14, 2009 at 12:47 PM
Edited Mar 14, 2009 at 12:50 PM
Yes nows my chance

1. As genbox stated its because you are basically outlining the fireball with a circle, it wont be the same outline as your texture.
If you would like to make it exactl like the texture i suggest using "textures to vertices"
The instructions in the manual are extremely clear and easy to follow, if youve got a png(or any image that supports transparency) using that technique will outline your character perfectly.
But make sure to take a look at "tunneling" and "geometries going into eachother" to solve your problem.

in spritebatch.draw you should make it like this.
spritebatch.draw(texture, geom.position, null, color.white, geom.rotation,
origin(you can get this using textures to vertices as well), 1f, spriteeffects.none, 0)

You may be doing this already but i just wanted to make sure : )

2. If you know how to use spritesheets you can use rectangle bodys, or polygon bodies on specific frames then just animate.
I wouldnt suggest textures to vertices on every frame, just ones where the body is extremely different(standing to crouch(or dead))
hope it helped!!!
Mar 14, 2009 at 3:44 PM
Wow, that's a great system you guys have. A farseer logo in the sky....hmmm : P

But thank you very much, that cleared up everything for me.