Problem with angles...

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.
Post Reply
User avatar
Lightcycler
Prole
Posts: 18
Joined: Sun Dec 16, 2012 4:31 pm

Problem with angles...

Post by Lightcycler »

Hey guys,

I've got a problem. I have a circle that faces my mouse, and since I want a soft rotation and need the impulse later, I used love.physics fot this task.

I have two vectors, one from the circle to the mouse and one "normed" vector. Then I calculate the angle between the two points, check the difference of the circle's angle and my newly calculated angle and this times an arbitrary factor is set as the circle's angular velocity.
This is all fine and works, BUT: the small gap between 1° and 359°, respectively 0.0..somethingsomethingsmall and 2*pi messes it up.

Code: Select all

function love.update(dt)
world:update(dt)
mousex = love.mouse.getX()
mousey = love.mouse.getY()
vectora[1] = mousex - posx
vectora[2] = mousey - posy

vectorb[1] = 50
vectorb[2] = 0

cosphi = (vectora[1]*vectorb[1]+vectora[2]*vectorb[2])/((math.sqrt(vectora[1]*vectora[1] + vectora[2]*vectora[2]))* (math.sqrt(vectorb[1]*vectorb[1]+vectorb[2]*vectorb[2])))
phi = math.acos(cosphi)
omega = (phi-objects.ball.body:getAngle())*10
objects.ball.body:setAngularVelocity(omega)
end
How can I avoid this gap? I can't think of anything right now...

I'll attach the love-file so you can try it out.
Attachments
help.love
(3.15 KiB) Downloaded 138 times
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Problem with angles...

Post by ivan »

Could be simplified to:

Code: Select all

    function love.update(dt)
    world:update(dt)
    mousex = love.mouse.getX()
    mousey = love.mouse.getY()
    -- vector to the mouse
    vectora[1] = mousex - posx
    vectora[2] = mousey - posy
    -- current angle
    local heading = objects.ball.body:getAngle()
    -- target angle
    local target = math.atan2(vectora[2], vectora[1])
    local arc = ( heading - target + math.pi ) % ( 2 * math.pi ) - math.pi

    objects.ball.body:setAngularVelocity(arc)
    end
User avatar
Lightcycler
Prole
Posts: 18
Joined: Sun Dec 16, 2012 4:31 pm

Re: Problem with angles...

Post by Lightcycler »

May I ask what Modulo does in this equation? Or do you have a link for a mathematical proof of that concept?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Problem with angles...

Post by ivan »

Lightcycler wrote:May I ask what Modulo does in this equation? Or do you have a link for a mathematical proof of that concept?
One way to think of modulo is an operation that confines a number between a certain range so:
n%360 (confines n between 0 and 360)
example:
370%360 = 10
360%360 = 0
-350%360 = 10

n%math.pi (confines n between 0 and math.pi)
Last edited by ivan on Tue Oct 21, 2014 4:41 am, edited 2 times in total.
User avatar
Lightcycler
Prole
Posts: 18
Joined: Sun Dec 16, 2012 4:31 pm

Re: Problem with angles...

Post by Lightcycler »

Thank you for your help!
So I tried it out but the circle twitches when he tries to align with the mouse (also turned around pi radians) ... And when the angle's too big, it stops and tries to go the other way around. Do you have an idea how to "fix" my approach? Except for the gap it looks more satisfying to me...
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Problem with angles...

Post by micha »

Is there another reason for using love.physics other than that it does the rotation for you? If not, I suggest not using it and implementing the rotation by hand.

Also, can you upload a .love of the new problem you got?
User avatar
Lightcycler
Prole
Posts: 18
Joined: Sun Dec 16, 2012 4:31 pm

Re: Problem with angles...

Post by Lightcycler »

Yes, I actually need the impulse later.
Though I might try to implement the turning by hand and add some turning vector. Hm...
Attachments
help2.love
(3 KiB) Downloaded 137 times
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Problem with angles...

Post by ivan »

Lightcycler wrote:Thank you for your help!
So I tried it out but the circle twitches when he tries to align with the mouse (also turned around pi radians) ... And when the angle's too big, it stops and tries to go the other way around. Do you have an idea how to "fix" my approach? Except for the gap it looks more satisfying to me...
atan2 assumes that 0 radians is East.
When you draw bodies, you might assume that 0 radians is North.
So you may have to add 90 degrees to the target angle:

Code: Select all

local target = math.atan2(vectora[2], vectora[1]) + math.rad(90)
User avatar
Lightcycler
Prole
Posts: 18
Joined: Sun Dec 16, 2012 4:31 pm

Re: Problem with angles...

Post by Lightcycler »

I finally solved the problem, thanks to you two and thanks to this thread: http://love2d.org/forums/viewtopic.php?f=4&t=76710

I expanded the code by adding a scale factor:

Code: Select all

if(diff>(x*dt))then
  return current + (x*dt)
end
With this, I can smoothly change the rotation speed as it suits my taste.

Thanks again and have a nice day :)
Post Reply

Who is online

Users browsing this forum: pgimeno and 16 guests