Watch my new tutorial about lighting, it’s like this but simpler and has opacity 🙂

Game maker 8 – 2D shadow/lighting tutorial
Simple and easy shadows in Game Maker without using surfaces.

Here is the code for the draw event for the obj_wall:

draw_triangle(x-16,y-16,x-16+lengthdir_x(1000,point_lt),y-16+lengthdir_y(1000,point_lt),x+16+lengthdir_x(1000,point_rt),y-16+lengthdir_y(1000,point_rt),0)
draw_triangle(x-16,y-16,x+16+lengthdir_x(1000,point_rt),y-16+lengthdir_y(1000,point_rt),x+16,y-16,0)
draw_triangle(x-16,y+16,x-16+lengthdir_x(1000,point_ld),y+16+lengthdir_y(1000,point_ld),x+16+lengthdir_x(1000,point_rd),y+16+lengthdir_y(1000,point_rd),0)
draw_triangle(x-16,y+16,x+16,y+16,x+16+lengthdir_x(1000,point_rd),y+16+lengthdir_y(1000,point_rd),0)
draw_triangle(x-16,y+16,x-16,y-16,x-16+lengthdir_x(1000,point_lt),y-16+lengthdir_y(1000,point_lt),0)
draw_triangle(x-16,y+16,x-16+lengthdir_x(1000,point_ld),y+16+lengthdir_y(1000,point_ld),x-16+lengthdir_x(1000,point_lt),y-16+lengthdir_y(1000,point_lt),0)
draw_triangle(x+16,y+16,x+16,y-16,x+16+lengthdir_x(1000,point_rd),y+16+lengthdir_y(1000,point_rd),0)
draw_triangle(x+16,y-16,x+16+lengthdir_x(1000,point_rt),y-16+lengthdir_y(1000,point_rt),x+16+lengthdir_x(1000,point_rd),y+16+lengthdir_y(1000,point_rd),0)

Nguồn: https://dorifaucet.com/

Xem thêm bài viết khác: https://dorifaucet.com/game/

28 Comments

  • The Campion
    Posted 40 mins ago 4:02 pm

    Thanks so much!

  • Mário Ti
    Posted 40 mins ago 4:02 pm

    Thanks

  • c00Ldud3
    Posted 40 mins ago 4:02 pm

    How can I do it with tiles? I have a random lvl generator that works with tiles and I want to add a static shadow fx to it but idk how 🙁

  • Gilbert Smith
    Posted 40 mins ago 4:02 pm

    If anyone's browsing Youtube for a good tutorial on a simple way to do this, I just wanna say this is the best one. A lot of the others use outdated code or may be just more ambitious than what you're trying to pull off.

  • WaffleNut와플넛
    Posted 40 mins ago 4:02 pm

    what this error_________________________________________
    ERROR in
    action number 1
    of Draw Event
    for object obj_wall:Error in code at line 1:
       draw_triangle(x-16,y-16,x-16+lengthdir_x(1000,point_lt),y-16+lengthdir_y(1000,point_lt),x+16+lengthdir_x(1000,point_rt),y-16+lengthdir_y(1000,point_rt),0)
                                                    ^
    at position 47: Unknown variable point_lt

  • Glen
    Posted 40 mins ago 4:02 pm

    I hope this isn't too late… but for some reason my shadows always start white…whats weird is tho.. i assigned a gamerestart key and when I press it, the shadows show up in black… I can restart the game many times all set to black perfectly…. but the very first time the shadows are always white. Any ideas as to how I could fix this please!!!

  • Charles A.
    Posted 40 mins ago 4:02 pm

    Thank you so much! I was really stumped and was about to give up on my project, but your video really helped me out!
    I just have one question about the code: what is the purpose of the 16 number?

  • Max Johnson
    Posted 40 mins ago 4:02 pm

    Right now, I'm having this light be determined by an enemy's location in a stealth game (essentially displaying the enemy's visibility. Is there a way to have this work for multiple instances of an object at once? I tried nearest instance and the results were very buggy and unrealistic.

  • Aaron Best
    Posted 40 mins ago 4:02 pm

    Hi,

    My shadows aren't aligning correctly – they're appearing about 20 px to the top/left of where they should be like this:
    http://imgur.com/a/SpvPI

    I'm unsure as to which part of the code I'll have to amend. my objects are 32X32

    Thanks

  • Srujan Kale
    Posted 40 mins ago 4:02 pm

    It is not working if I have have a black surface, a little help plz…

  • Noahwus
    Posted 40 mins ago 4:02 pm

    going to try reverse engineering this to have dynamic lighting as opposed to shadows. very cool stuff

  • Aziliz3
    Posted 40 mins ago 4:02 pm

    How would you be able to add in transparency to the shadows so it's not 100% darkness while maintaining the game below?

  • AbuFox
    Posted 40 mins ago 4:02 pm

    For anybody who made a wall_mask object, look no further. In the draw event of the obj_wall simply this line of code in the text
    draw_self()

  • Michael Duxbury
    Posted 40 mins ago 4:02 pm

    YES! It works!
    Not only that, but I've figured out HOW it works (have added comments to mine for easy reading later on, lol)!
    Many thanks for this, and thank you for the code snippet in the description. Makes life a lot easier!

  • THTerra
    Posted 40 mins ago 4:02 pm

    Sadly if you turn down the alpha of the Shadows, it looks like crap 🙁

  • Falkon Punch
    Posted 40 mins ago 4:02 pm

    Hi Edo Player, at first thank you for this cool engine!
    I got a question.. is it possible to use this engine and make a flashlight effect, so that you got a specific range of light?

    Thanks!

  • TheGuardian163
    Posted 40 mins ago 4:02 pm

    Thank you!! I'm using a surface at the same time but the problem with surfaces is, the light goes through the wall.

    So for now I'm using a combination of both. I make walls visible (and their shadows) only when they collide with my light to save resources, and let the surface do the rest. But it actually doesn't lag at all even without it

  • Tony Hardass
    Posted 40 mins ago 4:02 pm

    do these shadows work for precise collision objects ?

  • A Ha
    Posted 40 mins ago 4:02 pm

    Ah, triangles. Of course!

  • Agent Blackburn
    Posted 40 mins ago 4:02 pm

    My walls were in different shapes. Some were L-shaped, T-shaped, etc.

    What should I do to exact the shape of the shadow? Because when I tested your code, it gave me the cube shape instead of the L-shaped XD

  • LANLA
    Posted 40 mins ago 4:02 pm

    someone please send me a gmk, it's impossible to do it manually, i get error everytime.

  • Roddy
    Posted 40 mins ago 4:02 pm

    Can we change the alpha of the shadows using surfaces??

  • The Campion
    Posted 40 mins ago 4:02 pm

    Does this work in game maker studio pro?

  • Muaz Shihab
    Posted 40 mins ago 4:02 pm

    nice video

  • heffy mims
    Posted 40 mins ago 4:02 pm

    Bit of a noob here, but how would you add this to a character?

  • ColCustardKicksAss
    Posted 40 mins ago 4:02 pm

    Hey edo, my shadows overlap other walls. I can't seem to work out why – I have set the depth of obj_wall_mask to -1. Help would be greatly appreciated! Cheers.

  • Hajae Ko
    Posted 40 mins ago 4:02 pm

    +Nodzi Games
    The code up in the description has extra symbols. I fixed it up to be:
    draw_triangle(x,y-24,x+lengthdir_x(1000,point_lt),y-24+lengthdir_y(1000,point_lt),x+24+lengthdir_x(1000,point_rt),y-24+lengthdir_y(1000,point_rt),0)
    draw_triangle(x,y-24,x+24+lengthdir_x(1000,point_rt),y-24+lengthdir_y(1000,point_rt),x+24,y-24,0)
    draw_triangle(x,y,x+lengthdir_x(1000,point_ld),y+lengthdir_y(1000,point_ld),x+24+lengthdir_x(1000,point_rd),y+lengthdir_y(1000,point_rd),0)
    draw_triangle(x,y,x+24,y,x+24+lengthdir_x(1000,point_rd),y+lengthdir_y(1000,point_rd),0)
    draw_triangle(x,y,x,y-24,x+lengthdir_x(1000,point_lt),y-24+lengthdir_y(1000,point_lt),0)
    draw_triangle(x,y,x+lengthdir_x(1000,point_ld),y+lengthdir_y(1000,point_ld),x+lengthdir_x(1000,point_lt),y-24+lengthdir_y(1000,point_lt),0)
    draw_triangle(x+24,y,x+24,y-24,x+24+lengthdir_x(1000,point_rd),y+lengthdir_y(1000,point_rd),0)
    draw_triangle(x+24,y-24,x+24+lengthdir_x(1000,point_rt),y-24+lengthdir_y(1000,point_rt),x+24+lengthdir_x(1000,point_rd),y+lengthdir_y(1000,point_rd),0)

    draw_rectangle(x,y-24,x+24,y,0)

  • Julef Mapper
    Posted 40 mins ago 4:02 pm

    Awesome ! :DYou can also make them transparent using surfaces.

Leave a comment