Filter löschen
Filter löschen

issues with rotating and translating matrix for 2D simulation

2 Ansichten (letzte 30 Tage)
Hi,
I'm working on a project at school and thought I'd test some of my code and came to the conclusion that a 2D simulation would be a great way to confirm its functionality. But I'm having some issues with the code for the simulation. The code for calculations are irrelevant here so I opted from posting them.
function testSim()
x = [-9 , 3, 1, 9, 1, 3, -9 ];
y = [-3,-3,-9/2, 0, 9/2, 3, 3];
g = hgtransform;
patch('XData',x,'YData',y,'FaceColor','yellow','Parent',g)
axis equal
xlim([0 400])
ylim([0 400])
pt1 = [124 80 0];
pt2 = [384 314 0];
yObject = 314;
xObject =384;
pos = [xObject yObject 2 2];
rectangle('Position',pos,'Curvature',[1 1])
r1 = 0;
r2 = 2*pi/6;
for t=linspace(0,1,100)
g.Matrix = makehgtform('zrotate',r1 + t*(r2-r1));
drawnow
pause(0.05)
end
for t2=linspace(0,1,100)
g.Matrix = makehgtform('translate',pt1 + t2*(pt2-pt1));
drawnow
pause(0.05)
end
end
This code basically creates and plots a 2D matrix that has its values in x and y. The problem I'm encountering is the one presented in the for-loops. In the code above, "g.Matrix = makehgtform(...)" is what is used for rotating and translating. By using it twice, it seems like Matlab acknowledges it as two different plots. As it first shows a plot where I can see the object rotating. After that, the plot is reset and the object is set into its default place and orientation, and the movement begins.
Any input is greatly appreciated.
  3 Kommentare
Jan
Jan am 9 Mai 2018
Of course drawnow is a function. See doc drawnow.
I do not understand "it seems like Matlab acknowledges it as two different plots".
kablai tokhi
kablai tokhi am 9 Mai 2018
So when i run the code, what I want it to do, is rotate the object, and once the rotation is done for it to move from point A to B, all in one plot. However, once the rotation is done, that plot is scrapped and a new one is triggered with the object in its original state/orientation and then it finishes the movement for loop.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 9 Mai 2018
The translation must be combined with the former rotation. Replace:
g.Matrix = makehgtform('translate',pt1 + t2*(pt2-pt1));
by
g.Matrix = makehgtform('translate', 0.01 * (pt2-pt1)) * g.Matrix;
  3 Kommentare
kablai tokhi
kablai tokhi am 10 Mai 2018
Bearbeitet: kablai tokhi am 10 Mai 2018
I actually have 1 more question I'm hoping you could answer. Using pt1 and pt2, when translating the object moves in a straight line with a set incline. Is there anyway to make it so that i could create 2 random numbers using rand, and use these to increment x and y so it might come off course again?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance 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