Vectors (Spatial) - Need a little help

Fulla

Evil Overlord
Reaction score
31
Basically I'm trying to learn vectors. I thought I'd start out simple, by converting a spell I've already got completed.
I've managed to convert the X/Y calculation & now onto the Z part, but seeking help.

The projectile is shot in arc upwards, which slowly descends back downwards.
The plan is to convert the periodic calculations to just this:
JASS:

set dat.vz = ?? // Scale it down with gravity
call SetUnitFlyHeight(dat.missile, GetUnitZ(dat.missile) + dat.vz, 0)


So basically I'm looking for these calculations:
- Setting up the vz
- Scaling down the vz periodically with gravity
(vz = the Z Velocity Vector)

Here is the code stripped down to just the maths/calculations:
JASS:

    public constant real PERIOD                     = 0.034
    public constant real GRAVITY                    = 2500
    private constant real SPEED                     = 900
    private constant real MINIMUM_DISTANCE          = 400
    private constant real LAUNCH_Z                  = 100

//TIMER - Moving the projectile about:
    set dat.time = dat.time + PERIOD
    set x = GetUnitX(dat.missile)
    set y = GetUnitY(dat.missile)
    set z = GetZ(x, y)
  //Move missile  
    call SetUnitXY(dat.missile, x + dat.vx, y + dat.vy)
  //Adjust height 
    call SetUnitFlyHeight(dat.missile, dat.sz + dat.vz * dat.time - GRAVITY * dat.time * dat.time / 2 - z, 0)
  //Adjust angle/facing
    set aoa = R2I(Atan2BJ(dat.vz - GRAVITY * dat.time, SquareRoot(dat.vx * dat.vx + dat.vy * dat.vy))) + 90 
    call SetUnitAnimationByIndex(dat.missile, aoa)

//SETUP - Setting the structs & figures up
    local unit u = GetTriggerUnit()
    local location l = GetSpellTargetLoc()
    local real dx = GetLocationX(l) - GetUnitX(u)
    local real dy = GetLocationY(l) - GetUnitY(u)
    local real dz = GetLocationZ(l) - (GetUnitZ(u) + LAUNCH_Z)
    local real dist = SquareRoot(dx * dx + dy * dy)

    set dat.duration = dist / SPEED
    set dat.sx = GetUnitX(u)
    set dat.sy = GetUnitY(u)
    set dat.sz = GetUnitZ(u) + LAUNCH_Z
    set dat.vx = dx / dat.duration * PERIOD
    set dat.vy = dy / dat.duration * PERIOD
    set dat.vz = dz / dat.duration + GRAVITY * dat.duration / 2

Full Code:
JASS:
scope FreezingArrow

globals
    flamingarrowdata array FlamingArrowArray
    integer FlamingArrowTotal                       = 0
    
    public constant real PERIOD                     = 0.034
    public constant real GRAVITY                    = 2500
    
    private constant integer ABILITY_ID             = 'A002'
    private constant real IMPACT_DAMAGE             = 100
    private constant real SPEED                     = 900
    private constant real MINIMUM_DISTANCE          = 400
    private constant real LAUNCH_Z                  = 100
endglobals

struct flamingarrowdata
    unit missile = null
    effect sfx = null
    integer level = 0
    real sx = 0
    real sy = 0
    real sz = 0
    real vx = 0
    real vy = 0
    real vz = 0
    real time = 0
    real duration = 0
    real acceleration = 0
endstruct

private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == ABILITY_ID
endfunction

private function End takes unit u, integer level returns nothing
    local real x = GetUnitX(u)
    local real y = GetUnitY(u)
    call DestroyEffect(AddSpecialEffect("Abilities\\Weapons\\FrostWyrmMissile\\FrostWyrmMissile.mdl", x, y))
endfunction

private function Timer takes nothing returns nothing
    local flamingarrowdata dat
    local integer i = 0
    local integer aoa = 0
    local real x = 0
    local real y = 0
    local real z = 0
    loop
        exitwhen i >= FlamingArrowTotal
        set dat = FlamingArrowArray<i>
        set dat.time = dat.time + PERIOD
        set x = GetUnitX(dat.missile)
        set y = GetUnitY(dat.missile)
        set z = GetZ(x, y)
      //Move missile  
        call SetUnitXY(dat.missile, x + dat.vx, y + dat.vy)
      //Adjust height 
        call SetUnitFlyHeight(dat.missile, dat.sz + dat.vz * dat.time - GRAVITY * dat.time * dat.time / 2 - z, 0)
      //Adjust angle/facing
        set aoa = R2I(Atan2BJ(dat.vz - GRAVITY * dat.time, SquareRoot(dat.vx * dat.vx + dat.vy * dat.vy))) + 90 
        call SetUnitAnimationByIndex(dat.missile, aoa)
        
      //Has missile has reached its destination?  
        if GetUnitZ(dat.missile) &lt;= GetZ(x, y) + 1 then
          //Add an explosion
            call End(dat.missile, dat.level)
          //Destroy everything  
            call DestroyEffect(dat.sfx)
            call KillUnit(dat.missile)
            set FlamingArrowTotal = FlamingArrowTotal - 1
            set FlamingArrowArray<i> = FlamingArrowArray[FlamingArrowTotal]
            call dat.destroy()
            set i = i - 1
        endif
        set i = i + 1
    endloop
    if FlamingArrowTotal==0 then
        call ReleaseTimer(GetExpiredTimer())
    endif  
endfunction

private function Actions takes nothing returns nothing
    local flamingarrowdata dat = flamingarrowdata.create()
    local unit u = GetTriggerUnit()
    local location l = GetSpellTargetLoc()
    local real dx = GetLocationX(l) - GetUnitX(u)
    local real dy = GetLocationY(l) - GetUnitY(u)
    local real dz = GetLocationZ(l) - (GetUnitZ(u) + LAUNCH_Z)
    local real dist = SquareRoot(dx * dx + dy * dy)
    local real velocity = dist / SPEED
  //Minimum distance  
    if dist &lt; MINIMUM_DISTANCE then
        set dx = dx * MINIMUM_DISTANCE / dist
        set dy = dy * MINIMUM_DISTANCE / dist
        set dist = MINIMUM_DISTANCE
        set velocity = dist / SPEED
    endif
  //Setup structs  
    set dat.missile = CreateUnit(GetOwningPlayer(u), &#039;hmil&#039;, GetUnitX(u), GetUnitY(u), Atan2BJ(dy, dx))
    set dat.sfx = AddSpecialEffectTarget(GetAbilityEffectById(ABILITY_ID, EFFECT_TYPE_MISSILE, 1), dat.missile, &quot;origin&quot;)
    set dat.level = GetUnitAbilityLevel(u, ABILITY_ID)
    set dat.duration = dist / SPEED
    set dat.sx = GetUnitX(u)
    set dat.sy = GetUnitY(u)
    set dat.sz = GetUnitZ(u) + LAUNCH_Z
    set dat.vx = dx / velocity * PERIOD
    set dat.vy = dy / velocity * PERIOD
    set dat.vz = dz / dat.duration + GRAVITY * dat.duration / 2 
    //set acceleration = 
  //The Missile is shot/launched from 100 above the caster  
    call SetUnitFlyHeight(dat.missile, LAUNCH_Z, 0)
    if FlamingArrowTotal == 0 then
        call TimerStart(NewTimer(), PERIOD, true, function Timer)
    endif
    set FlamingArrowArray[FlamingArrowTotal] = dat
    set FlamingArrowTotal = FlamingArrowTotal + 1
    call RemoveLocation(l)
    set u = null
endfunction

//===========================================================================
public function InitTrig takes nothing returns nothing
    set Trig = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(Trig, EVENT_PLAYER_UNIT_SPELL_CAST)
    call TriggerAddCondition(Trig, Condition(function Conditions))
    call TriggerAddAction(Trig, function Actions)
endfunction
endscope</i></i>


Feel free to test out map
Thx for any help
 

Attachments

  • Spell 2.w3x
    54.3 KB · Views: 155

Strilanc

Veteran Scripter
Reaction score
42
I'm not exactly sure what you think vectors are, but you're essentially doing the equivalent already.
*edit* I misread the code, you're not doing it with store velocity like I thought.

I have no idea what you mean by "scaling down" vz. You shouldn't be multiplying vz.

But if you really want to get into it, you should write a vector struct.
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
JASS:
set dat.vz=dat.vz+GravityPerSecond*Period


Gravity per second would be negative then, and Period would be like .02 or something (but you know that one).

Gravity is acceleration, not velocity, so it's a constant addition to velocity, not something that changes. I believe on earth it's like 9.2f/s^2

In other words, add your previous gravities as well:
Code:
1: 1
2: 1+1
3: (1)+(2)+1
4: (1)+(2)+(4)+1
5: (1)+(2)+(4)+(8)+1
etc.

x^2 is the same as constant addition, which I think is what was throwing you off.
 

saw792

Is known to say things. That is all.
Reaction score
280
Just for interest's sake, gravity on earth = 9.8 m s^-2 (that is, 9.8 m/s/s).
 

Fulla

Evil Overlord
Reaction score
31
Sorry, I Just started getting interested in this so still very new to it all.

I'm not exactly sure what you think vectors are, but you're essentially doing the equivalent already.
*edit* I misread the code, you're not doing it with store velocity like I thought.

I have no idea what you mean by "scaling down" vz. You shouldn't be multiplying vz.

But if you really want to get into it, you should write a vector struct.

Sure I'd love to write a vector struct, but would need a hand.
The plan is to reduce the movement script each period/timer to just a simple:
JASS:
call SetUnitX(dat.missile, GetUnitX(dat.missile) + dat.vx)
call SetUnitY(dat.missile, GetUnitY(dat.missile) + dat.vy)
call SetUnitFlyHeight(dat.missile, GetUnitZ(dat.missile) + dat.vz, 0)


Then the only calculation I should need to do is alter the dat.vz each period as the projectile is 'dragged down' by gravity. Right?
The vx/vy will always just remain constant once the've been calculated?

Scaling down, sorry probably incorrect term, I just mean keep altering Z to achieve something like this:
Arc.jpg


So yea basically:
- How to I calculate dat.vz initially?
- How do I recalculate/adjust it each period compensating for gravity?

again thx for any help, much appreciated.
 

Viikuna

No Marlo no game.
Reaction score
265
I usually just use Anitarfs vectors, they just make everything fast and easy to code.

Basicly, you need position vector and velocity vector. At every timestep you just add velocity to position, and there you have new position of unit. For jumps and other 3d stuff, you can have one Gravity vector, then you just add Gravity to velocity. ( You could add half of that gravity before, and other half after, you add velocity to position. )


Anyways, here is Anitarfs vector system. link
Download that test map and see how he does it.

I believe that it is like this:
JASS:
set v.z = v.z + (Gravity/2)
set position.z = position.z + v.z
set v.z = v.z + (Gravity/2)

This works for me.
 

emjlr3

Change can be a good thing
Reaction score
395
u lost me at : constant real gravity = 2500 .... ???
 

Strilanc

Veteran Scripter
Reaction score
42
It's just a constant that determines the strength of gravity. It's set based on what looks best.
 

emjlr3

Change can be a good thing
Reaction score
395
obviously - but using an arbitrary value such as 2500 does nothing but confuse readers
 

Strilanc

Veteran Scripter
Reaction score
42
obviously - but using an arbitrary value such as 2500 does nothing but confuse readers

:nuts: You use the values that work.

I can't set it to 9.81 because that value doesn't work, at all. Game distance and game time aren't meters and seconds, so your constants are going to change.
 

emjlr3

Change can be a good thing
Reaction score
395
game time sure is seconds, and you can pretend 1 or 10 or 100 distance in game is a meter - check my KS system, which uses a value of 9.8 for gravity, makes things clearer IMO
 

Strilanc

Veteran Scripter
Reaction score
42
Game time is not in real seconds, it is in game seconds. I'm not sure what game speed is 1:1, but if that's the default then it doesn't really matter. It would be pretty easy to just have a 30 second timer run down, and time it vs a wall clock to get the ratio. Defining the game meter is a bit harder, because things in the game are not to scale, but you could probably get close enough.

If you had constants for both those things, you could define gravity to be 9.8*game_meter/game_second^2 and the right result should come out. But then you'll find those constants being required everywhere; when getting unit speed, etc.

I think it's a bit silly to impose the real world measurements because wc3 already has natural units of distance and time defined for you. Redefining all that just so you can change one constant is silly.
 

Fulla

Evil Overlord
Reaction score
31
Been working/away for 4 days, but now have the weekend of free time to have another crack at this.
I've checked over Anit's system a bit, as well as your spellStrilanc, thx for that.

It seems infact the method I was trying to accomplish was slightly different from the usual vectors used there. I never bothered storing the position vector etc. I was just simply aiming to calculate the x/y/z velocity (per period) & adding it onto the 'current position' on the projectile each period. I think this method is probably the easiest.

The spell I have now works perfectly but as I intend to be using this 'template' for alot of future spells I'd like to get down a favoured method first.
So hopefully now I can rephrase the questions I had for better understanding as I know what I'm talking about now :p

1) How do I calculate the Z velocity of the projectile it will move per period, i.e. every .03 seconds. Currently I do this, but the vz is a big figure e.g. (800):
JASS:
local real dz = GetLocationZ(l) - (GetUnitZ(u) + LAUNCH_Z)
set dat.duration = dist / SPEED
set dat.vz = dz / dat.duration + GRAVITY * dat.duration / 2

2) How do I re-calculate the Z velocity to incorporate gravity effecting it every period. Currently I just do this, I'd like to eliminate sz (starting sz) & time:
JASS:
call SetUnitFlyHeight(dat.missile, dat.sz + dat.vz * dat.time - GRAVITY * dat.time * dat.time / 2 - p, 0)

dat.time is just a real value that starts at 0 & each period is incremented, p is just the z position of projectile not includiny fly height.

===

For the X/Y movement I've already changed it to this method, & I think it works out alot easier.
Setting calculations up:
JASS:
set dat.vx = dx / dat.duration * PERIOD
set dat.vy = dy / dat.duration * PERIOD

Movement each period (x/y is just current position):
JASS:
call SetUnitXY(dat.missile, x + dat.vx, y + dat.vy)


So if your still reading, this is what I'd like to simply achieve per period:
JASS:
set dat.vz = dat.vz + //Recalculate vz with gravity
call SetUnitFlyHeight(dat.missile, GetUnitZ(dat/missile) + dat.vz, 0
 

Fulla

Evil Overlord
Reaction score
31
Ok I got it working, but with just trial & error.

Calulation the z velocity & converting it per period.
Gravity = 2500 & Period = .034
JASS:
local real dx = GetLocationX(l) - GetUnitX(u)
local real dy = GetLocationY(l) - GetUnitY(u)
local real dz = GetLocationZ(l) - (GetUnitZ(u) + LAUNCH_Z)
local real dist = SquareRoot(dx * dx + dy * dy)
local real time = dist / speed
set dat.vz = (dz / time + GRAVITY * time / 2) * PERIOD


Recalculating z velocity with a 'random' figure achieved through trial * error.
JASS:
set dat.vz = dat.vz - 2.8
call SetUnitFlyHeight(dat.missile, z + dat.vz, 0)


2.8 worked perfectly on all directions & distances I tried, i.e. missile always arc'd & hit ground where I cast.
I guess I'm happy with that, but just out of curiosity any idea how to get 2.8 from the gravity, period, position Z, flyheight figures?
It would be helpful as then it would auto adjust with configuration options, like gravity & period etc. (JESP spell afterall).

EDIT: Got angle/animation working.
 

emjlr3

Change can be a good thing
Reaction score
395
Redefining all that just so you can change one constant is silly.

what would need redifining - your timer runs in seconds - real life seconds(AFAIK), 1 distance in wc3 can be 1 meter or cm - it does not reall matter, it doesnt even need a unit, 1 works, since the WC3 wrodl size is arbitrary anyway, and then gravity can then again be 9.8/s, where you can treat the 9.8 as a meter, a cm, or nothing, juts a distance with no unit - hell I dont even know what the distance is in wc3 - pixel length? i dont know heh
 

Vexorian

Why no custom sig?
Reaction score
187
what would need redifining - your timer runs in seconds - real life seconds(AFAIK), 1 distance in wc3 can be 1 meter or cm - it does not reall matter, it doesnt even need a unit, 1 works, since the WC3 wrodl size is arbitrary anyway, and then gravity can then again be 9.8/s, where you can treat the 9.8 as a meter, a cm, or nothing, juts a distance with no unit - hell I dont even know what the distance is in wc3 - pixel length? i dont know heh
You are probably the only person so far using 9.8 in wc3 stuff, I've always seen values for gravity around the thousands, I think I am also using 2500 in my map, I don't remember well.

I don't think each tile is a meter, considering blizz is an american company and what not. If each tile. Though I think using 9.8 for gravity acceleration will probably make the animation look very slow unless you got some other factor in your code, mind showing that part of your code in which you accelerate particles?
 

Strilanc

Veteran Scripter
Reaction score
42
Aha, Mad helped me, 2.8 works out as:
JASS:
GRAVITY * PERIOD * PERIOD = 2.82
(2500   * 0.034  * 0.034)

That is definitely wrong, because the units don't match (g*t*t is in meters, but 2.82 is in meters per second). It should just be gravity * period.

You're getting a usable number out, but there's something wonky here.
 

emjlr3

Change can be a good thing
Reaction score
395
http://www.thehelper.net/forums/showthread.php?t=83498

its actually just an old, unfished 2d movement system, which allowed you to specify the mass of units, and apply an acceleration to them to knock them backwards - also includes an inellastic collision method, which takes into account units masses and movement velocity (along with their movement angle) to create a realistic, real-life like collision

the main purpose was to allowing larger/massier(tm) units to deal more substantial knockbacks, as well as to resist being knocked to a greater extent - an enable users to creat neat collisions between units/missiles/etc.

so basically you could consider mass being in grams, or kilos, and distances(velocities) being in m and m/s - which easily enables a realistic 9.8m/s gravitational acceleration - given WC3 distances are arbitrary, it allows you to give them some quantifiable value
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top