HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

GetUnitCollisionSize

07-06-2008, 04:29 AM#1
Vexorian
Gets a unit's collision size maybe?

Information:
  • ITERATIONS : This determines how many iterations it should use (it uses a binary search, 10 is fine, actually, 5 is fine as well, I don't think you need this to be ultra precise, but if you want, go ahead and have 30 iterations.
  • Flavors: It comes in three flavors, I figured some people may not like the idea of doing the iterations so much freaking times when collision size is constant per unit type, so using gamecache to memorize the values is possible, so one of the alternative versions uses CSSafeCache, while the other one uses Table. Please take note that I am not that sure gamecache is faster than the little 10 iterations this function does.


Expand Standalone:

Expand CSSafeCache version:



Expand Table version:
07-06-2008, 06:06 AM#2
grim001
I once extended this method to create a library which pre-caches the collision size of each unittype as it enters the map, then attaches it to each individual unit using the 0x100000 method, allowing you to set/retrieve individual unit collision sizes (i.e. if you make a unit grow or shrink you can update it) with simple inlined function calls. This means it is extremely speedy, and suitable for collision detection operations.

I will post it if anyone actually wants it, but it sits gathering dust because I figured no one will ever use such a thing.
01-02-2009, 10:21 PM#3
Themerion
Typo in Table-version:

Collapse JASS:
function GetUnitCollisionSize takes unit u returns real
 local integer i=0
 local real x=GetUnitX(u)
 local real y=GetUnitY(u)
 local integer typ=GetUnitTypeId(u)
 local real hi
 local real lo
 local real mid

    if (memo.exists(ktyp) ) then
        return I2R(memo[ktyp])
    endif

    set hi=MAX_COLLISION_SIZE
    set lo=0.0
    loop
        set mid=(lo+hi)/2.0
        exitwhen (i==ITERATIONS)
        if (IsUnitInRangeXY(u,x+mid,y,0)) then
            set lo=mid
        else
            set hi=mid
        endif
        set i=i+1
    endloop
    set memo[ktyp]=R2I(mid+0.500000001)
 return mid
endfunction
01-02-2009, 11:10 PM#4
Vexorian
all right.
07-24-2009, 03:06 AM#5
Anitarf
Wouldn't moving the GetUnitX/Y calls below the memo.exists check optimize the code somewhat? Not sure how significant the two calls are relative to the GC (soon to be handletable) call plus the cost of calling the function in the first place, but even if not that significant, not initializing unneeded values is the right thing to do.