• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Random Location - Offsets

Status
Not open for further replies.
Level 10
Joined
Apr 3, 2006
Messages
535
I am making an item spawn system from a dead unit, the problem is when i spawn multiple units based off the location of the dead unit they all appear on top of each other and its difficult to see each individual item. How would i make a random location based off a dying unit or offset to make each item appear slightly apart?

Thanks,
Corley
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
If you use vJASS, something like this:

JASS:
scope Test initializer onInit

    globals
    
        private constant real MIN_OFFSET    = 100.0
        private constant real MAX_OFFSET    = 250.0
        
    endglobals
    
    private function offsetTest takes nothing returns boolean
        local real dist = GetRandomReal(MIN_OFFSET, MAX_OFFSET)
        local real ang = GetRandomReal(1.0, 360.0)
        local real x = GetUnitX(GetTriggerUnit())
        local real y = GetUnitY(GetTriggerUnit())
        local real x2 = x + dist * Cos(Deg2Rad(ang))
        local real y2 = y + dist * Sin(Deg2Rad(ang))
        
        // More actions...such as:
        call CreateItem('bspd', x2, y2)
        
        return false
    endfunction

    private function onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerAddCondition(t, Condition(function offsetTest))
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
        set t = null
    endfunction
    
endscope

This simply creates Boots of Speed at a random point within 250 distance from a dying unit.
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
Of course, if you want a uniform distribution of items, you might want to take a random X and a random Y coordinate and check for it's distance to the unit.

I say this because, out of experience, I know that taking random numbers and using them as polar coordinates tends to give positions closer to the center more often than it does further outside.
 
Level 10
Joined
Apr 3, 2006
Messages
535
would this work:

  • Set BossDrop_Point = ((Position of (Dying unit)) offset by ((Random real number between -50.00 and 50.00), (Random real number between -50.00 and 50.00)))
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
corley, your trigger will work but it leaks, plus don't use negative as it would turn out the same result as positive, only random values.

Maker has showed you the way and it won't leak in any way, also a range from 0 to 64 unit range from the dying unit that the item will spawn.
 
Level 10
Joined
Apr 3, 2006
Messages
535
Thanks, its working now i was just trying to be clever/lazy and only use on variable but Makers method works perfectly - thanks all for help +rep all round
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
Thanks, its working now i was just trying to be clever/lazy and only use on variable but Makers method works perfectly - thanks all for help +rep all round
Your action was indeed a clever action, but not clever enough to tackle the "leaks" problem :)

Your trigger basically shortens the function (by not setting location into variable) but you will be having problems with leaks afterwards.

You can even set the random location without the use of points at all, just coordinates (Real).
 
Status
Not open for further replies.
Top