Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Algorithm Fun . . . >_>

I've been glancing at this every once in a while for the past few months, but all I've accomplished is to find more problems with my methods. I'm attempting to create an algorithm that functions as a "sight detection system"; if the player steps within a four tile radius of this moving event, a process is activated. Here's what I've used:



Code:
Player X = Player's Map X

Player Y = Player's Map Y

Lich X = This Event's Map X

Lich Y = This Event's Map Y

Distance X = Player X

Distance Y = Player Y

Distance X -= Lich X

Distance Y -= Lich Y

If Distance X >= -4

  If Distance X <= 4

    If Distance Y >= -4

      If Distance Y <=4

        Run processing etc. etc.

      End

   End

  End

End

This doesn't work very well. For one thing, the Lich can see as far behind it as it can ahead, which would take some complex Conditional Branches for checking direction which is confusing enough, in additon to the fact that I need to calculate that as a semi-circle rather than half a square, and that just seems impossible to me. D:

Any suggestions for applying the changes I mentioned above into this algorithm? Thanks!
 
Checking a semi-circle is easy. If you think about it, it basically mean "anything within X meters distance from object". The formula for distance is SquareRoot((X2-X1)^2 + (Y2-Y1)^2). Basically pythagoras. But with RMXP's tile based system, I doubt it'd work that well.

So you first start by checking if something is within X units of line of sight. Then you create a little script because events suck, in order to have OR conditionnals.

Something like : If (Facing up AND Y2 < Y1) OR (Facing Down AND Y2 > Y1) OR (Facing Left AND X2 < X1) OR (Facing Right AND X2 > X1).
 
Zekallinos":2zdbvo5c said:
Checking a semi-circle is easy. If you think about it, it basically mean "anything within X meters distance from object". The formula for distance is SquareRoot((X2-X1)^2 + (Y2-Y1)^2). Basically pythagoras. But with RMXP's tile based system, I doubt it'd work that well.

So you first start by checking if something is within X units of line of sight. Then you create a little script because events suck, in order to have OR conditionnals.

Something like : If (Facing up AND Y2 < Y1) OR (Facing Down AND Y2 > Y1) OR (Facing Left AND X2 < X1) OR (Facing Right AND X2 > X1).

Actually, the formula you gave will work very well, since each tile could easily function as a point on a graph. Just use the player's X/Y as X1/Y1, and the event's X/Y as X2/Y2. Just keep in mind that you'll have to check for the proper value, or 4, in your case. So, the formula, after you set up the variables, is the following:

(easily done in a conditional branch's script function)

Code:
((((player_x - event_x) ** 2) + ((player_y - event_y) **2)).to_f ** 0.5) <= 4

As I said, you have to set up the variables first. In this case, you might want to replace each of the variables I gave you with the following:

Code:
$game_variables[#]

Just make sure that you replace the # symbol with the number of the appropriate variable.
 
KRoP":2zsb94ws said:
I love you both. :specs: It seems like this should work; I'll mess around with this and post if I find any more problems in the future.

One thing to note is that 'm not entirely certain that that is the proper formula. I seem to remember it having one of the pairs flipped around. But, that might have been something else. (Just to note, it doesn't matter which pair gets flipped if that is the case, as this is not a subtraction problem)
 
There's only so much I can figure out by a playtest. But it seems that, although the sight range is no longer a square, the event still has a full circle of vision. This is probably as far as you intended it to work; but if it wouldn't be too much trouble, could you show the code in conjunction with circle being cut in half based on the direction the event is facing? I'd appreciate it. Thanks!
 
Well, once you get past the first conditional (within 4 units), you don't need to bother with the circles anymore. If your event is facing up, all you need to do is check is if the player is above the event. If Y1 < Y2. That should, by itself, create a half-circle (since it trims out the lower part).

That (modified) -> [If (Facing up AND Y1 < Y2) OR (Facing Down AND Y1 > Y2) OR (Facing Left AND X1 < X2) OR (Facing Right AND X1 > X2)]
 

Zeriab

Sponsor

Can you tell us how you want the sight to be in a visual manner?
Like editing the following image to show in which tiles the player is sighted.
grid.png


*hugs*
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top