• Advertisement
Sign in to follow this  

Tile Based Lighting - 'poping effect'

This topic is 3819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello fellow 2D programmers, I'm creating a 2D tile-based game - imagine the old Zelda titles. And now I'm trying to get tile-based lighting to work! It works fine when I don't move the view, aka. scroll the map. What happens is, if I move the view, is that every time the view crosses a tile the 'Light' jumpes suddenly to the next tile. The strange thing about that is, that it just suddenly jumps and then goes back to the expected position. Again: 1) I move down 2) Cross a tile 3) The light jumps one tile down 4) And jumps back up to the expected position without additional movement after a second. I've tried changing how I round floating point values to integers all day, nothing did the trick. I hope you have any ideas on how to help me out/improve it. Any resources about tile-based lighting would be great too! Here's the code that I use to process a "PointLight" into the buffer. I've removed all unrelated code. I'm rendering the ShadowLightTile[][] buffer by updating the color values in a grid of triangles. This grid is draw with an offset of -((int)scroll.X % tileSize) / -((int)scroll.Y % tileSize) to align the grid to the scene. Thanks alot for your time, Tick
      /// <summary>
        /// Processes the <see cref="PointLight"/> into the given <see cref="ShadowLightTile"/>-Buffer.
        /// </summary>
        /// <param name="buffer">
        /// The ShadowLightTile buffer, just a bit bigger than the screen.
        /// </param>
        public override void ProcessIntoBuffer( ShadowLightTile[][] buffer )
            const int bufferSafeOffset = 1;
            const int tileSize = 16;
            const int subDivisionCount = 4; // let's just draw a cross until we get it right, higher values needed for true point-lights
            const float angleStep      = MathHelper.TwoPi / (float)subDivisionCount;

            Vector2 start    = this.Position;
            Vector2 scroll   = this.Scene.TileMap.Scroll;
            Point tileScroll = new Point( ((int)scroll.X) / tileSize, ((int)scroll.Y) / tileSize );

            float angle = 0.0f;
            for( int i = 0; i < subDivisionCount; ++i, angle += angleStep )
                float angleCos   = (float)System.Math.Cos( angle ) * tileSize;
                float angleSin   = (float)System.Math.Sin( angle ) * tileSize;
                float intensity  = this.centerIntensity;
                Vector2 position = start;

                for( int r = 0; r < radius; ++r )
                    Point positionRounded = new Point( (int)position.X, (int)position.Y );
                    Point tilePosition    = new Point(
                        positionRounded.X / tileSize,
                        positionRounded.Y / tileSize );

                    Point tilePositionInGrid = new Point(
                        tilePosition.X - tileScroll.X - bufferSafeOffset,
                        tilePosition.Y - tileScroll.Y - bufferSafeOffset );

                    if( ShadowLightBuffer.IsInsideBuffer( tilePositionInGrid.X, tilePositionInGrid.Y ) )
                        ShadowLightTile tile = buffer[tilePositionInGrid.X][tilePositionInGrid.Y];

                        if( tile.Handled != ShadowLightTileHandledType.Handled )
                            tile.Intensity -= intensity;
                            tile.Color      = this.color;
                            tile.Handled    = ShadowLightTileHandledType.Handled;

                    position.X += angleCos;
                    position.Y += angleSin;
                    intensity  -= intensityLossPerTile;

Share this post

Link to post
Share on other sites

I quickly glanced at your code.
I wonder why you are using tileScroll ? Wouldn't it be easier to recompute lighting based on position ? Something like:

1- process movement. Derive new position.
2- recompute lighting.

Ghostly yours,

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement