Ok, here goes.. But I'm kind of embarassed
😳 , because I see now that it's checking against *ALL* other points multiple times! So I don't think it's even O(n*n), but worse.
How to use:Just pass your simple polygon to the function getConvexShapeSet() You can also tell the function how many points maximum you want per shape, because Box2D shapes can only be 8 vertices by default. The function will return an array of arrays, each sub-array contains a single shape.
The way I use it: every time the terrain changes I make a pointset out of the heightmap, and then I split that up into convex poly's with this function.
Code:
package noio.game.geom{
import flash.display.Shape;
import flash.display.Sprite;
public class Poly {
/*
* Transforms a heightmap array to a pointset
* @param h the heightmap
* @param step the distance between points on the heightmap
* @param base the distance that is added to heightmap values. (because flash coordinates are upside-down)
* @param pointclass, a class that has X and Y properties, and a constructor that takes (x,y):
* for example: the Box2D b2Vec2 Class (so you can return points to b2Vec2)
*/
public static function heightmapToPointset(h:Array,step:Number,base:Number,pointClass:Class = null):Array{
if(!pointClass) pointClass = DefaultPoint
var output:Array = new Array();
var xpos:Number = 0;
output.push(new pointClass(0,base));
for each(var val:Number in h){
output.push(new pointClass(xpos,base-val));
xpos += step;
}
output.push(new pointClass(xpos-step,base));
return output;
}
/*
* Transforms a heightmap array to a pointset, but points that don't cause large extrusions are left out.
* So for example: A whole flat heightmap (like 10,10,10,10,10,10)
* would only generate 4 points (top, right, bottom ,left).
* "tol" stands for tolerance, and that is hardcoded for convenience.
* @param h the heightmap
* @param step the distance between points on the heightmap
* @param base the distance that is added to heightmap values. (because flash coordinates are upside-down)
* @param pointclass, a class that has X and Y properties, and a constructor that takes (x,y):
* for example: the Box2D b2Vec2 Class (so you can return points to b2Vec2)
*/
public static function heightmapToSimplifiedPointset(h:Array,step:Number,base:Number, pointClass:Class = null):Array{
if(!pointClass) pointClass = DefaultPoint;
var output:Array = new Array();
var xpos:Number = step;
var tol = step/6;
var hL = h.length;
output.push(new pointClass(0,base));
output.push(new pointClass(0,base-h[0]));
var last:int = 0;
for(var i:int = 1; i<hL - 1; ++i){
if(Math.abs(h[last] + h[i+1] - 2*h[i]) > tol){
output.push(new pointClass(xpos,base-h[i]));
last = i;
}
xpos += step;
}
output.push(new pointClass(xpos,base - h[hL-1]));
output.push(new pointClass(xpos,base));
return output;
}
/*
* Returns a set of convex polygons that make up the given polygon. Polygon cannot overlap and has to be in CCW order
*/
public static function getConvexShapeSet(points:Array, maxPointsPerShape:int = 8):Array {
var shapes:Array = new Array()
while(points.length > 2){
var curShape = points.slice(0,3);
var STOP:int = 40; //Stop after 40 tries, (when you give it a complex polygon, it will never find an ear)
//Shift the polygon to find an 'ear'.
while((!Poly.isConvex(curShape)) || convexIncludesAny(curShape,points.slice(3))){
points.push(points.shift());
curShape = points.slice(0,3);
if(--STOP < 0){ throw new Error("Poly.getConvexShapeSet: Cannot find polygon ear")};
}
points.splice(1,1);
//shapes.push(curShape);
//continue;
//Expand the 'ear' to span more points.
var cL = curShape.length;
while((points.length > 2) && (cL < maxPointsPerShape) &&
(isLeft(curShape[cL-2],curShape[cL-1],points[2]) > 0) &&
(isLeft(curShape[cL-1],points[2],curShape[0]) > 0) &&
(isLeft(points[2],curShape[0],curShape[1]) > 0) &&
!convexIncludesAny(curShape.concat(points[2]),points.slice(3))
){
//If new shape is still convex with one point added, add that point
//trace("expansion loop",points[2].x,points[2].y);
curShape.push(points[2])
points.splice(1,1);
cL = curShape.length;
}
shapes.push(curShape);
}
return shapes;
}
/*
* Test whether the given polygon is convex, points have to be given in CCW order
*/
public static function isConvex(points:Array):Boolean {
var l = points.length
if(l <3 ) return false;
for(var i:int = 0; i < l -2 ; i++){
if(isLeft(points[i],points[i+1],points[i+2]) < 0) {
return false;
}
}
if (isLeft(points[l - 2], points[ l - 1], points[0]) < 0){
return false;
}
if(isLeft(points[l - 1], points[0], points[1]) < 0) {
return false;
}
return true;
}
/*
* Test whether P2 is on/left of/right of the line through P0 and P1
*/
public static function isLeft(P0:*, P1:*, P2:* ):Number
{
return (P1.x - P0.x)*(P2.y - P0.y) - (P2.x - P0.x)*(P1.y - P0.y);
}
public static function convexIncludesAny(shape:Array, points:Array){
for each(var point in points){
if(convexIncludes(shape, point)) return true;
}
return false;
}
public static function convexIncludes(points:Array,point:*){
for(var i:int = 0, j = points.length - 1; i < points.length; j = i++){
if(isLeft(points[j],points[i], point) < 0){
return false;
}
}
return true;
}
public static function drawMultiple(shapes:Array){
var s = new Sprite();
for each(var shape:Array in shapes){
s.addChild(draw(shape));
}
return s;
}
public static function draw(points:Array):Shape{
var s = new Shape;
s.graphics.lineStyle(1,0xffffff);
s.graphics.moveTo(points[0].x,points[0].y);
for ( var i : int = 1; i < points.length ; i ++){
s.graphics.lineTo(points[i].x,points[i].y);
}
s.graphics.lineTo(points[0].x,points[0].y);
return s;
}
}
}
class DefaultPoint {
var x : Number;
var y : Number;
public function DefaultPoint(_x:Number = 0, _y:Number = 0){
x = _x; y = _y;
}
}