How do i find the index number of a gameObject in a list using OnCollisionEventHandler

Topics: User Forum
Feb 8, 2013 at 6:01 AM
I have been using Userdata and it works if i do something like this
Car.Body.UserData  = Car_List.Count - 1;
but my problem is that i need userdata to also label this object as a car in the game and figure out what i am colliding with.

Does anyone know a way i may find the index number on collision?

Thanks for the help.
Feb 8, 2013 at 7:42 AM
Edited Feb 8, 2013 at 7:45 AM
I don't understand what exactly is your problem, but if you want have more than one information into the userdata, create a class with all necessary informations
and pass it to the userData.

class BodyUserData
{
int collisionIndex;
BodyLabel bodyLabel; //Enum (car...)
};

//At the creation of the body add this userData
BodyUserData NewBodyUserData=new BodyUserData();
NewBodyUserData.CollisionIndex=- 1;
NewBodyUserData.BodyLabel=BodyLabel.Car;
Car.Body.UserData=NewBodyUserData;

//OnCollisionEventHandler
if(fixture.Body.UserData!=null)
{
if(fixture.Body.UserData is BodyUserData)
{
 BodyUserData bodyUserDataToSet = fixture.Body.UserData as BodyUserData

 //Update the collision index
 bodyUserDataToSet.CollisionIndex=Car_List.Count - 1;
}
}

I swear that help you
Feb 9, 2013 at 2:06 AM
I figured out another way that is working without any problems and what i did was..
Car.Body.UserData = "Car";
Car_List.Add(Car);
Car.Body.BodyId = Car_List.Count - 1;

        public bool Player_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
            
            if (fixtureB.Body.UserData == "Car")
            {
                Car_Index = fixtureB.Body.BodyId ;
                Is_Touching = true;
                return true;
            }
            return true;
        }
it is working just fine like this

Thanks for your help