- /
-
new dvd bounce animation with while loop (longer timeout 🕰️ limit yay)
on 11 Nov 2023
- 5
- 30
- 0
- 0
- 1286
Remix with other bouncing graphics :)
drawframe(5);
function drawframe(frame)
persistent canol theta cyflymder lliw
H = 400; % set length of field
LL = 300; % set height of field
h = 90; % set length of rectangle
ll = 60; % set height of rectangle
% Initialize position and direction
if isempty(canol) && isempty(cyflymder)
canol = [rand*(H-h)+h/2 , rand*(LL-ll)+ll/2]; % initialises a random central position within the field and not beyond walls (x and y co-ord)
theta = pi/4; % you could also use a random direction angle 2*pi*rand(1)
cyflymder = 20*[cos(theta) sin(theta)]; % the velocity vector is defined with a magnitude per frame
lliw = rand(1,3);
end
clf ; axis equal; axis off; axis equal; hold on;
plot(polyshape([0,H,H,0], [LL,LL,0,0]),'FaceColor', 'k', 'FaceAlpha', 1); % plots the 'screen'
canol = canol + cyflymder;
x = [canol(1)-h/2 , canol(1)+h/2, canol(1)+h/2, canol(1)-h/2]; % x coord of rectangle corners
y = [canol(2)+ll/2 , canol(2)+ll/2, canol(2)-ll/2, canol(2)-ll/2]; % y coord of rectangle corners
if any(x < 0) | any(x > H)
cyflymder(1) = -cyflymder(1);
lliw = rand(1,3);
end
if any(y < 0) | any(y > LL)
cyflymder(2) = -cyflymder(2);
lliw = rand(1,3);
end
while any(x < 0) | any(x > H) | any(y < 0) | any(y > LL)
canol = canol + 0.1*cyflymder;
x = [canol(1)-h/2 , canol(1)+h/2, canol(1)+h/2, canol(1)-h/2]; % x coord of rectangle corners
y = [canol(2)+ll/2 , canol(2)+ll/2, canol(2)-ll/2, canol(2)-ll/2]; % y coord of rectangle corners
end
images.roi.Ellipse(gca, 'Center', [canol(1),canol(2)-ll/4], 'Semiaxes', [h, ll/2]/2, 'InteractionsAllowed', 'none', 'Color', lliw, 'FaceAlpha',1)
images.roi.Ellipse(gca, 'Center', [canol(1),canol(2)-ll/4], 'Semiaxes', [h, ll/2]/10, 'InteractionsAllowed', 'none', 'Color', 'k', 'FaceAlpha',1)
text(canol(1),canol(2)+ll/4,'DVD', 'Color', lliw, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle','FontWeight', 'bold','FontSize',15)
end