Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So I'm mixing XNA and Farseer, the first for graphics and the latter for collision and physics. If this is a horrible choice to start out with, tell me that as well.

In any case, I have set up a entity class upon which I build my other classes (players and enemies). However, none of the functions in Farseer seem to work. Nothing related to physics that is. I can walk through a box, my bullet gives no collision detection, nothing.


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

    //Create a lists
    List<Entity> entityList = new List<Entity>();

    Player player;

    Target target;

    float framerate;

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

        graphics.PreferMultiSampling = false;


    protected override void Initialize()

        //Create world
        world = new World(new Vector2(0f,0f));



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

        //Create player
        player = new Player(BodyType.Dynamic, Content, entityList, world);

        //Create target
        target = new Target(BodyType.Dynamic, Content, entityList, world);

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

    protected override void Update(GameTime gameTime)

        framerate = 1 / (float)gameTime.ElapsedGameTime.TotalSeconds;

        // Allows the game to exit
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))

        //Update the world

        //Update players and entities


    protected override void Draw(GameTime gameTime)


        //Draw target

        //Draw player



Target (child of Entity):

class Target : Entity {

    public Target(BodyType newBodyType, ContentManager content, List<Entity> entitylist, World newWorld)
        : base(content, entitylist, newWorld) {

        //Get texture
        Texture2D texture = content.Load<Texture2D>("Enemies/Target");

        //Create farseer object
        Body body = BodyFactory.CreateRectangle(newWorld, ConvertUnits.ToSimUnits(texture.Width), ConvertUnits.ToSimUnits(texture.Height), 1f);
        body.Position = ConvertUnits.ToSimUnits(new Vector2(300f, 300f));

        Fixture fixture = FixtureFactory.AttachRectangle(ConvertUnits.ToSimUnits(texture.Width), ConvertUnits.ToSimUnits(texture.Height), 1, new Vector2(0f, 0f), body);
        fixture.UserData = "Target";

        //Set other stuff


    public void Update() {


    public void Draw(SpriteBatch spriteBatch) {

        spriteBatch.Draw(getTexture(), ConvertUnits.ToDisplayUnits(getBody().Position), null, Color.White, getBody().Rotation, new Vector2(getTexture().Width / 2, getTexture().Height / 2), 3f, SpriteEffects.None, 1f);



class Entity

    public List<Entity> entityList;

    //Get physics stuff
    Texture2D texture;
    Fixture fixture;
    World world;
    Body body;

    public Entity(ContentManager content, List<Entity> entityList, World world){

        this.world = world;
        this.entityList = entityList;


    public void Update() {

        fixture.OnCollision += entityCollision;


    public void Draw(SpriteBatch spriteBatch) {


    public bool entityCollision(Fixture f1, Fixture f2, Contact contact) {

        Console.WriteLine("Collision detected!");
        Console.WriteLine("F1: " + f1.UserData);
        Console.WriteLine("F2: " + f2.UserData);
        return false;
share|improve this question
I'm using my phone right now so reading the code is a bit hard but do you actually set the body to dynamic? –  Mikael Högström Feb 3 '13 at 1:00
I feel amazingly stupid, I haven't actually done that. Now some other weird stuff is happening, which I'll have to look into a bit. Thanks! –  Randy Schouten Feb 3 '13 at 1:53
@RandySchouten can you update your question accordingly? Also, please mention which wierd stuff so we can help you troubleshoot. –  ashes999 Feb 3 '13 at 2:07
So what it does now is that it detects the collision (not between the Player and Target, but a between a bullet created in player and the target). It's not smooth however and it seems like the target and the player are teleporting to a random location. –  Randy Schouten Feb 3 '13 at 8:02
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.