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 Comments

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!

Sign in to comment.

 Accepted Answer

Rik
Rik on 15 Nov 2017
Edited: Rik on 15 Nov 2017
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 Comments

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!

Sign in to comment.

More Answers (0)

Categories

Find more on Strategy & Logic in Help Center and File Exchange

Tags

Asked:

on 14 Nov 2017

Commented:

on 16 Nov 2017

Community Treasure Hunt

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

Start Hunting!