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.

throw_body
function throw_body

global xys ha hf c xa1 ya1 hfl0 vx vy t dt fx hfl00 g xyh th hsm hlg
global m k isk vw isvw
close('all');

g=9.8; % gravitation acceleration
m=1; % mass
k=2; % friction corefficient
isk=false; % if apply friction
vw=-5; % horisontal wind velocity
isvw=false; % if apply horisontal wind velocity

% velocity:
vx=0;
vy=0;

% hystory:
xyh=[];
th=[];
hlg=0; % hystory length
hsm=5; % maximal history lenght

fx=false; % not fixed

t=0; % time
dt=0.01; % initial dt

c=[0.1 0.9 0.1]; % body color

% main figure:
hfm=figure('MenuBar','none','units','normalized','position',[0.1 0.48 0.8 0.45]);
ham=axes;
set(ham,'DataAspectRatio',[1 1 1],'PlotBoxAspectRatio',[2.7 1 1],'NextPlot','add');
xl10=-6;
xl20=7;

plot([xl10 xl20],[0 0],'k-','parent',ham);


%set(ham,'PlotBoxAspectRatioMode','manual','DataAspectRatioMode','manual');

% control figure:
hf=figure('MenuBar','none','units','normalized','position',[0.2 0.07 0.6 0.38]);
ha=axes('units','normalized','position',[0.02 0.02 0.7 0.96]);
set(ha,'DataAspectRatio',[1 1 1],'NextPlot','add','XTick',[],'YTick',[]);
xl1=-0.5;
xl2=0.5;
set(ha,'XLim',[xl1 xl2],'XlimMode','manual');
set(ha,'YLim',[-0.2 0.8],'YlimMode','manual');

plot([xl1 xl2],[0 0],'k-','parent',ha);

uicontrol('parent',hf,'units','normalized','Style', 'pushbutton', 'String', 'Return body',...
    'Position', [0.7 0.8 0.2 0.1], 'Callback', 'ret_body');

uicontrol('parent',hf,'units','normalized','Style', 'pushbutton', 'String', 'Parameters...',...
    'Position', [0.7 0.6 0.2 0.1], 'Callback', 'params');

uicontrol('parent',hf,'units','normalized','Style', 'text', 'String', 'hint: throw green circle with mouse',...
    'Position', [0.7 0.3 0.2 0.1], 'Callback', 'params');

x=0;
y=0;
xys=[x;y];

r=0.03; % body radius

hfl=create_object(x,y,r,1);
hfl00=hfl;

% plot body in main window:
% draw marker as circle:
nr=12; % resolution
al=0:pi/nr:(2*pi-pi/nr);
xa1=r*cos(al);
ya1=r*sin(al);
xa=x+xa1;
ya=y+ya1;
hfl0=fill(xa,ya,c,'parent',ham);

set(ham,'YlimMode','manual');
set(ham,'XlimMode','manual');
set(ham,'XLim',[xl10 xl20]);
set(ham,'YLim',[-1 4]);





Contact us at files@mathworks.com