Added a non-matrix example of rotating a square texture
mNo edit summary |
(Added a non-matrix example of rotating a square texture) |
||
| Line 153: | Line 153: | ||
local angle = math.rad(90); | local angle = math.rad(90); | ||
setCoords(texture, cos(angle), sin(angle), 1, -sin(angle), cos(angle), 1); | setCoords(texture, cos(angle), sin(angle), 1, -sin(angle), cos(angle), 1); | ||
==Simple rotation of square textures around the center== | |||
While the above section about matrix manipulation gives a lot of flexibility, most often much simpler math suffice. | |||
Here's an example of rotating a square texture around its center with an arbitrary angle: | |||
local s2 = sqrt(2); | |||
local cos, sin, rad = math.cos, math.sin, math.rad; | |||
local function CalculateCorner(angle) | |||
local r = rad(angle); | |||
return 0.5 + cos(r) / s2, 0.5 + sin(r) / s2; | |||
end | |||
local function RotateTexture(texture, angle) | |||
local LRx, LRy = CalculateCorner(angle + 45); | |||
local LLx, LLy = CalculateCorner(angle + 135); | |||
local ULx, ULy = CalculateCorner(angle + 225); | |||
local URx, URy = CalculateCorner(angle - 45); | |||
texture:SetTexCoord(ULx, ULy, LLx, LLy, URx, URy, LRx, LRy); | |||
end | |||