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

Converting scan area to screen size

Started by theoctagon, 27 March 2013 - 09:31 AM
theoctagon #1
Posted 27 March 2013 - 10:31 AM
Greetings,

I am trying to figure out how to make the conversion from a proximity sensor scan radius to the screen size of a monitor for display purposes.

I know how to get the screen size of the monitor (x,y = monitor.getSize()), and I know the radius of the scanner to be up to 64 units. If I remember correctly I can use something similar to scanrange = proximity.getSensorRange().

What I don't know how to do is to make the scale change so I can say something to the effect of:

monitor.setCursorPos(TargetX,TargetY)

and it be fairly accurate to being a representation of it being in relation to my own location (assuming center screen like a minimap)

Help (and Thanks!)
jag #2
Posted 27 March 2013 - 10:35 AM
Excuse me but, what the hell is a prioximity scan?
Could you link it?
theoctagon #3
Posted 27 March 2013 - 10:46 AM
Its one of the sensors from the OpenCCSensors mod.

http://www.computercraft.info/forums2/index.php?/topic/5996-147-cc-15-openccsensors/

My issue isn't the sensor, its converting radius to screen size scale. (I think) ;)/>
jag #4
Posted 27 March 2013 - 06:34 PM
Oh well I haven't seen that before, maybe should try take a look.
jag #5
Posted 27 March 2013 - 08:09 PM
I found a simple way to do it, basically inside a loop like this one

for i,v in pairs( prox.getTargets() ) do
I just extract the X and Z position and write the name of the entity on the screen relative to the screens center.

term.setCursorPos( w/2 + v.Position.X, h/2 + v.Position.Z )
term.write( i )
Simple enough, I hope this was what you asked for.
theoriginalbit #6
Posted 27 March 2013 - 08:39 PM
I found a simple way to do it, basically inside a loop like this one

for i,v in pairs( prox.getTargets() ) do
I just extract the X and Z position and write the name of the entity on the screen relative to the screens center.

term.setCursorPos( w/2 + v.Position.X, h/2 + v.Position.Z )
term.write( i )
Simple enough, I hope this was what you asked for.
Since it scans 64 out though, that means some would be off the screen just by using a simple addition, a formula is required to reduce the distance so even at all extremities its on the screen.
jag #7
Posted 27 March 2013 - 10:40 PM
Since it scans 64 out though, that means some would be off the screen just by using a simple addition, a formula is required to reduce the distance so even at all extremities its on the screen.

Well yeah you're right.
I can give you the new ekvation but first I need to know is it 64 blocks? And is that the radius or diameter?
LordIkol #8
Posted 27 March 2013 - 11:04 PM
Are you sure the Range is 64?
from what i see in the Wiki it depends on the Tier of the Sensor.
http://www.computerc...e=OpenCCSensors (bottom of the page)

Highest Range for Prox Sensor is 28 blocks radius Sphere so this would result in 56 blocks Range.
Or did i get that wrong?
theoriginalbit #9
Posted 27 March 2013 - 11:05 PM
And is that the radius or diameter?
That's the main reason I haven't posted an equation. OP never specified :P/>
LordIkol #10
Posted 28 March 2013 - 01:11 AM
Hi together,

I wrote sth that should work. I think you can make it nicer and maybe more accurate but i hope this helps you to get the clue.
In this code i suggest that you get the Coordinates based on a radius around the turtle like (5,-10,0)

Used some random numbers for testing on CCEmu cause im not at home :)/>


w,h = term.getSize()
diameter = 56 -- set this to radius*2
mw = w/diameter -- get the multiplier for wide
mh = h/diameter -- get the multiplier for high

centerw = w/2
centerh = h/2

term.clear()

local function drawposition(mobx, mobz,symbol)
mapx = math.floor(centerw+1 - math.floor(mobx*mw))
mapz = math.floor(centerh+1 - math.floor(mobz*mh))
term.setCursorPos( mapx, mapz )
term.write(symbol)
term.setCursorPos( centerw+1, centerh+1 )  --this can be deleted was just annoyed of the curser ending up somewhere stupid so i put him where the sensor would be :D/>
end

for i = 1,10 do
x = math.random(-28,28) --replace with "v.Position.X"
z = math.random(-28,28) --replace with "v.Position.Z"
drawposition(x, z,"0") --last parameter is the symbol that should be printed
end
Edited on 28 March 2013 - 12:19 AM
theoctagon #11
Posted 01 April 2013 - 09:03 AM
Wow! Thanks gang! These were all very helpful. In particular the last one as I can really use that to extrapolate the graphical sensor display on the monitor with that. Thanks so much.

Ciao for now!

(Check out my previous Minecraft build, Star Ship NCC-1962 at my website! I might even be able to incorporate this into the ships sensors and main viewer!)
theoctagon #12
Posted 12 April 2013 - 08:59 AM
Hi together,

I wrote sth that should work. I think you can make it nicer and maybe more accurate but i hope this helps you to get the clue.
In this code i suggest that you get the Coordinates based on a radius around the turtle like (5,-10,0)

Used some random numbers for testing on CCEmu cause im not at home :)/>


w,h = term.getSize()
diameter = 56 -- set this to radius*2
mw = w/diameter -- get the multiplier for wide
mh = h/diameter -- get the multiplier for high

centerw = w/2
centerh = h/2

term.clear()

local function drawposition(mobx, mobz,symbol)
mapx = math.floor(centerw+1 - math.floor(mobx*mw))
mapz = math.floor(centerh+1 - math.floor(mobz*mh))
term.setCursorPos( mapx, mapz )
term.write(symbol)
term.setCursorPos( centerw+1, centerh+1 )  --this can be deleted was just annoyed of the curser ending up somewhere stupid so i put him where the sensor would be :D/>/>/>
end

for i = 1,10 do
x = math.random(-28,28) --replace with "v.Position.X"
z = math.random(-28,28) --replace with "v.Position.Z"
drawposition(x, z,"0") --last parameter is the symbol that should be printed
end

Since you were so helpful, I thought I'd share the final product. This runs on any advanced computer/monitor. As described, it scans the area and displays the targets on the monitor with their names displayed. They appear is relation to the sensor location, which does work out to be just about center screen regardless of monitor stretch. I hope you find it useful and I'm certain there can be much extrapolation to this for folks down the road.

Enjoy and Thanks!

————————-

os.loadAPI("/ocs/apis/sensor")

sside = "bottom"
mside = "right"
moni = peripheral.wrap(mside)
moni.clear()
moni.setTextScale(.5)
moni.setBackgroundColor(colors.lime)
moni.setTextColor(colors.red)

w,h = moni.getSize()
diameter = 56 – set this to radius*2
mw = w/diameter – get the multiplier for wide
mh = h/diameter – get the multiplier for high
centerw = w/2
centerh = h/2

proximity = sensor.wrap(sside)

term.clear()
term.setCursorPos(1,1)
print("Scanning targets …")

local function drawposition(mobx, mobz,symbol,name)

mapx = math.floor(centerw+1 - math.floor(mobx*mw))
mapz = math.floor(centerh+1 - math.floor(mobz*mh))
moni.setTextColor(colors.red)
moni.setCursorPos( mapx, mapz )
moni.write(symbol)
moni.setTextColor(colors.black)
moni.setCursorPos(mapx,mapz-1)
moni.write(name)
moni.setCursorPos( centerw+1, centerh+1 ) –Sensor location
end

while true do
moni.clear()
targets = proximity.getTargets()
for k,v in pairs(targets) do
x = v.Position.X
z = v.Position.Z
name = v.Name
drawposition(x, z,"X",name) –last two parameters are the symbol that should be printed and above it the target name
os.sleep(.25)
end
end