This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Trot's profile picture

Turtle Orientation

Started by Trot, 05 April 2014 - 12:50 AM
Trot #1
Posted 05 April 2014 - 02:50 AM
I am trying to teach a turtle location and orientation. I currently am working on orientation but I ran into a problem with my system. I have a variable stated at the top of my code:
dir = 1
this denotes north. I planned to have it add one and subtract one for each turn, but this obviously falls apart at 1 and 4.
Any suggestions as to how to circumnavigate this problem? or maybe a better way to do it? Any help is appreciated!
guesswhat123212 #2
Posted 05 April 2014 - 02:55 AM
the way I always did it was by putting another function at the end of the turn that would check the dir number and loop it back around. something like this

function fixDir()
  if dir < 1 then
    dir = 4
  elseif dir > 4 then
    dir = 1
  end
end

also if you are using GPS you can make the turtle check its GPS, move forward (without moding your coords) and check the gps again to compare and auto get facing
Anavrins #3
Posted 05 April 2014 - 03:52 AM
Use the modulus % symbol.
dir = (dir + 1) % 4
In a loop, it will give 0,1,2,3,0,1,2,3,0,1,2,3…
Edited on 05 April 2014 - 01:52 AM