How to Rotate a Rectangle

Hi,

I'm trying to figure out how to rotate a rectangle on a single point origin, something like a propeller blade.

I using the SDK on windows.

So far I am able to create a rect instance and draw it on the screen.

local platform

local function initialize()
    platform = pd.geometry.rect.new(100,100,100,10)
end

initialize()

function  pd.update()
	gfx.clear()
	gfx.fillRect(platform)
	gfx.sprite.update()
end
  1. Is it possible to rotate a rectangle using the SDK?
  2. Do I need to use the affine transform somehow?

Thanks for any help you can provide!

Probably the easiest way is to convert the rect to a polygon and use affine transform. Something like:

local platformPolygon

local function initialize()
    platformPolygon = pd.geometry.rect.new(100,100,100,10):toPolygon()
end

initialize()

function  pd.update()
	gfx.clear()
	local transform = pd.geometry.affineTransform.new()
	transform:rotate(angle, centerOfRotation)
	local rotatedPlatform = transform:transformedPolygon(platformPolygon)
	gfx.fillPolygon(rotatedPlatform)
end

(this assumes you want it drawn at a different angle every frame. Also you could probably optimize it by storing the transform and updating it instead of creating a new transform each frame)

Thanks for getting back to me!

I tried this but I get an error saying this:

Update error: main.lua:24: bad argument #1 to 'rotate' (number expected, got nil)
stack traceback:
[C]: in method 'rotate'
main.lua:24: in function main.lua:21
15:27:07: Update failed, simulator paused.

am I missing something?

replace “angle” and “centerOfRotation” with your own values

Haha, that helped! Thanks again!