Page 1 of 1

Problem with angles...

Posted: Sun Oct 19, 2014 1:20 pm
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.

Re: Problem with angles...

Posted: Sun Oct 19, 2014 2:04 pm
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

Re: Problem with angles...

Posted: Sun Oct 19, 2014 3:24 pm
by Lightcycler
May I ask what Modulo does in this equation? Or do you have a link for a mathematical proof of that concept?

Re: Problem with angles...

Posted: Sun Oct 19, 2014 3:42 pm
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)

Re: Problem with angles...

Posted: Sun Oct 19, 2014 4:08 pm
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...

Re: Problem with angles...

Posted: Sun Oct 19, 2014 5:34 pm
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?

Re: Problem with angles...

Posted: Sun Oct 19, 2014 6:25 pm
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...

Re: Problem with angles...

Posted: Sun Oct 19, 2014 6:34 pm
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)

Re: Problem with angles...

Posted: Thu Oct 23, 2014 9:43 am
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 :)