Shadow Ball - my experiment with raycasting


  • Posts: 150
In this game your goal is to get all colored balls in the shadow by the time the countdown hits zero. You are playing as a white ball which is also the source of light.

This is just a demo. Note that the map and the number of balls is chosen at random now, so you won't know if you'll get a real easy or hard level. Try a few times for best experience ;)

<a href="" target="_blank" class="new_win"></a>
Use arrow keys for movement

This all began with a topic from 2 years ago I found. The guy made a really cool raycasting example. It checks for tiles and then calculates and draws the shadow behind it. I changed the code a bit so it would be more suitable for this game. The original also let the drawing event check the whole grid for existing tiles. What I did was at scene creation, create for each tile a sensor actor, so I could use 'for each actor of type' in the drawing event. Beneath is my whole drawing event, there's a little more than only the shadow drawing in it, but let's ignore that for now.

So we got the shadows now. The thing is.. drawings don't collide. So how are we going to check if a ball is in the shadow?
For each actor there's a tiny sensor created in the middle, this sensor rotates itself towards you (the white ball), then the distance is calculated and the sensor expands itself by that distance, this happens all the time. So what we get is some kind of spiderweb of lines between the white ball and all other balls. This might help visualising:

All that we need to do now is check if the lines collide with a tile(sensor), if it does, the corresponding ball's actor value 'behind' becomes true. It was a real pain in the ass for me to figure out a method to get the right sensor to refer to the right ball, but a simple list attribute saved me.

You might've noticed the colors are changing all the time while playing. I figured, since everything is drawn, and I'm using a 'color' attribute each time to get the same hue, I might as well loop through the whole spectrum. This was really, really easy.

So the whole game was working by this time and I wanted to be able to play on different maps/fields. Instead of creating a bunch of new scenes, I decided to approach this a little bit different since I discovered the 'get pixel at' block.
The field consists of 20x20 tiles, so a bitmap image with 20x20 pixels should be enough to contain everything needed to build a level. That size is tiny for an image, I can fit all the levels I need in just one. I created 8 simple levels for now, and this is how it looks:

Real small huh? The black pixels represent the tiles which become our obstacles, our 'sources of shadow'. The white ones are the border tiles, so we don't fly off the scene. The different shades of grey are just there to guide me what the center is.
The scene is then built by executing this:

That's it for now I think. I'll keep this topic updated, but don't expect too much too soon. I'm known to take my time.  ;D


  • Posts: 44
This is freakin' cool, man! Gonna need a bit of time to digest that code  :o... but this is great stuff. The level generation trick is also pretty neat!  :)


  • Posts: 853
This gives me something to do on the weekend!!


  • *
  • Posts: 2263
Big ups for this one!


  • *
  • Posts: 2182