Clear Filters
Clear Filters

issues with rotating and translating matrix for 2D simulation

2 views (last 30 days)
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 Comments
Jan
Jan on 9 May 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 on 9 May 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.

Sign in to comment.

Accepted Answer

Jan
Jan on 9 May 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 Comments
kablai tokhi
kablai tokhi on 10 May 2018
Edited: kablai tokhi on 10 May 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?

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!