How to stop falling cube?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi everyone,
I simulated a free falling cube (the code is provided as below). I want to stop falling as the lowest vertex reaches to ground. Could you please help me to do that? thanks
--------------------------------------------------
clear all;clc;close all;
%% Geomtry
%%
% Define the size of the cube and the radius of the balls
cube_size = 1; % size of the cube
ball_radius = 0.15; % radius of the balls
R=cube_size/2;% distance of origin of cube from the verticies
Size_of_View=2; % view of the the results
%% Dynamic parameters
g=10; % gravity
v0=0;% initial velocity
z0=3;% initial position in space
FinalTime=1;% Time priod
time = linspace(0, FinalTime, 50);
z_Falling=-0.5*g*time.^2+v0*time+z0;
%% Solving the equation of motion
for i=1:length(time)
% Create the vertices of the cube
clf;
%% Rotation
Angle_Rotation_X=-0.2;
Angle_Rotation_Y=0.3;
Angle_Rotation_Z=0;
eul = [Angle_Rotation_X Angle_Rotation_Y Angle_Rotation_Z];
rotmXYZ = eul2rotm(eul,'XYZ');
%% Creating Dynamic Cube
vertices = [-R -R -R;
R -R -R;
R R -R;
-R R -R;
-R -R R;
R -R R;
R R R;
-R R R;]*rotmXYZ;
vertices(:,3)=vertices(:,3)+z_Falling(i);
%% Create the faces of the cube
faces = [1 2 3 4;
2 6 7 3;
6 5 8 7;
5 1 4 8;
1 2 6 5;
4 3 7 8];
%% Plot the cube
patch('Vertices', vertices, 'Faces', faces, 'FaceColor', 'b', 'EdgeColor', 'k');
view([30 35])
% Create the balls at each vertex
hold on;
for j = 1:size(vertices, 1)
[x, y, z] = sphere;
x = x * ball_radius + vertices(j, 1);
y = y * ball_radius + vertices(j, 2);
z = z * ball_radius + vertices(j, 3);
surf(x, y, z, 'FaceColor', 'r', 'EdgeColor', 'none');
view([30 35])
end
%% Refrence Plane
vertices_Ref_Plane=[-Size_of_View -Size_of_View 0;
cube_size + Size_of_View -Size_of_View 0;
cube_size + Size_of_View cube_size + Size_of_View 0;
-Size_of_View cube_size + Size_of_View 0;];
faces_Plane = [1 2 3 4];
patch('Vertices', vertices_Ref_Plane, 'Faces',faces_Plane, 'FaceColor', 'c', 'EdgeColor', 'k');
%% Set axis limits
SV=Size_of_View;
xlim([- Size_of_View, cube_size + Size_of_View]);
ylim([- Size_of_View, cube_size + Size_of_View]);
zlim([0, cube_size + Size_of_View]);
% Set axis labels
xlabel('X');
ylabel('Y');
zlabel('Z');
grid on
drawnow
view(3);
view([-70 30])
end
댓글 수: 0
채택된 답변
Sulaymon Eshkabilov
2023년 6월 29일
Here is the corrected code to halt the cube at the ground:
% Define the size of the cube and the radius of the balls
cube_size = 1; % size of the cube
ball_radius = 0.15; % radius of the balls
R=cube_size/2;% distance of origin of cube from the verticies
Size_of_View=2; % view of the the results
%% Dynamic parameters
g=10; % gravity
v0=0;% initial velocity
z0=3;% initial position in space
FinalTime=1;% Time priod
time = linspace(0, FinalTime, 75);
z_Falling=-0.5*g*time.^2+v0*time+z0;
IDX = (z_Falling>0.6); % Approximately when the cube edge hits the ground
time = time(IDX);
z_Falling = z_Falling(IDX);
%% Solving the equation of motion
for i=1:length(time)
% Create the vertices of the cube
clf;
%Rotation
Angle_Rotation_X=-0.2;
Angle_Rotation_Y=0.3;
Angle_Rotation_Z=0;
eul = [Angle_Rotation_X Angle_Rotation_Y Angle_Rotation_Z];
rotmXYZ = eul2rotm(eul,'XYZ');
% Creating Dynamic Cube
vertices = [-R -R -R;
R -R -R;
R R -R;
-R R -R;
-R -R R;
R -R R;
R R R;
-R R R;]*rotmXYZ;
vertices(:,3)=vertices(:,3)+z_Falling(i);
%Create the faces of the cube
faces = [1 2 3 4;
2 6 7 3;
6 5 8 7;
5 1 4 8;
1 2 6 5;
4 3 7 8];
% Plot the cube
patch('Vertices', vertices, 'Faces', faces, 'FaceColor', 'b', 'EdgeColor', 'k');
view([30 35])
% Create the balls at each vertex
hold on;
for j = 1:size(vertices, 1)
[x, y, z] = sphere;
x = x * ball_radius + vertices(j, 1);
y = y * ball_radius + vertices(j, 2);
z = z * ball_radius + vertices(j, 3);
surf(x, y, z, 'FaceColor', 'r', 'EdgeColor', 'none');
view([30 35])
end
% Refrence Plane
vertices_Ref_Plane=[-Size_of_View -Size_of_View 0;
cube_size + Size_of_View -Size_of_View 0;
cube_size + Size_of_View cube_size + Size_of_View 0;
-Size_of_View cube_size + Size_of_View 0;];
faces_Plane = [1 2 3 4];
patch('Vertices', vertices_Ref_Plane, 'Faces',faces_Plane, 'FaceColor', 'c', 'EdgeColor', 'k');
% Set axis limits
SV=Size_of_View;
xlim([- Size_of_View, cube_size + Size_of_View]);
ylim([- Size_of_View, cube_size + Size_of_View]);
zlim([0, cube_size + Size_of_View]);
% Set axis labels
xlabel('X');
ylabel('Y');
zlabel('Z');
grid on
drawnow
view(3);
view([-70 30])
end
댓글 수: 3
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!