scaled circles and fixed complex refractions example

This commit is contained in:
Tim Anema 2015-02-18 16:20:21 -05:00
parent 6314e1907b
commit 7852da20d6
2 changed files with 22 additions and 14 deletions

View File

@ -162,6 +162,11 @@ function love.update(dt)
tileX = tileX + dt * 32.0 tileX = tileX + dt * 32.0
tileY = tileY + dt * 8.0 tileY = tileY + dt * 8.0
for i = 1, phyCnt do
if phyLight[i]:getType() == "refraction" then
phyLight[i]:setNormalTileOffset(tileX, tileY)
end
end
-- draw shader -- draw shader
if colorAberration > 0.0 then if colorAberration > 0.0 then

View File

@ -303,7 +303,7 @@ end
-- get radius -- get radius
function body:getRadius() function body:getRadius()
return self.radius return self.radius * self.scalex
end end
-- set radius -- set radius
@ -547,9 +547,11 @@ end
function body:inRange(l, t, w, h, s) function body:inRange(l, t, w, h, s)
local radius local radius
if self.type == 'circle' then if self.type == 'circle' then
radius = self.radius radius = self.radius * self.scalex
else else
radius = (self.width > self.height and self.width or self.height) local sw = (self.width * self.scalex)
local sh = (self.height * self.scaley)
radius = (sw > sh and sw or sh)
end end
local bx, by, bw, bh = self.x - radius, self.y - radius, radius * 2, radius * 2 local bx, by, bw, bh = self.x - radius, self.y - radius, radius * 2, radius * 2
@ -576,7 +578,7 @@ function body:drawGlow()
love.graphics.setColor(self.glowRed * self.glowStrength, self.glowGreen * self.glowStrength, self.glowBlue * self.glowStrength) love.graphics.setColor(self.glowRed * self.glowStrength, self.glowGreen * self.glowStrength, self.glowBlue * self.glowStrength)
if self.type == "circle" then if self.type == "circle" then
love.graphics.circle("fill", self.x, self.y, self.radius) love.graphics.circle("fill", self.x, self.y, self.radius * self.scalex)
elseif self.type == "polygon" then elseif self.type == "polygon" then
love.graphics.polygon("fill", unpack(self.data)) love.graphics.polygon("fill", unpack(self.data))
elseif (self.type == "image" or self.type == "animation") and self.img then elseif (self.type == "image" or self.type == "animation") and self.img then
@ -614,7 +616,7 @@ function body:drawRefraction()
if not self.refractive then if not self.refractive then
if self.type == "circle" then if self.type == "circle" then
love.graphics.circle("fill", self.x, self.y, self.radius) love.graphics.circle("fill", self.x, self.y, self.radius * self.scalex)
elseif self.type == "polygon" then elseif self.type == "polygon" then
love.graphics.polygon("fill", unpack(self.data)) love.graphics.polygon("fill", unpack(self.data))
elseif self.type == "image" and self.img then elseif self.type == "image" and self.img then
@ -715,23 +717,24 @@ function body:drawCircleShadow(light)
local lightPosition = vector(light.x, light.y) local lightPosition = vector(light.x, light.y)
local lh = lightPosition * self.zheight local lh = lightPosition * self.zheight
local height_diff = (self.zheight - light.z) local height_diff = (self.zheight - light.z)
local radius = self.radius * self.scalex
if height_diff == 0 then -- prevent inf if height_diff == 0 then -- prevent inf
height_diff = -0.001 height_diff = -0.001
end end
local angle = math.atan2(light.x - selfPos.x, selfPos.y - light.y) + math.pi / 2 local angle = math.atan2(light.x - selfPos.x, selfPos.y - light.y) + math.pi / 2
local point1 = vector(selfPos.x + math.sin(angle) * self.radius, local point1 = vector(selfPos.x + math.sin(angle) * radius,
selfPos.y - math.cos(angle) * self.radius) selfPos.y - math.cos(angle) * radius)
local point2 = vector(selfPos.x - math.sin(angle) * self.radius, local point2 = vector(selfPos.x - math.sin(angle) * radius,
selfPos.y + math.cos(angle) * self.radius) selfPos.y + math.cos(angle) * radius)
local point3 = (lh - (point1 * light.z))/height_diff local point3 = (lh - (point1 * light.z))/height_diff
local point4 = (lh - (point2 * light.z))/height_diff local point4 = (lh - (point2 * light.z))/height_diff
local radius = point3:dist(point4)/2 local shadow_radius = point3:dist(point4)/2
local circleCenter = (point3 + point4)/2 local circleCenter = (point3 + point4)/2
if lightPosition:dist(selfPos) <= self.radius then if lightPosition:dist(selfPos) <= radius then
love.graphics.circle("fill", circleCenter.x, circleCenter.y, radius) love.graphics.circle("fill", circleCenter.x, circleCenter.y, shadow_radius)
else else
love.graphics.polygon("fill", point1.x, point1.y, love.graphics.polygon("fill", point1.x, point1.y,
point2.x, point2.y, point2.x, point2.y,
@ -741,9 +744,9 @@ function body:drawCircleShadow(light)
local angle1 = math.atan2(point3.y - circleCenter.y, point3.x - circleCenter.x) local angle1 = math.atan2(point3.y - circleCenter.y, point3.x - circleCenter.x)
local angle2 = math.atan2(point4.y - circleCenter.y, point4.x - circleCenter.x) local angle2 = math.atan2(point4.y - circleCenter.y, point4.x - circleCenter.x)
if angle1 < angle2 then if angle1 < angle2 then
love.graphics.arc("fill", circleCenter.x, circleCenter.y, radius, angle1, angle2) love.graphics.arc("fill", circleCenter.x, circleCenter.y, shadow_radius, angle1, angle2)
else else
love.graphics.arc("fill", circleCenter.x, circleCenter.y, radius, angle1 - math.pi, angle2 - math.pi) love.graphics.arc("fill", circleCenter.x, circleCenter.y, shadow_radius, angle1 - math.pi, angle2 - math.pi)
end end
end end
end end