Light Collision Demo
I needed just a simple way to know all the places that a spot could “see” that were able to be got to by drawing an unbroken line to them.
The Code:
var degrees = 2 // number of degrees for each beam of light. Smaller numbers make more beams. var maxlength = 30 // how many checks to do in a line var currentlength = 0 var checklength = 10; // How long should each check be var xcheck = x; var ycheck = y; var gottox = x; var gottoy = y; var i; for (i = 0;i < 360; i += degrees) { currentlength = 0 xcheck = x; ycheck = y; while ( ! collision_line(xcheck,ycheck,gottox,gottoy,obj_wall,true,true) and currentlength < maxlength) { gottox = xcheck; gottoy = ycheck; xcheck = xcheck+lengthdir_x(checklength,i); ycheck = ycheck+lengthdir_y(checklength,i); currentlength ++ } draw_set_colour(c_yellow) draw_line(gottox,gottoy,x,y); }
Remarks:
Things I would improve.
- This version doesn’t cache the results like the final game did so if you go back to a spot it recalculates what it can see.
- Rather than drawing the line outwards from the centre I would have done a binary chop on the line as I think this is the most efficient way to find the place on the line there is a collisions.
About the Article:
Easy Difficulty
GameMaker
By David Strachan
Get an E-mail when I post something new: Signup