rotate to point using the shortest way

Hi,

I am trying to rotate an object using x,y co-ordinates to generate the needed angle.  I have this working fine, however if the point is for example 340 degrees, and the object is currently 10 degrees, the object transitions clockwise all the time, taking the long way round.

I have also tried to manual change the angle by changing

object.rotation to object rotation - 2 every enterframe, but this doesn’t work as the objects rotation goes into minus numbers rather than looping round to 360.

I’m basically looking for a way to tell if its quicker to rotate left or right.

If anybody could help I’d be grateful.

Thanks

Gary

Hi there,

I guess, I didn’t got your problem exactly, but wouldn’t a simple if check do the trick?

local start\_angle = 90 local end\_angle = 340 local direction = (start\_angle \< end\_angle) and 1 or -1 local distance = math.abs(end\_angle - start\_angle) local step\_direction if distance \< 180 then step\_direction = direction else step\_direction = -direction end

With this it should work, even if you swap the start and the end angle.

Use the variable step_direction as a factor for your rotation steps, as it will be 1 or -1

I hope that helps you out.

Greetings

Torben

Thanks, that has worked great!

Thanks

Gary

Hi there,

I guess, I didn’t got your problem exactly, but wouldn’t a simple if check do the trick?

local start\_angle = 90 local end\_angle = 340 local direction = (start\_angle \< end\_angle) and 1 or -1 local distance = math.abs(end\_angle - start\_angle) local step\_direction if distance \< 180 then step\_direction = direction else step\_direction = -direction end

With this it should work, even if you swap the start and the end angle.

Use the variable step_direction as a factor for your rotation steps, as it will be 1 or -1

I hope that helps you out.

Greetings

Torben

Thanks, that has worked great!

Thanks

Gary