## rotation question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
josephjohn
Prole
Posts: 10
Joined: Sat Dec 16, 2017 8:34 pm

### rotation question

So I have a piece of code to slowly rotate a sprite towards the mouse position, and it works. However, when it has to rotate from negative to positive (3.1 to -3.1) and vice-versa, it does a full rotation around. How can I make sure it rotates in the shortest distance instead of a full rotation?

xNick1
Party member
Posts: 265
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

### Re: rotation question

Maybe check in the update function the value of your rotation variable.

Code: Select all

If rotation < -3.1 then rotation = -3.1

The same when it's greater than 3.1

Then draw the sprite.

Code: Select all

love.graphics.draw(img, x, y, rotation, width, height, scalex, scaley)

Maybe there are more proper ways but it should work
Currently working on a lucid dreaming adventure called Aarstider: http://bit.ly/2mXmgPI

zorg
Party member
Posts: 2995
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: rotation question

What's the 3.1 exactly? Are you using degrees, radians, or something else?
Usually, you'd calculate both turns in one of the above systems, and use the one that's a smaller number (you might need to math.abs them for the comparison, but use the original signs otherwise)
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

### Re: rotation question

Perhaps look into atan2 (https://love2d.org/wiki/General_math)? I think you can then work out which way is the shortest, rather than always going the long way round

josephjohn
Prole
Posts: 10
Joined: Sat Dec 16, 2017 8:34 pm

### Re: rotation question

I might have explained it wrong. The problem is this:

Example:

mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

### Re: rotation question

As I said before, atan2 will help you fix the issue - here's a good explanation: https://gamedev.stackexchange.com/quest ... f-a-vector

EDIT: I see what your diagram is driving at now, but the way to solve it is still with atan2

josephjohn
Prole
Posts: 10
Joined: Sat Dec 16, 2017 8:34 pm

### Re: rotation question

mr_happy wrote:
Sun Dec 17, 2017 4:31 pm
There's something wrong with your diagram (or understanding, and I don't mean that in a nasty way) - there are 2PI radians (call it 6 and a bit radians) in 360% so "-3 to 3" is almost a complete rotation. As I said before, atan2 will help you fix the issue - here's a good explanation: https://gamedev.stackexchange.com/quest ... f-a-vector
I'm using atan2 to get the angle I want, and when I just set the sprite to instantly turn, it perfectly snaps to face the mouse, but the problem is when i make it slowly rotate, it does a full rotation instead of just moving 1rad or less.

mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

### Re: rotation question

Does it rotate the 'long way round' when your mouse is any quadrant? Sounds to me like you just need to get the signs right depending upon which quadrant the cursor is in. If you look at the output of atan2 using something like this you'll see what's required:

Code: Select all

radius = 100
for i = 0, 359 do
io.write(i, ' : ', math.atan2(x, y), '\t')
end

I just typed that in, I'm not on a machine with lua installed so I might have made a typo etc

josephjohn
Prole
Posts: 10
Joined: Sat Dec 16, 2017 8:34 pm

### Re: rotation question

mr_happy wrote:
Sun Dec 17, 2017 4:54 pm
Does it rotate the 'long way round' when your mouse is any quadrant? Sounds to me like you just need to get the signs right depending upon which quadrant the cursor is in. If you look at the output of atan2 using something like this you'll see what's required:

Code: Select all

radius = 100
for i = 0, 359 do
io.write(i, ' : ', math.atan2(x, y), '\t')
end

I just typed that in, I'm not on a machine with lua installed so I might have made a typo etc

No, it only rotates the long way around when going from about -2 to 2 and vice versa. Basically when going from anything less than -pi/2 to anything more than pi/2 and vice versa. Here is the code:

Code: Select all

function love.load()
x = 64
y = 64
tryX = 0
tryY = 0
velX = 0
velY = 0
rot = 0
tryRot = 0
rotDist = 0
end

function love.update(dt)
if rot < -math.pi or rot > math.pi then
rot = rot % math.pi
end

if self.rot ~= tryRot then
rotate(dt)
end
end

function love.draw()
love.graphics.draw(self.sprite, self.x - 3, self.y - 3, self.rot + math.rad(90), 3, 3, 8, 8)
end

function love.mousepressed(mx, my)
tryX = mx
tryY = my
tryRot = math.atan2(tryY - y, tryX - x)
rotDist = rot - tryRot
end

function rotate(dt)
rot = self.rot - rotDist/64
end

I don't know if this code works since i took out a bunch of irrelevant stuff and made it into a main.lua file, but that's the jist

mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

### Re: rotation question

atan2 takes (y, x)

### Who is online

Users browsing this forum: No registered users and 40 guests