GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2019-01-20 13:21:28

rez-dash
Member
Registered: 2019-01-20
Posts: 3

Fast, pixel perfect collisions for lighting system

Hello,

First, thanks for the excellent website.

I'm working on a lighting system, and could use some help. My current method for collision detection is using the range_finder script from this site, which works pretty well, but I'm finding it to be too slow and slightly inaccurate for my use case. Can anyone recommend an alternative?

The problem
My lighting system currently works by firing collision detection lines at every four points of every wall in the room. It then finds where the collisions are (either at the wall, or before it when blocked), and saves these points into a grid. I then loop through the grid to draw the shape of the light. I based this on information found here and here.

This works fairly well. However, there are two problems:
- It's too slow. When you have many squares, it needs thousands of collision lines using the range_finder script, and so performance isn't very good.
- It's not quite pixel perfect. The light seems to "vibrate" as the points move when the light does. It looks like the collisions may be out by a pixel or two.

I have uploaded my code, it should be available here. It's a GMS 1.4 project (moving to GMS 2 soon). You can use LMB to move the light or D for the debug view with all the points.

The solution
My lack of mathematics ability is severely slowing me down here, because I don't know a faster way to work out how to check collisions with the walls. I know there are much faster ways of doing this, and the link mentions one of them, but I'm struggling to understand them. I'm keen to start expanding this system with multiple lights and effects, all of which I know roughly how to do, but this is slowing me down.

Does anyone have an idea about a script or some technique that might work here?

Thanks.

Offline

#2 2019-01-21 15:24:04

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: Fast, pixel perfect collisions for lighting system

Yeah, that's going to be very slow. For a ray-casting system like in those links, the big problem is the script does pixel-based collisions instead of analytical ones. If you can describe your walls using geometric shapes, the collision calculations would be much faster and more accurate. For squares or rectangles, something like this might help:

https://www.gmlscripts.com/forums/viewtopic.php?id=2087

That said, if you are serious about lighting, and you are going down the geometric route, I recommend a completely different technique. It makes your GPU do all of the work.

https://github.com/GameMakerDiscord/YAILSE

Although I actually do not have direct experience with this implementation, I developed the system it is based upon. Thanks to massive improvements to GameMaker's rendering abilities, the technique has been simplified and improved. You can read about how it was originally developed here:

https://www.gmlscripts.com/forums/viewtopic.php?id=1574

I also have another very different pixel-based system that Juju improved with a shader implementation:

https://www.gmlscripts.com/forums/viewtopic.php?id=1657


Abusing forum power since 1986.

Offline

#3 2019-01-22 14:32:13

rez-dash
Member
Registered: 2019-01-20
Posts: 3

Re: Fast, pixel perfect collisions for lighting system

xot wrote:

Yeah, that's going to be very slow. For a ray-casting system like in those links, the big problem is the script does pixel-based collisions instead of analytical ones. If you can describe your walls using geometric shapes, the collision calculations would be much faster and more accurate. For squares or rectangles, something like this might help:

https://www.gmlscripts.com/forums/viewtopic.php?id=2087

Oh, that looks really good, thanks. Using this method seems like it would be much better. Although don't you need the exact position of the collision to be returned? Otherwise how would you do objects blocking one another?

xot wrote:

That said, if you are serious about lighting, and you are going down the geometric route, I recommend a completely different technique. It makes your GPU do all of the work.

https://github.com/GameMakerDiscord/YAILSE

Although I actually do not have direct experience with this implementation, I developed the system it is based upon. Thanks to massive improvements to GameMaker's rendering abilities, the technique has been simplified and improved. You can read about how it was originally developed here:

https://www.gmlscripts.com/forums/viewtopic.php?id=1574

I also have another very different pixel-based system that Juju improved with a shader implementation:

https://www.gmlscripts.com/forums/viewtopic.php?id=1657

Both of those look really interesting. I played around with YAILSE a little bit and it's very impressive. I'm a bit hesitant though because I don't fully understand the principle it's based on, so I'm reluctant to build on it - there's not much I could do to edit it.

Could I ask why you recommend those systems over the first one? Are they more performant because the GPU handles everything?

Thanks.

Offline

#4 2019-01-22 22:55:08

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: Fast, pixel perfect collisions for lighting system

If you look at this example you can see how you can get the point of intersection. The script returns true or false, but it can also write the intersection coordinates in an optionally supplied array. There is no reason you couldn't alter it to return the collision point directly if you preferred.

https://www.gmlscripts.com/forums/viewt … 3856#p3856

The reason I recommended YAILSE is because it is very efficient, probably the fastest possible option for 2D lighting and shadows. It requires very little CPU usage since it is mostly just clever GPU camera tricks.

But, I would also encourage you to continue with your own system. I think you'll get a lot of value out of working out your own solution. But if you find your system isn't going to be fast enough, YAILSE could be a good alternative for you.


Abusing forum power since 1986.

Offline

#5 2019-01-26 04:25:21

rez-dash
Member
Registered: 2019-01-20
Posts: 3

Re: Fast, pixel perfect collisions for lighting system

Excellent, thank you for that script, it looks perfect.

I think I'll continue with my system for now, and see how it works out. I've certainly learnt some stuff already.

Thanks.

Offline

Board footer

Powered by FluxBB