Turning works for all but below object

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
Skeletonxf
Citizen
Posts: 87
Joined: Tue Dec 30, 2014 6:07 pm

Turning works for all but below object

Post by Skeletonxf »

In my game I initially had instant snap-to-new-angle turning for the enemies which worked fine. I then created a new enemy with turning that takes time to reach the direction at the player and I cannot work out why it turns the wrong way when the player passes underneath the enemy, especially when it works perfectly for all other angles???

This snippet of code in game/game.lua controls turning for missle ships which is the type of ship I'm having the bug with

Code: Select all

            if ship.shipType == "missle" then
              if not ship.angle then ship.angle = math.atan2(dx,dy) end
              ship.oldAngle = ship.angle
              ship.idealAngle = math.atan2(dx,dy)
              -- the code has no special case for turning to something beneath????
              if ship.idealAngle > ship.oldAngle then
                ship.angle = ship.angle + 0.1
              else
                ship.angle = ship.angle - 0.1
              end
              --ship.angle = (ship.oldAngle + adjustAngle) Uncomment this line and snap-to turning has no glitches
            end
            -- cap to unit circle (radius of 1)
            dx, dy = -math.sin(ship.angle), -math.cos(ship.angle)
The ships then move like this

Code: Select all

ship.dx = ship.dx + (dx or 0)*dt*window.sx*95*0.07
ship.dy = ship.dy + (dy or 0)*dt*window.sy*75*0.07
ship.dx = ship.dx*math.pow(0.001,dt)
ship.dy = ship.dy*math.pow(0.001,dt)
Bug

https://youtu.be/Umn2UOJsHxA
sensor.love
(182.94 KiB) Downloaded 37 times
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Turning works for all but below object

Post by pgimeno »

I have only checked the video, but it seems clear that the problem happens in the transition from -180° to 180° or vice versa. See http://stackoverflow.com/questions/1878 ... n-2-angles

Edit: To be a bit more explicit, the sign of that angle difference is what you need to check instead of ship.idealAngle > ship.oldAngle.
Last edited by pgimeno on Wed Aug 24, 2016 2:02 pm, edited 1 time in total.
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: Turning works for all but below object

Post by Ulydev »

Hey Skeletonxk,

Here's my function to lerp from one angle to another. It might help you with transitioning angles correctly.

Code: Select all

math.lerpAngle(old, new, speed) --speed = dt for instance

Code: Select all

function math.lerpAngle(a, b, k)
  local pi = math.pi
  if (math.abs(a - b) > pi) then
    if (b > a) then
      a = a + pi * 2
    else
      b = b + pi * 2
    end
  end

  local value = (a + ((b - a) * k))

  local rangeZero = pi * 2

  if (value >= 0 and value <= pi * 2) then
    return value
  end

  return (value % rangeZero)
end
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Turning works for all but below object

Post by airstruck »

Here's a nice short one that was posted and/or written by some combination of Jasoco and/or Davidobot (sorry guys, memory not so clear on this one).

Code: Select all

local function angleDifference (t1, t2)
   return (t1 - t2 + math.pi) % (math.pi * 2) - math.pi
end
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Turning works for all but below object

Post by pgimeno »

Yes, that's the translation to radians of the SO reply :)
Skeletonxf
Citizen
Posts: 87
Joined: Tue Dec 30, 2014 6:07 pm

Re: Turning works for all but below object

Post by Skeletonxf »

Thanks the lerping function helps doubly because I had to make the turning frame rate independent next anyway, though I also had to add a math.min(k,1) to the line assigning value to prevent some really weird movement with high dt multipliers (probably over turning).
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Turning works for all but below object

Post by Jasoco »

airstruck wrote:Here's a nice short one that was posted and/or written by some combination of Jasoco and/or Davidobot (sorry guys, memory not so clear on this one).

Code: Select all

local function angleDifference (t1, t2)
   return (t1 - t2 + math.pi) % (math.pi * 2) - math.pi
end
Haha, yeah, I was just coming in here to post this myself. I had this same problem at various times and this should fix it.
Post Reply

Who is online

Users browsing this forum: No registered users and 187 guests