Lighting in box2d?

usually the lighting effect is used to reveal an area from a black haze

if you put the black haze over your objects you could get an effect like this
Image
by detecting the first and last ray cast to touch an object. rather than drawing your raycast around the object, drawing it through the object from first to last point.

to get a brightening effect like you have shown, you will need to do a bit more work i think.
you could still use the first and last raycast to touch any given object to give you the lighting shape, then use a bitmap effect to cause the lighting.
Tha sounds and looks like exactly the affect i was aiming for. Just wondering how i would go about doing that? Sorry for all the anoying questions guys im new to the box2d team. 🙂
i would follow this tutorial
http://www.emanueleferonato.com/2007/09/26/create-a-survival-horror-game-in-flash-tutorial-part-1/

with one very small alteration.
Image

normally you would end up with a mask in the shape of the green line
but if you pay attention to the red dots (the first and last of any consecutive hits of your raycasts) and line between those instead of all the hits in between. you end up with the alteration shown by the blue line. which sounds like what you want.

the problem i for see with this is when you are lighting directly at a wall... you wont get the effect you are looking for
try feathering (gradient ramp) your mask so that you will see a little past the edge of the mask as a fade out, and that may make it look acceptable.

hope that helps.
This will give a lot of problems with

a) non-convex bodies, you'll be able to light things that are behind it.
b) when you shine on a small box head on (i.e. the two red dots are the edges of a single plane), nothing will light up.
I knew what you meant i just dont understand how to go about actually doing it in box2d. Sorry for the misunderstanding 🙂
vedder do you have any suggestions that have fewer short comings?
Well avoiding non-convex structures in your level would solve the first problem. (more like, avoiding it)
If that's undesired you'd have to make a more complex system where you might have to make the blue line from your graphic another raycast, to check if it goes outside of the object. Not sure how that would be done exactly, but I'm pretty certain it's possible.

the second problem could be avoided by making the blue line in you picture a curve instead of a straight line. Then it would slightly light up a small piece of the wall. And it'd feel quite natural because a light like that would cast a oval spot.

Alternatively you could make the graphic or bounding box the raycast reacts to (not sure what you are using) smaller than the actual box graphic, so that edges of the box will be lit normally while the rest of the box is kept dark. This would be very easy to make, but won't look as pretty maybe.
very good points.


Quote:
Alternatively you could make the graphic or bounding box the raycast reacts to (not sure what you are using) smaller than the actual box graphic, so that edges of the box will be lit normally while the rest of the box is kept dark. This would be very easy to make, but won't look as pretty maybe.


as an alternative to this, you could use a bit of math to project all ray cast end points a bit farther from the character(/origin of light) than where ever they naturally end or hit. which would cause the lighting of the edges. without altering any boundaries for the physics. (assuming you are using the box2d raycast, which reacts to the physics shapes.)
Quote:
as an alternative to this, you could use a bit of math to project all ray cast end points a bit farther from the character(/origin of light) than where ever they naturally end or hit. which would cause the lighting of the edges. without altering any boundaries for the physics. (assuming you are using the box2d raycast, which reacts to the physics shapes.)

Ah this is what i have been trying to do but i cant figure out how to do it? Should i try and override the raycasting function?
increasing the length of a vector:(generic) //not using any box2d classes, although I'm sure some of this is built in there.

//this is not likely to be what you want but its simpler
multiplying length (+10%)

p1 p2 p1 p2 (example points)
0,0 10,20 -> 0,0 11,22
or
5,10 15,30 -> 5,10 16,32

based on those two situations we can tell that we need the LocalDistance of point2 from point1
LocalVector = p2x-p1x , p2y-p1y
LocalVector .x*=1.10
LocalVector .y*=1.10
p2.x = p1.x+LocalDistance.x
p2.y = p1.y+LocalDistance.y


//this is likely to be what you want
adding length (+6)
addLength = 6

p1 p2 p1 p2 (example points)
0,0 10,20 -> 0,0 12,24
or
5,10 15,30 -> 5,10 17,34

how do we get these numbers? our localized vector is 10,20... meaning we move 1/3x and 2/3y
LocalVector = p2x-p1x , p2y-p1y
percentX= LocalVector.x / (LocalVector.x + LocalVector.y)
percentY= 1 - percentX // or LocalVector.y / (LocalVector.x + LocalVector.y)

LocalVector.x += addLength * percentX
LocalVector.y += addLength * percentY

p2.x = p1.x+LocalDistance.x
p2.y = p1.y+LocalDistance.y



//more notes
this is not the cleanest or most efficient way to do this. but it should work.

a Unit Vector would be cleaner than percentX and percentY, providing the same values.

i also recall an article that had some useful knowledge about vectors and projecting vectors,
http://www.metanetsoftware.com/technique/tutorialA.html#section2
2nd to the last section here, they also have a downloadable pdf that is clearer on some things.

hopefully this is helpful to you.

Original thread: https://box2d.org/forum/viewtopic.php?t=3276