I have myself puzzled... simple math/logic problem

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
Araqiel
Citizen
Posts: 54
Joined: Sat Oct 30, 2010 7:33 pm

I have myself puzzled... simple math/logic problem

Post by Araqiel »

I have a Boid class. I was going to implement steering, but... I confused myself. How do you figure out which way to turn if you have a heading(0-360) and a target heading(0-360)? You obviously can't just do a simple comparison of greater than or less than, because 0 < 359, but it'd make more sense to just turn left 1 degree from 0 to get to 359. Hm.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: I have myself puzzled... simple math/logic problem

Post by TechnoCat »

(TargetDegrees - SourceDegrees) / 360
Keep whatever is left after the decimal and make it positive.
Test if it is greater or less than 0.5
Araqiel
Citizen
Posts: 54
Joined: Sat Oct 30, 2010 7:33 pm

Re: I have myself puzzled... simple math/logic problem

Post by Araqiel »

Thanks! I'll try that real quick.

[EDIT] Yay, it seems to work.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: I have myself puzzled... simple math/logic problem

Post by TechnoCat »

I wrote this and it seems to work:

Code: Select all

function CWorCCW(start,finish)
  local half = (finish-start)/(2*math.pi)
  local R = half - math.floor(half)
  if R<0 then print("CW") end
  if R>0 then print("CCW") end
  if R==0 then print("Nothing") end
  return R*2*math.pi
end
Sorry, it is in radians.
Post Reply

Who is online

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