Update log
Full Deep Space Exploitation update
The complete published notes, normalized for clean reading and source attribution.
Repeated intro
Hello! One frequently requested ship upgrade in the game is a kind of laser sight (and lasers in general!) to make lining up shots easier. So in preparation for Next Fest I thought I'd try and get this in, along with some of the cool ideas I had for it. I think it turned out really nicely and paves the way for mining lasers to be added soon. I thought I'd give a little explainer of how I went about creating this effect, since it's not too complicated and only really requires knowledge of stencil buffers. Before I get into it, I'll quickly mention that everything I talk about here is live in the demo that's currently on here Steam. Next Fest is a big deal for tiny indies like me, so wishlisting the game and sharing something about it around this time would really be awesome. Step 0 - What do we want? Most of all, this effect needs to be useful but not overwhelming. A big bright red line across the screen would be really dominating and take visual precedence over other things in the scene. Next, it needs to look convincingly like a thin beam of light and not some debug overlay. And finally, we want some cool simulation-like details, like lighting up other things along its path and reflections.
Extracted changes
- Gameplay
- Store
Step 1 - Tracing the path To trace the path of the laser I use ray casts against the physics objects in the game world (I've avoided calling these lasers ray traced, even though I'm technically tracing them with rays). I start from the player ship in its current facing direction, find the closest intersecting entity, then either stop there if the entity is non-reflective (like the asteroids) or continue from the intersection point using the normal of the intersection to calculate the new direction of the laser. The tracing continues like this until it runs out of distance. For each ray cast we store data about the position, direction, and what it intersected with (if it intersected at all), which we then use for drawing everything. One caveat to all of this is that I wanted the lasers to wrap around the game world like everything else, so at the start of each ray cast I calculate the distance to the edge of the play area in the current direction, and only cast the ray for that distance. If the ray reaches that full distance without intersecting anything then it means we have to wrap around to the other side, in which case we continue with a new ray cast with the starting position adjusted. Step 2 - Basic Drawing The basics of the effect are the fading out and fading in lines at the start and end of each segment of the laser, and the glow of the laser on the entity it collides with. The fading lines are draw point-by-point using Bresenham's line algorithm, this lets me draw the line without any aliasing (so it's guaranteed to be 1 pixel width) and also allows for better control over the fading out of the colour on the line (currently the fading is linear, but I'm thinking to bucket it so it has more of a pixelated feel). The glow where the laser collides with an entity is done by first writing the colliding entity's sprite to the stencil buffer, then simply drawing a sprite for the glow using that stencil (If you're not familiar with stencilling, check out this OpenGL explainer: https://learnopengl.com/Advanced-OpenGL/Stencil-testing). This gives us our
Source
