• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Find center point between units in unit group

Status
Not open for further replies.
Level 4
Joined
Nov 13, 2019
Messages
47
I need to find the center point between units in a unit group if that's possible. There are 4 units in this group.


This is because I need the Large units (see vid: Wolf rider taking up grid space 2x2) in my map to walk there and be centered when player issues a move order within the active grid space (blue squares).

The grid is all units with Locust on them, the ones the mouse is over are all put in a Unit Group.

It's simple when centering a small unit in a 1x1 grid square because all i need is the position of that one Grid Unit.. but cant wrap my head around how I would do it with the point in between 4.

NOTE: The single unit centering was not fixed at the time of this recording
 
Last edited:
Level 9
Joined
Sep 20, 2015
Messages
385
From what i've seen there you can make a if then else where if there are 4 units in the group, the selected unit is ordered to go in a point (CP) and not in the unit position. As far as iknow it's not possible to get the center of a unit group, so you will have to convert the units position to coordinates and then calculate the center with a mathematical equation.

I don't know an equation, but i think you can just calculate the middle point as the middle point of the diagonal between 2 points.


upload_2019-11-25_21-33-1.png


In the picture,the black square is the space of the rider unit when it occupies 4 swuares, the red squares are the single units, that you use as grid. The circles are the X Y coordinates of the single grid units, the middle point of all four should be the middle point of the green diagonal.
To get that you can check for the distance. I belive you already set the distance between the units, the orizontal and vertical distance is shorter than the diagonal one, so to check on wich unit you should calculate the middle point you can just check the distance first.
 
Level 4
Joined
Nov 13, 2019
Messages
47
Holy crap, math is not my strongest subject, haha

Anyone mind showing me what it would look like in triggerspeak?

Appreciate the help so far tho!
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,535
Here's some code showing what Tasyen said.
You need to store the X values of your units and the Y values of your units. Then divide the sum of the X values by the # of units. Then divide the sum of the Y values by the # of units. The value you get after dividing the X values will be your center's X coordinate, and the value you get after dividing the Y values will be your center's Y coordinate.

Here it is in GUI:
  • Test
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • -------- Reset Center Variables --------
      • Set XSum = 0.00
      • Set YSum = 0.00
      • -------- - --------
      • -------- Get Center of Units --------
      • Set TotalUnits = (Number of units in UnitGroup)
      • Unit Group - Pick every unit in UnitGroup and do (Actions)
        • Loop - Actions
          • Set TempPoint = (Position of (Picked unit))
          • Set XAxis = (X of TempPoint)
          • Set YAxis = (Y of TempPoint)
          • Set XSum = (XSum + XAxis)
          • Set YSum = (YSum + YAxis)
          • Custom script: call RemoveLocation (udg_TempPoint)
      • Set XSum = (XSum / (Real(TotalUnits)))
      • Set YSum = (YSum / (Real(TotalUnits)))
      • -------- - --------
      • -------- TempPoint is now set to the Center of the Units --------
      • Set TempPoint = (Point(XSum, YSum))
      • -------- - --------
      • -------- I create a Special Effect at the Center to show that it works --------
      • Special Effect - Create a special effect at TempPoint using Abilities\Spells\Orc\Bloodlust\BloodlustTarget.mdl
      • Custom script: call RemoveLocation (udg_TempPoint)
And some Lua code because why not:
Lua:
function GetCenter()
    local totalunits = 4
    local u1 = udg_Unit1 --Our 1st unit
    local u2 = udg_Unit2 --Our 2nd unit
    local u3 = udg_Unit3 --Our 3rd unit
    local u4 = udg_Unit4 --Our 4th unit
    local x1 = GetUnitX(u1)
    local y1 = GetUnitY(u1)
    local x2 = GetUnitX(u2)
    local y2 = GetUnitY(u2)
    local x3 = GetUnitX(u3)
    local y3 = GetUnitY(u3)
    local x4 = GetUnitX(u4)
    local y4 = GetUnitY(u4)
    local xsum = (x1 + x2 + x3 + x4) / totalunits
    local ysum = (y1 + y2 + y3 + y4) / totalunits

    AddSpecialEffect("Abilities\\Spells\\Orc\\Bloodlust\\BloodlustTarget.mdl", xsum, ysum, 0)
    print("x", xsum, "y", ysum)
end
 

Attachments

  • Get Center.w3x
    17.8 KB · Views: 24
Last edited:
Level 4
Joined
Nov 13, 2019
Messages
47
Here's some code showing what Tasyen said.
You need to store the X values of your units and the Y values of your units. Then divide the sum of the X values by the # of units. Then divide the sum of the Y values by the # of units. The value you get after dividing the X values will be your center's X coordinate, and the value you get after dividing the Y values will be your center's Y coordinate.

Here it is in GUI:
  • Test
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • -------- Reset Center Variables --------
      • Set XSum = 0.00
      • Set YSum = 0.00
      • -------- - --------
      • -------- Get Center of Units --------
      • Set TotalUnits = (Number of units in UnitGroup)
      • Unit Group - Pick every unit in UnitGroup and do (Actions)
        • Loop - Actions
          • Set TempPoint = (Position of (Picked unit))
          • Set XAxis = (X of TempPoint)
          • Set YAxis = (Y of TempPoint)
          • Set XSum = (XSum + XAxis)
          • Set YSum = (YSum + YAxis)
          • Custom script: call RemoveLocation (udg_TempPoint)
      • Set XSum = (XSum / (Real(TotalUnits)))
      • Set YSum = (YSum / (Real(TotalUnits)))
      • -------- - --------
      • -------- TempPoint is now set to the Center of the Units --------
      • Set TempPoint = (Point(XSum, YSum))
      • -------- - --------
      • -------- I create a Special Effect at the Center to show that it works --------
      • Special Effect - Create a special effect at TempPoint using Abilities\Spells\Orc\Bloodlust\BloodlustTarget.mdl
      • Custom script: call RemoveLocation (udg_TempPoint)
And some Lua code because why not:
Lua:
function GetCenter()
    local totalunits = 4
    local u1 = udg_Unit1 --Our 1st unit
    local u2 = udg_Unit2 --Our 2nd unit
    local u3 = udg_Unit3 --Our 3rd unit
    local u4 = udg_Unit4 --Our 4th unit
    local x1 = GetUnitX(u1)
    local y1 = GetUnitY(u1)
    local x2 = GetUnitX(u2)
    local y2 = GetUnitY(u2)
    local x3 = GetUnitX(u3)
    local y3 = GetUnitY(u3)
    local x4 = GetUnitX(u4)
    local y4 = GetUnitY(u4)
    local xsum = (x1 + x2 + x3 + x4) / totalunits
    local ysum = (y1 + y2 + y3 + y4) / totalunits

    AddSpecialEffect("Abilities\\Spells\\Orc\\Bloodlust\\BloodlustTarget.mdl", xsum, ysum, 0)
    print("x", xsum, "y", ysum)
end

That's excellent! Thanks for helping :thumbs_up:
 
Status
Not open for further replies.
Top