function [zs,isterminal,direction] = dog2(t,z,flag);
%
global w% w = speed of the dog
X= jogger2(t);
h= X-z; 
nh= norm(h);
if nargin < 3 | isempty(flag) % normal output
   zs= (w/nh)*h;
else
   switch(flag)
   case 'events'  % at ||h||=0 there is a singularity
      zs= (nh-1e-3);   % zero crossing at pos(dog) ~ pos(jogger)
      isterminal= 1; % this is a stopping event 
      direction= 0;  % don't care if it decreases or increases
   otherwise
      error(['Unknown flag: ' flag]);
   end
end
