TO LOAD THIS PICO-8 CART, type in immediate mode:
load #field_of_view
VVHAT'S NEW ?
- made them all tweetcard sized functions!
- made beam bigger.
- allows for different types of beams.
- made beam comparison smaller and faster code,
- uses less cpu.
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.
-- draw solid or dotted beam: function drawbeam(x,y,d,r,c,s) (131-tokens or 280-characters) -- x=sprite's x-position -- y=sprite's y-position -- d=direction sprite is -- facing: -- 0=left, 1=up, 2=right, -- 3=down -- r=radius of beam in pixels -- c=color to plot -- s=spacing: -- 1=normal, more cpu -- 2=dotted, less cpu |
-- check to see if player is -- in sight of beam. returns -- true if this is the case. -- assumes sprites are 8𝘹8 -- pixels: function inbeam(h,v,x,y,d,r) (121-tokens or 225-chars) -- h=player horizontal -- v=player vertical -- x=enemy horizontal -- y=enemy vertical -- d=direction beam is facing: -- 0=left, 1=up, 2=right, -- 3=down -- r=radius of beam in pixels |
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.
[Please log in to post a comment]