Difference between revisions of "Gps (API)"

From ComputerCraft Wiki
Jump to: navigation, search
(Changed to use API table template)
m (Changed "MineCraft" to "Minecraft")
Line 5: Line 5:
 
Note:  When entering in the coordinates for the ''host'' you need to put in the x,y,z of the computer, not the modem, as all rednet distances are measured from the block the computer is in.  
 
Note:  When entering in the coordinates for the ''host'' you need to put in the x,y,z of the computer, not the modem, as all rednet distances are measured from the block the computer is in.  
  
Also note that you may ''choose'' which axises x, y or z refers to - so long as your systems have the same definition as any GPS servers that're in range, it works just the same. For example, you might build a GPS cluster according to [http://www.computercraft.info/forums2/index.php?/topic/3088-how-to-guide-gps-global-position-system/ this tutorial], using z to account for height, or you might use y to account for height in the way that MineCraft's debug screen displays.
+
Also note that you may ''choose'' which axises x, y or z refers to - so long as your systems have the same definition as any GPS servers that're in range, it works just the same. For example, you might build a GPS cluster according to [http://www.computercraft.info/forums2/index.php?/topic/3088-how-to-guide-gps-global-position-system/ this tutorial], using z to account for height, or you might use y to account for height in the way that Minecraft's debug screen displays.
  
 
<!-- do not edit this without actually testing it yourself -->
 
<!-- do not edit this without actually testing it yourself -->

Revision as of 05:59, 20 March 2015

The GPS API provides a method for turtles and computers to retrieve their own locations.

It broadcasts a PING message over rednet and wait for responses. In order for this system to work, there must be at least 4 computers used as gps hosts which will respond and allow trilateration. Three of these hosts should be in a plane, and the fourth should be either above or below the other three. The three in a plane should not be in a line with each other. You can set up hosts using the gps program.

Note: When entering in the coordinates for the host you need to put in the x,y,z of the computer, not the modem, as all rednet distances are measured from the block the computer is in.

Also note that you may choose which axises x, y or z refers to - so long as your systems have the same definition as any GPS servers that're in range, it works just the same. For example, you might build a GPS cluster according to this tutorial, using z to account for height, or you might use y to account for height in the way that Minecraft's debug screen displays.


Grid disk.png  Gps (API)
Function Return values Description
gps.locate([number timeout [, boolean debug]]) (number x, number y, number z) or nil Tries to retrieve the computer or turtles own location. On success, returns the location of the turtle’s modem. On failure (if no responses are received for timeout seconds, by default 2), returns nil. If debug is true, debug messages are printed.

Example

 print("Man I am so lost right now!")
 local x, y, z = gps.locate(5)
 if not x then --If gps.locate didn't work, it won't return anything. So check to see if it did.
   print("Failed to get my location!")
 else
   print("I am at (" .. x .. ", " .. y .. ", " .. z .. ")") --This prints 'I am at (1,2,3)' or whatever your coordinates are
 end

Vector Example

GPS position and navigation can be handled more easily using a vector.

 local home = vector.new(45, 85, 20)
 local position = vector.new(gps.locate(5))
 local displacement = position - home
 
 print("I am ", tostring(displacement), " away from home!!!")