radar=peripheral.wrap("back:red")
function ping()
term.clear()
term.setCursorPos(1,1)
for i,v in pairs(radar.getEntities()) do
print(v)
end
end
while true do
ping()
sleep(0.1)
end
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
3D distance formula in CC
Started by jewelshisen, 20 January 2013 - 03:21 PMPosted 20 January 2013 - 04:21 PM
Hello! I am currently trying to make a program that will use the radar from the ICBM mod to track an incoming missile and then activate an MFFS force field if it comes too close to my base. To that end I need to know how you program in the square root function. I have the base of my code made and any tips on how to fetch the x,y,z variables out of the table that the radar puts out would be greatly appreciated. Here is my code so far:
Posted 20 January 2013 - 04:22 PM
http://en.wikipedia....lidean_distance
Try the Euclidean distance formula.
As for getting the locations, I believe you can use v[1], v[2] and v[3].
I believe v[1] = x, v[2] = z and v[3] = y, but I don't have the mod, so I cannot confirm this.
Try the Euclidean distance formula.
dist = math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2) + math.pow(z1 - z2, 2))
I thinkAs for getting the locations, I believe you can use v[1], v[2] and v[3].
I believe v[1] = x, v[2] = z and v[3] = y, but I don't have the mod, so I cannot confirm this.
Edited on 20 January 2013 - 03:27 PM
Posted 20 January 2013 - 04:25 PM
http://en.wikipedia....lidean_distance
Try the Euclidean distance formula.I thinkdist = math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2) + math.pow(z1 - z2, 2))
I said I know the formula… I just needed the lua script. But thanks.
Posted 20 January 2013 - 04:27 PM
yeh that looks about right…http://en.wikipedia....lidean_distance
Try the Euclidean distance formula.I thinkdist = math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2) + math.pow(z1 - z2, 2))
Posted 20 January 2013 - 04:29 PM
Thanks. Now to figure out how to get the missile variables out of the table.
Posted 20 January 2013 - 04:30 PM
I updated my answer with informationThanks. Now to figure out how to get the missile variables out of the table.
Posted 20 January 2013 - 04:31 PM
Loop the entities with a numeric for loop then to this…
radarEntities[i]["x"]
radarEntities[i]["y"]
radarEntities[i]["z"]
Posted 20 January 2013 - 04:33 PM
Loop the entities with a numeric for loop then to this…radarEntities[i]["x"] radarEntities[i]["y"] radarEntities[i]["z"]
Well I am already using the for loop to display the missile location so couldn't I just use a modification of that?
Posted 20 January 2013 - 04:35 PM
local entities = {}
function ping()
term.clear()
term.setCursorPos(1,1)
entities = {}
for i,v in pairs(radar.getEntities()) do
entities[#entities + 1]["x"] = v[1]
entities[#entities + 1]["z"] = v[2]
entities[#entities + 1]["y"] = v[3]
end
end
EDIT: Woo! Level up!
Posted 20 January 2013 - 04:41 PM
local entities = {} function ping() term.clear() term.setCursorPos(1,1) entities = {} for i,v in pairs(radar.getEntities()) do entities[#entities + 1]["x"] = v[1] entities[#entities + 1]["z"] = v[2] entities[#entities + 1]["y"] = v[3] end end
EDIT: Woo! Level up!
I'm not trying to make a new table… I'm trying to get info OUT of the table and IN to variables. The getEntities method RETURNS a table that does me no good for plugging into a math formula.
Posted 20 January 2013 - 04:44 PM
You're going to want it in tables anyhow, if you want to be able to track more than just one missile at a time. What's the opposition to tables?
Posted 20 January 2013 - 04:44 PM
*sigh*. Do you want me to spell it out for you? Alright…I'm not trying to make a new table… I'm trying to get info OUT of the table and IN to variables. The getEntities method RETURNS a table that does me no good for plugging into a math formula.
function ping()
term.clear()
term.setCursorPos(1,1)
for i,v in pairs(radar.getEntities()) do
local x = v[1]
local z = v[2]
local y = v[3]
end
end
Plug values into distance formula.Edited on 20 January 2013 - 03:45 PM
Posted 20 January 2013 - 04:45 PM
You're going to want it in tables anyhow, if you want to be able to track more than just one missile at a time. What's the opposition to tables?
Because the ICBM radar doesn't return the location for more than one missile.
Posted 20 January 2013 - 04:48 PM
*sigh*. Do you want me to spell it out for you? Alright…I'm not trying to make a new table… I'm trying to get info OUT of the table and IN to variables. The getEntities method RETURNS a table that does me no good for plugging into a math formula.Plug values into distance formula.function ping() term.clear() term.setCursorPos(1,1) for i,v in pairs(radar.getEntities()) do local x = v[1] local z = v[2] local y = v[3] end end
I'm not trying to be difficult but I have never used tables and never taken any kind of programming class, be it college or CC University. I'm learning this as I go along.
Posted 20 January 2013 - 04:48 PM
I'm.. sorry? Forgive me if there is a simple explanation, but why is there a radar.getEntities function if it only returns data for one missile? Seems a bit of an odd implementation?Because the ICBM radar doesn't return the location for more than one missile.
Alright then. We all had to learn something at some point.I'm not trying to be difficult but I have never used tables and never taken any kind of programming class, be it college or CC University. I'm learning this as I go along.
Edited on 20 January 2013 - 03:49 PM
Posted 20 January 2013 - 04:52 PM
I'm.. sorry? Forgive me if there is a simple explanation, but why is there a radar.getEntities function if it only returns data for one missile? Seems a bit of an odd implementation?Because the ICBM radar doesn't return the location for more than one missile.
Don't look at me… The GUI for the radar will show more than one missile so I *assume* there is a way to track more than one missile but I have no clue how.
Alright then. We all had to learn something at some point.I'm not trying to be difficult but I have never used tables and never taken any kind of programming class, be it college or CC University. I'm learning this as I go along.
I know and I am thankful for the help. I just tend to get frustrated easily when I don't understand things.
Posted 20 January 2013 - 04:56 PM
The getEntities function AND the getBlocks function BOTH return the data in a table…
function ping()
term.clear()
term.setCursorPos(1,1)
for i,v in pairs(radar.getEntities()) do
local entityX = v["x"]
local entityY = v["y"]
local entityZ = v["z"]
print( "Distance: "..-- formula goes here )
end
end
Edited on 20 January 2013 - 03:58 PM
Posted 20 January 2013 - 05:17 PM
Ok I have hit an issue… Here is my code:
The problem I have is that at like 13 it tells me 'attempt to index ? <a number value> '
What did I do wrong?
radar=peripheral.wrap("back:red")
print("Enter X:")
x1=tonumber(read())
print("Enter Y:")
y1=tonumber(read())
print("Enter Z:")
z1=tonumber(read())
function ping(x1,y1,z1)
term.clear()
term.setCursorPos(1,1)
for i,v in pairs(radar.getEntities()) do
print(v)
local entityX=v[1]
local entityY=v[2]
local entityZ=v[3]
dist=math.sqrt(math.pow(x1-entityX,2)+math.pow(y1-entityY,2)+math.pow(z1-entityZ,2))
print("Distance: "..dist)
end
end
while true do
ping()
sleep(0.1)
end
The problem I have is that at like 13 it tells me 'attempt to index ? <a number value> '
What did I do wrong?
Posted 20 January 2013 - 05:19 PM
Thats because the x,y,z data isn't stored in index 1 2 and 3… its stored in as a key/value!!!! Read my reply… I don't make it a habit to repeating myself for a 3rd time!Ok I have hit an issue… Here is my code:radar=peripheral.wrap("back:red") print("Enter X:") x1=tonumber(read()) print("Enter Y:") y1=tonumber(read()) print("Enter Z:") z1=tonumber(read()) function ping(x1,y1,z1) term.clear() term.setCursorPos(1,1) for i,v in pairs(radar.getEntities()) do print(v) local entityX=v[1] local entityY=v[2] local entityZ=v[3] dist=math.sqrt(math.pow(x1-entityX,2)+math.pow(y1-entityY,2)+math.pow(z1-entityZ,2)) print("Distance: "..dist) end end while true do ping() sleep(0.1) end
The problem I have is that at like 13 it tells me 'attempt to index ? <a number value> '
What did I do wrong?
Posted 20 January 2013 - 05:24 PM
Thats because the x,y,z data isn't stored in index 1 2 and 3… its stored in as a key/value!!!! Read my reply… I don't make it a habit to repeating myself for a 3rd time!
Sorry. Missed the quote marks.
Posted 20 January 2013 - 05:25 PM
not just quote marks… you had numbers instead of "x" "y" "z"Sorry. Missed the quote marks.Thats because the x,y,z data isn't stored in index 1 2 and 3… its stored in as a key/value!!!! Read my reply… I don't make it a habit to repeating myself for a 3rd time!
Posted 20 January 2013 - 05:29 PM
not just quote marks… you had numbers instead of "x" "y" "z"Sorry. Missed the quote marks.Thats because the x,y,z data isn't stored in index 1 2 and 3… its stored in as a key/value!!!! Read my reply… I don't make it a habit to repeating myself for a 3rd time!
Yea. Sorry i missed that. >_<
Posted 20 January 2013 - 05:34 PM
Ok… This makes no sense… I have tried it with the x,y,z and i still get the same error…
radar=peripheral.wrap("back:red")
print("Enter X:")
x1=tonumber(read())
print("Enter Y:")
y1=tonumber(read())
print("Enter Z:")
z1=tonumber(read())
function ping(x1,y1,z1)
term.clear()
term.setCursorPos(1,1)
for i,v in pairs(radar.getEntities()) do
local entityY=v["y"]
local entityX=v["x"]
local entityZ=v["z"]
dist=math.sqrt(math.pow(x1-entityX,2)+math.pow(y1-entityY,2)+math.pow(z1-entityZ,2))
print("Distance: "..dist)
end
end
while true do
ping()
sleep(0.1)
end
Posted 20 January 2013 - 05:58 PM
Try doing a
for i2,v2 in pairs(v) do
print(i2 .. "=" .. v2)
end
instead of getting the x/y/z and calculating the distance, just to see what sort of data is in the table.Posted 20 January 2013 - 06:03 PM
I did this:Try doing ainstead of getting the x/y/z and calculating the distance, just to see what sort of data is in the table.for i2,v2 in pairs(v) do print(i2 .. "=" .. v2) end
for i,v in pairs(radar.getEntities()) do
print(i..","..v)
end
It returns something like:
y,240
x,70
z,-240
Posted 20 January 2013 - 06:06 PM
Odd. Can you try something like-snip-
print(i)
before doing the i2,v2 thing?
for i,v in pairs(radar.getEntities()) do
print(i) --This
for i2,v2 in pairs(v) do
print(i2 .. "=" .. v2)
end
end
Edited on 20 January 2013 - 05:06 PM
Posted 20 January 2013 - 06:10 PM
I just returns y x z on three different lines.
Posted 20 January 2013 - 06:24 PM
Yes…I'm not sure what RunasSudo was thinking there (apparently that the values were subtables, though why I can't imagine, perhaps because your posted code makes the same error?).
Perhaps you should change the function to look more like:
Also, this code is a bit sloppy, I just hastily edited what you had. I did I f it does work you'll want to clean it some.
Perhaps you should change the function to look more like:
function ping(x1,y1,z1)
term.clear()
term.setCursorPos(1,1)
local p = radar.getEntities()
local dist=math.sqrt(math.pow(x1-p.x,2)+math.pow(y1-p.y,2)+math.pow(z1-p.z,2))
print("Distance: "..dist)
end
I don't know if this will work exactly, as I don't have the ICBM mod.Also, this code is a bit sloppy, I just hastily edited what you had. I did I
Edited on 20 January 2013 - 05:35 PM
Posted 20 January 2013 - 06:27 PM
Yes…I'm not sure what RunasSudo was thinking there (apparently that the values were subtables, though why I can't imagine, perhaps because your posted code makes the same error?).
Perhaps you should change the function to look more like:I don't know if this will work exactly, as I don't have the ICBM mod.function ping(x1,y1,z1) term.clear() term.setCursorPos(1,1) v = radar.getEntities() dist=math.sqrt(math.pow(v.x,2)+math.pow(y1-v.y,2)+math.pow(z1-v.z,2)) print("Distance: "..dist) end
Well i know that the getEntities() function returns a table but i will try your suggestion.
Posted 20 January 2013 - 06:33 PM
Yes, and you can access string indexed values of a table by writing the table name and the string index separated by a ".", as in "v.x". This only works for string indexes, but since that's what we're dealing with I didn't think it would be a problem.
Really, both v and dist should be declared local, and I noticed that I accidentally deleted the x1- from your formula, so the answer will be wrong :o/>
Very sloppy of me. Let me get those.
Really, both v and dist should be declared local, and I noticed that I accidentally deleted the x1- from your formula, so the answer will be wrong :o/>
Very sloppy of me. Let me get those.
Posted 20 January 2013 - 06:37 PM
Yes, and you can access string indexed values of a table by writing the table name and the string index separated by a ".", as in "v.x". This only works for string indexes, but since that's what we're dealing with I didn't think it would be a problem.
Really, both v and dist should be declared local, and I noticed that I accidentally deleted the x1- from your formula, so the answer will be wrong :o/>/>/>
Very sloppy of me. Let me get those.
Any way to make it wait for the getEntities() to not return nil so it stops giving an error?
Edit: Also How can i give it zero values for the v.x and so on as holder values so it won't error out?
Posted 20 January 2013 - 07:32 PM
I was sort of wondering what the keys for the getEntities table were (whether they were 'normal' like 1,2,3 or something weird like 'pig', 'cow170', 'OMGITSACREEPPPPEERRRR'). Not sure why you thought I thought the values were subtables…Yes…I'm not sure what RunasSudo was thinking there (apparently that the values were subtables, though why I can't imagine, perhaps because your posted code makes the same error?).
Any way to make it wait for the getEntities() to not return nil so it stops giving an error?
Edit: Also How can i give it zero values for the v.x and so on as holder values so it won't error out?
local entities = radar.getEntities()
if entities then --This checks to see if entities is not nil
--Do loopy thing
local x = v.x
local y = v.y
local z = v.z
if x and y and z then --This checks to see if all are not nil
--Calculate distance
end
end
Posted 20 January 2013 - 07:52 PM
Edit: Also How can i give it zero values for the v.x and so on as holder values so it won't error out?
local x = v.x or 0
local y = v.y or 0
local z = v.z or 0
If any of them are nil they will be set to 0…Posted 20 January 2013 - 08:37 PM
Edit: Also How can i give it zero values for the v.x and so on as holder values so it won't error out?If any of them are nil they will be set to 0…local x = v.x or 0 local y = v.y or 0 local z = v.z or 0
And finially my program is done!!!