made polygons moveable

This commit is contained in:
Tim Anema 2014-12-02 21:26:46 -05:00
parent 224822de31
commit c0a80da546
4 changed files with 53 additions and 36 deletions

View File

@ -1,10 +1,8 @@
:todoing :todoing
-make sure all draw calls check if the object is within range -make sure all draw calls check if the object is within range
-refactor rectablges to be polygons to reduce code -refactor rectablges to be polygons to reduce code
-handle polygons in a moveable way
-optimize shadow body calculations and drawing methods -optimize shadow body calculations and drawing methods
# light_world.lua # light_world.lua
This is the light modeling done by Priorblue [here](https://bitbucket.org/PriorBlue/love2d-light-and-shadow-engine), This is the light modeling done by Priorblue [here](https://bitbucket.org/PriorBlue/love2d-light-and-shadow-engine),

View File

@ -195,7 +195,7 @@ function love.update(dt)
for i = 1, phyCnt do for i = 1, phyCnt do
if phyBody[i] and (phyBody[i]:isAwake() or offsetChanged) then if phyBody[i] and (phyBody[i]:isAwake() or offsetChanged) then
if phyLight[i]:getType() == "polygon" then if phyLight[i]:getType() == "polygon" then
phyLight[i]:setPoints(phyBody[i]:getWorldPoints(phyShape[i]:getPoints())) --phyLight[i]:setPoints(phyBody[i]:getWorldPoints(phyShape[i]:getPoints()))
elseif phyLight[i]:getType() == "circle" then elseif phyLight[i]:getType() == "circle" then
phyLight[i]:setPosition(phyBody[i]:getPosition()) phyLight[i]:setPosition(phyBody[i]:getPosition())
elseif phyLight[i]:getType() == "image" then elseif phyLight[i]:getType() == "image" then

View File

@ -31,6 +31,7 @@ function love.load()
-- create shadow bodys -- create shadow bodys
circleTest = lightWorld:newCircle(256, 256, 16) circleTest = lightWorld:newCircle(256, 256, 16)
rectangleTest = lightWorld:newRectangle(512, 512, 64, 64) rectangleTest = lightWorld:newRectangle(512, 512, 64, 64)
polygonTest = lightWorld:newPolygon(100, 200, 120, 200, 150, 250, 100, 250)
imageTest = lightWorld:newImage(image, 64, 64, 24, 6) imageTest = lightWorld:newImage(image, 64, 64, 24, 6)
imageTest:setNormalMap(image_normal) imageTest:setNormalMap(image_normal)
@ -83,7 +84,7 @@ function love.keypressed(k)
lightWorld:remove(lightMouse) lightWorld:remove(lightMouse)
elseif k == "g" then elseif k == "g" then
lightWorld:remove(circleTest) lightWorld:remove(circleTest)
elseif k == "h" then elseif k == "d" then
lightWorld:remove(rectangleTest) lightWorld:remove(rectangleTest)
end end
end end
@ -103,6 +104,18 @@ function love.update(dt)
x = x + dt * 200 x = x + dt * 200
end end
if love.keyboard.isDown("k") then
polygonTest:move(0, -(dt * 200))
elseif love.keyboard.isDown("j") then
polygonTest:move(0, (dt * 200))
end
if love.keyboard.isDown("h") then
polygonTest:move(-(dt * 200), 0)
elseif love.keyboard.isDown("l") then
polygonTest:move((dt * 200), 0)
end
if love.keyboard.isDown("-") then if love.keyboard.isDown("-") then
scale = scale - 0.01 scale = scale - 0.01
elseif love.keyboard.isDown("=") then elseif love.keyboard.isDown("=") then
@ -141,6 +154,7 @@ function love.draw()
local cx, cy = circleTest:getPosition() local cx, cy = circleTest:getPosition()
love.graphics.circle("fill", cx, cy, circleTest:getRadius()) love.graphics.circle("fill", cx, cy, circleTest:getRadius())
love.graphics.polygon("fill", rectangleTest:getPoints()) love.graphics.polygon("fill", rectangleTest:getPoints())
love.graphics.polygon("fill", polygonTest:getPoints())
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
love.graphics.draw(image, 64 - image:getWidth() * 0.5, 64 - image:getHeight() * 0.5) love.graphics.draw(image, 64 - image:getWidth() * 0.5, 64 - image:getHeight() * 0.5)
end) end)

View File

@ -58,34 +58,7 @@ function body:init(id, type, ...)
self:setShadowType('rectangle', args[3], args[4]) self:setShadowType('rectangle', args[3], args[4])
elseif self.type == "polygon" then elseif self.type == "polygon" then
local points = {...} self:setPoints(...)
self.x, self.y, self.width, self.height = points[1], points[2], 0, 0
for i = 1, #points, 2 do
local px, py = points[i], points[i+1]
if px < self.x then self.x = px end
if py < self.y then self.y = py end
if px > self.width then self.width = px end
if py > self.height then self.height = py end
end
self.width = self.width - self.x
self.height = self.height - self.y
for i = 1, #points, 2 do
points[i], points[i+1] = points[i] - self.x, points[i+1] - self.y
end
poly_canvas = love.graphics.newCanvas(self.width, self.height)
util.drawto(poly_canvas, 0, 0, 1, function()
love.graphics.polygon('fill', points)
end)
self.img = love.graphics.newImage(poly_canvas:getImageData())
self.imgWidth = self.img:getWidth()
self.imgHeight = self.img:getHeight()
self.ix = self.imgWidth * 0.5
self.iy = self.imgHeight * 0.5
self:generateNormalMapFlat("top")
self:setShadowType('polygon', ...)
elseif self.type == "image" then elseif self.type == "image" then
self.img = args[1] self.img = args[1]
self.x = args[2] or 0 self.x = args[2] or 0
@ -106,6 +79,7 @@ function body:init(id, type, ...)
self:initNormal(...) self:initNormal(...)
self.reflection = true self.reflection = true
end end
self.old_x, self.old_y = self.x, self.y
end end
function body:initNormal(...) function body:initNormal(...)
@ -138,13 +112,19 @@ end
-- refresh -- refresh
function body:refresh() function body:refresh()
if self.x and self.y and self.width and self.height and self.ox and self.oy then if self.shadowType == "rectangle" then
self.data = { self.data = {
self.x - self.ox, self.y - self.oy, self.x - self.ox, self.y - self.oy,
self.x - self.ox + self.width, self.y - self.oy, self.x - self.ox + self.width, self.y - self.oy,
self.x - self.ox + self.width, self.y - self.oy + self.height, self.x - self.ox + self.width, self.y - self.oy + self.height,
self.x - self.ox, self.y - self.oy + self.height self.x - self.ox, self.y - self.oy + self.height
} }
elseif self.shadowType == 'polygon' and (self.old_x ~= self.x or self.old_y ~= self.y) then
local dx, dy = self.x - self.old_x, self.y - self.old_y
for i = 1, #self.data, 2 do
self.data[i], self.data[i+1] = self.data[i] + dx, self.data[i+1] + dy
end
self.old_x, self.old_y = self.x, self.y
end end
end end
@ -214,7 +194,6 @@ function body:setImageOffset(ix, iy)
if ix ~= self.ix or iy ~= self.iy then if ix ~= self.ix or iy ~= self.iy then
self.ix = ix self.ix = ix
self.iy = iy self.iy = iy
self:refresh()
end end
end end
@ -223,7 +202,6 @@ function body:setNormalOffset(nx, ny)
if nx ~= self.nx or ny ~= self.ny then if nx ~= self.nx or ny ~= self.ny then
self.nx = nx self.nx = nx
self.ny = ny self.ny = ny
self:refresh()
end end
end end
@ -253,7 +231,34 @@ end
-- set polygon data -- set polygon data
function body:setPoints(...) function body:setPoints(...)
self.data = {...} local points = {...}
self.x, self.y, self.width, self.height = points[1], points[2], 0, 0
for i = 1, #points, 2 do
local px, py = points[i], points[i+1]
if px < self.x then self.x = px end
if py < self.y then self.y = py end
if px > self.width then self.width = px end
if py > self.height then self.height = py end
end
self.width = self.width - self.x
self.height = self.height - self.y
for i = 1, #points, 2 do
points[i], points[i+1] = points[i] - self.x, points[i+1] - self.y
end
poly_canvas = love.graphics.newCanvas(self.width, self.height)
util.drawto(poly_canvas, 0, 0, 1, function()
love.graphics.polygon('fill', points)
end)
self.img = love.graphics.newImage(poly_canvas:getImageData())
self.imgWidth = self.img:getWidth()
self.imgHeight = self.img:getHeight()
self.ix = self.imgWidth * 0.5
self.iy = self.imgHeight * 0.5
self:generateNormalMapFlat("top")
self.nx, self.ny = 0, 0
self:setShadowType('polygon', ...)
end end
-- get polygon data -- get polygon data