How to detect the color a maze wall and prevent a figure to jump it?
2 次查看(过去 30 天)
显示 更早的评论
Im currently working on a maze project. It is composed soley of buttons, a little mouse and a maze which are images. I programmed the 4 buttons (left, right, up, down) to help the mouse move. The mouse must move throught the maze (which is an image with an empty background except for the maze walls) and finish it. My problem is the mouse cannot jump the walls but i cant find a way to prohibit the mouse to do so, my only possible solution is making over 200 if statements for each wall but is there an easier way to do this? Im attaching the image in case it helps. Thanks in advanced!![WhatsApp Image 2019-10-09 at 12.30.10.jpeg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/241874/WhatsApp%20Image%202019-10-09%20at%2012.30.10.jpeg)
![WhatsApp Image 2019-10-09 at 12.30.10.jpeg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/241874/WhatsApp%20Image%202019-10-09%20at%2012.30.10.jpeg)
0 个评论
回答(1 个)
Pranjal Kaura
2021-7-30
编辑:Image Analyst
2022-5-9
Hey Marcelo,
It is my understanding that you want to develop a collision detection system between the mouse and the walls for a maze. You do not want to use if-else statements to do the same.
It is my assumption that you have been able to extract the coordinates of the walls from the image. If not, you can refer to the code attached here.
"maze_solution" is one of the several submissions in MATLAB File Exchange on MATLAB Central which is a forum for our product users to interact, exchange information and knowledge, without MathWorks' involvement. Feel free to contact the author of this submission directly for specific questions about the implementation
A workaround could be to use for loops to achieve the same result. An array can be created in which 2 points(that define a line segment) can be added for every wall in the maze. You could also define 'x' points as extremities which will define the border for the mouse (e.g. 4 corners if the mouse has a rectangular bounding box). A for loop can then be added to check for any collisions. You can further refer to this answer to know about collision detection between a point and a line.
Here is pseudo code for the same:
%Wall_Array is the array that contains identifiers for all the walls,
%Mouse_Array is the array that contains extremities of the mouse.
%shortest_distance is a function that'll give the shortest distance between
%a point and a line.
for i = 1:length(Wall_Array)
for i2 = 1:length(mouse_Array)
if shortest_distance(mouse_Array(i2), Wall_Array(i) == 0)
%Collision
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Labyrinth problems 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!