Skip to content

Instantly share code, notes, and snippets.

@cgbeutler
Last active April 7, 2020 01:32
Show Gist options
  • Save cgbeutler/e14e57d8244011ed7b76ea92fe42da4e to your computer and use it in GitHub Desktop.
Save cgbeutler/e14e57d8244011ed7b76ea92fe42da4e to your computer and use it in GitHub Desktop.
tool
extends CollisionShape2D
export var draw := false setget set_draw
func set_draw( value :bool ) -> void:
draw = value
update()
export var color := Color.white setget set_color
func set_color( value :Color ) -> void:
color = value
if draw: update()
var __delay_ticker := INF
func _process(delta):
if __delay_ticker > 5:
__delay_ticker = 0.0
update()
else:
__delay_ticker += delta
func _draw():
if not draw: return
if shape is CapsuleShape2D: draw_polyline( capsule(shape.radius, shape.height), color )
elif shape is CircleShape2D: draw_polyline( circle(shape.radius), color )
elif shape is ConcavePolygonShape2D:
if len(shape.segments)>1: draw_multiline(shape.segments, color )
elif shape is ConvexPolygonShape2D:
if len(shape.points)>1:
var wrapped_poly = PoolVector2Array(shape.points)
wrapped_poly.push_back(shape.points[0])
draw_polyline(wrapped_poly, color )
elif shape is LineShape2D: push_error( "Not implemented" )
elif shape is RayShape2D: push_error( "Not implemented" )
elif shape is RectangleShape2D: draw_rect( rect(shape.extents), color, false )
elif shape is SegmentShape2D: draw_line(shape.a, shape.b, color)
else: push_error(" Unknown shape ")
static func circle( radius :float = 1.0, center :Vector2 = Vector2.ZERO ) -> PoolVector2Array:
var segments :int = 4*floor(radius/32)+16
var points = []; points.resize(segments+1)
var segment_size = TAU / segments
for i in range(segments):
points[i] = Vector2( cos(i * segment_size) * radius + center.x, sin(i * segment_size) * radius + center.y )
points[segments] = points[0]
return PoolVector2Array(points)
static func rect( extents :Vector2 ):
return Rect2(-extents.x, -extents.y, extents.x * 2, extents.y * 2)
static func capsule( radius :float, height :float ) -> PoolVector2Array:
var pill_width = radius*2
var half_height = clamp( height / 2.0, 0, INF )
var cap_circle :PoolVector2Array = circle( radius )
var points := []; points.resize(len(cap_circle) + 2)
var arch_count = (len(cap_circle) / 2) + 1
for i in range( arch_count ):
points[i] = cap_circle[i] + Vector2(0,half_height)
for i in range( arch_count - 1, len(cap_circle) ):
points[i + 1] = cap_circle[i] + Vector2(0,-half_height)
points[-1] = points[0]
return PoolVector2Array(points)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment