Page 1 of 2
question about coordinate unit.
Posted: 28 Mar 2013, 12:25
by Broker
how can I get the coordinates of the center of the screen on the map?
which means z coordinate on the map? Does it need consider when ordering others to move units.
Re: question about coordinate unit.
Posted: 28 Mar 2013, 12:30
by gajop
I assume you mean map size (screen size is resolution Spring's using):
http://springrts.com/wiki/Lua_ConstGame#Map.
X and Z coordinates define the map size (Y is height), so yes, it's important to specify both X and Z when giving map-specific orders.
Re: question about coordinate unit.
Posted: 28 Mar 2013, 12:43
by Broker
gajop wrote:I assume you mean map size (screen size is resolution Spring's using):
http://springrts.com/wiki/Lua_ConstGame#Map.
X and Z coordinates define the map size (Y is height), so yes, it's important to specify both X and Z when giving map-specific orders.
Thank you. I understand about the coordinates.
my task is to select a unit closest to the center of the screen on the map. I need to determine the coordinates of the center of the screen on the map.
Re: question about coordinate unit.
Posted: 28 Mar 2013, 12:47
by gajop
divide by 2
Re: question about coordinate unit.
Posted: 28 Mar 2013, 12:52
by Broker
gajop wrote:divide by 2
not the center of the map. center of the screen. as the coordinates of the mouse cursor on the map.
Re: question about coordinate unit.
Posted: 28 Mar 2013, 12:59
by gajop
OK, do you need the mouse position or the screen center?
mouse:
local x, y = Spring.GetMouseState()
local result, coords = Spring.TraceScreenRay(x, y, true)
if result == "ground" then
local mapX, mapY, mapZ = unpack(coords)
...
EDIT:
screen: (not tested, corrected by below examples)
local x, y = Spring.GetViewGeometry()
local result, coords = Spring.TraceScreenRay(x, y, true)
if result == "ground" then
local mapX, mapY, mapZ = unpack(coords)
...
Re: question about coordinate unit.
Posted: 28 Mar 2013, 13:02
by knorke
Re: question about coordinate unit.
Posted: 28 Mar 2013, 13:06
by Anarchid
local x, y = Spring.GetCameraPosition()
GetCameraPosition returns 3d coordinates of camera in world space, so is unfit as source of parameters for TraceScreenRay.
What you want instead should look like:
Code: Select all
local x, y = Spring.GetViewGeometry()
local result, coords = Spring.TraceScreenRay(x, y, true)
if result == "ground" then
local mapX, mapY, mapZ = unpack(coords)
etc ...
Pretty much what ninjaknorke said.
Re: question about coordinate unit.
Posted: 28 Mar 2013, 13:23
by Broker
Anarchid wrote: local x, y = Spring.GetCameraPosition()
GetCameraPosition returns 3d coordinates of camera in world space, so is unfit as source of parameters for TraceScreenRay.
What you want instead should look like:
Code: Select all
local x, y = Spring.GetViewGeometry()
local result, coords = Spring.TraceScreenRay(x, y, true)
if result == "ground" then
local mapX, mapY, mapZ = unpack(coords)
etc ...
Pretty much what ninjaknorke said.
Code: Select all
local x, y = Spring.GetViewGeometry()
local result, coords = Spring.TraceScreenRay(x, y, true)
if result == "ground" then
local mapX, mapY, mapZ = unpack(coords)
local x, y, z = spGetUnitPosition(factID)
local dist = math.sqrt((mapX-x)^2+ (mapZ-z)^2)
dist is the distance between the center of the screen and the unit. ok?
Re: question about coordinate unit.
Posted: 29 Mar 2013, 09:19
by Broker
Anarchid wrote: local x, y = Spring.GetCameraPosition()
GetCameraPosition returns 3d coordinates of camera in world space, so is unfit as source of parameters for TraceScreenRay.
What you want instead should look like:
Code: Select all
local x, y = Spring.GetViewGeometry()
local result, coords = Spring.TraceScreenRay(x, y, true)
if result == "ground" then
local mapX, mapY, mapZ = unpack(coords)
etc ...
Pretty much what ninjaknorke said.
does not work.
Re: question about coordinate unit.
Posted: 29 Mar 2013, 09:42
by gajop
You need to put some effort in this yourself if you want help. Post the exact error, or show what isn't working.
Re: question about coordinate unit.
Posted: 29 Mar 2013, 09:48
by Broker
gajop wrote:You need to put some effort in this yourself if you want help. Post the exact error, or show what isn't working.
sorry
Code: Select all
local x, y = Spring.GetViewGeometry()
local result, coords = Spring.TraceScreenRay(x, y, true)
if result == "ground" then
local mapX, mapY, mapZ = unpack(coords)
end
echo(x)
echo(y)
echo(mapX)
echo(mapZ)
function echo(msg)
if type(msg) == 'string' or type(msg) == 'number' then
Spring.SendCommands({"echo " .. msg})
elseif type(msg) == 'table' then
Spring.SendCommands({"echo echo failed on table"})
else
Spring.SendCommands({"echo broke :-"})
end
end
and result in console
[f=0003013] 1594
[f=0003013] 965
[f=0003013] broke :-
[f=0003013] broke :-
numbers do not change when you move the camera.
Re: question about coordinate unit.
Posted: 29 Mar 2013, 09:54
by gajop
you are referring to variables outside their scope
also that can't be the entirety of the code, right?
Re: question about coordinate unit.
Posted: 29 Mar 2013, 11:43
by Broker
gajop wrote:you are referring to variables outside their scope
also that can't be the entirety of the code, right?
yes. full code is
it is part from gui_idle_builders_new.lua
[code]
function widget:MouseRelease(x, y, button)
if not activePress then return -1 end
activePress = false
local iconNum = MouseOverIcon(x, y)
if iconNum < 0 then return -1 end
local unitID = drawTable[iconNum+1].units
local unitDefID = drawTable[iconNum+1].unitDefID
if type(unitID) == 'table' then
if Clicks[unitDefID] then
Clicks[unitDefID] = Clicks[unitDefID] + 1
else
Clicks[unitDefID] = 1
end
local x1, y1 = Spring.GetViewGeometry()
local result, coords = Spring.TraceScreenRay(x1, y1, true)
if result == "ground" then
local mapX, mapY, mapZ = unpack(coords)
end
echo(x1)
echo(y1)
echo(mapX)
echo(mapZ)
unitID = unitID[math.fmod(Clicks[unitDefID], table.getn(unitID))+1]
local x2, y2, z2 = spGetUnitPosition(unitID)
local dist = math.sqrt((mapX-x2)^2+ (mapZ-z2)^2)
end
local alt, ctrl, meta, shift = Spring.GetModKeyState()
if (button == 1) then -- left mouse
Spring.SelectUnitArray({unitID})
elseif (button == 2) then -- middle mouse
Spring.SelectUnitArray({unitID})
Spring.SendCommands({"viewselection"})
end
return -1
end
[/code]
dist is the distance between the center of the screen and the unit.
Re: question about coordinate unit.
Posted: 29 Mar 2013, 12:16
by gajop
ok start by fixing this
gajop wrote:you are referring to variables outside their scope
mapX, mapY and mapZ are referred out of scope
next, are you *sure* you want to get the center of the screen for this, and not the mouse position? in case you want to use the mouse position, you can just use the x and y coordinates from the event (pass them to TraceScreenRay as you would usually)
Re: question about coordinate unit.
Posted: 29 Mar 2013, 12:35
by Broker
gajop wrote:ok start by fixing this
gajop wrote:you are referring to variables outside their scope
mapX, mapY and mapZ are referred out of scope
next, are you *sure* you want to get the center of the screen for this, and not the mouse position? in case you want to use the mouse position, you can just use the x and y coordinates from the event (pass them to TraceScreenRay as you would usually)
idea is this. choose from a list of builders closest to the proposed site of construction. unit with the minimum distance. usually place construction of buildings is the center screen.
Re: question about coordinate unit.
Posted: 29 Mar 2013, 13:59
by zwzsg
return -1 ? Really? I know of some callins where you return true to say your widget ate the event or nil/false to keep it available to other events. But -1 ?
Re: question about coordinate unit.
Posted: 29 Mar 2013, 14:05
by Broker
zwzsg wrote:return -1 ? Really? I know of some callins where you return true to say your widget ate the event or nil/false to keep it available to other events. But -1 ?
I do not know. this is the code of the original widget.
Re: question about coordinate unit.
Posted: 29 Mar 2013, 14:23
by zwzsg
you are referring to variables outside their scope
This means that you have to change
Code: Select all
function fct()
if tst then
local a,b = ...
end
Spring.Echo(a,b)
end
into :
Code: Select all
function fct()
local a,b
if tst then
a,b = ...
end
Spring.Echo(a,b)
end
Re: question about coordinate unit.
Posted: 29 Mar 2013, 14:47
by Broker
zwzsg wrote:you are referring to variables outside their scope
This means that you have to change
Code: Select all
function fct()
if tst then
local a,b = ...
end
Spring.Echo(a,b)
end
into :
Code: Select all
function fct()
local a,b
if tst then
a,b = ...
end
Spring.Echo(a,b)
end
I understand. I changed it but nothing happened.