Rotational Movement of an object
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Doug Hull
2013-11-18
A video using HGTransform to move an airplane around the screen. Similar techniques would apply.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!