MATLAB Answers

How to detect the color a maze wall and prevent a figure to jump it?

6 views (last 30 days)
Marcelo Marquez
Marcelo Marquez on 9 Oct 2019
Answered: Pranjal Kaura on 30 Jul 2021
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

Answers (1)

Pranjal Kaura
Pranjal Kaura on 30 Jul 2021
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 co-ordinates 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 collsion detection between a point and a line
Here is a 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)

Community Treasure Hunt

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

Start Hunting!

Translated by