Collision not always detected

Topics: User Forum
Mar 20, 2011 at 11:41 AM

Hi all,

In my pinball game my collision detection is not always returning a result. It seems to be that the point of impact matters.

I am using Farseer XNA 3.2 .

It is a collision between a circle (pinball) and bumper-bodies made from a picture.

The circle:

 /* Circle Fixture:Pinball */

            Vector2 _circlePosition = new Vector2(280 / _meterInPx, 650 / _meterInPx);
            _circleFixture = FixtureFactory.CreateCircle(_world, 36f / (2f * _meterInPx), 0.5f, _circlePosition);
            _circleFixture.Body.BodyType = BodyType.Dynamic;
            _circleFixture.Body.IsBullet = true;
            _circleFixture.Restitution = 0.01f;
            _circleFixture.Friction = 0.5f;
            _circleFixture.Body.Mass = 0.6f;
            _circleFixture.OnCollision += bumperOnCollision;
            _circleFixture.OnCollision += SensorOnCollision;

One of the bumpers:

            //bumperLeft

            uint[] bumperLeftdata = new uint[bumperLeftTexture.Width * bumperLeftTexture.Height];
            bumperLeftTexture.GetData(bumperLeftdata);
            Vertices bumperLeftverts = PolygonTools.CreatePolygon(bumperLeftdata, bumperLeftTexture.Width, bumperLeftTexture.Height, true);
            Vector2 bumperLeftscale = new Vector2(0.015f, 0.014f);
            bumperLeftverts.Scale(ref bumperLeftscale);
            _list = BayazitDecomposer.ConvexPartition(bumperLeftverts);
            bumperLeft = FixtureFactory.CreateCompoundPolygon(_world, _list, 1);
            bumperLeft[0].Body.BodyType = BodyType.Static;
            bumperLeft[0].Body.Position = new Vector2(bumperleftPosition.X / _meterInPx, bumperleftPosition.Y / _meterInPx);
            bumperLeft[0].Restitution = 1f;

The detection:

    public bool bumperOnCollision(Fixture circleFixture, Fixture Bumper, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
            if (Bumper == bumperLeft[0])
            {
                if (bumperleftLit) points += 100;
                if (!bumperleftLit) points += 10;
                bumperLInstance.Play();
            }

            if (Bumper == bumperMid[0])
            {
                if (bumpermidLit) points += 100;
                if (!bumpermidLit) points += 10;
                bumperMInstance.Play();
            }
            if (Bumper == bumperRight[0])
            {
                if (bumperrightLit) points += 100;
                if (!bumperrightLit) points += 10;
                bumperRInstance.Play();
            }

            return true;
        }
    }
If I use The earclip decomposer for the bumper, than the detection behavior is different but still in both cases about 50% misses.

Is there a way to solve this?

 

Thanks in advance,

Groover

Developer
Mar 20, 2011 at 11:59 AM

One look at the documentation would have told you, that collisions are handled per fixture. Obviously your bumpers consist of more than one fixture. You only check against the first one in all cases though:

if (Bumper == bumperRight[0])

You also have to check against bumperRight[1..n] here. Please try to understand the basics of bodies, fixtures, and shapes, before jumping into advanced stuff like Texture2Fixture conversion without knowing what you are doing.

Mar 20, 2011 at 12:30 PM

Thank U for your fast reply,

I will use ellipses of the same size as sensors at the same place as the bumpers for the  collisiondetection.

Thanks,

Groover