Line rotation

In the following code, the line is displaying in the proper position, but I am trying to figure out how to get it to rotate on its middle axis… instead of at the end like it is doing now. Any idea how to do this?

line = display.newRect(display.contentWidth / 2, display.contentHeight / 2 , 300 , 2)

line.anchorX = 0

line.anchorY = 0

local function myFunction()

if line then 

– line.anchorX = .5

– line.anchorY = .5

line.rotation = line.rotation + 15

end

end

timer.performWithDelay(1, myFunction, 36)

It is a bit messy to use classic rotation because it is dependent on the anchor point which moves as it rotates. In my simple animation thing (under development) coccyx https://github.com/autismuk/ there is a routine somewhere to put a line in any arbitrary position (e.g. you can convert it to a line between any two coordinates. You may be better off having a line and calculating its start and end positions using trigonometry.

The other way (might be easier) is to have two lines that are half the length ? Not sure if the anchor issue would occur again though.

If you want it to rotate around it’s middle, then why are you setting the anchors to 0?

If you set them to 0.5 (or don’t set them at all since 0.5 is the default), then they will rotate around the middle. If this causes a problem when you first position them, then just offset the position by half the object’s width and height:

line = display.newRect(display.contentWidth / 2, display.contentHeight / 2 , 300 , 2) line.x = line.x + (line.width \* 0.5) line.y = line.y + (line.height \* 0.5) local function myFunction() if line then line.rotation = line.rotation + 15 end end timer.performWithDelay(1, myFunction, 36)

It is a bit messy to use classic rotation because it is dependent on the anchor point which moves as it rotates. In my simple animation thing (under development) coccyx https://github.com/autismuk/ there is a routine somewhere to put a line in any arbitrary position (e.g. you can convert it to a line between any two coordinates. You may be better off having a line and calculating its start and end positions using trigonometry.

The other way (might be easier) is to have two lines that are half the length ? Not sure if the anchor issue would occur again though.

If you want it to rotate around it’s middle, then why are you setting the anchors to 0?

If you set them to 0.5 (or don’t set them at all since 0.5 is the default), then they will rotate around the middle. If this causes a problem when you first position them, then just offset the position by half the object’s width and height:

line = display.newRect(display.contentWidth / 2, display.contentHeight / 2 , 300 , 2) line.x = line.x + (line.width \* 0.5) line.y = line.y + (line.height \* 0.5) local function myFunction() if line then line.rotation = line.rotation + 15 end end timer.performWithDelay(1, myFunction, 36)