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

N.
N. 2017년 11월 15일
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 2017년 11월 15일
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일

0 개 추천

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.?
Rik
Rik 2017년 11월 16일
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에 대해 자세히 알아보기

태그

질문:

2017년 11월 14일

댓글:

2017년 11월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by