PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

2D fake lighting

Hi,

Anybody have an idea how to fake lighting on a 2D circle? In the game I'm working on I have several circles that represent planets, and I'd like to light/shade them as if they were lit from a sun, and have the light fall-off to give the circle the appearance of being rounded.

One idea I had was to create a circular "light map" to apply as I draw the circle, but I'd prefer a more dynamic solution, since the planets are randomly placed, and I'd like the sun location to be random too.

Thanks,

Pam
Pam wrote:Hi,

Anybody have an idea how to fake lighting on a 2D circle? In the game I'm working on I have several circles that represent planets, and I'd like to light/shade them as if they were lit from a sun, and have the light fall-off to give the circle the appearance of being rounded.

One idea I had was to create a circular "light map" to apply as I draw the circle, but I'd prefer a more dynamic solution, since the planets are randomly placed, and I'd like the sun location to be random too.

Thanks,

Pam


It depends on how you store information about the sun and the planets (circles). Are you using a 3D coordinates for that?
Diffuse shading is normally calculated via cosine of the angle between the light vector and the normal vector of the surface.
You could also apply some fake shading by computing the distance between the sun and each point within your circle and do some light attenuation.
It's easily implemented...

Just need to know more details to help you on that topic...

Peter
Yes attenuation by distance is easy and quick. You can also precompute sphere normals for each pixel in your circle and do a proper diffuse/specular calculation. All that information is in the Foley graphics book. Enough to make your own ray tracer. :-)
You could have some light maps, varying by size, and then blit it clipping the circle on the position relative to the sun, so if the sun is on the top-right ( just calculate the angle between the planet X sun, I think you should use atan for this ), you would clip a circle to the bottom-left pf the map so the bright from the map will be located on the top-right of the circle.
"add" the bright from this map on top of your planet image and you´ll have the planet with the brightness located according to the sun position

pretty simple but may be better than coding a mega 3d phong rasterizer :)
Assuming that you want to do them once at the start of the game, I would consider treating them as spheres and ray-trace each pixel on the sphere to the light-source.

I did a quick google, and turned up:
http://occs.cs.oberlin.edu/faculty/jdon ... ure31.html

For a small number of spheres on a 2d-plane over a single-screen sized area, it shouldn't take all that long...

(Side-note, I'm presuming Pam wants to render planets spread over a 2d-plane as in <a href="http://jammiesoft.arwen.com/StellarCombat">StellarCombat</a>)
Thanks everyone!

The planets are stored with center x,y and a radius. I use a simple scanline algorithm to draw them.

They are drawn only once at the start of the game, although this may change if I implement some of the other suggestions people have made. (eg. destructible planets)

It is indeed for StellarCombat (thx ref!)

Pam
I sorta took this as a challenge...

Written in pocketFrog, but applicable:
Code:

void DrawPlanet(DisplayDevice* display, int Cx, int Cy, int Cr, int Sx, int Sy, unsigned char red, unsigned char green, unsigned char blue)
{

for(int x=Cx-Cr; x<Cx+Cr; x++)
for(int y=Cy-Cr; y<Cy+Cr; y++)
{
int dx = Cx - x;
int dy = Cy - y;

float r = sqrt(dx*dx + dy*dy);

float theta = atan2(Sx - Cx, Sy - Sx) - atan2(x - Cx, y - Cy);

float intensity = sin(r/Cr * 3.14f) * cos(theta);

const float perct = 0.3f;

int Col = Color( red*(1.0f - perct) + perct*red*intensity
, green*(1.0f - perct) + perct*green*intensity
, blue*(1.0f - perct) + perct*blue*intensity);

if(r <= Cr)
display->SetPixel(x, y, Col);

}

}


Could be optimized a bit, I'm sure.
Cx, Cy, Cr = circle x,y,and r.
Sx, Sy = Sun coords.
perct is percentage pseudo reflection/ambient.
Excellent! Thanks!

Since I just got laid-off today from my real job, I may have more time to work on my game. Or maybe I won't. I don't know. Life sucks.
Ouch.
A related problem is texturing a circle as if it was a sphere, so it can be rotated about any axis....

I'm planning to do this for my pool balls (then blend it with a light map to get some fake specular highlights), but you might want to have a go at making your planets rotate?

I've got some ideas in my head about how to do it, but not actually tried it yet...