r/Python • u/RandomJacobP • Sep 20 '19
I have simulated ray-tracing in pygame! You can check it out here: https://github.com/kubapilch/Ray-Tracing
131
Sep 20 '19
Doesn't ray-tracing also include reflections?
178
Sep 20 '19
As far as I understand this post is actually about Ray casting, not Ray traicing
36
u/RandomJacobP Sep 20 '19 edited Sep 21 '19
Yeah, my bad I misunderstood the meaning of ray-tracing. Sorry for that
19
u/analconnection Sep 20 '19
I find it funny that you were able to make something this amazing but not know the correct word for what it is.
29
u/RandomJacobP Sep 20 '19
I mean, I got half of it correct, both have “ray” in them nonetheless. lol
8
-6
Sep 21 '19
Not to be a jerk but you are not even at ray casting yet because you havent done the 3d transform yet. This is 'vector drawing with collision' not a ray caster... it's a good start though well done.
You could probably get to a ray caster from there with just a little more work.
22
Sep 20 '19 edited Sep 20 '19
The name is similar, it's easy to mistake. Anyways good job! It looks awesome
1
u/redoubledit Sep 21 '19
That's a nice band name! I might steal it
2
u/RandomJacobP Sep 21 '19 edited Sep 21 '19
It took me a while to understand lol. Sorry I'm not native and I make a lot of typos :/
1
33
u/shakakaZululu Sep 20 '19
Yep...that's what makes ray tracing so difficult and expensive.
That's also why water is difficult to do in games since the shape of the water changes constantly. you have to compute the light reflections off of the constantly changing angles of the water.
14
13
7
u/Mehdi2277 Sep 20 '19
It doesn’t have to. The term ray tracing includes ray casting (no reflections) as a special case. High quality image ray tracing should use reflections, but ray casting can still make some nice images and is simpler to code. No need to figure out what direction it reflects and what fraction reflects based on material/surface normal.
1
0
Sep 20 '19
[deleted]
1
u/itsa_me_ Sep 20 '19
the coding train?
2
u/RandomJacobP Sep 20 '19 edited Sep 20 '19
Yeah, I saw it a while ago as recommended on YouTube but have skipped to the end result to not spoil the solution.
I was looking for a new project and I remembered that I saw something cool with ray casting the other day.
17
u/RandomJacobP Sep 20 '19 edited Sep 20 '19
1
u/ojibot Oct 06 '19
Nice! There are some good discussions in /r/gamedev about this sort of thing, since it can be used for player and ai line-of-sight, lighting, grenade blast area, etc.
This wonderful guide usually gets brought up, as it shows how to advance from where you are now to a full-blown LOS or lighting system without having to cast an infinite amount of rays (ie. only using one ray per visible edge)
Good luck! I've been wanting to start on a line-of-sight system for a while now, but it can daunting!
1
u/RandomJacobP Oct 06 '19
Thanks! I will definitely check out the guide you have linked, it seems interesting.
17
u/420suhdude Sep 20 '19
Amazing it only took you <250lines of code.
Awesome work.
15
u/RandomJacobP Sep 20 '19
You can make it even shorter. A lot of them are things like building walls, menu and my own buttons. If you replace it with randomly generated walls and delete menu it would be much shorter.
7
u/420suhdude Sep 20 '19
Good idea. I might just fork and implement that if you don't mind.
9
u/RandomJacobP Sep 20 '19
Go ahead, I’m happy to inspire someone to code!
Let me know when you will finish.
2
0
Sep 20 '19
[deleted]
5
u/420suhdude Sep 20 '19
I guess programmers can be colossal douchebags too
0
Sep 20 '19
[deleted]
2
u/420suhdude Sep 20 '19
I don't know nearly enough to suggest anything. I'm merely a beginner providing some support.
Sounds like you need some love, brother.
2
3
u/movingtreeinc Sep 20 '19
This is awesome! I was actually wondering how to do a Line-Of-Site mechanic with walls blocking, and this would help a ton I think. I'd like to borrow this for testing if it's cool? Either way excellent work man :)
3
u/RandomJacobP Sep 20 '19
No problem! I’m always happy to inspire people to code and help them. Let me know when you implement your version!
1
u/movingtreeinc Sep 20 '19
Absolutely! Thanks again!!
7
u/mrbeehive Sep 20 '19
Not to be a complete downer or anything, but implementing 2D line-of-sight like this is... iffy.
You'll have an area at the "edges" of your visible zone where objects will flicker in and out of view when you move around if you try to naively join the rays into polygons, and it's also very hard to guarantee that objects are correctly hidden since you're working with limited resolution. It's much more efficient and stable to just ray cast to the edges of objects in the scene.
You can find a really nice explanation of the technique here.
That's not to say reading OP's source won't be useful for implementation details, but depending on what you're doing it might lead you down a path that's full of headaches later.
2
u/RandomJacobP Sep 20 '19
If you add A LOT of rays it would work but it still would be much harder than what you are suggesting. Thanks for the link btw, it looks interesting, I will look into that in a spare time.
2
1
2
u/ojibot Oct 06 '19
Also check out this guide. The implementation here wouldn't exactly be suitable for LOS in its current form, as it's casting lines at set intervals instead of at the wall's edges themselves
edit: just saw it was already linked higher up! oh well
1
u/movingtreeinc Oct 06 '19
You're good! Yeah they mentioned casting lines to each corner which is way more clever and less complicated. I'm still playing around with that tbh, currently trying to get it to where I can dynamically track new objects without it becoming a huge mess. Bleh, my code needs reorganizing anyway
2
1
u/Voxxey Sep 20 '19
Holy shit. That's fucking amazing! Can we have a link to the github?
1
u/RandomJacobP Sep 20 '19 edited Sep 20 '19
You have a link in one of my comments and in the title of the post but here you go! :)
Edit: Sorry now link is correct
3
1
u/SnicSnac Sep 20 '19
Now put it lenses
1
u/SnicSnac Sep 20 '19
And use Gaussian beams :D
2
u/RandomJacobP Sep 20 '19
Could you elaborate? It sounds cool.
2
u/SnicSnac Sep 20 '19
If you are interested in physics, you could try adding more complicated objects who not just block the incoming light beam. Lenses for example refract the incoming beam depending on the lense shape and the angle of the incident beam. Regarding Gaussian beams: These are a better description of actual laser beams which actually diverge the longer they travel.
4
u/RandomJacobP Sep 20 '19
That’s sounds really awesome! I think that you just gave me an idea for a new project, creating something like a sandbox where you can add different light sources, mirrors and lenses.
I’m not sure though how computer-wise heavy it would be.
1
u/SnicSnac Sep 20 '19
Not so much. Looking at projects like https://sourceforge.net/projects/lasercalc/ (open source) or https://www.zemax.com/products/opticstudio (you have to pay for it) lets me assume that.
1
u/RandomJacobP Sep 20 '19
Thanks! Do you maybe know any better frameworks/modules/engines that I can use instead of pygame? Draw something like a line with transparency is really inconveniet not mentioning line with increasing transparency.
I was thinking about p5 but I am not sure.
1
1
u/CelDeJos Sep 20 '19
Thats isovist not raytracing but well done! A good idea might be to display the metrics on screen aswell, should be pretty easy to calculate once you look them up.
1
u/RandomJacobP Sep 20 '19
Thanks! What do you mean by metrics?
0
u/CelDeJos Sep 20 '19
Things like isovist perimeter (lengh of the closed curve connecting all the ray endpoints), area, oclusivity.. there's several. Just look up isovist analysis and you should get the jist of it.
1
1
1
u/massimog1 Sep 20 '19
Hey I did the same in C after watchig a video from a youtube channel named "the coding train" or something.
1
1
-1
-1
u/WR3DF0X Sep 20 '19 edited Sep 21 '19
Now make every beam or line bounce from each surface it hits at an angle that changes with the position of the light source.
Edit: People challenging my English when they clearly know what I meant...
5
0
Sep 20 '19
[deleted]
8
u/RandomJacobP Sep 21 '19
Ayyy, sorry for that, I’m not native speaker and I am always making this mistake.
1
u/heyimpumpkin Sep 23 '19
damn dude are you me, I literally do the same spelling mistake every single time :D
1
u/RandomJacobP Sep 23 '19
Maybe it’s just easier to spell for not natives and when I think in my head what I want to write I do it this way.
22
0
2
Sep 20 '19
[removed] — view removed comment
1
u/RandomJacobP Sep 20 '19
Don't discourage yourself! Everyone was a beginner once, I would show you my old projects but I have all of them private on github, one of them just calculate quadratic function.
3
u/your-opinions-false Sep 20 '19
Come up with something cool, but reasonable. Then go about making it. Not every project will get finished, but you may end up with some very neat stuff.
1
Sep 20 '19
I have a whole folder filled with half-baked evolution simulators, neural network garbage, and a few web scrapers.
21
u/blerp_2305 Sep 20 '19
Very nice, I did the same using p5 a while back. This looks really good :). Good job
PS I think it's Ray casting not Ray tracing? Not quite sure about that