Ball trapped inside a box

조회 수: 2 (최근 30일)
Robert Mirea
Robert Mirea 2022년 5월 18일
답변: Ayush 2023년 11월 2일
I have a ball that needs to be trapped inside a moving car ,while the car moving forward ball should go to the back of the car, if the car is stopping tbe ball sould go forward, and i don't know how to do that
x_casa = [2,12,12,7,2];
y_casa = [1,1,10,14,10]; % house object
casa = hgtransform;
patch('XData',x_casa,'YData',y_casa,'FaceColor','white','Parent',casa)
x_drum=[2,2 , 100 , 100 ];
y_drum=[1, 1.2 , 1.2 ,1]; % road object
drum = hgtransform;
patch('XData',x_drum,'YData',y_drum,'FaceColor','black','Parent',drum)
roata_spate = hgtransform;
theta = 0:0.1:2*pi;
rad=0.3;
xCent = 13.5;
yCent = 1.3;
xCoord = xCent+rad*cos(theta);
yCoord = yCent+rad*sin(theta);
patch('XData',xCoord,'YData',yCoord,'FaceColor','blue','Parent',roata_spate)
roata_fata = hgtransform;
theta = 0:0.1:2*pi;
rad=0.3;
xCent2 = 16;
yCent2 = 1.3;
xCoord2 = xCent2+rad*cos(theta);
yCoord2 = yCent2+rad*sin(theta);
patch('XData',xCoord2,'YData',yCoord2,'FaceColor','blue','Parent',roata_fata)
x_sasiu = [12.5, 17.5, 17.5, 15.5, 12.5, 12.3];
y_sasiu = [1.6, 1.6, 3, 4 , 4 , 1.6];
sasiu = hgtransform;
patch('XData',x_sasiu,'YData',y_sasiu,'FaceColor','white','Parent',sasiu)
cerc = hgtransform;
theta = 0:0.1:2*pi;
rad=1;
xCent3 = 15;
yCent3 = 2.7;
xCoord3 = xCent3+rad*cos(theta);
yCoord3 = yCent3+rad*sin(theta);
patch('XData',xCoord3,'YData',yCoord3,'FaceColor','red','Parent',cerc)
axis equal
xlim([0 100])
ylim([0 30])
pt1 = [0 0 0];
pt2 = [80 0 0];
for t=linspace(0,1,2500)
sasiu.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
roata_fata.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
roata_spate.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
drawnow
end

답변 (1개)

Ayush
Ayush 2023년 11월 2일
Hi Robert,
I understand that you want to make a trapped ball in a car move back of the car when the car is moving forward, and if the car is stopping, the ball should go forward.
You can follow several steps:
  1. Make two loops: one will be for the duration when the car is moving forward, and another will be for the duration when the car is moving backwards.
  2. You can assign the coordinates to the ball as “chk2 = pt1 + t*(pt2-pt1);”. The coordinate of the ball will change as the car moves forward; this can be done by using “ up_pos = chk2(1)-t*10”.
  3. Similar logic will be used for the duration when the car is moving backwards. You would be required to change the range of the “t” variable, which will become “ t=linspace(1,0,2500)”. Use the similar command as given above for updating the coordinate of the ball, i.e. “chk3 = pt1 + t*(pt2-pt1);” and “dn_pos = chk3(1)-t*2;”
I have provided the final code with the changes; you can refer to it for a better understanding:
x_casa = [2,12,12,7,2];
y_casa = [1,1,10,14,10]; % house object
casa = hgtransform;
patch('XData',x_casa,'YData',y_casa,'FaceColor','white','Parent',casa)
x_drum=[2,2 , 100 , 100 ];
y_drum=[1, 1.2 , 1.2 ,1]; % road object
drum = hgtransform;
patch('XData',x_drum,'YData',y_drum,'FaceColor','black','Parent',drum)
roata_spate = hgtransform;
theta = 0:0.1:2*pi;
rad=0.3;
xCent = 13.5;
yCent = 1.3;
xCoord = xCent+rad*cos(theta);
yCoord = yCent+rad*sin(theta);
patch('XData',xCoord,'YData',yCoord,'FaceColor','blue','Parent',roata_spate)
roata_fata = hgtransform;
theta = 0:0.1:2*pi;
rad=0.3;
xCent2 = 16;
yCent2 = 1.3;
xCoord2 = xCent2+rad*cos(theta);
yCoord2 = yCent2+rad*sin(theta);
patch('XData',xCoord2,'YData',yCoord2,'FaceColor','blue','Parent',roata_fata)
x_sasiu = [12.5, 17.5, 17.5, 15.5, 12.5, 12.3];
y_sasiu = [1.6, 1.6, 3, 4 , 4 , 1.6];
sasiu = hgtransform;
patch('XData',x_sasiu,'YData',y_sasiu,'FaceColor','white','Parent',sasiu)
cerc = hgtransform;
theta = 0:0.1:2*pi;
rad=1;
xCent3 = 15;
yCent3 = 2.7;
xCoord3 = xCent3+rad*cos(theta);
yCoord3 = yCent3+rad*sin(theta);
patch('XData',xCoord3,'YData',yCoord3,'FaceColor','red','Parent',cerc)
axis equal
xlim([0 100])
ylim([0 30])
pt1 = [0 0 0];
pt2 = [80 0 0];
% Initialize ball position
ballX = 1;
ballY = 1;
ballZ = 0;
%For forward direction
for t=linspace(0,1,2500)
sasiu.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
roata_fata.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
roata_spate.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
chk2 = pt1 + t*(pt2-pt1);
up_pos = chk2(1)-t*10; % You can change the speed of the ball by changing constant 10
% Provide a condition such that the ball remain contraint in the car
if(up_pos>12)
up_pos = chk2(1)-2;
end
cerc.Matrix = makehgtform('translate',[up_pos, ballY, ballZ]);
drawnow
end
%For backward direction
for t=linspace(1,0,2500)
sasiu.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
roata_fata.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
roata_spate.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
chk3 = pt1 + t*(pt2-pt1);
dn_pos = chk3(1)-t*2;
cerc.Matrix = makehgtform('translate',[dn_pos, ballY, ballZ]);
drawnow
end
I hope this helps!
Regards,
Ayush

카테고리

Help CenterFile Exchange에서 Object Containers에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by