Beginning with Farseer

Topics: Developer Forum, User Forum
Jun 1, 2010 at 12:11 PM

Hi guys,

I'm starting using Farseer with XNA.

When I tried to make my first example, it is acting strangely.

Before saying anything, my code is :

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 FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics.Collisions;

namespace TestePhysics
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D bola;
        Body body;
        Geom geom;
        PhysicsSimulator simulador;
        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()
        {
            
            // TODO: Add your initialization logic here

            simulador = new PhysicsSimulator();
            simulador.Gravity = new Vector2(0, 100);

            base.Initialize();
        }

        /// <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);
            bola = Content.Load<Texture2D>("alienFos");

            // TODO: use this.Content to load your game content here
        }

        /// <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)
                this.Exit();
            body = BodyFactory.Instance.CreateCircleBody(simulador, bola.Width / 2, 50);
            geom = GeomFactory.Instance.CreateCircleGeom(simulador, body, bola.Width / 2, 12);
            body.Position = new Vector2(400, 10);

            // TODO: Add your update logic here
            simulador.Update(gameTime.ElapsedGameTime.Milliseconds * 0.001f);
            base.Update(gameTime);
        }

        /// <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
            spriteBatch.Begin();
            spriteBatch.Draw(bola,body.Position,null,Color.White,body.Rotation,new Vector2(bola.Width/2,bola.Height/2),
                1,SpriteEffects.None,1);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

The problem is that my ball doens't fall.

It moves just a little up and down (like it was "tilted").

 

What did I do wrong ?

 

Thanks

Jun 1, 2010 at 12:56 PM
Edited Jun 1, 2010 at 12:57 PM

A game's Update is called by default up to 60 times per second. It is the place where you would *update* your game according to all kinds of input. It is not the place to create a body or geom, because it will be recreated on every update, which is 60 times per second by default. You should do it in either Initialize() or LoadContent(). Those are called once per game (or component).

So move:

 

body = BodyFactory.Instance.CreateCircleBody(simulador, bola.Width / 2, 50);
geom = GeomFactory.Instance.CreateCircleGeom(simulador, body, bola.Width / 2, 12);
body.Position = new Vector2(400, 10);

To Initialize() or LoadContent().

More info on the basic architecture of XNA can be found on MSDN: http://msdn.microsoft.com/en-us/library/bb203893.aspx#ID4ENE

 

Jun 1, 2010 at 2:03 PM

Well, worked like a charm !

Thanks very much for the help and quick reply !