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 » 28 Mar 2013, 12:25

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.
0 x

gajop
Moderator
Posts: 2952
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post by gajop » 28 Mar 2013, 12:30

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.
0 x

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

Re: question about coordinate unit.

Post by Broker » 28 Mar 2013, 12:43

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.
0 x

gajop
Moderator
Posts: 2952
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post by gajop » 28 Mar 2013, 12:47

divide by 2
0 x

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

Re: question about coordinate unit.

Post by Broker » 28 Mar 2013, 12:52

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.
0 x

gajop
Moderator
Posts: 2952
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post by gajop » 28 Mar 2013, 12:59

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)
...
0 x

User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: question about coordinate unit.

Post by knorke » 28 Mar 2013, 13:02

0 x

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

Re: question about coordinate unit.

Post by Anarchid » 28 Mar 2013, 13:06

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.
0 x

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

Re: question about coordinate unit.

Post by Broker » 28 Mar 2013, 13:23

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?
0 x

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

Re: question about coordinate unit.

Post by Broker » 29 Mar 2013, 09:19

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.
0 x

gajop
Moderator
Posts: 2952
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post by gajop » 29 Mar 2013, 09:42

You need to put some effort in this yourself if you want help. Post the exact error, or show what isn't working.
0 x

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

Re: question about coordinate unit.

Post by Broker » 29 Mar 2013, 09:48

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.
0 x

gajop
Moderator
Posts: 2952
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post by gajop » 29 Mar 2013, 09:54

you are referring to variables outside their scope
also that can't be the entirety of the code, right?
0 x

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

Re: question about coordinate unit.

Post by Broker » 29 Mar 2013, 11:43

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.
0 x

gajop
Moderator
Posts: 2952
Joined: 05 Aug 2009, 20:42

Re: question about coordinate unit.

Post by gajop » 29 Mar 2013, 12:16

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)
0 x

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

Re: question about coordinate unit.

Post by Broker » 29 Mar 2013, 12:35

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.
0 x

User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7002
Joined: 16 Nov 2004, 13:08

Re: question about coordinate unit.

Post by zwzsg » 29 Mar 2013, 13:59

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 ?
0 x

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

Re: question about coordinate unit.

Post by Broker » 29 Mar 2013, 14:05

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.
0 x

User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7002
Joined: 16 Nov 2004, 13:08

Re: question about coordinate unit.

Post by zwzsg » 29 Mar 2013, 14:23

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
0 x

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

Re: question about coordinate unit.

Post by Broker » 29 Mar 2013, 14:47

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.
0 x

Post Reply

Return to “Lua Scripts”