From 993e029a2f672d578f74a4b54e497f98dacc964a Mon Sep 17 00:00:00 2001 From: Tim Anema Date: Fri, 28 Nov 2014 16:56:05 -0500 Subject: [PATCH] made circle shadow bodies use arc instead of circle, also optimized so they were not drawn if not neccessary --- lib/body.lua | 15 ++++++++++++++- lib/init.lua | 2 +- lib/light.lua | 3 +++ lib/stencils.lua | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/body.lua b/lib/body.lua index 7da797b..c92f31f 100644 --- a/lib/body.lua +++ b/lib/body.lua @@ -636,7 +636,20 @@ function body:calculateCircleShadow(light) local radius = math.sqrt(math.pow(curShadowGeometry[7] - curShadowGeometry[5], 2) + math.pow(curShadowGeometry[8]-curShadowGeometry[6], 2)) / 2 local cx, cy = (curShadowGeometry[5] + curShadowGeometry[7])/2, (curShadowGeometry[6] + curShadowGeometry[8])/2 - curShadowGeometry.circle = {cx, cy, radius} + local angle1 = math.atan2(curShadowGeometry[6] - cy, curShadowGeometry[5] - cx) + local angle2 = math.atan2(curShadowGeometry[8] - cy, curShadowGeometry[7] - cx) + local distance1 = math.sqrt(math.pow(light.x - self.x, 2) + math.pow(light.y - self.y, 2)) / 2 + local distance2 = math.sqrt(math.pow(light.x - cx, 2) + math.pow(light.y - cy, 2)) / 2 + + if distance1 <= self.radius then + curShadowGeometry.circle = {cx, cy, radius, 0, (math.pi * 2)} + elseif distance2 < light.range then -- dont draw circle if way off screen + if angle1 > angle2 then + curShadowGeometry.circle = {cx, cy, radius, angle1, angle2} + else + curShadowGeometry.circle = {cx, cy, radius, angle1 - math.pi, angle2 - math.pi} + end + end curShadowGeometry.red = self.red curShadowGeometry.green = self.green diff --git a/lib/init.lua b/lib/init.lua index 77854c5..e53fa3f 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -1,7 +1,7 @@ --[[ The MIT License (MIT) -Copyright (c) 2014 Marcus Ihde +Copyright (c) 2014 Marcus Ihde, Tim Anema Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/lib/light.lua b/lib/light.lua index a2a7f42..c8d8f29 100644 --- a/lib/light.lua +++ b/lib/light.lua @@ -165,6 +165,9 @@ function light:drawShadow(l,t,w,h,s,bodies, canvas) shadow_geometry[k].blue * (1.0 - shadow_geometry[k].alpha) ) love.graphics.polygon("fill", unpack(shadow_geometry[k])) + if shadow_geometry[k].circle then + love.graphics.arc("fill", unpack(shadow_geometry[k].circle)) + end end end diff --git a/lib/stencils.lua b/lib/stencils.lua index 2a1a903..e3ff160 100644 --- a/lib/stencils.lua +++ b/lib/stencils.lua @@ -7,7 +7,7 @@ function stencils.shadow(geometry, bodies) if geometry[i].alpha == 1.0 then love.graphics.polygon("fill", unpack(geometry[i])) if geometry[i].circle then - love.graphics.circle("fill", unpack(geometry[i].circle)) + love.graphics.arc("fill", unpack(geometry[i].circle)) end end end