Rotational Movement of an object
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I wanted to know how I could work up a code for matlab which would make a rectangle rotate by say 5 degrees and then also make that rectangle move in a straight line at that same angle so it looks as if it moves in a straight line always but any increment to the angle would make it rotate slightly and change its velocity. I have written a code for applying increments and used 35 as an example but I dont know how to make the rectangle move in a straight line made at the angle 35. The code isnt complete though, its missing some increments for the left and right arrow keys but they can be added as well.
fig = figure;
axis equal
axis([0 100 0 100]);
set(fig, 'KeyPressFcn', 'a = get(gcf, ''CurrentCharacter'')');
a = 1;
r = 5;
x = 10;
y = 10;
dy = 5;
ball = rectangle('Position', [x+r y+r r r], 'FaceColor','r');
for i=1:100
pause;
if double(a) == 28 %left arrow
dx1a = x*cosd(35)-y*sind(35)
dx1b = x*sind(35)+y*cosd(35)
end
if double(a) == 29 %right arrow
dx2a = x*cosd(35)-y*sind(35)
dx2b = x*sind(35)+y*cosd(35)
end
if double(a) == 30 %up arrow
y = y + dy;
end
if double(a) == 31 %down arrow
y = y - dy;
end
set(ball,'Position',[x+r y+r 5 5]);
end
0 comentarios
Respuestas (1)
Doug Hull
el 18 de Nov. de 2013
A video using HGTransform to move an airplane around the screen. Similar techniques would apply.
1 comentario
Ver también
Categorías
Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!