1 Replies - 1647 Views - Last Post: 03 September 2012 - 06:48 PM

#1 sellers04  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-June 12

Implementing Krypton Lighting engine for use in a top-down 2d game

Posted 29 June 2012 - 01:40 AM

Hey everyone,

I'm a sophomore CS student at Oregon State and am still new to XNA and programming. I'm creating a 2d top down game which uses a camera to follow the player. I only ran into trouble when I tried implementing Krypton, a free lighting engine that is apparently quite popular. Many people on their message boards have had similar issues to mine but I can't translate their findings to my code and have it work. Any lights I create through the engine stay still relative to the screen, while the scenery it should be attached to moves behind it. I can't seem to feed Krypton the right view matrix for it to work properly. Any help I could get from here would be great, the message boards at Krypton aren't too lively, so i figured i'd try this out.

This code snippet right here is in the draw method, taken from the testbed example of the camera. This is the code I need to mess with for it to have the lights "stick" to my actual game world.
             // Create a world view projection matrix to use with krypton
            Matrix world = Matrix.Identity;
            //Matrix view = mainCamera.get_transformation(graphics.GraphicsDevice);
            Matrix view = Matrix.CreateTranslation(new Vector3((float)0, (float)0, 0));
            Matrix projection = Matrix.CreateOrthographic(this.mVerticalUnits * this.GraphicsDevice.Viewport.AspectRatio, this.mVerticalUnits, 0, 1);
            // Assign the matrix and pre-render the lightmap.
            // Make sure not to change the position of any lights or shadow hulls after this call, as it won't take effect till the next frame!
            //this.krypton.Matrix = world * view * projection;
            //this.krypton.SpriteBatchCompatablityEnabled = true;
            //this.krypton.Matrix = testMatrix;
            this.krypton.Matrix = world * view * projection;
            this.krypton.Bluriness = 4;
            this.krypton.LightMapPrepare();
            this.krypton.Draw(gameTime);
            this.DebugDraw(); 


But I also utilize a camera:
//Draw method
            spriteBatch.Begin(SpriteSortMode.BackToFront,
                BlendState.AlphaBlend,
                null, null, null, null,
                mainCamera.get_transformation(graphics.GraphicsDevice));


Camera.cs
public class Camera
    {
        protected float cameraZoomValue; // Camera Zoom
        public Matrix cameraTransformValue; // Matrix Transform
        public Vector2 cameraPosition; // Camera Position
        protected float cameraRotationValue; // Camera Rotation
 
        public Camera()
        {
            cameraZoomValue = 1.0f;
            cameraRotationValue = 0.0f;
            cameraPosition = Vector2.Zero;
        }

        public float Zoom
        {
            get { return cameraZoomValue; }
            set { cameraZoomValue = value; if (cameraZoomValue < 0.1f) cameraZoomValue = 0.1f; } // Negative zoom will flip image
        }

        public float Rotation
        {
            get { return cameraRotationValue; }
            set { cameraRotationValue = value; }
        }

        // Auxiliary function to move the camera
        public void Move(Vector2 amount)
        {
            cameraPosition += amount;
        }
        // Get set position
        public Vector2 Position
        {
            get { return cameraPosition; }
            set { cameraPosition = value; }
        }

        public Matrix get_transformation(GraphicsDevice graphicsDevice)
        {
            cameraTransformValue =
              Matrix.CreateTranslation(new Vector3(-cameraPosition.X, -cameraPosition.Y, 0)) *
                                         Matrix.CreateRotationZ(Rotation) *
                                         Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                                         Matrix.CreateTranslation(new Vector3(graphicsDevice.Viewport.Width * 0.5f, graphicsDevice.Viewport.Height * 0.5f, 0));
            return cameraTransformValue;
        }
    }



I am still very new to coding forums and asking for help, so if I need to provide any more code let me know!

Is This A Good Question/Topic? 0
  • +

Replies To: Implementing Krypton Lighting engine for use in a top-down 2d game

#2 LiberLogic969  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 43
  • Joined: 03-September 12

Re: Implementing Krypton Lighting engine for use in a top-down 2d game

Posted 03 September 2012 - 06:48 PM

Hello,

Ive had trouble with this a while back. After you initialize krypton, you need to set the SpriteBatchCompatabilityEnabled bool to true. Now, in the update method you need to set kryptons matrix to your cameras matrix. That should do the trick. If lights and hulls are not showing up you might need to mess around with kryptons CullMode property, or set the GraphicsDevice.CullMode(...or GraphicsDeviceManager) property manually. Hope this helps!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1