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

This commit is contained in:
Lorenzo Cogotti 2024-01-26 16:29:53 +01:00
parent d1d452f19f
commit 6108f5cb28
1 changed files with 32 additions and 26 deletions

58
vec.lua
View File

@ -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.
--
-- @param x (number) World coordinate X.
-- @param y (number) World coordinate Y.
-- @param vx (number|nil) Point of view X coordinate, defaults to w/2.
-- @param vy (number|nil) Point of view Y coordinate, defaults to h/2.
-- @param rot (number|nil) View rotation in radians, defaults to 0.
-- @param scale (number|nil) View scale (zoom), defaults to 1.
-- @param left (number|nil) Viewport left corner, defaults to 0.
-- @param top (number|nil) Viewport top corner, defaults to 0.
-- @param w (number|nil) Viewport width, defaults to love.graphics.getWidth().
-- @param h (number|nil) Viewport height, defaults to love.graphics.getHeight().
-- @number x World coordinate X.
-- @number y World coordinate Y.
-- @number xview Point of view X coordinate, defaults to w/2.
-- @number yview Point of view Y coordinate, defaults to h/2.
-- @number rot View rotation in radians, defaults to 0.
-- @number scale View scale (zoom), defaults to 1.
-- @number left Viewport left corner, defaults to 0.
-- @number top Viewport top corner, defaults to 0.
-- @number w Viewport width, defaults to love.graphics.getWidth().
-- @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
-- 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
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
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
scale = scale or 1
xparallax,yparallax = xparallax or 1, yparallax or 1
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
return x*scale + halfw + left, y*scale + halfh + top
end
--- Transform screen coordinates to world coordinates.
--
-- @param x (number) Screen coordinate X.
-- @param y (number) Screen coordinate Y.
-- @param vx (number|nil) Point of view X coordinate, defaults to w/2.
-- @param vy (number|nil) Point of view Y coordinate, defaults to h/2.
-- @param rot (number|nil) View rotation in radians, defaults to 0.
-- @param scale (number|nil) View scale (zoom), defaults to 1.
-- @param left (number|nil) Viewport left corner, defaults to 0.
-- @param top (number|nil) Viewport top corner, defaults to 0.
-- @param w (number|nil) Viewport width, defaults to love.graphics.getWidth().
-- @param h (number|nil) Viewport height, defaults to love.graphics.getHeight().
-- @number x Screen coordinate X.
-- @number y Screen coordinate Y.
-- @number xview Point of view X coordinate, defaults to w/2.
-- @number yview Point of view Y coordinate, defaults to h/2.
-- @number rot View rotation in radians, defaults to 0.
-- @number scale View scale (zoom), defaults to 1.
-- @number left Viewport left corner, defaults to 0.
-- @number top Viewport top corner, defaults to 0.
-- @number w Viewport width, defaults to love.graphics.getWidth().
-- @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
-- 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
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
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
scale = scale or 1
xparallax,yparallax = xparallax or 1, yparallax or 1
local sina,cosa = sin(-rot),cos(-rot)
x,y = (x - halfw - left) / scale, (y - halfh - top) / scale
x,y = cosa*x - sina*y, sina*x + cosa*y
return x+vx, y+vy
return x + xview*xparallax, y + yview*yparallax
end
return vec