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

Get the direction the turtle face

Started by vanir, 12 May 2012 - 10:56 PM
vanir #1
Posted 13 May 2012 - 12:56 AM
Hi,

i writing on a script, that let the turtle return to a specific point to drop its inventory. I want to do all the positioning through gps locations. Is it possible to find out in wich direction the turtle faces? I need this for the moving, maybe there is another way to do this.

at the moment i do it with this:


function axis()
x1 , y1, z1 = gps.locate( 2, false )
while turtle.detect() do
  turtle.dig()
    end
    turtle.forward()
x2 , y2 , z3=gps.locate( 2, false )
if x2-x1 == 1 and y2-y1 ==0 then
  dir="X"
elseif x2-x1 == -1 and y2-y1 ==0 then
  dir="-X"
elseif x2-x1 == 0 and y2-y1 ==1 then
  dir="Y"
elseif x2-x1 == 0 and y2-y1 ==-1 then
  dir="-Y"
else
  dir="something went terrible wrong"
end
return(dir)
end

I hope i say clear what i want. Thanks for help :P/>/>
Lyqyd #2
Posted 13 May 2012 - 01:47 AM

function getOrientation()
loc1 = vector.new(gps.locate(2, false))
if not turtle.forward() then
    for j=1,6 do
	    if not turtle.forward() then
		    turtle.dig()
   	 else break end
    end
end
loc2 = vector.new(gps.locate(2, false))
heading = loc2 - loc1
return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
end

--[[orientation will be:
-x = 1
-z = 2
+x = 3
+z = 4
This matches exactly with orientation in game, except that Minecraft uses 0 for +z instead of 4.
--]]

The reverse, by the way (heading movements from heading code) is:

xMovement = (orientation - 2) * (orientation % 2)
zMovement = (orientation - 3) * ((orientation + 1) % 2)
vanir #3
Posted 13 May 2012 - 12:51 PM
Ahh i see ur code seem a lot more compact than mine, thank u. But i have another question is it possible to get the facing without moving?
Lyqyd #4
Posted 13 May 2012 - 03:18 PM
Not with the tools we are provided. All we can get is the location, so we need to know two of them to determine the direction of travel.
MysticT #5
Posted 13 May 2012 - 03:24 PM
You could keep track of the direction and change it each time you turn:

local nHeading = 1

local function turnLeft()
  nHeading = nHeading + 1
  turtle.turnLeft()
end

local function turnRight()
  nHeading = nHeading - 1
  turtle.turnRight()
end
I'm not sure if that's right, you may have to change the +/- 1 to match the correct heading.
You can provide the initial heading yourself, or use your function to determine it.
Lyqyd #6
Posted 13 May 2012 - 04:58 PM
You could keep track of the direction and change it each time you turn:

local nHeading = 1

local function turnLeft()
  nHeading = nHeading + 1
  turtle.turnLeft()
end

local function turnRight()
  nHeading = nHeading - 1
  turtle.turnRight()
end
I'm not sure if that's right, you may have to change the +/- 1 to match the correct heading.
You can provide the initial heading yourself, or use your function to determine it.

Close! You forgot to bound them. Looks like you also got them backwards, but that's an easy fix.


function left(orientation)
    if turtle then turtle.turnLeft() end
    orientation = orientation - 1
    if orientation < 1 then orientation = orientation + 4 end
    return orientation
end

function right(orientation)
    if turtle then turtle.turnRight() end
    orientation = orientation + 1
    if orientation > 4 then orientation = orientation - 4 end
    return orientation
end
vanir #7
Posted 13 May 2012 - 07:19 PM
Thank u guys, its a big help.