Code covered by the BSD License  

Highlights from
throw body with mouse

image thumbnail
from throw body with mouse by Maxim Vedenyov
It throws body in gravitation. Initial velocity defined by pointer velocity.

buf
function buf
global fx hf xys vx vy xa1 ya1 t dt hfl00 g xyh th hsm hlg
global m k isk vw isvw

set(hf,'WindowButtonMotionFcn','');

if hlg==hsm
    vx=(xyh(1,end)-xyh(1,1))/(th(1,end)-th(1,1));
    vy=(xyh(2,end)-xyh(2,1))/(th(1,end)-th(1,1));
end

tic;
dt=0;
t=0;
fx=false;
while xys(2,:)>=0

        if ~isk
            vy=vy-g*dt;
        else
            if isvw
                vx=vx+(-(vx-vw)*k/m)*dt;
                vy=vy+(-(vy*k/m)-g)*dt;
            else
                vx=vx+(-(vx)*k/m)*dt;
                vy=vy+(-(vy*k/m)-g)*dt;
            end
        end
        xys(1,:)=xys(1,:)+vx*dt;
        xys(2,:)=xys(2,:)+vy*dt;
       

    dt=toc-t;
    t=t+dt;
    update_in_main;
    % update in small:
    xa=xys(1,:)+xa1;
    ya=xys(2,:)+ya1;
    set(hfl00,'Xdata',xa,'Ydata',ya);
    
     if xys(2,:)<=0
            vy=0;
            vx=0;
            xys(2,:)=0;
            break;
        end
    
    drawnow;
    end

% reset hystory:
xyh=[];
th=[];
hlg=0; % hystory length




Contact us at files@mathworks.com