User Tag List

Results 1 to 7 of 7

Thread: Using Box2D and Surface object for lightning (Shadowing)

  1. #1
    Clicker Multimedia Fusion 2
    GreenOnion's Avatar
    Join Date
    Oct 2012
    Location
    Sweden
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Using Box2D and Surface object for lightning (Shadowing)

    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 X("Light") , Y("Light") , X("Light")+(Cos(LoopIndex("Lights2"))*100) , Y("Light")+(Cos(LoopIndex("Lights2"))*100) 
    OnLoop: "Lights2" ++ RaycastNumHits("Box2D Object")=0 > Surface Object; Add Polygon Point: X("Light")+(Cos(LoopIndex("Lights2"))*100) , Y("Light")+(Cos(LoopIndex("Lights2"))*100)
    OnLoop: "Lights2" ++ RaycastNumHits("Box2D Object")>0 > Surface Object; Add Polygon Point: RaycastTargetX( "Box2D Object", 0) , RaycastTargetY( "Box2D Object", 0)
    Always > Surface Object; Draw polygon at (0,0) with RGB(255,0,0)
    And the result is this:


    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!

  2. #2
    Clicker Fusion 2.5 Developer

    Join Date
    Jul 2008
    Posts
    1,295
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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: http://www.sketchy.co.nf (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)

  3. #3
    No Products Registered

    Join Date
    Apr 2013
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    Clicker Multimedia Fusion 2
    GreenOnion's Avatar
    Join Date
    Oct 2012
    Location
    Sweden
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 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: http://www.sketchy.co.nf (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 Originally Posted by 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: https://www.dropbox.com/s/8fjg823pmb9phgt/Lightning.mfa

    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.

  5. #5
    No Products Registered

    Join Date
    Apr 2013
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 http://community.clickteam.com/threads/59100-Dynamic-Light-and-Shadow-Engine-4
    Attached files Attached files

  6. #6
    Clicker Multimedia Fusion 2
    GreenOnion's Avatar
    Join Date
    Oct 2012
    Location
    Sweden
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot BigHatLogan,This seems to be functioning perfectly now, just like I want it!

    Kudos to you

  7. #7
    Clicker Fusion 2.5 Developer

    Join Date
    Jul 2008
    Posts
    1,295
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I see you already have a solution, but I thought I'd tweak and upload my version anyway:
    http://community.clickteam.com/threads/82961-Polygon-based-Shadowcaster?p=608052#post608052

Similar Threads

  1. Lightning initiating from an object and hitting another object
    By StardustSpeedman in forum Multimedia Fusion 2 - Technical Support
    Replies: 7
    Last Post: 25th June 2012, 01:58 PM
  2. Surface object
    By Looki in forum Released Extensions
    Replies: 4
    Last Post: 18th October 2010, 01:38 AM
  3. Two quick unrelated questions (Box2D and Surface)
    By Jacob in forum Multimedia Fusion 2 - Technical Support
    Replies: 2
    Last Post: 7th October 2010, 10:01 PM
  4. Another Lightning Example!
    By Jaffob in forum File Archive
    Replies: 6
    Last Post: 19th October 2009, 02:16 PM
  5. [REQUEST] Drop Shadowing
    By Tuna in forum Multimedia Fusion 2 - Technical Support
    Replies: 12
    Last Post: 27th March 2008, 04:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •