question about coordinate unit.

question about coordinate unit.

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

question about coordinate unit.

Post 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.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post 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.
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: question about coordinate unit.

Post 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.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post by gajop »

divide by 2
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: question about coordinate unit.

Post 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.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post 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)
...
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: question about coordinate unit.

Post by knorke »

User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: question about coordinate unit.

Post 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.
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: question about coordinate unit.

Post 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?
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: question about coordinate unit.

Post 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.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post 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.
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: question about coordinate unit.

Post 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.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post by gajop »

you are referring to variables outside their scope
also that can't be the entirety of the code, right?
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: question about coordinate unit.

Post 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.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post 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)
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: question about coordinate unit.

Post 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.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: question about coordinate unit.

Post 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 ?
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: question about coordinate unit.

Post 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.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: question about coordinate unit.

Post 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
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: question about coordinate unit.

Post 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.
Post Reply

Return to “Lua Scripts”