function Xpost = cg_impact(Xpre)

% At impact, 1) legs swap identity and 2) angular momentum
% conservation requires an instantaneous change in angular
% velocities.  

% Constants defining geometry and mass distribution:
cg_params

% 1) GEOMETRY: swap legs, by definition of states shown below:
%      X(1) = qns = nonstance leg angle, wrt y axis (CCW)
%      X(2) = qs = stance leg angle, wrt y axis (CCW)
%      X(3) = dqns/dt
%      X(4) = dqs/dt
Xpost = 0*Xpre; % Make same size, then fill in values, one at a time...
Xpre = Xpre(:); % ensure it is a column...

% stance and nonstance legs change identities:
Xpost(1) = Xpre(2); 
Xpost(2) = Xpre(1); 

% 2) ANGULAR MOMENTUM: balance about both collision point and hip:
%    Qm*q^- = Qp*q^+

two_alpha = Xpost(2)-Xpost(1); % interleg angle is "2*alpha"
cos_2alpha = cos(two_alpha);
Qminus=[-m*a*b        ,  -m*a*b+(mh*L^2 + 2*m*a*L)*cos_2alpha
    0                 ,  -m*a*b];
Qplus = [m*b*(b-L*cos_2alpha)  ,   m*L*(L-b*cos_2alpha) + m*a^2 + mh*L^2
    m*b^2                      ,   -m*b*L*cos_2alpha];

dq_minus = Xpre(3:4); % pre-collision velocities
dq_plus = (Qplus^-1)*Qminus*dq_minus;  % post-collision velocities
        
Xpost(3:4) = dq_plus;

