question about coordinate unit.
Moderator: Moderators
question about coordinate unit.
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.
which means z coordinate on the map? Does it need consider when ordering others to move units.
Re: question about coordinate unit.
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.
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.
Thank you. I understand about the coordinates.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.
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.
divide by 2
Re: question about coordinate unit.
not the center of the map. center of the screen. as the coordinates of the mouse cursor on the map.gajop wrote:divide by 2
Re: question about coordinate unit.
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)
...
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.
http://springrts.com/wiki/Lua_UnsyncedRead#Screen_Trace
and
http://springrts.com/wiki/Lua_UnsyncedR ... w_Geometry
(search forum for these functions, there are some examples)
and
http://springrts.com/wiki/Lua_UnsyncedR ... w_Geometry
(search forum for these functions, there are some examples)
Re: question about coordinate unit.
GetCameraPosition returns 3d coordinates of camera in world space, so is unfit as source of parameters for TraceScreenRay.local x, y = Spring.GetCameraPosition()
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 ...
Re: question about coordinate unit.
Anarchid wrote:GetCameraPosition returns 3d coordinates of camera in world space, so is unfit as source of parameters for TraceScreenRay.local x, y = Spring.GetCameraPosition()
What you want instead should look like: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) etc ...
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)
Re: question about coordinate unit.
does not work.Anarchid wrote:GetCameraPosition returns 3d coordinates of camera in world space, so is unfit as source of parameters for TraceScreenRay.local x, y = Spring.GetCameraPosition()
What you want instead should look like: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) etc ...
Re: question about coordinate unit.
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.
sorrygajop wrote:You need to put some effort in this yourself if you want help. Post the exact error, or show what isn't working.
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.
you are referring to variables outside their scope
also that can't be the entirety of the code, right?
also that can't be the entirety of the code, right?
Re: question about coordinate unit.
yes. full code isgajop wrote:you are referring to variables outside their scope
also that can't be the entirety of the code, right?
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.
ok start by fixing this
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)
mapX, mapY and mapZ are referred out of scopegajop wrote:you are referring to variables outside their 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.
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.gajop wrote:ok start by fixing thismapX, mapY and mapZ are referred out of scopegajop wrote:you are referring to variables outside their 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.
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.
I do not know. this is the code of the original widget.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 ?
Re: question about coordinate unit.
This means that you have to changeyou are referring to variables outside their scope
Code: Select all
function fct()
if tst then
local a,b = ...
end
Spring.Echo(a,b)
end
Code: Select all
function fct()
local a,b
if tst then
a,b = ...
end
Spring.Echo(a,b)
end
Re: question about coordinate unit.
zwzsg wrote:This means that you have to changeyou are referring to variables outside their scopeinto :Code: Select all
function fct() if tst then local a,b = ... end Spring.Echo(a,b) end
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.