This project has moved and is read-only. For the latest updates, please go here.

How do i Control a body's fixtureList Collision?

Topics: User Forum
Mar 1, 2013 at 8:55 PM

In this game i am creating i have added a fixture to my cars body which is a circle that is bigger than the car and i want to controll what happens to this body when something is in this circle.

How do setup a collision code for this??
I have tried something like this but it doesnt work the way i want it to.

        public bool AI_Car_Collisions(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)

            if (fixtureA.Body.FixtureList[1].UserData == "AI_Collision_Radius"
                && fixtureB.Body.UserData == "AI_Car"
                || fixtureA.Body.UserData == "AI_Car"
                && fixtureB.Body.FixtureList[1].UserData == "AI_Collision_Radius")
                return true;
            return false;
I was returning true for now just to see if only the AI Car and the Collision radius collide but they aren't the only ones colliding the cars also collide..

This code was just a test to see if i can make certain bodies collide without using categories, because i want to add events that take place when this collision happens without a physical collision(Without being able to collide)

basically its a collision zone so if something is in this zone i need something to happen to that object.

i hope that makes sense lol.

Anyone know something i can do?

Thanks for the help.
Mar 1, 2013 at 11:09 PM
Ok so i got it to work but i still am not sure if this is the correct way so what i have done is...

this is not from actual game project i am creating. this is from a tutorial i found online using farseer physics engine, and i have been messing with it to see what i can and cant do.

the tutorial i am using is :

        private void SpawnCrate()
            DrawablePhysicsObject crate;
            crate = new DrawablePhysicsObject(world, Content.Load<Texture2D>("Crate"), new Vector2(50.0f, 80.0f), 1.0f);
            crate.Position = new Vector2(random.Next(50, GraphicsDevice.Viewport.Width - 50), random.Next(80, GraphicsDevice.Viewport.Height - 80));

            crate.Body.UserData = "AI_Car";
            crate.Body.Mass = ConvertUnits.ToSimUnits(30.0f);
            crate.Body.AngularDamping = ConvertUnits.ToSimUnits(500.0f);
            crate.Body.Friction = ConvertUnits.ToSimUnits(1000.0f);
            crate.Body.LinearDamping = ConvertUnits.ToSimUnits(2500.0f);
            crate.Body.Restitution = ConvertUnits.ToSimUnits(1.0f);

            crateList[crateList.Count - 1].Body.BodyId = crateList.Count - 1;

                                        crateList[crateList.Count - 1].Body,

            crateList[crateList.Count - 1].Body.FixtureList[0].UserData = "AI_Car";
            crateList[crateList.Count - 1].Body.FixtureList[0].Body.BodyId = crateList.Count - 1;
            crateList[crateList.Count - 1].Body.FixtureList[0].OnCollision += AI_Car_Collisions;

            crateList[crateList.Count - 1].Body.FixtureList[1].UserData = "AI_Collision_Radius";
            crateList[crateList.Count - 1].Body.FixtureList[1].Body.BodyId = crateList.Count - 1;

            crateList[crateList.Count - 1].Body.FixtureList[1].OnCollision += AI_Car_Collisions;

            crateList[crateList.Count - 1].Body.OnCollision += AI_Car_Collisions;

        public bool AI_Car_Collisions(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)

            if (fixtureA.Body.FixtureList[1].Body.BodyId != fixtureB.Body.FixtureList[0].Body.BodyId
                && fixtureA.UserData == "AI_Collision_Radius"
                && fixtureB.UserData == "AI_Car"
                || fixtureA.Body.FixtureList[0].Body.BodyId != fixtureB.Body.FixtureList[1].Body.BodyId
                && fixtureA.UserData == "AI_Car"
                && fixtureB.UserData == "AI_Collision_Radius")
                return true;
            return false;
so for right now it works but like i have mentioned i am not sure if this is the correct way to do this.

can anyone tell me if this way is okay or if there is another way of doing this?

Thank you for any help i recieve