[Ace] Ray tracing script (sight)

I tried to write a ray tracing script, essentially, it's supposed to check if a particular event can see a particular map coordinate. Unfortunately, it doesn't seem to be working and I haven't a clue why, been trying to fix it for half an hour but I'm shooting in the dark here...

My code below:

class Game_Interpreter def get_event(i) if i == 0 return $game_map.events[@event_id] end if i > 0 return $game_map.events end end def sightBlocked(e, x, y) event = get_event(e) ex = event.x ey = event.y #get difference dx = x-ex dy = y-ey dist = Math.sqrt(dx*dx+dy*dy) #get ray increment amount incx = dx / dist incy = dy / dist #loop across ray and test if passable for i in 1..(dist) ex += incx ey += incy if !event.passable?(ex.floor, ey.floor, 0) #floor effectively returns the integer value of a float return true #sight blocked end end return false #sight not blocked endendNote: I am aware that the tracing may see past corners but that isn't the problem, it seems to be working only half the time, if that.
What makes you say it isn't working?
This demonstrates what I mean:

SightTest.zip

Edit: Note, it says not blocked when the player is below the event to the left.
What is this test doing? Seeing if you can see event 1 or not?
Seeing if the event can see the player. It's checking if there is anything blocking the view from one to the other. Or trying to...
Surely it behaves incorrectly (it appears like it says true if you're positioned to the NE or SW of the event and false otherwise, and completely seems to forget about checking passability).

I did some testing, changed particularly this line:

if !event.passable?(ex.floor, ey.floor, 0)...to this... thing:

if !event.passable?(ex.floor, ey.floor, 2) && !event.passable?(ex.floor, ey.floor, 4) && !event.passable?(ex.floor, ey.floor, 6) && !event.passable?(ex.floor, ey.floor, 8)...as it seems that giving zero as the direction does nothing, unlike on previous RM editions where zero meant "check all directions".

This way, "works" and gets to check things, however it fails on two cases: when a tile has one direction open (like the top part of the walls you used, which you can enter from below).

http://puu.sh/4NVHu.jpg

I tried changing all ANDs to ORs so it would not happen, but doesn't seems to work either (gives me blocked vision ALWAYS).

And second one is when you're on certain corners (only to the right side, and that's probably by cause of the way increase works).

http://puu.sh/4NVIE.jpg

Both combined gives... tarararararararaaaannnn...

http://puu.sh/4NWb8.jpg

(after some minutes)


Okay forgive me for making this response this long, but wanted to share that previous part too. It seems I've got to solve the previous issues by using .round instead of .floor lol (the second one, sure, but didn't thought it would solve the first one!).

So forgive the last part, here is that long line again:

if !event.passable?(ex.round, ey.round, 2) && !event.passable?(ex.round, ey.round, 4) && !event.passable?(ex.round, ey.round, 6) && !event.passable?(ex.round, ey.round, 8)There is an explanation for this.

If your increment is positive for any of the coordinates, it behaves normally. Your 4 will not be 5 until it gets to be 5 or more.

But if your increment is a negative, and let's say you have 4, when you apply just one "increment" you get 3 with floor (instead of waiting for it to be 3 or less). With round instead, it will iterate and get 4 until the increment is enough to give you 3 (when it is less than 3.5).

Another thing you could do is to apply floor or ceil depending on increment's sign (ceil for negative, floor for positive). But as I tested it works fine with round, so there's no need for an extra check. Anyways, didn't tested a lot so I could be wrong.

Hope this works, and hope my explanation is not that unclear, I'm bad at explaining stuff,

Orochii Zouveleki
Thanks for this, I had no idea 0 as a direction would break it.

But there is still a problem here, the event can apparently see through the ceiling tiles.

I think I'll write a custom passable function, the characterBase's version is doing too much I don't need.
I have it working now. Thanks for the help, it was much appreciated.

I might just make this an official script, lets see.

Original thread: https://forums.rpgmakerweb.com/threads/ace-ray-tracing-script-sight.18966/