Best way to move multiple objects with a preallocated matrix of coordinates?
조회 수: 8 (최근 30일)
이전 댓글 표시
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.
채택된 답변
Matt J
2021년 2월 18일
편집: Matt J
2021년 2월 18일
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
Matt J
2021년 2월 18일
편집: Matt J
2021년 2월 18일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!