Combined rotation and translation on a cube
Show older comments
Hi All
I am trying to draw a simple cube in matlab and then translate this to the origin, rotate and then move back again. I want to achieve this by combining my translation and rotation matricies and apply this to each of my verticies.
I have tried various matrix manipulations and i keep getting errors telling me that the matrix dimensions dont agree or that there is an error in the patch command.
Below is the code i have so far. Im sure im within striking distance but i just cant see what the issue is. Any help will be greatly appreciated. Many Thanks
Andrew
close all;
z=1;
rotation=[cos(z),-sin(z),0,0;sin(z),cos(z),0,0;0,0,1,0;0,0,0,1];
move2origin=[-150,0,0,0;0,-150,0,0;0,0,1,0;0,0,0,1];
moveback=[150,0,0,0;0,150,0,0;0,0,1,0;0,0,0,1];
combined=move2origin*rotation*moveback;
CV = zeros(8,4); % vertex matrix
CF = zeros(6,4); % face matrix
CV(1,:) = [-50,-50,0]*combined; %vertex 1
CV(2,:) = [50,-50,0]*combined; %vertex 2
CV(3,:) = [50,50,0]*combined; %vertex 3
CV(4,:) = [-50,50,0]*combined; %vertex 4
CV(5,:) = [-50,-50,100]*combined; %vertex 5
CV(6,:) = [50,-50,100]*combined; %vertex 6
CV(7,:) = [50,50,100]*combined; %vertex 7
CV(8,:) = [-50,50,100]*combined; %vertex 8
CF(1,:) = [1,2,6,5];
CF(2,:) = [2,3,7,6];
CF(3,:) = [3,4,8,7];
CF(4,:) = [4,1,5,8];
CF(5,:) = [5,6,7,8];
CF(6,:) = [1,2,3,4];
view(3); axis([-200 200 -200 200 -200 200]);
patch('Vertices', CV, 'Faces', CF,'FaceVertexCData', hsv(6), 'FaceColor', 'flat');
Accepted Answer
More Answers (2)
Arnaud Miege
on 7 Apr 2011
Here's why it doesn't work:
>> size(combined)
ans =
4 4
>> size([-50,-50,0])
ans =
1 3
You are trying to multiply a 1x3 vector by a 4x4 matrix, hence the error message about matrix dimensions. Either make your matrix 3x3 or your vector 1x4.
HTH,
Arnaud
2 Comments
Andrew
on 7 Apr 2011
Arnaud Miege
on 7 Apr 2011
I'm not very familiar with geometry, but shouldn't your rotation, etc... matrices be 3x3 instead of 4x4, which would mean that CV and CF should 8x3 and 6x3. Did you have a look at the documentation for the patch command:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/patch.html
alejandro perez
on 2 Feb 2020
0 votes
In the description of this video is the code of a cube which rotates, moves and grows up.
Categories
Find more on Surface and Mesh Plots 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!