Generate the motion (Trajectory) of a sphere (or point) with given velocity value

10 次查看(过去 30 天)
Hi. I have this file 'DATA'. The first 3 columns are the nodes in space (X,Y,Z in [m]). The 4th column is not of interest. The fifth column is the velocity in [m/s].
Is it possible to visualize the motion (green direction) of a sphere (e.g., the red one in the figure) moving following the velocity values in the fifth column of DATA?
load("DATA.mat");
figure
plot3(DATA(:,1),DATA(:,2),DATA(:,3),'-k','LineWidth',0.1);
hold on
plot3(DATA(1,1),DATA(1,2),DATA(1,3),'r.','MarkerSize',30);
hold off
axis equal
xlabel('x')
ylabel('y')
zlabel('z')
grid off

回答(2 个)

Aquatris
Aquatris 2024-8-21
编辑:Aquatris 2024-8-21
If you dont care about the actual timing and things, here is one way where the colors of the path indicate the speed and a simple animation show the full motion:
load('data.mat')
x = [DATA(:,1);nan];
y = [DATA(:,2);nan];
z = [DATA(:,3);nan];
v = [DATA(:,5);nan];
l = length(x);
figure(1)
for i = 1:1 % change it to 1:l to animate
clf
colormap(winter)
patch(x,y,z,v,'Facecolor','none','EdgeColor','interp')
colorbar
view(150,15)
grid on
grid minor
hold on
plot3(x(i),y(i),z(i),'ro','MarkerSize',15)
hold off
pause(0.1)
end
  2 个评论
Alberto Acri
Alberto Acri 2024-8-21
Thank you. It might be helpful. If I would like to display multiple 'DATA' matrices present in a cell 'newData_SEL_cell' how can I display the trends simultaneously in the same figure?
I've tried a for loop but it doesn't seem to work the way it's supposed to. Suggestion?
load('newData_SEL_cell.mat')
figure
for S = 1:height(newData_SEL_cell)
DATA = newData_SEL_cell(S,1); % 1 e 50
DATA = cell2mat(DATA);
x = [DATA(:,1);nan];
y = [DATA(:,2);nan];
z = [DATA(:,3);nan];
v = [DATA(:,5);nan];
l = length(x);
%figure
for i = 1:l % change it to 1:l to animate
clf
colormap(winter)
patch(x,y,z,v,'Facecolor','none','EdgeColor','interp')
colorbar
view(150,15)
grid off
%grid minor
hold on
plot3(x(i),y(i),z(i),'ko','MarkerSize',2)
plot3(x(i),y(i),z(i),'ko','MarkerSize',1)
plot3(x(i),y(i),z(i),'ko','MarkerSize',3)
hold off
axis equal
pause(0.1)
end
end
Aquatris
Aquatris 2024-8-21
编辑:Aquatris 2024-8-21
Assuming each row happens at the same time:
load('newData_SEL_cell.mat')
% get x,y,z,v as cell arrays
x = cellfun(@(x) {[x(:,1);nan]},newData_SEL_cell);
y = cellfun(@(x) {[x(:,2);nan]},newData_SEL_cell);
z = cellfun(@(x) {[x(:,3);nan]},newData_SEL_cell);
v = cellfun(@(x) {[x(:,5);nan]},newData_SEL_cell);
% maximum number of data points in the whole data set
L = max(cellfun(@(A) length(A),x));
figure(1)
% loop through data points
for i = 1:1 % change it to 1:(L-1) to animate
clf
for j = 1:length(x) % loop through data sets
colormap(winter)
patch(x{j},y{j},z{j},v{j},'Facecolor','none','EdgeColor','interp')
colorbar
view(150,15)
grid on
grid minor
hold on
% because the 3 datasets are not the same length we need to check
if (length(x{j}) >= i) % check if we are not at the end point
plot3(x{j}(i),y{j}(i),z{j}(i),'ro','MarkerSize',15)
else
% (end-1) cause last element we put a 'nan' to prevent unnecessary
% connection between last point and first point during 'patch'
plot3(x{j}(end-1),y{j}(end-1),z{j}(end-1),'ro','MarkerSize',15)
end
end
hold off
pause(0.1)
end

请先登录,再进行评论。


Madheswaran
Madheswaran 2024-8-23
Hello,
The data appears somewhat unclear, as it specifies the object's velocity only at certain spatial positions, necessitating some assumptions. Here is the assumption made regarding the object's velocity in the intermediate position between the position mentioned in any two rows:
For the space between DATA(i, 1:3) and DATA(i+1, 1:3), the object is assumed to moving at a constant velocity as described in DATA(i, 5).
Below is a simple script to visualize the object's movement using a loop:
load DATA.mat
figure
plot3(DATA(:, 1), DATA(:, 2), DATA(:, 3));
% changing the aspect ratio and view to get the similar view as the posted image
pbaspect([1 1 4])
view([154.8 19.8])
ln = size(DATA, 1);
time_unit_divisor = 10;
hold on
pt = plot3(DATA(1, 1), DATA(1, 2), DATA(1, 3), 'r.', 'MarkerSize',30);
for i = 2:ln
distance = norm(DATA(i, 1:3) - DATA(i-1, 1:3));
velocity = DATA(i-1, 5);
time_taken = distance/velocity;
adjusted_time = time_taken/time_unit_divisor;
pause(adjusted_time);
%update the new position
pt.XData = DATA(i, 1);
pt.YData = DATA(i, 2);
pt.ZData = DATA(i, 3);
end
hold off
The above code would produce the plot like this:
You can adjust the speed of the animation by modifying the ‘time_unit_divisor’ variable. To enhance the smoothness of the animation, consider plotting additional points within each iteration by interpolating the intermediate positions of the object.
Hope this answers your question!

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by