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:
- 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.
- 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”.
- 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