Rotational Movement of an object

2 次查看(过去 30 天)
Taimur Saleem
Taimur Saleem 2013-11-18
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

回答(1 个)

Doug Hull
Doug Hull 2013-11-18
A video using HGTransform to move an airplane around the screen. Similar techniques would apply.
  1 个评论
Taimur Saleem
Taimur Saleem 2013-11-18
编辑:Taimur Saleem 2013-11-18
The principles might be the same but I am having trouble in the actual movement of the rectangle in the set path. The path I defined is the same at all angles which is a straight line but moving it in a straight line results in the object staying in the orientation. Also I want the angle to be an increment so that moving the arrow keys twice may increment the angle twice and then the forward arrow key moves it at that particular angle in a straight line. -->this part in particular is what I cant do.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by