Basic Collision Detection between two boxes.

Oct 30, 2009 at 9:47 PM

Hi, I'm new to both farseer and this community and I'm having some trouble. I've read over the manual, looked at samples and googled my problem but there has to be something huge I am missing for this simple problem. I can't get two boxes to collide. I created 2 separate bodies and Geoms and I can control one of the boxes by applying forces from the right thumb stick but the problem is they don't collide at all what more do I have to do to get them to collide?

Oct 31, 2009 at 2:13 AM
93hamsal wrote:

Hi, I'm new to both farseer and this community and I'm having some trouble. I've read over the manual, looked at samples and googled my problem but there has to be something huge I am missing for this simple problem. I can't get two boxes to collide. I created 2 separate bodies and Geoms and I can control one of the boxes by applying forces from the right thumb stick but the problem is they don't collide at all what more do I have to do to get them to collide?

Hi mate and welcome let me say!

Firstly, are you drawing your sprites where the body position is? Do you have debug viewer enabled to see the geometries? Do they just pass right through each other? so you ARE applying force and not just changing the body's position? as it got me just this morning i kept changing its position and it just went right through the collideable object ^^

//_physsim is my physics simulator
//its important to add your body to the simulator!


//Create a body at 100,100 that will just stay there
Body _body = BodyFactory.Instance.CreateRectangleBody(_physsim,Height,Width, 5);
_body.IsStatic = true;
_body.Position = new Vector2(100,100);
Geom _geom = GeomFactory.Instance.CreateRectangleGeom(_physsim, _body, Height, Width);

//Create second body that u can control
Body _body2 = BodyFactory.Instance.CreateRectangleBody(_physsim,Height,Width, 5);
Geom _geom2 = GeomFactory.Instance.CreateRectangleGeom(_physsim, _body, Height, Width);


Maybe paste your code here for us to have a look at?

Oct 31, 2009 at 2:28 AM
Edited Oct 31, 2009 at 2:42 AM

EDIT:

It works now! thank you for your post or I wouldn't have noticed, I was leaving out the Physics Simulator Parameter when creating the geometry objects.

//EDIT

 

 

Hey thanks for the reply, yeah as far as I know I think I'm drawing the sprites at the correct spot I'm using the rectangle brush to draw from the demobase project to draw it.

 

I'll paste my code for you guys. My bodies are called bullet and bullet2, and bulletG and bullet2G are the Geoms

 

 

	PhysicsSimulator PhysSim;
        PhysicsSimulatorView PhysSimView;


        RectangleBrush bulletBrush;
        RectangleBrush bulletBrush2;

        Body Bullet;
        Body Bullet2;

        Geom BulletG;
        Geom Bullet2G;

 

 

 

////******/////

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

            bulletBrush = new RectangleBrush(20, 20, Color.Gold, Color.Black);
            bulletBrush.Load(GraphicsDevice);

            bulletBrush2 = new RectangleBrush(20, 20, Color.Red, Color.Black);
            bulletBrush2.Load(GraphicsDevice);

            Bullet = BodyFactory.Instance.CreateRectangleBody(PhysSim, 20, 20, 1);
            Bullet.Position = new Vector2(300, 300);
            BulletG = GeomFactory.Instance.CreateRectangleGeom(Bullet, 20, 20);

            BulletG.CollisionResponseEnabled = true;


            Bullet2 = BodyFactory.Instance.CreateRectangleBody(PhysSim, 20, 20, 1);
            Bullet2.Position = new Vector2(400, 300);
            Bullet2G = GeomFactory.Instance.CreateRectangleGeom(Bullet2, 20, 20);

            Bullet2G.CollisionResponseEnabled = true;




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

 

 

////******/////

Update();

 

Bullet.ApplyForce(GamePad.GetState(PlayerIndex.One).ThumbSticks.Right*50);

            PhysSim.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);

 

 

 

////******/////

 

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

            // TODO: Add your drawing code here

            spriteBatch.Begin();
            bulletBrush.Draw(spriteBatch, Bullet.Position, Bullet.Rotation);
            bulletBrush2.Draw(spriteBatch, Bullet2.Position, Bullet2.Rotation);
            spriteBatch.End();

            base.Draw(gameTime);
        }

 

 

 

 

 

 

 

Oct 31, 2009 at 8:22 AM

Don't forget to instantiate Physics simulator

PhysSim = new PhysicsSimulator();//U can specify gravity here as a vector2, a gravity of y200 is usually good

Don't forget to add the geom to the phys sim.

 

 

Hope this helps.