mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
scaling and rotation now works
This commit is contained in:
parent
115d64f51f
commit
9b5765e0ed
55
lib/body.lua
55
lib/body.lua
@ -117,30 +117,22 @@ end
|
|||||||
-- refresh
|
-- refresh
|
||||||
function body:refresh()
|
function body:refresh()
|
||||||
if self.shadowType == 'polygon' and self:has_changed() then
|
if self.shadowType == 'polygon' and self:has_changed() then
|
||||||
local dx, dy, dr, dsx, dsy = self:changes()
|
self.data = {unpack(self.unit_data)}
|
||||||
if self:position_changed() then
|
|
||||||
for i = 1, #self.data, 2 do
|
|
||||||
self.data[i], self.data[i+1] = self.data[i] + dx, self.data[i+1] + dy
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if self:rotation_changed() then
|
|
||||||
local center = vector(self.x, self.y)
|
local center = vector(self.x, self.y)
|
||||||
for i = 1, #self.data, 2 do
|
for i = 1, #self.data, 2 do
|
||||||
self.data[i], self.data[i+1] = vector(self.data[i], self.data[i+1]):rotateAround(center, dr):unpack()
|
local point = vector(self.data[i], self.data[i+1])
|
||||||
end
|
point = point:rotate(self.rotation)
|
||||||
end
|
point = point:scale(self.scalex, self.scaley)
|
||||||
if self:scale_changed() then
|
self.data[i], self.data[i+1] = (point + center):unpack()
|
||||||
for i = 1, #self.data, 2 do
|
|
||||||
self.data[i] = self.x + (self.data[i] - self.x) + ((self.data[i] - self.x) * dsx)
|
|
||||||
self.data[i+1] = self.y + (self.data[i+1] - self.y) + ((self.data[i+1] - self.y) * dsy)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
self:commit_changes()
|
self:commit_changes()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:has_changed()
|
function body:has_changed()
|
||||||
return self:position_changed() or self:rotation_changed() or self:scale_changed()
|
return self:position_changed() or
|
||||||
|
self:rotation_changed() or
|
||||||
|
self:scale_changed()
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:position_changed()
|
function body:position_changed()
|
||||||
@ -157,14 +149,6 @@ function body:scale_changed()
|
|||||||
self.old_scaley ~= self.scaley
|
self.old_scaley ~= self.scaley
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:changes()
|
|
||||||
return self.x - self.old_x,
|
|
||||||
self.y - self.old_y,
|
|
||||||
self.rotation - self.old_rotation,
|
|
||||||
self.scalex - self.old_scalex,
|
|
||||||
self.scaley - self.old_scaley
|
|
||||||
end
|
|
||||||
|
|
||||||
function body:commit_changes()
|
function body:commit_changes()
|
||||||
self.old_x, self.old_y = self.x, self.y
|
self.old_x, self.old_y = self.x, self.y
|
||||||
self.old_rotation = self.rotation
|
self.old_rotation = self.rotation
|
||||||
@ -331,28 +315,37 @@ end
|
|||||||
|
|
||||||
-- set polygon data
|
-- set polygon data
|
||||||
function body:setPoints(...)
|
function body:setPoints(...)
|
||||||
local points = {...}
|
self.unit_data = {...}
|
||||||
self.x, self.y, self.width, self.height = points[1], points[2], 0, 0
|
|
||||||
for i = 1, #points, 2 do
|
--calculate l,r,t,b
|
||||||
local px, py = points[i], points[i+1]
|
self.x, self.y, self.width, self.height = self.unit_data[1], self.unit_data[2], 0, 0
|
||||||
|
for i = 1, #self.unit_data, 2 do
|
||||||
|
local px, py = self.unit_data[i], self.unit_data[i+1]
|
||||||
if px < self.x then self.x = px end
|
if px < self.x then self.x = px end
|
||||||
if py < self.y then self.y = py end
|
if py < self.y then self.y = py end
|
||||||
if px > self.width then self.width = px end
|
if px > self.width then self.width = px end
|
||||||
if py > self.height then self.height = py end
|
if py > self.height then self.height = py end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- normalize width and height
|
-- normalize width and height
|
||||||
self.width = self.width - self.x
|
self.width = self.width - self.x
|
||||||
self.height = self.height - self.y
|
self.height = self.height - self.y
|
||||||
for i = 1, #points, 2 do
|
for i = 1, #self.unit_data, 2 do
|
||||||
points[i], points[i+1] = points[i] - self.x, points[i+1] - self.y
|
self.unit_data[i], self.unit_data[i+1] = self.unit_data[i] - self.x, self.unit_data[i+1] - self.y
|
||||||
end
|
end
|
||||||
self.x = self.x + (self.width * 0.5)
|
self.x = self.x + (self.width * 0.5)
|
||||||
self.y = self.y + (self.height * 0.5)
|
self.y = self.y + (self.height * 0.5)
|
||||||
|
|
||||||
local poly_canvas = love.graphics.newCanvas(self.width, self.height)
|
local poly_canvas = love.graphics.newCanvas(self.width, self.height)
|
||||||
util.drawto(poly_canvas, 0, 0, 1, function()
|
util.drawto(poly_canvas, 0, 0, 1, function()
|
||||||
love.graphics.polygon('fill', points)
|
love.graphics.polygon('fill', self.unit_data)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
--normalize points to be around the center x y
|
||||||
|
for i = 1, #self.unit_data, 2 do
|
||||||
|
self.unit_data[i], self.unit_data[i+1] = self.unit_data[i] - self.width * 0.5, self.unit_data[i+1] - self.height * 0.5
|
||||||
|
end
|
||||||
|
|
||||||
self.img = love.graphics.newImage(poly_canvas:getImageData())
|
self.img = love.graphics.newImage(poly_canvas:getImageData())
|
||||||
self.imgWidth = self.img:getWidth()
|
self.imgWidth = self.img:getWidth()
|
||||||
self.imgHeight = self.img:getHeight()
|
self.imgHeight = self.img:getHeight()
|
||||||
|
@ -47,19 +47,13 @@ function vector:unpack()
|
|||||||
return self.x, self.y
|
return self.x, self.y
|
||||||
end
|
end
|
||||||
|
|
||||||
function vector:rotateAround(origin, angle)
|
function vector:rotate(theta)
|
||||||
local s = math.sin(angle)
|
return new((math.cos(theta) * self.x) - (math.sin(theta) * self.y),
|
||||||
local c = math.cos(angle)
|
(math.sin(theta) * self.x) + (math.cos(theta) * self.y))
|
||||||
-- translate point back to origin
|
end
|
||||||
self.x = self.x - origin.x
|
|
||||||
self.y = self.y - origin.y
|
function vector:scale(sx, sy)
|
||||||
-- rotate point
|
return new(self.x * sx, self.y * sy)
|
||||||
self.x = (self.x * c - self.y * s)
|
|
||||||
self.y = (self.x * s + self.y * c)
|
|
||||||
-- translate point back
|
|
||||||
self.x = self.x + origin.x
|
|
||||||
self.y = self.y + origin.y
|
|
||||||
return self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return setmetatable({new = new}, {__call = function(_, ...) return new(...) end})
|
return setmetatable({new = new}, {__call = function(_, ...) return new(...) end})
|
||||||
|
Loading…
Reference in New Issue
Block a user