Field Of View

Published 2023-06-12


TO LOAD THIS PICO-8 CART, type in immediate mode: load #field_of_view

VVHAT'S NEW ?

I have coded this so the two primary elements of drawing the beam and checking to see if there is a collision with the beam are now small functions.

The drawbeam() function for instance is 280-characters and the inbeam() function is 225-characters which returns TRUE if there is a collision.

It was recently discussed, asking for a method of Line Of Sight for a game. The responses involving atan2() and sqrt() were rather confusing for me to understand, so I thought it about it in my head, basically 4-triangles. And decided to write my own here.

Based upon a cart I wrote earlier to determine finger swipes or mouse movements on the Pico-8 screen.

https://www.lexaloffle.com/bbs/?tid=27757

While it does not use a circle for line of sight, it still gives you the triangle view which you want your enemies to have, and it does it using only two commands, abs() and /2 quite simply, divided by 2. it is =NOT= scanning any pixels at all, collision is determined entirely through two math functions - so you could have the cone's pixels turned off but still be able to register collisions in it.

The code should also be easy to read as I have given helpful comments for every part of it.

It also only uses 2% of your CPU with cone view visible and 0% of your CPU when cone view is not visible, so you could definitely have a whole bunch of baddies running around still with their line of sight, determining if they can see the player or not even if you cannot see their field of view.

In this cart you use the arrow keys for navigation, 🅾️ to turn on and off the view cone for the enemy, and ❎ key to swap between moving the player and the enemy. Pressing an arrow direction for the enemy also faces them in that direction.

The field of view is seen as a simple dotted field and turns bright red if the player is in the line of sight.

And there you have it ! No complex calculations, works perfectly. Examine fully documented source for details.

If you have any questions, feel free to let me know.