Ball trapped inside a car
1 回表示 (過去 30 日間)
古いコメントを表示
I have a car moving forward and i ned to trap a ball inside it.
x_casa = [2,12,12,7,2];
y_casa = [1,1,10,14,10]; %obiectul casa
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]; %obiectul drum
drum = hgtransform;
patch('XData',x_drum,'YData',y_drum,'FaceColor','black','Parent',drum)
roata_spate = hgtransform; %back wheel
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; %front wheel
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]; % car chassis
y_sasiu = [1.6, 1.6, 3, 4 , 4 , 1.6];
sasiu = hgtransform;
patch('XData',x_sasiu,'YData',y_sasiu,'FaceColor','white','Parent',sasiu)
ball = hgtransform; %ball
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',ball)
axis equal
xlim([0 100])
ylim([0 30])
pt1 = [0 0 0];
pt2 = [80 0 0];
for t=linspace(0,1,2500) %making the car moving forward
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
3 件のコメント
Walter Roberson
2022 年 5 月 29 日
Instead of having the ball have independent coordinates, parent it to the car, so that when the car moves it automatically moves as well. The coordinates of the ball at any time should be the position of the ball relative to some reference point inside the car.
It is legal to have one hgtransform be parented to another.
回答 (1 件)
VINAYAK LUHA
2023 年 9 月 21 日
Hi Robert Mirea,
It is my understanding that you would like to have the ball trapped inside the car such that it moves along with the latter.
Here’s a workaround in which the ball.matrix transformation is updated using the same translation transformation applied to the car chassis. This ensures that the ball remains trapped inside the car as it moves forward. I’ve also updated the “yCoord3” variable such that the ball fits inside the car.
Here’s the modified code for your reference-
x_casa = [2,12,12,7,2];
y_casa = [1,1,10,14,10]; %obiectul casa
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]; %obiectul drum
drum = hgtransform;
patch('XData',x_drum,'YData',y_drum,'FaceColor','black','Parent',drum)
roata_spate = hgtransform; %back wheel
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; %front wheel
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]; % car chassis
y_sasiu = [1.6, 1.6, 3, 4 , 4 , 1.6];
sasiu = hgtransform;
patch('XData',x_sasiu,'YData',y_sasiu,'FaceColor','white','Parent',sasiu)
ball = hgtransform; %ball
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-2.5,'FaceColor','red','Parent',ball)
axis equal
xlim([0 100])
ylim([0 30])
pt1 = [0 0 0];
pt2 = [80 0 0];
for t=linspace(0,1,2500) %making the car moving forward
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));
%%Update the position of the ball relative to the car
ball.Matrix = makehgtform('translate',[pt1(1) + t*(pt2(1)-pt1(1)), yCent3, 0]);
drawnow
end
Regards
Vinayak Luha
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Object Containers についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!