love.math.newTransform

Available since LÖVE 11.0
This function is not supported in earlier versions.

Creates a new Transform object.

Function

Creates a Transform with no transformations applied. Call methods on the returned object to apply transformations.

Synopsis

transform = love.math.newTransform( )

Arguments

None.

Returns

Transform transform
The new Transform object.

Function

Creates a Transform with the specified transformation applied on creation.

Synopsis

transform = love.math.newTransform( x, y, angle, sx, sy, ox, oy, kx, ky )

Arguments

number x
The position of the new Transform on the x-axis.
number y
The position of the new Transform on the y-axis.
number angle (0)
The orientation of the new Transform in radians.
number sx (1)
Scale factor on the x-axis.
number sy (sx)
Scale factor on the y-axis.
number ox (0)
Origin offset on the x-axis.
number oy (0)
Origin offset on the y-axis.
number kx (0)
Shearing / skew factor on the x-axis.
number ky (0)
Shearing / skew factor on the y-axis.

Returns

Transform transform
The new Transform object.

Examples

Creates a new Transform object and uses it to position and rotate a rectangle around its center:

function love.load()
    rectwidth = 100
    rectheight = 100

    -- arguments are: x, y, angle, scalex, scaley, offsetx, offsety
    transform = love.math.newTransform(100, 100, math.pi/4, 1, 1, rectwidth / 2, rectheight / 2)
end

function love.draw()
    love.graphics.applyTransform(transform)
    love.graphics.rectangle("fill", 0, 0, rectwidth, rectheight)
end


Creates two rectangles and rotates it around its center:

local function createTransform(rect)
	local transform = love.math.newTransform()
	transform:translate(rect.x + rect.width/2, rect.y + rect.height/2)
	transform:rotate(rect.rotation)
	return transform
end

function love.load()
	rect1 = {x=100, y=100, width=200, height=200, rotation=1}
	rect1.transform = createTransform(rect1)
	rect2 = {x=300, y=100, width=200, height=100, rotation=0.1}
	rect2.transform = createTransform(rect2)
end

function love.draw()
	for i, rectangle in ipairs ({rect1, rect2}) do
		love.graphics.push()
			love.graphics.applyTransform(rectangle.transform)
			love.graphics.rectangle("line", -rectangle.width/2, -rectangle.height/2, rectangle.width, rectangle.height)
		love.graphics.pop()
		-- the line shows the middle of the rectangle:
		love.graphics.line (0, 0, rectangle.x+rectangle.width/2, rectangle.y+rectangle.height/2)
	end
end

See Also

Other Languages