issues with rotating and translating matrix for 2D simulation
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Jan
2018년 5월 9일
I do not understand "it seems like Matlab acknowledges it as two different plots".
채택된 답변
Jan
2018년 5월 9일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!