buildLand:function(){ this.xs=1; this.ys=0; this.mypoly.removeAll(true); points.x=[]; points.y=[]; superData=this.land.context.getImageData(this.xs,this.ys,800,600).data; console.log(superData); this.polygon=geom.contour(defineNonTransparent); //contour it //this.polygon=this.downSample(edgeDistance); //downsample it via interpolation this.polygon = simplify(this.polygon,1/edgeDistance); //downsample using douglas peucker algorithm this.polygon.forEach(this.addEdge, this);//this will build your polygon from the interpolation data, again you can build the primitive shape too if you want }, downSample: function(edgeDistance){ var polygon=[]; var x = 1 / game.world.width; for (var i = 0; i <= 1; i += x/edgeDistance) { var px = game.math.linearInterpolation(points.x,i); var py = game.math.linearInterpolation(points.y,i); //var px = game.math.catmullRomInterpolation(points.x, i); //same result in this case //var py = game.math.catmullRomInterpolation(points.y, i); //same result in this case polygon.push([px,py]); } return polygon; }, addEdge:function(element,index,polygon) { if(index<polygon.length-1){ point2=index<polygon.length-1?polygon[index+1]:this[0]; point1=element; distanceToNext=this.magnitude(point2[0],point1[0],point2[1],point1[1]); angleRadians = Math.atan2(point2[1] - point1[1], point2[0] - point1[0]); line=game.add.sprite(point1[0]+this.xs,point1[1]+this.ys); game.physics.p2.enable(line,true); line.body.static=true; line.body.setRectangle(distanceToNext,this.edgeSize,distanceToNext/2); line.body.rotation=angleRadians; this.mypoly.add(line); } }, magnitude:function(x1,x2,y1,y2){ return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); }, };
cedarcantab
Comments