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

What is up with add vector

Started by The Inspector, 08 March 2013 - 12:17 PM
The Inspector #1
Posted 08 March 2013 - 01:17 PM
In my code, I have:

local LastPos = vector.new(0,0,0) -- Keep Y = 0

and then


MoveToPos(LastPos:add(vector.new(0,0,10)))
LastPos = LastPos:add(vector.new(0,0,10))

These are the only occurrences of the variable LastPos.

Now when I put this code in action (it loops) It will first go to (0,0,10) like a good little peice of code, but as it loops around again, it suddenly goes to (10,0,20) and rises exponentially every time it loops, so the next one will be (20,0,40) and (40,0,80), its complete nuts, I don't even understand how it can touch the X component. I have tried everything and have come down to the fact that either :add does not work the way I thought it did, or its broken.

Also on a completely different note, is there anyway to get the position of a block with Feed the beast that doesn't use GPS, at the moment I'm using relative coordinates which are a pain to deal with and can lead to a fair few problems if everything doesn't work perfectly. I can't use GPS because my machine is constantly on the move and easily goes out of range of GPS within a short time.
LordIkol #2
Posted 08 March 2013 - 07:58 PM
Hi Inspector,

I think you have to post the whole Code, i would say the Problem is somewhere else.
LastPos = LastPos:add(vector.new(0,0,10))
does increment the z value by 10. so i guess the problem is in the MoveToPos or somewhere else in the code.

Greets
Loki
Lyqyd #3
Posted 08 March 2013 - 08:02 PM
Please post the whole code.
The Inspector #4
Posted 08 March 2013 - 08:03 PM
Sure, its long, that's why I didn't post it straight up but like I said those are the only occurrences of LastPos.


local Pos = vector.new(0,0,0) --Base Position
local Side = "bottom"
local LastPos = vector.new(0,10,0) -- Keep Y = 0
function North(int)
print("North: "..int)
for I=1,int do
  Pos = Pos:sub(vector.new(0,0,1))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,8)
  sleep(1.5)
end
end
function East(int)
print("East: "..int)
for I=1,int do
  Pos = Pos:add(vector.new(1,0,0))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,1)
  sleep(1.5)
end
end
function South(int)
print("South: "..int)
for I=1,int do
  Pos = Pos:sub(vector.new(1,0,0))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,4)
  sleep(1.5)
end
end
function West(int)
print("West: "..int)
for I=1,int do
  Pos = Pos:sub(vector.new(1,0,0))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,2)
  sleep(1.5)
end
end
function Up(int)
print("Up: "..int)
for I=1,int do
  Pos = Pos:add(vector.new(0,1,0))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,16)
  sleep(1.5)
end
end
function Down(int,bool)
print("Down: "..int)
for I=1,int do
  Pos = Pos:sub(vector.new(0,1,0))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,32)
  sleep(1.5)
  if bool == true then
   if Pos.y < -7 then --ADJUSTABLE
	redstone.setOutput("top",true)
   end
  end
end
redstone.setOutput("top",false)
end
function MoveToPos(vec)
local x = vec.x
local y = vec.y
local z = vec.z

if y > Pos.y then
  Up(y - Pos.y)
elseif y < Pos.y then
  Down(Pos.y - y,false)
end

if x > Pos.x then
  East(x - Pos.x)
elseif x < Pos.x then
  West(Pos.x - x)
end

if z > Pos.z then
  South(z - Pos.z)
elseif z < Pos.z then
  North(Pos.z - z)
end
end
function Mine()
Down(63,true) --ADJUSTABLE
sleep(1.5)
Up(63) --ADJUSTABLE
end
while true do
if redstone.getInput("front") then
  MoveToPos(LastPos:add(vector.new(0,0,10)))
  LastPos = LastPos:add(vector.new(0,0,10))
  print("LastPos = "..LastPos:tostring())
  if redstone.getInput("front") then
   Mine()
  end
else
  if Pos ~= vector.new(0,0,0) then
   MoveToPos(vector.new(0,0,0))
  end
  sleep(10)
end
end
It appears my indentation didn't copy from Notepad++ oh well, its semi readable still.
immibis #5
Posted 08 March 2013 - 08:51 PM
South subtracts 1 from x.
The Inspector #6
Posted 08 March 2013 - 09:01 PM
South subtracts 1 from x.
It subtracts 1 from the x for relative position, not the LastPos variable?
LordIkol #7
Posted 08 March 2013 - 09:10 PM
hm i can not test the code at the moment cause im at work but from looking at it i can not explain why it behaves like this.
just for testing i would try change this



if redstone.getInput("front") then
MoveToPos(LastPos:add(vector.new(0,0,10)))
LastPos = LastPos:add(vector.new(0,0,10))
print("LastPos = "..LastPos:tostring())
to this

if redstone.getInput("front") then
LastPos.z = LastPos.z+10
MoveToPos(LastPos)
print("LastPos = "..LastPos:tostring())

to see if it throws out the same error

edit: removed question about second if :D/>
greets
Loki
LordIkol #8
Posted 08 March 2013 - 09:38 PM
South subtracts 1 from x.

South Substracts from Pos not from LastPos so this should not affect Lastpos

edit: sorry Inspector did not see your post :D/>
Edited on 08 March 2013 - 08:39 PM
immibis #9
Posted 08 March 2013 - 09:39 PM
South subtracts 1 from x.

South Substracts from Pos not from LastPos so this should not affect Lastpos
It would cause the turtle to end up at a very wrong location. In fact it could cause the pattern he's describing, if he's measuring the turtle's actual position instead of LastPos.

hm i can not test the code at the moment cause im at work but from looking at it i can not explain why it behaves like this.
just for testing i would try change this


if redstone.getInput("front") then
MoveToPos(LastPos:add(vector.new(0,0,10)))
LastPos = LastPos:add(vector.new(0,0,10))
print("LastPos = "..LastPos:tostring())


to this


if redstone.getInput("front") then
LastPos.z = LastPos.z+10
MoveToPos(LastPos)
print("LastPos = "..LastPos:tostring())

to see if it throws out the same error

and another question is why you do this?


if redstone.getInput("front") then —Same if clause here
MoveToPos(LastPos:add(vector.new(0,0,10)))
LastPos = LastPos:add(vector.new(0,0,10))
print("LastPos = "..LastPos:tostring())
if redstone.getInput("front") then – and here
Mine()
end

better do it like this


if redstone.getInput("front") then
MoveToPos(LastPos:add(vector.new(0,0,10)))
LastPos = LastPos:add(vector.new(0,0,10))
print("LastPos = "..LastPos:tostring())
Mine()
end


greets
Loki
He wants to know if there's still a redstone signal after the turtle moves.
LordIkol #10
Posted 08 March 2013 - 09:45 PM
what makes you think this is a turtlecode?

but you are right for the South function.
Related to the North function where we have



print("North: "..int)
for I=1,int do
  Pos = Pos:sub(vector.new(0,0,1))

South should do the Opposite like this



function South(int)
print("South: "..int)
for I=1,int do
  Pos = Pos:add(vector.new(0,0,1))


and yes about the redstone signal check you are right i did not see this before :D/>
thanks :)/>
immibis #11
Posted 08 March 2013 - 10:14 PM
what makes you think this is a turtlecode?

and yes about the redstone signal check you are right i did not see this before :D/>/>
thanks :)/>/>

s/turtle/frame machine/g
then
Mads #12
Posted 08 March 2013 - 11:55 PM
If you're not sure how it works, just make your own function.


local function addVec(vec1, vec2)
    return vector.new(vec1.x + vec2.x, ...)
end
SuicidalSTDz #13
Posted 09 March 2013 - 12:38 AM
Please post the whole code.
and nothing but the code.

Since this thread is about vectors, I will put this out here instead of making another thread about vectors:

Can vectors store the information of, oh let's say, a GPS program? If so, would it be something like this?

local gps = vector.new(gps.locate())

Sorry if i'm such a nub with vectors :P/> (God I hate them)
The Inspector #14
Posted 09 March 2013 - 01:23 AM
Right I now see what you guys are saying about the South problem, I don't believe that is the cause though. I will test it though.

Its also a massive frame drill, turtles are disabled for non donators on the server I play on :(/>

Is there any sort of remote control device also available?

Right I changed a few bits around, and it seems to of stopped the random x component increasing, but still the Z is increasing too much. (It jumped from 10 to 40) The machine moved 10 blocks normally, drilled down to bed rock, came back up, but then wanted to move 30 blocks which its not meant to.


local Pos = vector.new(0,0,0) --Base Position
local Side = "bottom"
local LastPos = vector.new(0,0,0) -- Keep Y = 0
function North(int)
print("North: "..int)
for I=1,int do
  Pos = Pos:sub(vector.new(0,0,1))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,8)
  sleep(1.5)
end
end
function East(int)
print("East: "..int)
for I=1,int do
  Pos = Pos:add(vector.new(1,0,0))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,1)
  sleep(1.5)
end
end
function South(int)
print("South: "..int)
for I=1,int do
  Pos = Pos:sub(vector.new(0,0,1))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,4)
  sleep(1.5)
end
end
function West(int)
print("West: "..int)
for I=1,int do
  Pos = Pos:sub(vector.new(1,0,0))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,2)
  sleep(1.5)
end
end
function Up(int)
print("Up: "..int)
for I=1,int do
  Pos = Pos:add(vector.new(0,1,0))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,16)
  sleep(1.5)
end
end
function Down(int,bool)
print("Down: "..int)
for I=1,int do
  Pos = Pos:sub(vector.new(0,1,0))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,32)
  sleep(1.5)
  if bool == true then
   if Pos.y < -8 then --ADJUSTABLE
    redstone.setOutput("top",true)
   end
  end
end
redstone.setOutput("top",false)
end
function MoveToPos(vec)
local x = vec.x
local y = vec.y
local z = vec.z

if y > Pos.y then
  Up(y - Pos.y)
elseif y < Pos.y then
  Down(Pos.y - y,false)
end

if x > Pos.x then
  East(x - Pos.x)
elseif x < Pos.x then
  West(Pos.x - x)
end

if z > Pos.z then
  South(z - Pos.z)
elseif z < Pos.z then
  North(Pos.z - z)
end
end
function Mine()
Down(63,true) --ADJUSTABLE
sleep(1.5)
Up(63) --ADJUSTABLE
end
while true do
if redstone.getInput("front") then
  LastPos.z = LastPos.z + 10
  MoveToPos(LastPos)
  print("LastPos = "..LastPos:tostring())
  if redstone.getInput("front") then
   Mine()
  end
else
  if Pos ~= vector.new(0,0,0) then
   MoveToPos(vector.new(0,0,0))
  end
  sleep(10)
end
end
LordIkol #15
Posted 09 March 2013 - 02:36 AM
south is still wrong in the code you posted it has to be



function South(int)
print("South: "..int)
for I=1,int do
  Pos = Pos:add(vector.new(0,0,1))
  redstone.setBundledOutput(Side,0)
  sleep(1.5)
  redstone.setBundledOutput(Side,4)
  sleep(1.5)
end
end

in your code its still substracted instead of added
@Edit: testet the code whith add instead of subtract for South. That fixes the Bug
The Inspector #16
Posted 09 March 2013 - 03:57 PM
God dammit. Its always something so bloody simple. Wow and I now I see how Pos and LastPos interact, can't believe I missed that.