effectively emulating a laser pointer for my cat using Matlab
I'm trying to write a code using matlab that emulates a laser pointer in a way that my cat will enjoy chasing it on the screen. This is what I've done so far:
figure('menubar','none','color','k')
h = plot(0,'r.','MarkerSize',20);
xlim([-1 1]); ylim([-1 1])
axis off
phi1=(1+sqrt(5))/2;
phi2=sqrt(3);
step= 0.0001; % change according to machine speed
for t=0:step:100
set(h,'xdata',sin(t+phi1*t),'ydata',cos(phi2*t))
drawnow
end
The "issues" with this code are the following:
the pointer moves more or less at a constant speed and doesn't slow to a near stop and then unexpectedly proceed.
The trajectory is somewhat repeating itself, though I tried to make it using irrational numbers, the overall motions is continuous from right to left. I think a sharper trajectory change will help.
I know this is not a traditional programming question but still I want to solve a programming issue. I'd appreciate your help and of course open to new ways to answer my question that doesn't use the code I added.
Brilliant question, so good I thought I'd take 15 minutes of my life to have a go myself. After extensive YouTube research on laser technique i thought using the equations of motion to move between random points would work well:
n = 20; %number of steps
pos = [0,0]; % initial position
vel = 4; % laser velocity
acc = 400; % laser acelertation
dt = 0.01; % timestep interval
figure
set(gcf,'Position',get(0,'Screensize'));
for i=1:n
point = rand(1,2);
dist = 1;
while dist > 0.05 % loop until we reach the point
plot(pos(1),pos(2),'o','color','r','MarkerFaceColor','r')
axis equal
xlim([0,1])
ylim([0,1])
drawnow
% create random point to move towards
dist = pdist([point;pos],'euclidean');
% calculate the direction & mag vector to the point
dir = (point-pos)/norm((point-pos));
mag = norm(point-pos);
% update position
displ = vel*dt - 0.5*acc*mag*dt^2;
pos = pos + dir*displ;
end
end
Play around with the parameters till you find something your cat likes :0)
链接地址: http://www.djcxy.com/p/14748.html上一篇: Javascript命名空间约定