OnCollision Not Firing?

Topics: User Forum
Jun 20, 2010 at 6:45 PM
Edited Jun 20, 2010 at 7:08 PM

I'm not sure why my OnCollision is not firing. This is with FP3. Here is the code:

 

 

            //Create and connect fixture to mainBody
            mainBody.CreateFixture(rectShape);
            
            //Now set the friction to this fixture
            mainBody.FixtureList[0].Friction = .2f;

            //Create segment fixture
            Fixture lineSegmentFixture = FixtureFactory.CreateEdge(Farseer.World, new Vector2(0, 0), new Vector2(0, 2), 1.0f);
            
            //Make a sensor and set on collision work to happen
            lineSegmentFixture.IsSensor = true;
            lineSegmentFixture.OnCollision += this.OnCollision;

            //Now add it to the mainBody
            mainBody.FixtureList.Add(lineSegmentFixture);

 

The mainBody is dynamic as well. Here is what it looks like below. I was reading another thread having the same problem with collisions against static bodies but his was FP2.1.x and my mainBody is dynamic whereas his was not. 

Also I have moved the OnCollision code to the circleBody that you can see in the picture and it works. I would use that but I had a lot of problems with it not firing back in FP2.1.x. Maybe it's ok to do in FP3?

 

Developer
Jun 20, 2010 at 11:05 PM

Try using Create.Fixture() instead of just adding the Fixture to the list. I'm pretty sure the list is there for convenience only anyway.

                   //Create and connect fixture to mainBody
                    mainBody.CreateFixture(rectShape);

                    //Now set the friction to this fixture
                    mainBody.FixtureList[0].Friction = .2f;

                    //Create segment shape
                    PolygonShape lineSegmentShape = new PolygonShape(PolygonTools.CreateEdge(new Vector2(0, 0), new Vector2(0, 2)), 1.0f);

                    Fixture linSegmentFixture = mainBody.CreateFixture(lineSegmentShape);
                    //Make a sensor and set on collision work to happen


                    linSegmentFixture.IsSensor = true;
                    lineSegmentFixture.OnCollision += this.OnCollision;
Anyway, try this and let me know how it works. About using just the Circle, it might work, 3.0 is way more stable then 2.1 ever was.

Jun 20, 2010 at 11:28 PM

That did it :) Thanks! And I did mess around with the circle and it seemed to work fine but I'm going to stick with the small line segment because of the sides of the circle allowing you to jump.