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 个评论

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)
Wouldn't it be more straightforward to save position separately? Because now a player can be overwritten by a monster.
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.
Why is world a cell array instead of a regular numerical array????? Don't complicate things!

请先登录,再进行评论。

 采纳的回答

Rik
Rik 2017-11-15
编辑:Rik 2017-11-15
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 个评论

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.

What are the classes of Sword, Shield, etc.?
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.
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!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Strategy & Logic 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by