Filter löschen
Filter löschen

Rotational Movement of an object

1 Ansicht (letzte 30 Tage)
Taimur Saleem
Taimur Saleem am 18 Nov. 2013
Bearbeitet: Taimur Saleem am 18 Nov. 2013
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

Antworten (1)

Doug Hull
Doug Hull am 18 Nov. 2013
A video using HGTransform to move an airplane around the screen. Similar techniques would apply.
  1 Kommentar
Taimur Saleem
Taimur Saleem am 18 Nov. 2013
Bearbeitet: Taimur Saleem am 18 Nov. 2013
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by