Best way to move multiple objects with a preallocated matrix of coordinates?

5 vues (au cours des 30 derniers jours)
Jack Smillie
Jack Smillie le 18 Fév 2021
Modifié(e) : Matt J le 18 Fév 2021
Hi,
I want to be able to move multiple objects along a plot of a road or someother way to create an image with previously created coordinate data and I'm not sure what's the best way. I've been trying to see if I can create a patch object and then transform that using hgtransform and makehgtform which is shown in the code below the %=/=/=% line ( the %----% lines are to help indicate sections of code which are functions) but it doesn't transform how I want it to so I'm not sure if it's down to my coding or if it's not suitable. It seems to only transform to the first coordinate and stays there rather than completeing the transition to the final set of coordinates. Advice on where to proceed from this point would be appreciated.
maxCarVelocity=5;
nStarts=2;
nCarsPerStart=100;
deltaT=1;
t=300;
carPosition=carCoords(nStarts,nCarsPerStart);
%--------------------------------------------------------%
function [carPosition] = carCoords(nStarts,nCarsPerStart)
% Creates a matrix of car positions based on their starting locations
% Creates a NaN matrix and each location( or a lane in the model) has a
% set number of vehicles generated backwards from the start coord of 0.
% This is a cumulative sum from a random integer between two values to replicate the
% semi-random disribution of distance between cars on a road.
carPosition=NaN(nStarts*nCarsPerStart,nStarts);
for i=1:nStarts
carPosition((1:nCarsPerStart)+(i-1)*nCarsPerStart,i)=cumsum([0; randi([-10,-2],nCarsPerStart-1 ,1)]);
end
end
%-------------------------------------------------------%
carProperties=intitalCarProperties(nStarts,nCarsPerStart,maxCarVelocity);
%-----------------------------------------------------%
function [carProperties] = intitalCarProperties(nStarts,nCarsPerStart,maxCarVelocity)
% Creates a matrix indicating each car's properties in the simulation.
% Detailed explanation goes here
% Max car velocity = 30 m/s ~ to 60mph in UK
driverBehaviour=1; % 1 = Normal behaviour
typeOfVehicle=1; %1 = Car (Average 2D dimensions = 4.7m x 1.9m)
carProperties=zeros(nStarts*nCarsPerStart,3);
for i=1:nStarts*nCarsPerStart
carProperties(:,1)=maxCarVelocity;
carProperties(:,2)=driverBehaviour;
carProperties(:,3)=typeOfVehicle;
end
end
%------------------------------------------------%
carPosWithTimeStep=repmat(carPosition,1,t+1);
timeSteps = repmat(0:deltaT:t,nStarts,1);
timeSteps = timeSteps(1:end);
for i=1:1:nStarts*nCarsPerStart
for k=nStarts+1:1:nStarts*(t+1)
carPosWithTimeStep(i,k)=carPosWithTimeStep(i,k)+carProperties(i,1)*timeSteps(1,k);
end
end
%=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=%
roadLength=1000;
figure('units','normalized','position',[0 0 1 1])
road=plot ([0,roadLength],[7.3,7.3],'k',[0,roadLength],[3.65,3.65],'k--',...
[0,roadLength],[0,0],'k','linewidth', 1.5);
axis equal
startingXPos1 = [0, 4.5, 4.5, 0];
startingYPos1 = [0.9, 0.9, 2.7, 2.7];
g = hgtransform;
patch('XData',startingXPos1,'YData',startingYPos1,'FaceColor','Red','Parent',g)
for b=1:1:nStarts*nCarsPerStart
for h=nStarts+1:nStarts:nStarts*(t+1)
g.Matrix = makehgtform('translate',carPosWithTimeStep(b,h)-carPosWithTimeStep(b,h-nStarts),0,0);
drawnow
end
end
Essentially the goal I'm trying to achieve is to create a matrix of coordinate data that I can then plot after to create a moving simulation. Hopefully then I'll be able to create a simulation of a number of cars driving down a road after all the calculations have been done.
  2 commentaires
Matt J
Matt J le 18 Fév 2021
And...what is the problem?
Jack Smillie
Jack Smillie le 18 Fév 2021
Sorry, I realised I didn't specify in my post. I'll edit it after this. The transformed patch seems transforms to one coordinate from my preallocated matrix of coordinate data when its plotted and stays there rather than moving across the graph.
The other question was as the title says, what matlab functions would be best to create an object that follows a preallocated matrix of coordinate data?

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 18 Fév 2021
Modifié(e) : Matt J le 18 Fév 2021
I see movement when I do this,
xl=xlim;
for b=1:1:nStarts*nCarsPerStart
for h=nStarts+1:nStarts:nStarts*(t+1)
pos = carPosWithTimeStep(b,h);
if xl(2)<=pos, break; end
g.Matrix = makehgtform('translate',pos,0,0);
drawnow
end
end
  2 commentaires
Jack Smillie
Jack Smillie le 18 Fév 2021
Thank you, that seems to work. So would proceeding with the hgtransform function be best to model several objects moving at the same time on the same plot?
Matt J
Matt J le 18 Fév 2021
Modifié(e) : Matt J le 18 Fév 2021
It's certainly a valid way, but here, it seems much simpler just to update the patch XData property:
for b=1:1:nStarts*nCarsPerStart
p=patch('XData',startingXPos1,'YData',startingYPos1,'FaceColor','Red');
for h=nStarts+1:nStarts:nStarts*(t+1)
pos = carPosWithTimeStep(b,h);
xdelta=pos-carPosWithTimeStep(b,h-nStarts);
if xl(2)<=pos, delete(p); break; end
p.XData=p.XData+xdelta;
drawnow
end
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by