[vec] Add parallax factor to toworldcoords() and toscreencoords()

master
Lorenzo Cogotti 3 months ago
parent d1d452f19f
commit 6108f5cb28

@ -206,66 +206,72 @@ function vec.angleto(x1,y1, x2,y2) return atan2(y1,x1) - atan2(y2,x2) end
--- Transform world coordinates to screen coordinates. --- Transform world coordinates to screen coordinates.
-- --
-- @param x (number) World coordinate X. -- @number x World coordinate X.
-- @param y (number) World coordinate Y. -- @number y World coordinate Y.
-- @param vx (number|nil) Point of view X coordinate, defaults to w/2. -- @number xview Point of view X coordinate, defaults to w/2.
-- @param vy (number|nil) Point of view Y coordinate, defaults to h/2. -- @number yview Point of view Y coordinate, defaults to h/2.
-- @param rot (number|nil) View rotation in radians, defaults to 0. -- @number rot View rotation in radians, defaults to 0.
-- @param scale (number|nil) View scale (zoom), defaults to 1. -- @number scale View scale (zoom), defaults to 1.
-- @param left (number|nil) Viewport left corner, defaults to 0. -- @number left Viewport left corner, defaults to 0.
-- @param top (number|nil) Viewport top corner, defaults to 0. -- @number top Viewport top corner, defaults to 0.
-- @param w (number|nil) Viewport width, defaults to love.graphics.getWidth(). -- @number w Viewport width, defaults to love.graphics.getWidth().
-- @param h (number|nil) Viewport height, defaults to love.graphics.getHeight(). -- @number h Viewport height, defaults to love.graphics.getHeight().
-- @number xparallax Parallax factor over X, defaults to 1.
-- @number yparallax Parallax factor over Y, defaults to 1.
-- --
-- @return (x,y) Transformed to screen coordinates according to -- @return (x,y) Transformed to screen coordinates according to
-- viewport and offset. -- viewport and offset.
function vec.toscreencoords(x,y, vx,vy, rot, scale, left,top, w,h) function vec.toscreencoords(x,y, xview,yview, rot, scale, left,top,w,h, xparallax,yparallax)
left,top = left or 0, top or 0 left,top = left or 0, top or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight() w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
local halfw,halfh = w/2, h/2 local halfw,halfh = w/2, h/2
vx,vy = vx or halfw, vy or halfh xview,yview = xview or halfw, yview or halfh
rot = rot or 0 rot = rot or 0
scale = scale or 1 scale = scale or 1
xparallax,yparallax = xparallax or 1, yparallax or 1
local sina,cosa = sin(rot),cos(rot) local sina,cosa = sin(rot),cos(rot)
x,y = x - vx, y - vy x,y = x - xview*xparallax, y - yview*yparallax
x,y = cosa*x - sina*y, sina*x + cosa*y x,y = cosa*x - sina*y, sina*x + cosa*y
return x*scale + halfw + left, y*scale + halfh + top return x*scale + halfw + left, y*scale + halfh + top
end end
--- Transform screen coordinates to world coordinates. --- Transform screen coordinates to world coordinates.
-- --
-- @param x (number) Screen coordinate X. -- @number x Screen coordinate X.
-- @param y (number) Screen coordinate Y. -- @number y Screen coordinate Y.
-- @param vx (number|nil) Point of view X coordinate, defaults to w/2. -- @number xview Point of view X coordinate, defaults to w/2.
-- @param vy (number|nil) Point of view Y coordinate, defaults to h/2. -- @number yview Point of view Y coordinate, defaults to h/2.
-- @param rot (number|nil) View rotation in radians, defaults to 0. -- @number rot View rotation in radians, defaults to 0.
-- @param scale (number|nil) View scale (zoom), defaults to 1. -- @number scale View scale (zoom), defaults to 1.
-- @param left (number|nil) Viewport left corner, defaults to 0. -- @number left Viewport left corner, defaults to 0.
-- @param top (number|nil) Viewport top corner, defaults to 0. -- @number top Viewport top corner, defaults to 0.
-- @param w (number|nil) Viewport width, defaults to love.graphics.getWidth(). -- @number w Viewport width, defaults to love.graphics.getWidth().
-- @param h (number|nil) Viewport height, defaults to love.graphics.getHeight(). -- @number h Viewport height, defaults to love.graphics.getHeight().
-- @number xparallax Parallax factor over X, defaults to 1.
-- @number yparallax Parallax factor over Y, defaults to 1.
-- --
-- @return (x,y) Transformed to world coordinates according to -- @return (x,y) Transformed to world coordinates according to
-- viewport and offset. -- viewport and offset.
function vec.toworldcoords(x,y, vx,vy, rot, scale, left,top,w,h) function vec.toworldcoords(x,y, xview,yview, rot, scale, left,top,w,h, xparallax,yparallax)
left, top = left or 0, top or 0 left, top = left or 0, top or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight() w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
local halfw,halfh = w/2, h/2 local halfw,halfh = w/2, h/2
vx,vy = vx or halfw, vy or halfh xview,yview = xview or halfw, yview or halfh
rot = rot or 0 rot = rot or 0
scale = scale or 1 scale = scale or 1
xparallax,yparallax = xparallax or 1, yparallax or 1
local sina,cosa = sin(-rot),cos(-rot) local sina,cosa = sin(-rot),cos(-rot)
x,y = (x - halfw - left) / scale, (y - halfh - top) / scale x,y = (x - halfw - left) / scale, (y - halfh - top) / scale
x,y = cosa*x - sina*y, sina*x + cosa*y x,y = cosa*x - sina*y, sina*x + cosa*y
return x+vx, y+vy return x + xview*xparallax, y + yview*yparallax
end end
return vec return vec

Loading…
Cancel
Save