HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Detect when a unit is moving and what direction?

05-20-2008, 01:19 AM#1
Salbrismind
I want to make a spell where a unit latches a magical chain to a unit and because of it can move relative to that unit. Such that if the caster did not move itself, with the chain it would move exactly like the other unit. Therefore it could move about twice as fast as normal.

The problem I can't rap my head around is detecting when the unit is moving and in what direction as to make the caster go that way and move at that time.

The other part of the spell is that the effect is also transferred to other allied units nearby the caster, but it should be easy once this problem is cleared up.

How would I do this? Thanks.
05-20-2008, 01:35 AM#2
Vexorian
so, have a 0.25 timer that logs the position of a unit and compares it with the previous one, saving the data you need in a variable or something.

I must warn you that this chain stuff is not going to look good without some physics.
05-20-2008, 02:41 AM#3
Strilanc
You might also want to consider having a very small region and catching the 'exit' event, instead of polling the position.
05-20-2008, 02:48 AM#4
Vexorian
Quote:
Originally Posted by Vexorian
so, have a 0.25 timer that logs the position of a unit and compares it with the previous one, saving the data you need in a variable or something.

I must warn you that this chain stuff is not going to look good without some physics.
Actually, that's not true.

Collapse JASS:
//every 0.025 seconds?
function errloop takes nothing returns nothing
 local integer i=0
 local integer n=N
 local data d
 local real   dx
 local real   dy
   set N=0
   loop
          set d=V[i]
          set dx=GetUnitX(d.main)-d.lastx
          set dy=GetUnitY(d.main)-d.lasty
          call SetUnitX(d.slave, GetUnitX(d.slave) + dx)
          call SetUnitY(d.slave, GetUnitY(d.slave) + dy)
          set d.lastx=GetUnitX(d.main)
          set d.lasty=GetUnitY(d.main)

          

          if( d.timeToEnd) then
                 call d.destroy()
          else
                 set V[N]=d
                 set N=N+1
          endif

          set i=i+1
   endloop

endfunction

05-20-2008, 02:48 AM#5
grim001
Strilanc's method won't be very accurate. Region enter/exit events are noticably delayed.
05-20-2008, 03:20 AM#6
Strilanc
Quote:
Originally Posted by grim001
Strilanc's method won't be very accurate. Region enter/exit events are noticably delayed.

Ummm... what? The region enter event fires before the entering unit is even inside the rect (although it is inside the region). It might be that regions include the unit size or something.

If regions do include unit size then use polling, because the unit will need to move too far before firing the exit event.
05-20-2008, 07:07 PM#7
Salbrismind
Thanks Vex. Your methods seems the most solid. Except where is the exit statement for that loop?

Strilanc, sorry that seems very complicated. I'd much rather by accurate than anything. I can't see rects being this accurate without so much more work.