- Add conditions to check if the balls collide with the restricted areas or the warp gates. If a ball collides with a restricted area, you can change its direction accordingly. If a ball collides with a warp gate, you can teleport it to the corresponding position on the other side of the window.
- Modify the code inside the main loop to include these conditions and update the ball positions accordingly.
Need help with restricted areas and warp gates
1 次查看(过去 30 天)
显示 更早的评论
clc
% Prompts user for the desired input method
Input = input('Type of input (User or File): ', 's');
if strcmp(Input, 'file')
% File input
filename = input('Filename ', 's');
fid = fopen(filename);
Width = fscanf(fid, '%f',1);
Radius = fscanf(fid, '%f',1);
Direction = fscanf(fid,'%f', 1);
Coords = fscanf(fid, '%f', [2,inf]);
fclose(fid);
Window = createWindowS24(Width);
Alpha = []; % List of ball handles
for i =1:size(Coords,2)
Alpha(i) = drawBall(Coords(1,i), Coords(2, i), Radius,'g');
end
else
% User input
Width = input('Please input desired width of the window: ');
Radius = input('Please input desired radius of the balls: ');
BallNumber = input('Please input the desired number of balls: ');
Direction = input('Please input the desired start position of the balls (deg): ');
Window = createWindowS24(Width);
Alpha = []; % List of ball handles
for i = 1:BallNumber
while true
Bravo= randi([Radius,Width - Radius]);
Charlie= randi([Radius, Width-Radius]);
% Check for overlap with existing balls
overlap = false;
for j = 1:length(Alpha)
[xc, yc] = getCenter(Alpha(j));
if norm([Bravo - xc, Charlie - yc]) < 2 * Radius
overlap = true;
break;
end
end
if ~overlap
break;
end
end
Alpha(i) = drawBall(Bravo, Charlie, Radius, 'b');
end
end
BallNumber = numel(Alpha);
Direction = Direction*ones(1, BallNumber);
while true
for i = 1:BallNumber
[Bravo, Charlie] = getCenter(Alpha(i));
Delta= Bravo + cosd(Direction(i));
Echo= Charlie + sind(Direction(i));
if Delta <Radius|| Delta>Width- Radius
Direction(i) = 180 - Direction(i);
end
if Echo < Radius || Echo > Width -Radius
Direction(i)=-Direction(i);
end
xMove(Alpha(i), cosd(Direction(i)));
yMove(Alpha(i), sind(Direction(i)));
end
for i = 1:BallNumber
for j = (i + 1):BallNumber
[x1, y1] = getCenter(Alpha(i));
[x2, y2] = getCenter(Alpha(j));
% Check if two balls collide
Collision = sqrt((x2-x1)^2 + (y2 - y1)^2);
if Collision <2*Radius
% Calculate the angle of collision
CAngle = atan2d(y2 - y1, x2 - x1);
% Update directions after collision (elastic collision)
Direction1=[cosd(Direction(i)), sind(Direction(i))];
Direction2 = [cosd(Direction(j)), sind(Direction(j))];
EC = [(x2-x1)/Collision,(y2-y1)/Collision];
Dir1F = Direction1 - 2*dot(Direction1, EC) * EC;
Dir2F = Direction2 - 2 * dot(Direction2, EC) * EC;
Direction(i) = atan2d(Dir1F(2),Dir1F(1));
Direction(j) = atan2d(Dir2F(2),Dir2F(1));
end
end
end
% Redraw all objects
redraw();
end
I have this quite long code for some balls that will collide with each other and the window bounds. However, I cannot seem to figure out how to make it collide with the restricted area and go through the warp gates. Any assistance would be appreciated. Below this is the function for the window, if that helps.
function f = createWindowS24(w)
close all
% createWindowall(w)
% function to create a square window whose width is w pixels and
% whose height is w pixels; its bottom left corner is placed
% 50 pixels up and 50 pixels to the right of the bottom left
% corner of the monitor
% This version of the function also generates a restricted zone in the top
% left and top right corners of the square.
% (note: the function returns a handle to the window, if needed;
% usage would then need to be f=createWindowS24(w)
left = 50;
bottom = 50;
pos = [left bottom w w];
f = figure;
set (f, 'Position', pos)
set (f, 'MenuBar', 'none')
set (f, 'Name', 'AE 227 - Spring 2024')
x = [0 w];
y = [0 w];
hold on
axis equal
axis ([x y])
axis off
x1=[0,w,w,0,0];
y1=[0,0,w,w,0];
fill(x1,y1,[1,1,1]); %draw the boundary
%Top Left
x2 = [0,0,w/2,0];
y2 = [w/2,w,w,w/2];
fill(x2,y2,[0.5 0.5 0.5])
%Top Right
x3 = [w/2,w,w];
y3 = [w,w,w/2];
fill(x3,y3,[0.5 0.5 0.5])
diff=0.01*w;
%Top Left Diagonal Warp
x5=[w/6,w/6-diff,w/3-diff,w/3,w/6];
y5=[2*w/3,2*w/3+diff,5*w/6+diff,5*w/6,2*w/3];
fill(x5,y5,[.6,.6,1]);
%Top Right Diagonal Warp
x5=[5*w/6,5*w/6+diff,2*w/3+diff,2*w/3,5*w/6];
y5=[2*w/3,2*w/3+diff,5*w/6+diff,5*w/6,2*w/3];
fill(x5,y5,[.6,.6,1]);
0 个评论
回答(1 个)
Anurag Ojha
2024-6-11
Hello
To make the balls collide with the restricted area and go through the warp gates, you can modify the code as follows:
I have added a code below that checks if a ball collides with the restricted areas or the warp gates and updates its direction or position accordingly. You can adjust the conditions and positions based on your specific requirements.
while true
for i = 1:BallNumber
[Bravo, Charlie] = getCenter(Alpha(i));
Delta = Bravo + cosd(Direction(i));
Echo = Charlie + sind(Direction(i));
% Check if the ball collides with the restricted areas
if (Delta < Radius && Echo > Width - Radius) || (Delta < Radius && Echo > Width - Radius)
% Change the direction of the ball
Direction(i) = -Direction(i);
end
% Check if the ball collides with the warp gates
if (Delta < Radius && Echo > 2*Width/3) || (Delta > Width - Radius && Echo > 2*Width/3)
% Teleport the ball to the corresponding position on the other side of the window
if Delta < Radius
Bravo = Bravo + Width;
else
Bravo = Bravo - Width;
end
end
% Update the ball position
xMove(Alpha(i), cosd(Direction(i)));
yMove(Alpha(i), sind(Direction(i)));
end
% Rest of the code...
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!