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.

Main:

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

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

    //Player
    Player player;

    //Target
    Target target;

    //Others
    float framerate;

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

        graphics.PreferMultiSampling = false;
        graphics.ApplyChanges();

    }

    protected override void Initialize()
    {

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

        base.Initialize();

    }

    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);
        entityList.Add(player);

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

    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))
            this.Exit();

        //Update the world
        world.Step(0.3333f);

        //Update players and entities
        player.Update();
        target.Update();

        base.Update(gameTime);
    }


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

        spriteBatch.Begin();

        //Draw target
        target.Draw(spriteBatch);

        //Draw player
        player.Draw(spriteBatch);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}

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
        setWorld(newWorld);
        setBody(body);
        setFixture(fixture);
        setTexture(texture);
        Update();

    }

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

    }
}

Entity:

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

put on hold as off-topic by Anko, Krom Stern, Josh Petrie Dec 15 at 17:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Anko, Krom Stern, Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
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

Browse other questions tagged or ask your own question.