Simple collision detection not working for me

Topics: Developer Forum, User Forum
Mar 8, 2010 at 8:03 PM

Hi everybody!

I was trying out Farseer today. It is a great tool for creating 2D Physics and I like it very much. However I can't seem to get collision detection working. This is currently my (relevant) code:

MainGame.cs

 

public class MainGame : Microsoft.Xna.Framework.Game {
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;
        private PhysicsSimulator physics;
        private Paddle paddle;

        private Texture2D pixel;
        private Geom geomLeft;

        public PhysicsSimulator PhysicsSimulator {
            get { return physics; }
        }

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

        protected override void Initialize() {
            IsFixedTimeStep = true;
            TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 6); // 60fps

            graphics.PreferMultiSampling = true;
            graphics.PreferredBackBufferHeight = Config.Height;
            graphics.PreferredBackBufferWidth = Config.Width;

            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
            physics = new PhysicsSimulator(new Vector2(-100, 0));

            pixel = Content.Load<Texture2D>("pixel");
            Body leftBody = BodyFactory.Instance.CreateRectangleBody(Config.BorderWidth, Config.Height, 1);
            leftBody.Position = new Vector2(0, 0);
            leftBody.IsStatic = true;
            geomLeft = GeomFactory.Instance.CreateRectangleGeom(leftBody, Config.BorderWidth, Config.Height);

            paddle = new Paddle(this);

            physics.Add(leftBody);
            physics.Add(geomLeft);
            physics.Add(paddle.Geom.Body);
            physics.Add(paddle.Geom);
            Components.Add(paddle);

            base.Initialize();
        }

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

            // Update Physics
            physics.Update(gameTime.ElapsedGameTime.Milliseconds * 0.001f);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime) {
            GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.Draw(pixel, new Rectangle((int)geomLeft.Body.Position.X, (int)geomLeft.Body.Position.Y, (int)Config.BorderWidth, Config.Height), null,
                Color.White, geomLeft.Body.Rotation, new Vector2(0, 0), SpriteEffects.None, 0);
            spriteBatch.End();

            base.Draw(gameTime);
        }

Paddle.cs

 

    public class Paddle : Microsoft.Xna.Framework.DrawableGameComponent {

        private Texture2D texture;
        private Vector2 position;
        private SpriteBatch spriteBatch;
        private PhysicsSimulator physics;
        private Geom geom;

        public Texture2D Texture {
            get { return texture; }
        }

        public Geom Geom {
            get { return geom; }
        }

        public Paddle(MainGame game) : base(game) {
            spriteBatch = game.SpriteBatch;
            physics = game.PhysicsSimulator;
            Initialize();
        }

        protected override void LoadContent() {
            texture = Game.Content.Load<Texture2D>("paddle");

            Body body = BodyFactory.Instance.CreateRectangleBody(texture.Width, texture.Height, 1f);
            geom = GeomFactory.Instance.CreateRectangleGeom(body, texture.Width, texture.Height);

            geom.Body.Position = new Vector2(300, 300);
        }

        public override void Update(GameTime gameTime) {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime) {
            spriteBatch.Begin();
            spriteBatch.Draw(texture,
                new Rectangle((int)geom.Body.Position.X, (int)geom.Body.Position.Y, texture.Width, texture.Height),
                null,
                Color.White,
                geom.Body.Rotation,
                new Vector2(0, 0),
                SpriteEffects.None,
                0f);
            spriteBatch.End();

            base.Draw(gameTime);
        }
The gravity, force, torque and impulse are working great however! I also tried out changing the CollisionGroup and debugging my game, but it didn't worked out. Thus can anybody help me out with this (probably simple error) ?

 

 

Mar 8, 2010 at 11:31 PM

So things just fly through eachother?

Mar 9, 2010 at 4:50 PM
drakeshe wrote:

So things just fly through eachother?

Correct. :)

Mar 9, 2010 at 5:23 PM

You forgot to add the geom to the physics simulator in the paddle code :)