No collisions?

Topics: Developer Forum, User Forum
Apr 13, 2013 at 12:55 PM
Edited Apr 13, 2013 at 12:59 PM
Hi folks,

I'm in the process of integrating Farseer physics in to my 2D level editor, and have everything set up... except I'm not getting any collisions.

I'm using a component object system, and when physics is enabled in the editor all of the bodies for the physics objects are created (I've tried various pixels-per-meter settings, but am currently using 100):
public void CreateBody()
{
    IEPhysicsService physicsService = Engine.GetInstance().GetService<IEPhysicsService>();
    Debug.Assert( physicsService != null );

    float       pixelsPerMeter  = physicsService.PixelsPerMeter;
    Rectangle   bounds          = myParent.Bounds;

     if( myBody != null )
     {
          physicsService.World.RemoveBody( myBody );
          myBody = null;
      }

     // Create the body
     switch( CollisionShape )
     {
       case IECollisionShape.CL_Box :
        {
            myBody = BodyFactory.CreateRectangle( physicsService.World, bounds.Width / pixelsPerMeter, bounds.Height / pixelsPerMeter, (float)Density, myParent.Position );
         }
         break;

         case IECollisionShape.CL_Sphere :
         {
            myBody = BodyFactory.CreateCircle( physicsService.World, bounds.Width / pixelsPerMeter, (float)Density, myParent.Position );
          }
          break;

          default :
             break;
           }

           // Make sure we have a valid body
           if( myBody == null )
           {
              Debug.Assert( false );
              return;
           }

            // Set the body type
            switch( BodyType )
            {
                case IEBodyType.BT_Dynamic :
                    myBody.BodyType = FarseerPhysics.Dynamics.BodyType.Dynamic;
                    break;

                case IEBodyType.BT_Static :
                    myBody.IsStatic = true;
                    break;

                case IEBodyType.BT_Kinematic :
                    myBody.BodyType = FarseerPhysics.Dynamics.BodyType.Kinematic;
                    break;
            }

            // Give the body some bounce and friction
            myBody.Friction     = (float)Friction;
            myBody.Restitution  = (float)Restitution;
            myBody.Rotation     = (float)myParent.Rotation;
        }
Each frame my physics service is updated, and I call World.Step, then update the positions of the objects I'm rendering from the values taken from the physics component bodies. I thought it could be the case that the world wasn't getting updates often enough (that my elapsed time was too large), but I've tried hard coding it to 0.001f.
        /// <summary>
        /// Updates the physics world
        /// </summary>
        public override void Update( double anElapsedTime )
        {
            if( !Enabled )
            {
                return;
            }

            // Update the physics world
            myWorld.Step( (float)anElapsedTime );
            
            // Update the positions & rotations of all the physics objects
            foreach( IEObject currentObject in myPhysicsObjects )
            {
                IEPhysicsComponent physicsComponent = currentObject.GetComponent<IEPhysicsComponent>();
                if( physicsComponent == null )
                {
                    myPhysicsObjects.Remove( currentObject );
                    continue;
                }

                currentObject.Position = physicsComponent.Body.Position;
                currentObject.Rotation = physicsComponent.Body.Rotation;
            }
        }
Everything looks like it's working, the position updates seem correct (and my dynamic objects fall correctly) but none of my dynamic objects are actually colliding with my static objects - they just fall straight through. Looking at the Farseer World instance there are definitely two bodies that it's updating, one of them static one of them dynamic.

The objects that I'm adding have a width/height of (100,00) - so essentially 1 square meter.

Any ideas what I'm missing here?
Apr 13, 2013 at 1:21 PM
Oops! I wasn't dividing the object position by meters per pixel :)