필터 지우기
필터 지우기

issues with rotating and translating matrix for 2D simulation

조회 수: 2 (최근 30일)
kablai tokhi
kablai tokhi 2018년 5월 9일
편집: kablai tokhi 2018년 5월 10일
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
Jan 2018년 5월 9일
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 2018년 5월 9일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
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
kablai tokhi
kablai tokhi 2018년 5월 10일
It works! thank you.
kablai tokhi
kablai tokhi 2018년 5월 10일
편집: kablai tokhi 2018년 5월 10일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by