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

Body falls through body

Topics: Developer Forum
Mar 3, 2013 at 3:41 PM
What I have here is a ball and and rectangle. 2 Different objects. Now the rectangle behaves as a lift that comes from the bottom of the screen a moves upwards. When it reaches the top of the screen it starts again at the bottom. Now if my ball touches the rectangle the rectangle pushes the ball upwards. But what tends to happen is even though it is being pushed up like the ball was on a lift it slowly falls through the rectangle. Here is how my 2 objects are set up:

this.body = BodyFactory.CreateCircle(myWorld, ConvertUnits.ToSimUnits((this.image.Width / 2) - 3), 10);
            this.body.BodyType = BodyType.Dynamic;
            this.body.Position = ConvertUnits.ToSimUnits(this.position);
            this.body.Restitution = _bounce;      //Bounceability
            this.body.ApplyForce(new Vector2(1.95f, 0.01f));
            this.body.Friction = 0;
            this.body.IsSensor = false;
            this.body.IsBullet = true;
            this.body.UserData = _strObjectName; 
this.body = ImageToPolygonBody(this.image, myWorld,0.01f,1f, this.origin);
            this.body.BodyType = BodyType.Static;
            this.body.Position = ConvertUnits.ToSimUnits(this.position);
            this.body.IsStatic = true;
            this.body.Restitution = _bounce;       //Bounceability
            this.body.Friction = _friction;
            this.body.IsSensor = false;
            this.body.FixedRotation = _fixedRotation;
            this.body.UserData = _strObjectName;
Now the rectangles position is updated in the update sub by:
elevator2.body.Position = ConvertUnits.ToSimUnits(750, fltEle2);
            if (fltEle2 < 300) { fltEle2 = 450; }
Can anyone see what I am doing wrong. I have introduced other static objects and ball behaves fine if it hit them, it seems if the object is moving it will slowly pass through it.

Mar 4, 2013 at 7:33 PM
Edited Mar 4, 2013 at 7:34 PM
The problem is your rectangle BodyType is set to Static. Physics engines don't like it when you manually move static bodies -- it expects them to be one place and does some calculations, if you then move it it's as though the body/position came out of nowhere.

To solve your problem set your Rectangle body to be dynamic and use Joints to achieve the affect you're after (in this case you'll probably want the FixedPrismaticJoint to create a "Lift").

In general you'll avoid oddness in your physics simulation if, after initial setup, you avoid manually setting body positions and use forces/impulses/joints instead.

Also, unrelated to your problem, but common for issues of "one body passing through another" is called "Tunneling". This happens if one body is moving very quickly. The physics solver interpolates where a body should be each update -- if it's going really fast it might end up on the other side of another body instead of correctly colliding with it. Farseer has the .IsBullet property of body and CCD (continuous collision detection) setting to solve this issue (again, not the problem in your case... just including it for completeness). You sacrifice some performance in exchange for accuracy by using CCD.

Good luck,
Mar 5, 2013 at 1:56 PM
Thanks for the pointers Mike, I will take a look at FixedPrismaticJoint's.