How do I reference when an image touches another?

I am essentially creating a game within MATLAB. Currently I am having an issue with causing something to happen if two objects touch.
An example of how the world is set up is a 10x10 cell named World.
so if World{x, y} = Player; World{5, 5} = Monster;
How would I be able to decrease Health by 1 if the player touches the monster? A simple Player == Monster doesn't work and I need advice.

4 comentarios

N.
N. el 15 de Nov. de 2017
you can check if the player is next to the monster. That means, if the player is on (x,y) and the monster is on (x',y'), you check if (x,y) is equals to (x'-1,y') or (x'+1,y') or (x',y'-1) or (x',y'+1)
Rik
Rik el 15 de Nov. de 2017
Wouldn't it be more straightforward to save position separately? Because now a player can be overwritten by a monster.
Josh Ondayko
Josh Ondayko el 15 de Nov. de 2017
Yes actually that is a great idea. Would it be easiest to save it as an array? (Location positions) but the only thing I am getting confused in that context is how to accurately save the location, even if it can be random. If I try something such as A = [Player Monster] it saves it as not an array.
Image Analyst
Image Analyst el 15 de Nov. de 2017
Why is world a cell array instead of a regular numerical array????? Don't complicate things!

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 15 de Nov. de 2017
Editada: Rik el 15 de Nov. de 2017

0 votos

You can save the positions of the player and the monster in separate position vectors (then you can use sqrt((x_p-x_m)^2+(y_p-y_m)^2) to calculate the distance). This would support non-integer locations.
You could also use binary matrices for location, which would enable you to check for a collision with any(monster_loc & player_loc). This method supports multiple monsters.
Which of these is the best approach depends on what the rest of you program does/needs.

4 comentarios

Josh Ondayko
Josh Ondayko el 15 de Nov. de 2017

Due to the nature of the project, I am working on, I need to make sure World is a cell. So essentially this is what the code looks like for placing items:

World{10, 10} = Door;
World{5,5} = Sword;
World{3,4} = Shield;
World{6,9} = Health;
World{2,8} = Health;
World{3,7} = Monster;
World{8,8} = Monster;
World{7,3} = Monster;

Is there a better way to hold these values through an array or vector? I am just very confused on how to accurately hold these values since World is a cell (Unless if I can convert it from a cell and it still remains similar, I don't know how to do this properly) I haven't worked with using images + cells before so I am kinda confused.

Image Analyst
Image Analyst el 16 de Nov. de 2017
What are the classes of Sword, Shield, etc.?
Rik
Rik el 16 de Nov. de 2017
You should also note that your current setup does not support collisions it the sense of two objects having the same position, as only one object can be in one cell element.
Josh Ondayko
Josh Ondayko el 16 de Nov. de 2017
I essentially turned it into each x, y coordinate to be its own variable and set it up through that, it ended up working fine. I did collisions by comparing the values essentially. I appreciate the help everyone!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Strategy & Logic en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Nov. de 2017

Comentada:

el 16 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by