Code: Select all
//change as necessary
float maxSnapRange = 1;
bool allowSnappingToVertexOfSameFixture = true;
vertex[] svs = sv();
vertex[] avs = av();
array<vertex> newPoints;
array<vec2> newPointPositions;
for (uint i = 0; i < svs.length; i++) {
vertex v = svs[i];
vertex closest;
float closestDist = 999999;
for (uint k = 0; k < avs.length; k++) {
vertex other = avs[k];
if ( v == other )
continue;
if ( other.isSelected() )
continue;
if ( other == v.next() || other == v.prev() )
continue;
if ( ! allowSnappingToVertexOfSameFixture && v.getFixture() == other.getFixture() )
continue;
float dist = v.wpos.distanceTo( other.wpos );
if ( dist > maxSnapRange )
continue;
if ( dist < closestDist ) {
closest = other;
closestDist = dist;
}
}
if ( closestDist != 999999 ){
newPoints.insertLast( v );
newPointPositions.insertLast( closest.wpos );
}
}
for (uint i = 0; i < newPoints.length; i++) {
vertex v = newPoints[i];
vec2 wpos = newPointPositions[i];
v.setWorldPos( wpos );
}