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

WP7: Checking for contact on a world without stepping the world

Topics: Developer Forum, User Forum
Aug 9, 2011 at 9:55 AM


I want to have the users place objects on screen, without running the world, and i want to prevent them from placing an item on top of another one.

What i have is this:

 _draggedBody.Position += delta;


// Check if the dragged body, in its new position, collides with

 // anything. If it does, don't allow the drag (== reposition it).


_world.ContactManager.BroadPhase.UpdatePairs((ref FixtureProxy a, ref FixtureProxy 

b) => { });


contact = GetFirstEnabledContact(_draggedBody);

 if (contact != null



  _draggedBody.Position -= delta;





where GetFirstEnabledContact() checks the ContactList of the body to find a contact.

However, after touching body B with body A, if I try to move body A somewhere else, it still always shows B as its contact.

There's a high chance that i'm doing something wrong here... can someone please advise?




Aug 13, 2011 at 5:01 PM

Well, I managed to get this working, but it doesn't look pretty... I basically need to do all the permutations on all shapes of all fixtures in the screen to check for collision, and it looks like this:


        private bool MoveBodyIfNotOverlapping(Body bodyToCheck, Vector2 positionDelta)
            bodyToCheck.Position += positionDelta;
            foreach (var fixture in bodyToCheck.FixtureList)
                Transform transform;
                bodyToCheck.GetTransform(out transform);
                for (int i = 0; i < fixture.Shape.ChildCount; i++)
                    foreach (var body in _allBodies)
                        if (body != bodyToCheck)
                            Transform testedBodyTransform;
                            body.GetTransform(out testedBodyTransform);
                            foreach (var testedBodyFixture in body.FixtureList)
                                for (int j = 0; j < testedBodyFixture.Shape.ChildCount; j++)
                                    if (AABB.TestOverlap(fixture.Shape, i, testedBodyFixture.Shape, j,
                                        ref transform, ref testedBodyTransform))
                                        bodyToCheck.Position -= positionDelta;
                                        return true;
            return false;

Hope this helps... :)