How to locate a unit on 2D screen ? [Solved]

Everything about mods can be found here.

Moderator: Morax

How to locate a unit on 2D screen ? [Solved]

Postby Franck83 » 30 Dec 2016, 13:12

Hi all,

How can i find the x,y position on the screen for any unit ?

I found the VECTOR3 GetPosition() that give me the x,y,z world position. But i need to find the x,y position on the screen for Ui features.

Any idea ?


My brother and me are working since one year on a RPG class system for all units in game with advanced XP, stances, Defense and Attack rating, skills, powers (buffs, debuffs, passive abilities, active ones...)
All units are able to promote as Heroes then specialize in base and prestige classes, train skills... This mod is potentially compatible with all kind of unit pack mods since it listen the unit bp to active and surcharge class features. The work is an advanced state now, functionnal as you can see in the screenshot, but it needs a lot of work (especially graphical ones, architectural improvements, a lot of balance work).

Image

The mod system in supcom give us an outstanding liberty to craft a powerfull tactical mod. The unit diversity of supcom give some impressive possibilities.

If you are excited with this possibilities as we are, stay tuned, we'll need some help in the future months.
Last edited by Franck83 on 02 Jan 2017, 18:10, edited 2 times in total.
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Evaluator
 
Posts: 538
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 114 times
Been liked: 122 times
FAF User Name: Franck83

Re: Heroes mod project. How to locate a unit on screen ?

Postby Exotic_Retard » 30 Dec 2016, 13:24

hello, try:
Project()
INFO: VECTOR2 Project(self,VECTOR3) - given a point in world space, projects the point to control space
or maybe this:
GetScreenPos()

INFO: (vector2f|nil) = GetScreenPos(unit)


look at this luadoc which contains a bunch of methods in it:
http://supcom.wikia.com/wiki/LUADOC_1.5.3599

you want the ui section probably.
User avatar
Exotic_Retard
Contributor
 
Posts: 1470
Joined: 21 Mar 2013, 22:51
Has liked: 557 times
Been liked: 626 times
FAF User Name: Exotic_Retard

Re: Heroes mod project. How to locate a unit on screen ?

Postby Franck83 » 30 Dec 2016, 13:27

11 min to obtain a response. You are a boss Exotic_Retard :) !

Thanks a lot, i will try this.
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Evaluator
 
Posts: 538
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 114 times
Been liked: 122 times
FAF User Name: Franck83

Re: Heroes mod project. How to locate a unit on screen ?

Postby Franck83 » 30 Dec 2016, 14:58

It seems that i need to manage a Worldview object in order to use the User.CUIWorldView.GetScreenPos() function.

It's logical because we need a 2D plan to projet the unit 3D world coordonates. It depends of the camera position too and its zoom state.

But my supcom understanding is not enough good to grab a worldview object (moho.UIWorldView:__init(parent_control, cameraName, depth, isMiniMap, trackCamera)).

If somebody has some experience with this kind of usage :roll:. If not, i will try futher when i will be able to understand how it works.
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Evaluator
 
Posts: 538
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 114 times
Been liked: 122 times
FAF User Name: Franck83

Re: Heroes mod project. How to locate a unit on screen ?

Postby Franck83 » 30 Dec 2016, 22:41

Hi again, i tried :

Code: Select all
local WorldView = import('/lua/ui/controls/worldview.lua').WorldView
--
local unit = GetSelectedUnits()
local V2pos  =  WorldView.GetScreenPos(unit[1])
LOG(repr(V2pos))   


WorldView is a right object, a repr(WorldView) is successfull but the log say that i'm missing one argument :

WARNING: Error running lua script: (vector2f|nil) = GetScreenPos(unit)
expected 2 args, but got 1


Any idea ? Thanks for your help.
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Evaluator
 
Posts: 538
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 114 times
Been liked: 122 times
FAF User Name: Franck83

Re: Heroes mod project. How to locate a unit on screen ?

Postby nine2 » 31 Dec 2016, 13:25

change the dot to a colon

WorldView.GetScreenPos(unit[1])
WorldView:GetScreenPos(unit[1])
nine2
Councillor - Promotion
 
Posts: 2416
Joined: 16 Apr 2013, 10:10
Has liked: 285 times
Been liked: 515 times
FAF User Name: Anihilnine

Re: Heroes mod project. How to locate a unit on screen ?

Postby Franck83 » 31 Dec 2016, 19:59

Thanks Anni for your reply.

I thought same thing as you and i tried this but i still got an error on log :

WARNING:Error running lua script: ... forged alliance\mods\heroes\modules\unit_mainui.lua(431): Expected a game object. (Did you call with '.' instead of ':'?)
stack traceback:
[C]: in function `GetScreenPos'


I must have missed something

If i'm putting a repr(WorldView), i'm getting [...] GetScreenPos=moho.UIWorldView.GetScreenPos [...] in the LOG.
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Evaluator
 
Posts: 538
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 114 times
Been liked: 122 times
FAF User Name: Franck83

Re: Heroes mod project. How to locate a unit on screen ?

Postby Exotic_Retard » 31 Dec 2016, 20:58

try unit[1]:GetScreenPos()
this is kinda of guesswork over here tbh.

looking at exising lua code we see that apparently getscreenpos wasnt used much, and not in faf.

local coords = self:Project(Vector(pingData.Location[1], pingData.Location[2], pingData.Location[3]))

it returns a table with x and y in it so you can use coords.y for example and it appears to be in pixels?

pingBmp.Top:Set(function() return self.Top() + coords.y - pingBmp.Height() / 2 end)

this is how you get a 2d vector from a 3d one (unit:GetPosition() returns a 3d vector for example) which should hopefully correspond to your screen space, and this line is used in worldview.lua for pings. so try that.
User avatar
Exotic_Retard
Contributor
 
Posts: 1470
Joined: 21 Mar 2013, 22:51
Has liked: 557 times
Been liked: 626 times
FAF User Name: Exotic_Retard

Re: Heroes mod project. How to locate a unit on screen ?

Postby Franck83 » 01 Jan 2017, 20:04

Thanks Exo for your the time you spent.

I saw that the health mini bar under the unit and the unit name is are Ui feature that necessary need 2D screen position. I didn't find where getscreenpos is coded. Maybe it's hard coded.
When we rotate the camera vertically, the health mni bar and the unit name disapear. It means that the Ui appear at a fixed camera orientation.

I will try what you suggested.
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Evaluator
 
Posts: 538
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 114 times
Been liked: 122 times
FAF User Name: Franck83

Re: Heroes mod project. How to locate a unit on screen ?

Postby Franck83 » 02 Jan 2017, 12:13

Hi Again,


It's working now :) !

For the topic purpose, this the working code that locate the unit pixel 2D position in the screen (in the example in the world main view and in the mini map).
It's for Ui side :

Code: Select all
UnitEntity = GetUnitById(id)
local V3pos  =  UnitEntity:GetPosition()
local views = import('/lua/ui/game/worldview.lua').GetWorldViews()
for _, view in views do
   local coords = view:Project(V3pos)
   LOG("Unit 2D coordonates : " .. repr(coords))   
end


or by using GetScreenPos

Code: Select all
UnitEntity = GetUnitById(id)
local views = import('/lua/ui/game/worldview.lua').GetWorldViews()
for _, view in views do
   local coords = view:GetScreenPos(UnitEntity)
   LOG("Unit 2D coordonates : " .. repr(coords))   
end


This offer nice possibilities. For my mod, it allows to print damages values just over the unit in realtime. But you can add value bars under the unit or some special informations.

Thanks for your valuable help Anni and Exo.
Alliance of Heroes Mod is out ! Try it ! It's in the Mod Vault !
User avatar
Franck83
Evaluator
 
Posts: 538
Joined: 30 Dec 2016, 11:59
Location: France
Has liked: 114 times
Been liked: 122 times
FAF User Name: Franck83

Next

Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest