extends Line2D const END_FITTING_DISTANCE=1 #fitting distance from end of pipe const CORNER_FITTING_DISTANCE=4 #fitting of last pipe before a corner piece const CORNER_NEXT_FITTING_DISTANCE=8 #fitting after corner const PIPE_MAX_LENGTH=2*55 #1m=55,172px const WEIGHT_DISTANCE_FROM_PIPE=9 const WEIGHT_RANDOM_OFFSET=0 const WEIGHT_DISTANCE_MIN=0.5*55 const WEIGHT_DISTANCE_MAX=4*55 const MIN_WEIGHT_DISTANCE_FROM_CORNER=20 #Smoothing Parameters @export var smoothingradius=30 @export var minsmoothingsradius=5 @export var maxsmoothangle=175 #degrees # Called when the node enters the scene tree for the first time. func _ready() -> void: #var staticbody=get_child(0) #get road collision object (StaticBody2D) var rightborder=true var staticbody:StaticBody2D=$road_r if staticbody==null: staticbody=$road_l rightborder=false #is left border if points.size()<2: print("Not enough points") return points=smoothLine(points) # Add Visual extras addCaps($fitting) addWeights($weight,rightborder) addTrim($trim,rightborder) # Create Collision Polygon var line_poly=Geometry2D.offset_polyline(points,width/2) for poly in line_poly: var col = CollisionPolygon2D.new() col.polygon=poly staticbody.add_child(col) ''' var vcol = Polygon2D.new() vcol.polygon=poly staticbody.add_child(vcol) ''' func smoothLine(points:PackedVector2Array) -> PackedVector2Array: print("Running Smooth Line") var newpoints:PackedVector2Array var lastlastpoint=null var lastpoint=null var pointscopy=points.duplicate() if closed: pointscopy.append(points[0]) #when closes, add first point to array to smooth the last before last corner for p in pointscopy: if lastlastpoint==null and lastpoint!=null: #p is 2nd point, lastpoint is first point newpoints.append(lastpoint) #add first point if lastlastpoint!=null: # around lastpoint two points exist smoothSegment(newpoints,lastlastpoint,lastpoint,p,smoothingradius,minsmoothingsradius,maxsmoothangle) lastlastpoint=lastpoint lastpoint=p if not closed: newpoints.append(points[-1]) #add last point print( "From numpoints="+str(points.size())+" to "+str(newpoints.size())+" (+"+str(newpoints.size()-points.size())+")"+" ="+str(newpoints.size()/points.size()*100.0)+"%") return newpoints func smoothSegment(pointarray:PackedVector2Array,plast,panchor,pnext,r,minr,maxsmoothangle): print("Smooth segment r"+str(r)) var dirlast=(plast-panchor).normalized() var lenlast=(plast-panchor).length() var dirnext=(pnext-panchor).normalized() var lennext=(pnext-panchor).length() if dirlast.dot(dirnext)