rotation question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
josephjohn
Prole
Posts: 10
Joined: Sat Dec 16, 2017 8:34 pm

rotation question

Post by josephjohn »

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?
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: rotation question

Post by xNick1 »

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
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: rotation question

Post by zorg »

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 :3True 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.
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: rotation question

Post by mr_happy »

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 :D
josephjohn
Prole
Posts: 10
Joined: Sat Dec 16, 2017 8:34 pm

Re: rotation question

Post by josephjohn »

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

I have a sprite, which rotates towards the mouse when I click. I works great when going from 1rad to 2rad, or from 1 rad to -1 rad, but when it goes from -3rad to 3rad, it rotates around the larger angle, so instead of going from -3rad to 3rad by moving the shorter route(about 0.28rad), it moves 6rad.

Example:
Image
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: rotation question

Post by mr_happy »

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

Post by josephjohn »

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.
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: rotation question

Post by mr_happy »

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
   x = radius * math.cos(math.rad(i))
   y = radius * math.sin(math.rad(i))
   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 :D
josephjohn
Prole
Posts: 10
Joined: Sat Dec 16, 2017 8:34 pm

Re: rotation question

Post by josephjohn »

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
   x = radius * math.cos(math.rad(i))
   y = radius * math.sin(math.rad(i))
   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 :D

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
    sprite = love.graphics.newImage("sprites/YOUR SPRITE HERE.png")
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
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: rotation question

Post by mr_happy »

atan2 takes (y, x)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 117 guests