rotation question
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- 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?
Re: rotation question
Maybe check in the update function the value of your rotation variable.
The same when it's greater than 3.1
Then draw the sprite.
Maybe there are more proper ways but it should work
Code: Select all
If rotation < -3.1 then rotation = -3.1
Then draw the sprite.
Code: Select all
love.graphics.draw(img, x, y, rotation, width, height, scalex, scaley)
- zorg
- Party member
- Posts: 3468
- 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)
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.
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
-
- Prole
- Posts: 10
- Joined: Sat Dec 16, 2017 8:34 pm
Re: rotation question
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:
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:
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
EDIT: I see what your diagram is driving at now, but the way to solve it is still with atan2
-
- Prole
- Posts: 10
- Joined: Sat Dec 16, 2017 8:34 pm
Re: rotation question
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 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
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:
I just typed that in, I'm not on a machine with lua installed so I might have made a typo etc
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
-
- 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:
I just typed that in, I'm not on a machine with lua installed so I might have made a typo etcCode: 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
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
Re: rotation question
atan2 takes (y, x)
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 7 guests