Generate the motion (Trajectory) of a sphere (or point) with given velocity value
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Aquatris
on 21 Aug 2024
Edited: Aquatris
on 21 Aug 2024
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 Comments
Aquatris
on 21 Aug 2024
Edited: Aquatris
on 21 Aug 2024
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
on 23 Aug 2024
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!
0 Comments
See Also
Categories
Find more on Animation 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!