It sounds like you need to know the bounding box of a rotated rectangle. I find that ChatGPT is pretty good at solving these types of small problems. I haven't tested this code, but here is what it gave me. It assumes the sprite is rotated from the top-left corner, but I don't know if you are rotating from the center point or some other pivot.
Prompt: Write a function in Lua that calculates the bounding box of a rotated rectangle, given an x, y, width, height, and angle in degrees.
Answer: Certainly! Here is a function that calculates the bounding box of a rotated rectangle in Lua:
function boundingBox(x, y, w, h, angle)
local function rotate(px, py, ox, oy, angle)
local radians = math.rad(angle)
local s, c = math.sin(radians), math.cos(radians)
return ox + (px - ox) * c - (py - oy) * s, oy + (px - ox) * s + (py - oy) * c
local x1, y1 = rotate(x, y, x, y, angle)
local x2, y2 = rotate(x + w, y, x, y, angle)
local x3, y3 = rotate(x, y + h, x, y, angle)
local x4, y4 = rotate(x + w, y + h, x, y, angle)
local minX = math.min(x1, x2, x3, x4)
local maxX = math.max(x1, x2, x3, x4)
local minY = math.min(y1, y2, y3, y4)
local maxY = math.max(y1, y2, y3, y4)
return minX, maxX, minY, maxY
This function takes the following arguments:
x : The x-coordinate of the rectangle's top-left corner
y : The y-coordinate of the rectangle's top-left corner
w : The width of the rectangle
h : The height of the rectangle
angle : The angle of rotation in degrees
It returns a tuple of four values: the minimum x-coordinate, the maximum x-coordinate, the minimum y-coordinate, and the maximum y-coordinate of the bounding box.
For example, you could use this function like this:
local minX, maxX, minY, maxY = boundingBox(10, 20, 30, 40, 45)
This would calculate the bounding box of a rectangle with top-left corner at (10, 20), width 30, height 40, and 45 degrees of rotation. The function would return the minimum x-coordinate, maximum x-coordinate, minimum y-coordinate, and maximum y-coordinate of the bounding box.