• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

GUI friendly way of getting center of unit group

Status
Not open for further replies.

Deleted member 219079

D

Deleted member 219079

Like, using only unit group as data, and returning center of it.

I remember doing it once, when I knew JASS a little.

It's like sum of X coordinates divided by count of units in the group.
Then for Y, sum of Y coordinates divided by count of units in the group.
Then converting them, X and Y, to a point.
I think this would be too inefficient to do in GUI, so I'm requesting a little help here ^^

Also, importing instructions would be nice, or would it be like this:
  • Custom script: set udg_p = call GetCenter(udg_ug)
And where to put the function itself, lol sorry I've forgotten JASS T.T
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
Get the maximum x and y coordinates and then divide it in half. The one your describing sounds like a median of the elements of x and y.
 

Deleted member 219079

D

Deleted member 219079

What?

Edit: Red points are units, green are the most distant ones, having the max x and y. That wouldn't return the center of the group.
 

Attachments

  • asdasd.png
    asdasd.png
    6.2 KB · Views: 120
Level 23
Joined
Apr 16, 2012
Messages
4,041
you should calculate abosulte value of Xmin - Xmax and Ymin - Ymax, then that will give you lets say A and B, you do Xmin + A and Ymin + B, and you should get the center of unit group

in jass it would be something like this:

JASS:
//if you dont want to use vJass, or cant
//create those variables, otherwise uncomment the globals block
//globals
//    real udg_GetCenter_minX = 0
//    real udg_GetCenter_maxX = 0
//    real udg_GetCenter_minY = 0
//    real udg_GetCenter_maxY = 0
//endglobals

function enum_group_GetCenter takes nothing returns nothing
    local real x = GetUnitX(GetEnumUnit())
    local real y = GetUnitY(GetEnumUnit())
    if x < udg_GetCenter_minX then
        set udg_GetCenter_minX = x
    elseif x > udg_GetCenter_maxX then
        set udg_GetCenter_maxX = x
    endif
    if y < udg_GetCenter_minY then
        set udg_GetCenter_minY = y
    elseif y > udg_GetCenter_maxY then
        set udg_GetCenter_maxY = y
    endif
endfunction

function GetCenter takes group g returns location
    local real x = 0
    local real y = 0
    call ForGroup(g, function enum_group_GetCenter)
    set x = RAbsBJ((udg_GetCenter_maxX - udg_GetCenter_minX)/2) + udg_GetCenter_minX
    set y = RAbsBJ((udg_GetCenter_maxY - udg_GetCenter_minY)/2) + udg_GetCenter_minY
    set udg_GetCenter_minX = 0
    set udg_GetCenter_maxX = 0
    set udg_GetCenter_minY = 0
    set udg_GetCenter_maxY = 0
    return Location(x, y)
endfunction

hopefully this has no errors in it

EDIT: the 0-lifying of the reals is not really necessary, but I didnt test it so cant risk bugs :D
 

Deleted member 219079

D

Deleted member 219079

That's nice you gave me other ways to do it, but the problem is I can't do it in GUI, it uses coordinates which are inefficient in GUI.

I'd really appreciate if someone had time to make this function for me in JASS :)

edo494, that might work, but I'd use average X and Y, because your way would mess everything up if there'd be 1 unit fapping at the corner of map :/

Edit: Red points are units, green are the most distant ones, having the max x and y. That'd return the center of the group, but it'd be unpractical. My way would pay attention to others' position as well, not only to the most distant ones.

You may say "but didn't you request for center of the group?" yes I did, I'm sorry if I confused you, but this goes for my map.

Okay, I can explain to you where I'm going to use it. After playing cinematic, this system automatically pans player's camera back to the action. It uses the units selected by player assuming the action is on them.
 

Attachments

  • asdasdasd.png
    asdasdasd.png
    5.9 KB · Views: 64
Level 23
Joined
Apr 16, 2012
Messages
4,041
you want to average each x? you have to sum it up then, and divide it, that could work maybe :D

JASS:
//if you dont want to use vJass, or cant
//create those variables, otherwise uncomment the globals block
//globals
//    real udg_GetCenter_Y = 0
//    real udg_GetCenter_X = 0
//    integer udg_GetCenter_count = 0
//endglobals

function enum_group_GetCenter takes nothing returns nothing
    set udg_GetCenter_X = udg_GetCenter_X + GetUnitX(GetEnumUnit())
    set udg_GetCenter_Y = udg_GetCenter_Y + GetUnitY(GetEnumUnit())
    set udg_GetCenter_count = udg_GetCenter_count + 1
endfunction

function GetCenter takes group g returns location
    local real x = 0
    local real y = 0
    call ForGroup(g, function enum_group_GetCenter)
    set x = udg_GetCenter_X / udg_GetCenter_count
    set x = udg_GetCenter_Y / udg_GetCenter_count
    set udg_GetCenter_Y = 0
    set udg_GetCenter_X = 0
    set udg_GetCenter_count = 0
    return Location(x, y)
endfunction

maybe? or does it do the same thing? :D
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
You asked for the center, that's why I thought you meant the center and not the median point.

Try edo's script, that might work, I suck at math I'm not sure.
 

Deleted member 219079

D

Deleted member 219079

You asked for the center, that's why I thought you meant the center and not the median point.

Try edo's script, that might work, I suck at math I'm not sure.

Didn't you learn averages like on 3rd or 4th grade? o_O

Edit: T.T (See attachment) But thank you very much for making that code :)

Edit2: I use newgen WE if you're wondering. Jasshelper 0.A.2.B
 

Attachments

  • asdasdasdasd.png
    asdasdasdasd.png
    1.9 MB · Views: 101
  • asdasdasd.w3x
    18.7 KB · Views: 64

Deleted member 219079

D

Deleted member 219079

That was rude man. Just say it straight, I'm dumb.

I mean it's normal if you forget things as you age up ^^ We've been taught so dozens of things, you can't remember all of them, can you not

Edit: You set the X two times I see?
 

Deleted member 219079

D

Deleted member 219079

I did so, it works fine by now, and is very efficient :)
Thank you, I'll be sure to add edo494 to credits :)

Edit:
edit: can you use structs? or you are bound to pure Jass?
The hell are structs lol. I'm pretty sure your code is efficient enough without "structs", I'm going to loop it 5 times at maximum, and the unit group won't be any bigger than what you can select.
 
Last edited by a moderator:

Deleted member 219079

D

Deleted member 219079

well I dont think one and a half function is worth mentioning in credits :D

Okay, I won't give you credit, is this enough for you?
It's pedobear :D just kidding
attachment.php
 

Attachments

  • edobear.png
    edobear.png
    1.4 MB · Views: 134
Status
Not open for further replies.
Top