Line Of Sight or Shadow Casting in 2D

Sdílet
Vložit
  • čas přidán 11. 10. 2018
  • In this (long, sorry) video I cover a variety of things to implement a classing line-of-sight mapping, or shadow casting depending on your perspective. It's also the first olc::PixelGameEngine project I've done a video for, so I spend some time showing you how to set up Visual Studio to use this engine. I'm quite pleased with the end result of these algorithms and I expect to use them quite a bit in more ambitious tile-based projects in the future.
    These resources are excellent regarding the things seen in the video:
    stackoverflow.com/questions/5...
    www.redblobgames.com/articles...
    ncase.me/sight-and-light/
    Source: github.com/OneLoneCoder/Javid...
    / discord
    Blog: www.onelonecoder.com
    Twitter: @javidx9
    Twitch: javidx9
  • Věda a technologie

Komentáře • 311

  •  Před 4 lety +158

    0:00 _Intro_
    2:26 _Setting up Pixel Engine in VS_
    5:41 Setting up demo
    6:30 _Theory talk_
    7:02 *Theory* - Converting tiles to edges (on paper)
    8:26 _Talk about advantages of converting tiles to edges_
    8:50 *Theory* - Algorithm (on paper)
    15:44 Implementation - Generating edges
    26:21 _Optimization talk_
    26:45 *Theory* - Ray casting? (on paper)
    31:05 _Resources_
    32:17 Implementation _Setup_ - Visibility
    32:58 *Theory* - Ray casting order? (on paper)
    33:50 Implementation - Visibility
    35:42 _Ray/Edge intersection talk_
    36:02 Implementation _Setup_ - Ray/Edge intersection
    36:13 *Theory* - Ray/Edge intersection (on paper)
    37:01 _Resources_ - Line intersection Algorithm
    37:11 Implementation - Ray/Edge intersection
    39:57 Implementation - Casting shadows
    40:56 _Implementation result_
    41:40 Implementation - Bug fixing
    42:12 _Implementation result_
    42:42 Implementation - Generating boundaries
    43:03 _Implementation result_
    43:23 _Visualizing light only with rays_
    44:14 Implementation - Reducing numbers of rays
    45:33 _Implementation result_
    46:37 Implementation - Adding sprite for light source
    49:02 *Final result*
    49:26 Compiling in release mode
    49:46 _Off screen buffer talk_

    • @5daydreams
      @5daydreams Před 3 lety +2

      Wow, this is very organized! Thanks a lot :D

  • @TheMadmaxster
    @TheMadmaxster Před 5 lety +120

    I haven’t enjoyed CZcams videos this much in a long time, fantastic content!

    • @javidx9
      @javidx9  Před 5 lety +6

      That's really pleasing to hear MadMaxster, thanks!

    • @dewijones92
      @dewijones92 Před 5 lety

      +1

    • @clodgozon3968
      @clodgozon3968 Před 5 lety +2

      It's true. I don't know why I do not enjoy Cherno and other people's programming videos, except this channel!

    • @aimanal-eryani7809
      @aimanal-eryani7809 Před 5 lety +1

      Yes indeed, dude's old-school

  • @harryfox4389
    @harryfox4389 Před 5 lety +63

    I've just scrolled through your channel and you answer lots of questions and have tutorials on everything I've been trying to do.
    Pseudo 3d. Check
    Shadows. Check.
    Zoom and panning. Check
    Thanks a lot

    • @javidx9
      @javidx9  Před 5 lety +11

      Hey welcome to the community buddy!

  • @avirosenblum233
    @avirosenblum233 Před 5 lety +6

    There's an important error in your raycast function. In it you solve for t1 by dividing by rdx. If the ray being tested is vertical, rdx will be zero and therefore the result will be NaN.
    To fix this, add a check for rdx != 0 before that step. If rdx == 0, use the following to solve for t1 instead:
    t1 = (e2 .sy + sdy * t2 - oy) / rdy
    I implemented this and it fixed the issue. Hope this helped anyone else who had this problem.

  • @mothtolias
    @mothtolias Před 5 lety +6

    the moment you pointed out the "fan" was triangles my mind was blown. fascinating video - thank you for sharing all these resources!

    • @javidx9
      @javidx9  Před 5 lety

      Yeah, its a nice conclusion - always reduce a problem into things you can do, sounds obvious I know, but so very very true...

  • @stevea.b.9282
    @stevea.b.9282 Před rokem +4

    I can't tell you how much this video helped me, I've been struggling to get LOS working for months, and it works perfectly now (i'm making a 2d maze game). Thank you!
    I cut the number of extra rays needed down to 1 (rather than 2) by making a Point struct with an enum to describe the point type based on which walls it connects (north, east end west, north/west corner, T-junction etc) . If they are "continuer" types (end of a wall and corners) I add an extra ray slightly further along toward the end of the wall, or outwards from the corner, 0.1f is enough. Currently working on how to cull un-needed rays - will post result and method if I find one.
    Thanks again javidx9, you are a life saver!

  • @psyjax2
    @psyjax2 Před 5 lety +47

    26:09 How dare you draw the Holy Hand Grenade of Antioch without the proper blessing!

    • @javidx9
      @javidx9  Před 5 lety +12

      dammit XD

    • @mothtolias
      @mothtolias Před 5 lety +1

      looks more like a map church symbol to me.

  • @Jay-og4yb
    @Jay-og4yb Před 5 lety +1

    This guy is ridiculously talented.
    Well done man
    The tutorials are much appreciated!

  • @GermsOilCotten
    @GermsOilCotten Před 5 lety +22

    Thanks for including the set up. I'm very new to this stuff and it was very helpful. I know lots of people may know that stuff and not want to watch more explaination about it but for me that is amazingly helpful. Love your channel.

    • @clodgozon3968
      @clodgozon3968 Před 5 lety

      Duh, I love explanations too. You're not the only one, lmao.

  • @thePrinceOfPurpose
    @thePrinceOfPurpose Před 5 lety +2

    Thank you for coming to youtube! I love your work!

  • @nivalius
    @nivalius Před 5 lety +1

    long videos are good
    thank you for all you're doing, great stuff

  • @namless0o7
    @namless0o7 Před 4 lety +1

    Your content is very compelling, well constructed, and excellent video quality. Keep up the good work! I have learned so much from you!

  • @laureven
    @laureven Před 5 lety +1

    Absolutely Awesome Video

  • @nilspin
    @nilspin Před 5 lety +2

    I was having problem understanding Ray casting in voxel engines, and your video made it very clear how it's done. I understood everything you explain in one go and don't have to rewind to jump back and forth topics. You explain things in a linear and structured fashion which I think sets you apart from most other programming channels.
    Keep up the good work!

  • @kevinjad4506
    @kevinjad4506 Před 4 lety +3

    I LOVED HOW U EXPLAINED THE FAN of triangle CONCEPT

  • @kubamaruszczyk4043
    @kubamaruszczyk4043 Před 5 lety +2

    Excellent video! Your explanations are very clear and very well visualised. Thank you for sharing your expertise!

  • @KSG__
    @KSG__ Před 5 lety +11

    perfect time for a javid video :D

    • @javidx9
      @javidx9  Před 5 lety +4

      lol, perhaps not in UK, it was 12:30am here :D

  • @alxklgn364
    @alxklgn364 Před 5 lety +1

    A pleasure to watch. Thanks.

  • @Otakutaru
    @Otakutaru Před 5 lety +7

    I've noticed that in a 4 edge tiled enviroment, bounding vertices always have an even number of edges attached, and any vertex with an odd number will not be relevant. So using this idea the first step would be to purge the irrelevant vertices from a grid array.
    Also, the number of edges in a row/column will be the number of vertex in that row/col divided by 2 (duh. but has neat properties). This means that you can start and end connections intermitently any time you encounter another vertex when running down the row/col without worrying about messing up the geometry. That way you can convert a vertex grid into an array of edges with starting and ending points.
    Then merge edges into vertex shapes by checking their vertices. Select one edge, compare to the next one, if they share a point, push the new vertex of the two into an ordered array until you find the edge that closes the shape by also checking if the starting and ending vertices match up, select another free edge if there are any left, repeat. You'll end up with an array of closed shapes and uninterrupted edges (hopefully).

  • @videopsybeam7220
    @videopsybeam7220 Před 5 lety +28

    This is gonna come in real handy for GameHut's 16-bit PUBG demake.

    • @SnakeEngine
      @SnakeEngine Před 5 lety +6

      On Sega Saturn!

    • @javidx9
      @javidx9  Před 5 lety +10

      Yeah Psybeam a few comments have pointed this out - it looks like a great project from an experienced coder, one to watch for sure.

  • @fuanka1724
    @fuanka1724 Před 4 lety +1

    Thanks for sharing this video. Lots to learn indeed.

  • @bryphi77
    @bryphi77 Před 5 lety +3

    Always such great content!

  • @rampage14x13
    @rampage14x13 Před 3 lety

    Wow, this is just what I was looking for! Thank you so much, I haven’t seen the video yet but based off of your other ones I’m sure this will not disappoint! It’s late at night where I am atm so I will provide an update at some other time on how my project is going after watching and hopefully implementing this into my own project..

  • @GG-fj5er
    @GG-fj5er Před 4 lety +1

    this is amazing. thank you so much. you could absolutely charge for these videos.

  • @lucaxtal
    @lucaxtal Před 5 lety +1

    Great video and very clear explanation, keep doing this great job!

    • @javidx9
      @javidx9  Před 5 lety

      Cheers Luca, will do!

  • @SamFisk
    @SamFisk Před 5 lety +3

    I've been working on this on and off for last couple weeks, and lo and behold a video appears in my feed. :P Decided to scrap the method I had been trying and go to something like this and after an afternoon of work got everything working. :D Thanks for the resource, would have been amazing to have few weeks earlier though. Definitely going to be very valuable to others.

    • @javidx9
      @javidx9  Před 5 lety +1

      Sorry it was later than you needed Sam, oh well! At least you got it working and thanks for the support - its appreciated!

  • @Valentyn90A
    @Valentyn90A Před 5 lety +1

    This channel is amazing. Thank you for this!

    • @javidx9
      @javidx9  Před 5 lety +1

      Hey thanks Val, my pleasure!

  • @kepeb1
    @kepeb1 Před 5 lety +6

    Thanks.
    It's a great idea to include the little VS setup. I switched from code:blocks a week or so back and it was surprising how little information on this there was.

    • @kepeb1
      @kepeb1 Před 5 lety +1

      Actually, it was a few things. A gap in my learning, majority of interesting CZcams tutorials seem to use it, and it is easier to follow for a beginner if it looks the same. I am not really skilled enough to argue the merits of either.
      But I can say VS was WAY easier to set up.
      There were a number of code:blocks specific hurdles that kept interrupting my progress.
      Before I was even introduced to all the fundamentals there were extra steps I had to figure out in order to simply follow along.

    • @javidx9
      @javidx9  Před 5 lety

      Hey Thanks Turncoat, I'm pleased it was useful to someone, its a question I get asked a lot.

  • @Revan727
    @Revan727 Před 4 lety

    If you don't teach you should. Your explanation of things are so clear and concise that I was able to convert your code into C#. Well at least for the converting cells into a polygon. I am now working on the visibility polygon. Awesome work!

  • @mattpilot
    @mattpilot Před 5 lety +3

    Thanks for your amazing videos. I dont do any C++, but they way you explain things makes it so easy to follow along and understand i have no problem applying it to javascript. Keep it up - maybe one day you'll bless us with javascript game dev videos :-)

    • @javidx9
      @javidx9  Před 5 lety +1

      Thanks M P, I appreciate it, although I will say it is unlikely I'll do any javascript videos, but I will be doing some lua script pretty soon!

  • @florianwicher
    @florianwicher Před 2 lety

    This is a beautifully-done tutorial, sir. Edit: Just got it working in Java :)

  • @michaeljones3448
    @michaeljones3448 Před 3 lety

    awesome channel, hope to get visual studios, seems like a neat program. Keep it up!

  • @Plrang
    @Plrang Před 5 lety +1

    Another short and simple video, great to watch right before a sleep at 3AM ;) tx

    • @javidx9
      @javidx9  Před 5 lety +2

      lol, yeah yeah, I get it, shorter videos :P Sleeping just stops you from doing cool stuff anyway...

    • @Plrang
      @Plrang Před 5 lety +1

      @@javidx9 I got a fresh kid, don't sleep anyway;) And I know you have, watching that all the time

  • @Morphox
    @Morphox Před 5 lety +8

    Nice video!
    I once programmed something similar in C# with OpenGL
    I used my own method which was to iterate over every vertex of the obstacles as lines and then using dot product of the line's normal and the vertex-player direction vector to detect whether the player can see the line or not. It only works for convex shapes tho...

    • @javidx9
      @javidx9  Před 5 lety +4

      That's a nice approach Tomlow, and is probably valid in many situations, may be worth doing a performance comparison in the future!

  • @beron_the_colossus
    @beron_the_colossus Před 5 lety +7

    Was just about to go to sleep but this is better!

  • @rockyrivermushrooms529
    @rockyrivermushrooms529 Před 5 lety +2

    I just found your channel and enjoy these videos. I feel like youll gain lots of subscribers

    • @javidx9
      @javidx9  Před 5 lety +1

      Hi there FireFungi, hey thanks a lot buddy! As long as people enjoy the videos Im happy :D

  • @killereks
    @killereks Před 5 lety +7

    Finally I found a channel that teaches you useful algorithms for making 2D and 3D games while also teaching you new math. I was looking for a channel like this for ages! However I do not code in C++ and will have to learn it.

    • @BlazertronGames
      @BlazertronGames Před 5 lety +1

      I'm sure you could apply it to a different programming langauge.

    • @BlazertronGames
      @BlazertronGames Před 5 lety +1

      Also, if you're going to learn c++, make sure you get a good book. I've heard that free online beginner resources usually teach bad habits/techniques. And don't trust amazon reviews, make sure you research if you're going to buy one.

    • @killereks
      @killereks Před 5 lety

      hey, thanks for the help however i do code in C# and other languages for a long time so i have good understanding of private variables, friendly functions, destructors etc. I even learned how pointers work so I'm pretty sure i could learn c++ pretty quickly but I just don't know how to do graphics in it so I haven't tried yet. Also I don't like learning from books especially programming, I think it's better to go and try to code something or mess around with other people's code. Thanks again.

    • @javidx9
      @javidx9  Před 5 lety +2

      Hey Killereks, nice to have you on board!

    • @mothtolias
      @mothtolias Před 5 lety

      yes, most of what's described here is abstract and could be implemented in just about any language.

  • @felixspeagel5915
    @felixspeagel5915 Před 4 lety +1

    I think there is alternative way of "filling the gaps" than shown at 30:08 . Look, after you sort the list of triangles, you will find that between neighboring ones there will be "gaps in angles" eg.: between triangle A starting at alpha_1 and ending at alpha_2, and triangle B starting at beta_1 and ending at beta_2, there can be no triangle starting at alpha_2 and ending at beta_1. This means you can add a triangle starting at alpha_2 and ending at beta_1, with radius of the maximal radius allowed in your app.
    Of course, you have to take into account that the resulting triangle could have got an angle bigger than 180°, so there should be also some kind of function that will cut that triangle into smaller pieces :).

  • @mypersonalvideos2786
    @mypersonalvideos2786 Před 5 lety +2

    really good video thx

  • @unaigom
    @unaigom Před 5 lety +3

    New subscriber... instant follower!

  • @xeridea
    @xeridea Před 2 lety

    In my game, I am using the fast raycasting from your other video for lighting. Implemented a bit different than here, as raycasting is calculated, it adds light to the tile it is in, which gets reduced as the ray travels on. If a solid tile is hit, light is reduced, rather than blocked, giving a semi transparent property. Ray distance is limited by how far it takes the light to attenuate to zero. It casts 1 ray to each tile of the edge of the effective light reach. The light level at each tile is then moved to a texture (it is actually a shade drawn alpha blended over other tiles...), that is then stretched over the viewing area, making it look very natural. This also means only 1 draw call is used for the entire scene, combined with the efficient algorithm, lighting is nearly free performance wise.

  • @skilz8098
    @skilz8098 Před 4 lety +1

    As for the nearest neighbor algorithm, something that might be a little more efficient would be to use a kernel scheme. You will have to take into an account for and check for world boundary edges and corner cases, however, we can model it after something like this where each bit represents a specific direction being present. Of the 4 bits starting with the MSB being West we would end up with W,N,E,S respectively. If there is a 1 present that direction is set. If you notice there are no values of {5,7,A,B} which makes sense since 5 = 0101 where North and South would be combined. The same for 7 which would be 0111 giving a combination of N, S, & E and A being 1010 as this would result in E & W. None of those cases makes any sense, so those values don't exist in the kernel. So you'll never have opposing directions - bits set simultaneously and you'll never have 3 bits present as there are either only 1 or 2 bits set or none for the location of the object being the center of the kernel. This has good symmetry since the center object always has a value of 0 when you are looking for the 8 adjacent neighbors and is very versatile for many applications!
    NW, N, NE, 1100, 0100, 0110
    W, C, E, = 1000, 0000, 0010
    SW, S, SE 1001, 0001, 0011
    We can create a descriptive enum or class enum for this:
    enum DirKernel {
    CE = 0x00, // Center
    SO = 0x01, // South
    EA = 0x02, // East
    SE = 0x03, // South East
    NO = 0x04, // North
    NE = 0x06, // North East
    WE = 0x08, // West
    SW = 0x09, // South West
    NW = 0x0C // North West
    };
    We can have a const lookup_kernel in two different manners:
    const unsigned lookup[3][3] = { {12, 4, 6 }, {8, 0, 2}, {9, 1, 3}};
    or by having
    const std::array { 12,4,6,8,0,2,9,1,3};
    Now the actual values above are completely arbitrary as one could start with N and have that as either the MSB or LSB and you can go in a CCW or CW direction... just as long as you stay consistent with the bit patterns and making sure that none of them have more than 2 or opposing directions set.
    Now, know that we are only going to have 4 bits that are used for independent directions. This can possibly be simplified even further to make the algorithms even more efficient by using `std::bitset direction`. There are so many possibilities that one can do with this kind of design.
    We can see that the current sell on the world scan here will be at the center of these lookup tables (kernels) and the values will give the bit indicator of a cell's neighboring location and direction. However, the downside to this would be more memory consumption in that each cell would have to its own (x,y) world coordinate and or grid position along with two arrays. Now the arrays are not large in themselves such that one would be an std::array neighbors and the other would be std::array neighbors_exist... As the world is being generated by the clicking of the mouse to either draw or delete such a box, these arrays would need to be updated and since they are arrays, you have const time access, linear time iteration. The math performed to test a single cell with its eight neighbors would be simply bitwise arithmetic and logic. You could even use `std::vector` instead of `array` since we know that these arrays will always be 9 in size and never change if preferred.
    With modern c++ we might be able to get away with a simple for loop...
    for (auto& c : cells) {
    c.check_neighbors()
    }
    And we can use the kernels within check_neighbors method... which could theoretically reduce a lot of the multiplication and nested if loops... You don't have to specifically have an if west, then if north, then if south, etc... Just loop through the grid or vector of cells and check each cell's neighbor's vector and apply the kernel to it to generate the vector of bools. The nice thing about this with the kernel is the symmetry. The current cell will always be in the center of the kernel as it will always have 4 cells before it and 4 cells after it and it will always have a value of 0. In the enum it doesn't look or appear this way as they are listed numerically in ascending order, however, they would be in the kernel vector of values as seen above in the lookup tables. {NW, N, NE, W , C, E, SW,S,SE}... Also what is nice is if you need to get a particular neighbor's existing record you can query into that vector anytime and retrieve a dir_kern::NW which is very descriptive instead of hardcoded or defined.
    Don't get me wrong; I truly enjoy watching your videos, and I learn quite a bit. I like seeing other people's perspectives, their train of thoughts, their problem-solving abilities, and their algorithm building or designing techniques, but I also like to give possible insight on specific topics. Yes, there might be a slightly larger memory footprint but the speeds of the calculations would reduce bottlenecks. Besides that, the extra memory can be stored in a hashmap if needed for future use and fast lookup time. You will see many tricks like this when performing specific lighting and shading techniques within DirectX, OpenGL - SpirV shaders to perform complex calculations very efficiently. Create a mask and a kernel saves a bunch of computational times which is great when you begin to work with large 3D scenes with lots of objects and multiple lights. It's a good technique for ray, light, and shadow casting, applying filters, etc. You can see them here a little bit: learnopengl.com/Advanced-Lighting/SSAO and you can see them in some of these www.rastertek.com/tutindex.html.
    Not only this, but this kernel setup also works very well with a physics engine for collision detections, pathfinding, AI algorithms, A* search, and more. It can even be used in 3D Audio Rendering for directional sound displacement! It can be used in animations and even particle engines!
    Outside of this, I did like how you converted the intersection lines into triangle fans to reduce the number of rays that are being cast out, yet it will still cover a full 360-degree rotation field of view. I also liked your pre/post-processing - deferred rendering and only drawing when something is present or updated. Excellent optimizations!

  • @VictorQianYT
    @VictorQianYT Před 4 lety +2

    Thank you! Thank you! Thank you! Thank you! Thank you!

  • @up4life108
    @up4life108 Před 5 lety +7

    thank you for this video. ive been trying to come up with my own way of casting 2d shadows a while ago and failed which then stopped me from actually making the game i was gonna build

    • @javidx9
      @javidx9  Před 5 lety +4

      Oh dang Up4Life, I hate it when stuff like that happens. Perhaps now its time to dig up those idea and give it another go?

    • @up4life108
      @up4life108 Před 5 lety +2

      it definetly is! Thank you for your respond, i will give it another try now!

  • @francescopiazza4882
    @francescopiazza4882 Před 4 lety

    Very nice !

  • @focuseletronica
    @focuseletronica Před 5 lety +2

    Fantastic!!

  • @erichenriquez3641
    @erichenriquez3641 Před 4 lety +1

    Love your video, I was Playing with P5.js library and wanted to create some RayCasting, but my aproach was very bad, your algorithm is far better, will try to use it and write it in JS. Nice Content, new Subscriber here.
    Im not a native english speaker so sorry if I made some mistakes.

    • @javidx9
      @javidx9  Před 4 lety +1

      Glad it helped! Thanks Eric!

  • @James-uw4it
    @James-uw4it Před 5 lety +2

    Normally I don't comment, but your videos are really great

    • @javidx9
      @javidx9  Před 5 lety +1

      Manuel, I really appreciate that, thanks buddy.

  • @amankaushik5833
    @amankaushik5833 Před 4 lety

    this is awesome!!

  • @TomershalevMan
    @TomershalevMan Před 4 lety +1

    great content.
    I believe the co linearity test you perform is only good for axis aligned co linearity, therefore mis detecting other tilted cases

  • @jsflood
    @jsflood Před 5 lety +2

    Excellent! :-D

  • @AndreasEDM
    @AndreasEDM Před 5 lety

    best channel.

  • @cvabds
    @cvabds Před 5 lety +2

    you rock! Um abraço brasileiro!

    • @javidx9
      @javidx9  Před 5 lety +1

      Aww, thanks cvabds!

  • @SketchpunkLabs
    @SketchpunkLabs Před 5 lety +2

    great job. i likey

  • @JoshRosario310
    @JoshRosario310 Před 3 lety

    The most visually satisfying first 1:30

  • @patrickelliott2169
    @patrickelliott2169 Před 3 lety +1

    Hmm. Sprite is a neat trick, but.... For some game engines you might want to have the "character" with different light sources, or even variable ones, so you need, instead, a "falloff" effect, and.. it maybe possible to create a "fake boundary", based on that falloff (which could be useful in some cases). At least, the case I would use this for would need this, in any case.

  • @PierreSoubourou
    @PierreSoubourou Před 3 lety

    Thanks for showing quite large text (for those like me watching on a phone)

  • @nixonian1
    @nixonian1 Před 5 lety +2

    If you go to new Project/Windows Desktop Wizard/Applicationtype:Console Application you can create a truly blank console app without the precompiled headers. Just uncheck those and check empty project.

    • @javidx9
      @javidx9  Před 5 lety

      lol, Im fairly certain youve told me this before XD - I'm just so set in my ways - It's like its built in "delete auto generated stuff"

  • @marcusotter
    @marcusotter Před 5 lety +4

    "This is the very latest version of Visual Studio"
    *Yellow update flag in the top right corner*
    Loved the vid!

  • @bovineox1111
    @bovineox1111 Před 2 lety +2

    I love the mood that line of sight brings to a game. I was just looking to see if there was some other way. My own Unity based solution does some math on a shadow structure to cast the shadow onto a render texture achieving much the same result but using this in my shaders everywhere to pick visibility based on the LOS. It's pretty good but I was wondering about system that can be more efficient, less gpu\memory bound.

  • @heylittleguy26
    @heylittleguy26 Před 3 lety +1

    1:28 I know this means nothing, but this is the exact frame used in the thumbnail.

  • @guacamolen
    @guacamolen Před 5 lety +2

    Great video--you always put out amazing stuff. Do you have any pointers on limited line of sight, such as a flashlight with a limited angle or a softer light that doesn't extend to the edges of the screen? Or at least where to begin my research into the topic?

    • @javidx9
      @javidx9  Před 5 lety +2

      Hi Micheal, thanks buddy. yes you can do both by adding fake edges in the shape you need around your light source before casting.

  • @jimboli9400
    @jimboli9400 Před 4 lety +41

    javidx9: **posts detailed step by step raycasting engine** **gets 34K views**
    Random dude: **posts 'How to ddos' free python script** **gets millions of views**
    CZcams this isnt fair :(

  • @botteu
    @botteu Před 2 lety +1

    Great video ✨ I found this while trying to find out if it’s possible to do ray casting against arbitrarily shaped pixel formations. Like the terrain in Worms for example or if I were to draw a bit of terrain in a bitmap editing software where transparent pixels mean “pass through” and black pixels mean “solid”. Is it possible then to cast rays and find out what the normals of the terrain is at different points? I can figure out how to do it with a polygon etc but not with an arbitrary pixel shape. Maybe I could use something like marching squares to make a polygon out of the pixels?

  • @jackpret4547
    @jackpret4547 Před 5 lety +2

    Neat!

  • @phynx-victumterra652
    @phynx-victumterra652 Před 4 lety +1

    I use a rather crude version of this in my game and would upgrade to something closer to this however I need to optimize it quite a bit, one of the reference articles talk about using a sweeping ray to detect the closest line segments and only use those segments for the rest of the algorithm. I need to figure out to implement that optimization but not quite sure where to start, any ideas?

  • @cheako91155
    @cheako91155 Před 5 lety

    For edge_id[] and edge_exists[] you could use struct(s) instead and if u ever need to index them u can just use a pointer. Also i think these could be one uint where edge id 0 is no edge.

  • @DamienFromPoison
    @DamienFromPoison Před 5 lety +2

    Hey there, great video as always. I am just wondering, would this stuff not be a bit easier, if you use the points of the lines facing the player and then cast rays from these points to form a quad. Like in the old 3D shadow volume stuff? No sorting or line intersection would be needed and visually it should look the same. So you do not draw triangles for the visible area, you would obstruct the hidden space. But anyways, big fan :)

    • @javidx9
      @javidx9  Před 5 lety

      Hi Damien, Its a perfectly valid approach. The only downside I can see is the redundancy in drawing multiple "shadow" quads that may be redundant (say an edge lies within the shadow of another edge) - certainly worth experimenting with anyway.

  • @cheako91155
    @cheako91155 Před 5 lety

    For types used to index arrays in C u should use size_t instead of int... For i, n, s, w, e in convert tile map to poly map.

  • @Thoracius
    @Thoracius Před 4 lety

    Is casting three slightly dispersed rays at eat intersection point more efficient than checking for each intersecting point whether the opposite points of both segments lie on the same side of the ray? (if so, then you know the triangle on the opposite side extends past the point.)

  • @kalejaneth2457
    @kalejaneth2457 Před 3 lety +1

    When you copy-and-pasted the code to make similar blocks of code just after 23:44 , would it be better to "abstract it away" with a function with some parameters, or would the function possibly be harder to read/understand, or harder to implement, or even perform worse than the direct but long approach?

  • @kpopalitfonzelitaclide2147

    you need more subs how have you only got 16k I could see you having 1 mill subs

    • @javidx9
      @javidx9  Před 5 lety

      Lol thanks Homie! I appreciate that. I guess ive never really pushed the channel anywhere!

  • @davep7176
    @davep7176 Před rokem

    If you can combine this effect with your Orthographic Dungeon code, this would be crazy

  • @conandoyle1859
    @conandoyle1859 Před 5 lety +3

    я не знаю английский язык, но мне интересно смотреть твои видео!

  • @sirknight1291
    @sirknight1291 Před 5 lety +1

    Instead of iterating through the whole array you can just check the newest block when its added. That should increase the performance in bigger maps.

    • @javidx9
      @javidx9  Před 5 lety

      Absolutely Sir Knight, in fact there are many additional optimizations one can perform here. Block removal gets a bit more complex though!

  • @TheVersatilefungus
    @TheVersatilefungus Před 5 lety +2

    Hey Javid, thanks for another great video. Is there any chance of doing a tutorial video on voxel game design at some point?

    • @javidx9
      @javidx9  Před 5 lety +1

      Hi Versatil Fungus and thanks! Im not interested in doing voxels the expected/conventional(?) way (i.e. yet another minecraft clone) so if I can think of a more interesting vehicle to demonstrate them then I will. After all, voxels have been around waaaay longer than minecraft so it shouldn't be too difficult ;)

    • @TheVersatilefungus
      @TheVersatilefungus Před 5 lety +1

      @@javidx9 I wasn't thinking so much like a Minecraft clone. More in a sense to make destructible objects with physics. Something similar to this perhaps: czcams.com/video/TvKhYH9RDHg/video.html

  • @prajjwalpathak2718
    @prajjwalpathak2718 Před 3 lety +1

    Hello sir, I was following your tutorial and implementing it in python, but got stuck at the point where you sorted the vector. I wanted to know how was it sorted ( I don't know C++ ), as t1 and t2 are float values while vecVisibilityPolygonPoints is a vector or list in python

  • @beyondcatastrophe_
    @beyondcatastrophe_ Před 5 lety +3

    To improve the raycasting, can't you create a new list with only the edges in a certain radius. That should make calculations faster (at least for larger maps)...

    • @javidx9
      @javidx9  Před 5 lety

      You are correct! There are several ways to optimise this, but certainly this is one of the simpler ones. Checking if points are within a radius can be fiddly to though (obviously the check is simple, but you dont want to have to check all points)

  • @callumjohn6622
    @callumjohn6622 Před 4 lety

    The sudden change in audio quality and volume made me audibly jump.

  • @chris_burrows
    @chris_burrows Před 3 lety +1

    Does anybody understand why the ray sweep optimization (not covered here) is faster and how it works???

  • @jakubsebek
    @jakubsebek Před 5 lety +3

    Can you please explain why is it faster when you set the realease mode?

    • @javidx9
      @javidx9  Před 5 lety +2

      Hi Jacob, in debug mode, the compiler adds all sorts of additional code behind the scenes so you can debug your program, i.e. hooks and variable information that the debugger can display. Other things it checks for is going out of bounds on arrays, stuff like that. In release mode, it will not do any of these things, so its much higher performance

    • @jakubsebek
      @jakubsebek Před 5 lety

      @@javidx9 thx

  • @user-kf1xn1dq9t
    @user-kf1xn1dq9t Před 3 lety

    > In this (long, sorry) video ...
    It explained everything clearly, so its okay.

  • @unveil7762
    @unveil7762 Před rokem

    can you just store the postion of each box... than check if there are shared edge, if yes delete them and sum the rest by checking the y or the x position so you know where to connect them ... if no share make all four bounds...

  • @dstri1
    @dstri1 Před 4 lety

    Hello, i love the way you are vast in your knowledge. I admire you... And also wants to learn. Please can you recommend good books in graphics physics and maths that are relevant to software development.

  • @androth1502
    @androth1502 Před 5 lety

    great demo. just wanted to point out that the mouse coordinates don't work for the Y axis using 2x2 pixel width/height on my laptop. it works using 1x1 pixels. this is likely due to the odd wide screen format of my display which is 1366x768. it works normally on another pc with a more standard display mode.
    i suspect it's due to a loss of precision in converting from float to int because a quick test showed me that 2.9 is being rounded down to 2 instead of rounding to 3 and we're off by an entire row.

    • @javidx9
      @javidx9  Před 5 lety

      Hi androth, rounding down in this instance is actually desirable. The mouse coordinates become inaccurate if you resize the window, or use a window manager that resizes it for you. Are you running on linux?

    • @androth1502
      @androth1502 Před 5 lety

      @@javidx9 i'm running windows 10 on both systems. the same code works on my desktop, but not on my laptop. the only thing different between these two is the display resolution.
      i'm trying to figure out why i'm losing more rows precision the further down i click in the window, and why if i switch to 1x1 pixels, the problem goes away. sorry, i'm coming from C#/WPF. haven't looked at C++ in over 10 years :)
      edit: i think it's because windows is resizing my window. the program needs 800 pixel screen height and my laptop maxes out at 768. i figured windows would just create the full size window and allow it to extend beyond the screen.

    • @javidx9
      @javidx9  Před 5 lety

      What size window are you creating?

    • @androth1502
      @androth1502 Před 5 lety

      @@javidx9 same as in your program, 640x400, but with 2x2 pixels, it's really 1280x800. i think that is the issue, it just didn't register in my brain. i guess i'm getting slow in my old age.
      anyway, i appreciate what you're doing here. i made something very similar (win32/linux gui library) in assembly language over a decade ago, but never took it beyond demo.

    • @javidx9
      @javidx9  Před 5 lety

      Lol its ok androth. Sometimes just need a fresh pair of eyes! I currently dont support normalised coordinates so resizing the screen introduces mouse weirdness.

  • @ianmarshall3310
    @ianmarshall3310 Před 5 lety +1

    Can you explain faster raycasting with tan and grid intersection in the next video?

    • @javidx9
      @javidx9  Před 5 lety

      Hi Ian, Funnily enough Ive been thinking about this in the context of the First Person Shooter video - I'll see what I can do for the next one.

  • @beaconofwierd1883
    @beaconofwierd1883 Před 2 lety

    Thought this video would talk about how to optimize which points to cast from when some are shadowed by other points :(

  • @EvilStreaks
    @EvilStreaks Před 4 lety +1

    I watched this, looked at that site, and made my own version. It appears to behave perfectly but casting shadows at certain angles causes a glitch where ther radial order of the vertices changes. No clue what I'm doing wrong...

    • @EvilStreaks
      @EvilStreaks Před 4 lety

      Fixed. The coordinates of the vertices had to be floats

  • @mrferret980
    @mrferret980 Před 5 lety +1

    which video is "here" where you explained the edge grouping algorithm? XD

  • @fredg8328
    @fredg8328 Před 5 lety +1

    nice

    • @javidx9
      @javidx9  Před 5 lety

      Cheers Fred!

    • @fredg8328
      @fredg8328 Před 5 lety

      I never wrote a complete code to do this, but a wrote an article on my website to answer a question of somebody on FB some time ago, that was talking about raycasting and shadow polygons. fredericgoset.ovh/informatique/divers/en/raycast.html

  • @robertboran6234
    @robertboran6234 Před 5 lety +15

    There is nothing better then wake up an feed the brain.

    • @javidx9
      @javidx9  Před 5 lety +1

      I agree entirely Robert XD

  • @davidkozak9959
    @davidkozak9959 Před 2 lety

    How would the mechanics change if the edge geometry was planometrically isometric circular bodies w circular edges (or cylindrical if 3D) instead of square? E.G. Assume each cubed grid point embodied a scattered set of circular bodies contained with each isometric grid point...

  • @stonekase
    @stonekase Před 5 lety +2

    @javidx9 don't you think this "int w = (y + sy) * pitch + (x + sx - 1); // Western Neighbour" has a potential to cause confusion for newbies like me as there is an argument with the same name "w" in the same function. I know it is local but considering someone trying this tutorial out for the first time.
    A GREAT TUTORIAL TO BE HONEST

    • @javidx9
      @javidx9  Před 5 lety

      Hey Akawo, you are correct in this regard - a bit lazy of me to not use a different name, to be fair, I just didnt notice :D

  • @Bunny99s
    @Bunny99s Před 4 lety

    I have never used or looked into your PixelEngine but doesn't it support texture mapping when drawing the triangle fan? If it does, showing the sprite would be just a matter of assigning proper texture coordinates to each vertex. At least that would be my first approach -.-

  • @haudiweg
    @haudiweg Před 4 lety +1

    I have programmed a game i have a invertet version from your code and without rays(but im not finished)

  • @flobuilds
    @flobuilds Před 5 lety +2

    Can you extend this project and add some antialiasing? That would be so awesome to see what the windows console can realy do and it would look so much better with that smoothing effect.

    • @javidx9
      @javidx9  Před 5 lety +2

      Hi Bluestroker, just to avoid confusion, this is not rendering using a windows command prompt anymore (though the code could be ported to ConsoleGameEngine quite easily). I hope to do a video about various image processing techniques soon - I have already experimented with blurring algorithms for soft shadows and they look awesome.

    • @flobuilds
      @flobuilds Před 5 lety

      @@javidx9 ok that's awesome. I love this c++ code

  • @Cinicraft00
    @Cinicraft00 Před 4 lety

    This is what was used in Diablo 2

  • @NonExistChannel
    @NonExistChannel Před 5 lety +1

    There is a much simpler way doing the same thing and at the same time achieving global illumination: A 2D-Pathtracer

  • @williankoessler5935
    @williankoessler5935 Před 5 lety +1

    PGE

  • @OddStare
    @OddStare Před 4 lety +1

    I would like to do this in 3d but how do you keep a pool of corners in 3d ?

    • @javidx9
      @javidx9  Před 4 lety

      It would just be the 3d vertex no?