help moving an object

20 views (last 30 days)
Kate Stanjevich
Kate Stanjevich on 12 Dec 2020
Edited: Mario Malic on 12 Dec 2020
Hi,
I'm creating a street intersection that has changing lights and moving cars and pedestrians but I'm struggling to get the cars/pedestrians to move within my program without leaving its copies in its path. I've tried using a while loop but it isn't doing what I'm looking for. The picture is a bit of my code and what it looks like when I run it. My traffic lights are in a for loop and I've placed my while loop to move the cars within the for loop.
An example of one sequence of my traffic lights with my while loop for two of my cars.
%%-lower right corner light------------------------
rectangle('Position', [0+64 0+30 2 4],'Curvature', 0.2,'FaceColor', 'k')
axis equal
hold on;
format long g; format compact;
darkGray = [0.2, 0.2, 0.2];
green = [0, 132, 80] / 255;
yellow = [239, 183, 0] / 255;
red = [184, 29, 19] / 255;
pos = [0.5+64 2.75+30 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.5+64 1.5+30 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.5+64 0.25+30 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', red)
%%-upper left corner ---------------------------------
rectangle('Position', [0+35 0+64 2 4],'Curvature', 0.2,'FaceColor', 'k')
axis equal
hold on;
format long g; format compact;
darkGray = [0.2, 0.2, 0.2];
green = [0, 132, 80] / 255;
yellow = [239, 183, 0] / 255;
red = [184, 29, 19] / 255;
pos = [0.5+35 2.75+64 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', red)
pos = [0.5+35 1.5+64 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.5+35 0.25+64 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
%%-lower left corner light-------------------------------
rectangle('Position', [0+30 0+35 4 2],'Curvature', 0.2,'FaceColor', 'k')
axis equal
hold on;
format long g; format compact;
darkGray = [0.2, 0.2, 0.2];
green = [0, 132, 80] / 255;
yellow = [239, 183, 0] / 255;
red = [184, 29, 19] / 255;
pos = [2.75+30 0.5+35 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', green)
pos = [1.5+30 0.5+35 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.25+30 0.5+35 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
%%-upper right corner light--------------------------
rectangle('Position', [0+65 0+65 4 2],'Curvature', 0.2,'FaceColor', 'k')
axis equal
hold on;
format long g; format compact;
darkGray = [0.2, 0.2, 0.2];
green = [0, 132, 80] / 255;
yellow = [239, 183, 0] / 255;
red = [184, 29, 19] / 255;
pos = [2.75+65 0.5+65 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [1.5+65 0.5+65 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.25+65 0.5+65 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', green)
k=0;
while k<=90
k=k+1;
rectangle('FaceColor','red','Edgecolor','red','Position',[55 k 3 7])
pause(0.1)
end
k=65;
while k<=65 && k>=0
k=k-1;
rectangle('FaceColor','magenta','EdgeColor','magenta','Position',[43 k 4 8])
pause(0.1)
end
pause(5)

Accepted Answer

Mario Malic
Mario Malic on 12 Dec 2020
Edited: Mario Malic on 12 Dec 2020
Hello,
I have cleaned up a little bit of your code. You can create a function that will plot a cornerlight on the position to make it even more compact. The issue was with rectangle function and hold on, rectangle plots each time a new one when you call it. Getting a handle for rectangle and changing its Position property as shown below can solve your issue.
axis equal
hold on;
format long g; format compact;
%%-lower right corner light------------------------
LRCL = rectangle('Position', [0+64 0+30 2 4],'Curvature', 0.2,'FaceColor', 'k');
darkGray = [0.2, 0.2, 0.2];
green = [0, 132, 80] / 255;
yellow = [239, 183, 0] / 255;
red = [184, 29, 19] / 255;
pos = [0.5+64 2.75+30 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.5+64 1.5+30 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.5+64 0.25+30 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', red)
%%-upper left corner ---------------------------------
ULCL = rectangle('Position', [0+35 0+64 2 4],'Curvature', 0.2,'FaceColor', 'k')
darkGray = [0.2, 0.2, 0.2];
green = [0, 132, 80] / 255;
yellow = [239, 183, 0] / 255;
red = [184, 29, 19] / 255;
pos = [0.5+35 2.75+64 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', red)
pos = [0.5+35 1.5+64 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.5+35 0.25+64 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
%%-lower left corner light-------------------------------
rectangle('Position', [0+30 0+35 4 2],'Curvature', 0.2,'FaceColor', 'k')
darkGray = [0.2, 0.2, 0.2];
green = [0, 132, 80] / 255;
yellow = [239, 183, 0] / 255;
red = [184, 29, 19] / 255;
pos = [2.75+30 0.5+35 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', green)
pos = [1.5+30 0.5+35 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.25+30 0.5+35 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
%%-upper right corner light--------------------------
rectangle('Position', [0+65 0+65 4 2],'Curvature', 0.2,'FaceColor', 'k')
darkGray = [0.2, 0.2, 0.2];
green = [0, 132, 80] / 255;
yellow = [239, 183, 0] / 255;
red = [184, 29, 19] / 255;
pos = [2.75+65 0.5+65 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [1.5+65 0.5+65 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.25+65 0.5+65 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', green)
k=0;
% defining red car
redCar = rectangle('FaceColor','red','Edgecolor','red','Position',[55 k 3 7]);
while k<=90
redCar.Position = [55 k 3 7];
k=k+1;
pause(0.1)
end
% defining magenta car
k=65;
magentaCar = rectangle('FaceColor','magenta','EdgeColor','magenta','Position',[43 k 4 8])
while k<=65 && k>=0
k=k-1;
magentaCar.Position = [43 k 4 8];
pause(0.1)
end
pause(5)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!