Sign in to follow this  
okraM

2D (Deferred) Lightning - Rotating light

Recommended Posts

Hi,

Well in last two-three weeks I started working in XNA (game dev in general). After reading/watching few tutorials, i started giving myself "homework" :). So first i managed to make simple 2D shooting game, and after that particle engine and so on... Few days ago i was drawn to shaders and 2D lighting. After searching for tutorials (most of them for 3D) I found this "http://www.soolstyle.com/2010/02/2d-deferred-lightning/". I really like the effect, but now I want something more than a circle of light :). For example the guy who made that "tutorial" implemented that technique in his game (on link I gave you can watch a youtube video of that game), a character in game has some sort of a flashlight, and light is rotating, following a mouse.

So I have a few questions for you guys:
1. How do I change a shape of a light (from circle shape to something like in that video game)?
If somebody is willing to explain to me why is this line of code in HSLS drawing light in circle shape?
[CODE]
pixelPosition.x = screenWidth * PSIn.TexCoord.x;
pixelPosition.y = screenHeight * PSIn.TexCoord.y;
pixelPosition.z = depth * 2.0f;
[/CODE]

2. And how do i make a rotating light?
I tried experimenting with everything that I could think of, and I thought that calculating angle between light source and mouse position could give me desired effect but with no luck.
[CODE]

float angle = atan2(mousePosition.x - lightPosition.x, mousePosition.y - lightPosition.y);

lightPosition.x = lightPosition.x * cos(angle) - lightPosition.y * sin(angle);
lightPosition.y = lightPosition.x * sin(angle) + lightPosition.y * cos(angle);
pixelPosition.x = screenWidth * PSIn.TexCoord.x;
pixelPosition.y = screenHeight * PSIn.TexCoord.y;
pixelPosition.z = depth * 2.0f;


//////////////////////////////
OR
//////////////////////////////

float angle = atan2(mousePosition.x - lightPosition.x, mousePosition.y - lightPosition.y);

pixelPosition.x = screenHeight * (PSIn.TexCoord.x * cos(angle) - PSIn.TexCoord.y * sin(angle));
pixelPosition.y = screenHeight * (PSIn.TexCoord.x * sin(angle) + PSIn.TexCoord.y * cos(angle));
pixelPosition.z = depth * 2.0f;
[/CODE]

You can see a necessary code on a web page I gave you.

Thanks in advance :)

P.S. Sorry for my bad English. :(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this