# How to preserve topology for penetration correction?

I have a sorted list of blocks - some may overlap some may not. Each block has an ideal starting position and may be constrained to one axis. There are no velocity or acceleration or other forces involved - just the position. Some blocks are totally fixed and can never be moved (boundary blocks).

How do I solve the penetrations in a way that the blocks don't overlap anymore, but stay in the initial order?

I can easily solve the penetrations with normal position correction methods, like Baumgarte's stabilization in combination with separation axis theorem. The problem though, a separation normal might be found which pushes the block in a way that the order is changed, which is not what I want, because the application I am writing is a numerical simulation with some visualizations.

I created a simple javascript sample to show what I mean: http://jsfiddle.net/358hra2j/1/

I updated the JSFiddle sample with the solution: Here's the updated version.

It was just two lines of code:

var sign = shapeA.id - shapeB.id > 0 ? 1 : -1;
n.multScalar(sign);


Found a solution, using the sign from the shapes order as a multiplication factor for the normal solved the problem.

Just two lines of code:

    // Fix direction based on order of creation
var sign = shapeA.id - shapeB.id > 0 ? 1 : -1;
n.multScalar(sign);

• It would be nice if you provide a better explanation for future visitors. – concept3d Oct 8 '14 at 12:39