Using Box2D and Surface object for lightning (Shadowing)

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • Hello peeps!

    I'm trying to make a shadow effect while using the Box2D extension by using raycasting, but it's not turning out so well. 'Single-Shape', 'Multi-Shape' or 'Custom Raycasting', neither works very solidly (See attached image).
    My current set up is as follows:

    Code
    Always > Surface Object; Clear with color RGB(0,0,0)
    Always > Start loop "Lights1" (Number of "light" ActiveObjects)
    OnLoop: "Lights1" > Start loop "Lights2" (360)
    OnLoop: "Lights2" ++ ID("Light")=LoopIndex("Lights1") > Box2D Object; Do Multi-Shape Ray Cast between [B]X("Light") , Y("Light") , X("Light")+(Cos(LoopIndex("Lights2"))*100) , Y("Light")+(Cos(LoopIndex("Lights2"))*100)[/B] 
    OnLoop: "Lights2" ++ RaycastNumHits("Box2D Object")=0 > Surface Object; Add Polygon Point: [B]X("Light")+(Cos(LoopIndex("Lights2"))*100) , Y("Light")+(Cos(LoopIndex("Lights2"))*100)[/B]
    OnLoop: "Lights2" ++ RaycastNumHits("Box2D Object")>0 > Surface Object; Add Polygon Point: [B]RaycastTargetX( "Box2D Object", 0) , RaycastTargetY( "Box2D Object", 0)[/B]
    Always > Surface Object; Draw polygon at (0,0) with RGB(255,0,0)

    And the result is this:
    Please login to see this picture.

    The black boxes are all.. black.. boxes, with Box2D collision as such. It looks weird, I know, like there's some triangles involved - But no. It's the ray-casting that's being wonky. Notice the transparent arrows.

    The red area is going to be the "negative space" of the shadow, as in, that area will be light while the area around it will be dark. Using the subtract mask effect on the surface object should work. Eventhough it seems fine (Like it's not going through the actual boxes, just clips into them) I cannot rest assured it won't be a occurring problem in the future.

    So, yes, basically I'm asking openly if anyone knows a better way to accomplish this with ray-casting (It won't be squares but a much more polygonal terrain in the future). Also, on the subject of this, how exactly does the Box2D Ray-casting filter collision thing work? Like, if I want the light to shine through "glass" bodies but the bodies are still solid to other bodies? Both of these things I'd really like to know.

    Thanks in beforehand!

    ... And down we go!

  • I don't see any picture, so don't know what the issue is. At any rate, I've never used the Box2D object either, so probably wouldn't be much help.
    However, I did make a shadow-casting example that uses polygonal obstacles - see the "Polygon Shadows" example here: Please login to see this link. (there was a reason I used all those global values instead of any array or whatever, which would be better in most situations, but I totally forget what that reason was)

  • Quote from MuddyMole

    I don't see any picture, so don't know what the issue is. At any rate, I've never used the Box2D object either, so probably wouldn't be much help.
    However, I did make a shadow-casting example that uses polygonal obstacles - see the "Polygon Shadows" example here: Please login to see this link. (there was a reason I used all those global values instead of any array or whatever, which would be better in most situations, but I totally forget what that reason was)

    While this one is sort of what I'm looking for - I'm not too sure of how it works. Actually, I have no idea in the slightest. Plus, it's in a cone rather than a 360 circle; the latter of which is what I want. If you'd rewrite this to allow that and show me in detail how your shadowing ticks (which I'd be very thankful for) I can consider using this workaround - and with that, credit you in the final game (if it's ever finished).

    Quote from BigHatLogan

    Can you post example please? I think I know what is going on in the picture, but i would like to test if I'm right first. I may be completely wrong.

    Sure thing: Please login to see this link.

    You need both the Surface Object and the Box2D 2.0.0 Object to open this (Should be obvious, but, you'd never know). Good luck on whatever you think could be a fix.

    ... And down we go!

  • It was as I thought it would be. Raycasting does not return hits sorted by distance. So "hit 0" can actually be hit on the object further away. Simple sort of all returned hits to determine closest one fixed it. And the small artifacts like the one at top-right or bottom-left are caused by different Y offset of the raycaster by -10 pixels.

    But if you are set on these real time shadows, there was example on the forum of quite nice and decently fast technique by drawing shadow volumes. This method is much more precise as 360deg is actually not that much and will lead to some artifacts all the time. Especially around edges.

    edit: link for the other method Please login to see this link.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!