Rotational Movement of an object

조회 수: 5 (최근 30일)
Taimur Saleem
Taimur Saleem 2013년 11월 18일
편집: 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에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by