How to stop falling cube?

2 Ansichten (letzte 30 Tage)
N/A
N/A am 29 Jun. 2023
Kommentiert: N/A am 29 Jun. 2023
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

Akzeptierte Antwort

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 29 Jun. 2023
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 Kommentare
Sulaymon Eshkabilov
Sulaymon Eshkabilov am 29 Jun. 2023
Most welcome! Glad to help.
N/A
N/A am 29 Jun. 2023
Just one thing remained... I entered the part you programmed inside the loop. I want to stop loop as it reaches to the ground. However, I programmed as you can see lines from 43 to 47. However, it is not the optimized way to save memory. Could you please suggest me a better way for stopping the loop after the ball reaches to the ground? I pasted the code again in following. Thanks,
% 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_step=75;
time = linspace(0, FinalTime, time_step);
z_Falling=-0.5*g*time.^2+v0*time+z0;
Angle_Rotation_X=-0.5;
Angle_Rotation_Y=-0.2;
Angle_Rotation_Z=0.0;
eul = [Angle_Rotation_X Angle_Rotation_Y Angle_Rotation_Z];
rotmXYZ = eul2rotm(eul,'XYZ');
%% Solving the equation of motion
for i=1:length(time)
% Create the vertices of the cube
clf;
%Rotation
% 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;
IDX = (min(vertices(:,3))+z_Falling-ball_radius>0); % Approximately when the cube edge hits the ground
time = time(IDX);
z_Falling = z_Falling(IDX);
z_Falling(end) % Can you please refine these followign 5 lines for the code here for saving memory?
if i>=length(IDX)
z_Falling(i)= z_Falling(end);
time(i)=0;
end
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by