TouchedPlus | A raycasting solution to collision detection

TouchedPlus

Raycast Based Collision Detection For Cuboids

Robloxs Built in Touched & TouchEnded events are sensitive and near unusuable. I haven’t since found a good, fast and performant alternative to this - so decided to make one. Even Zone+ has limitations as it uses Region3 which is more costly than raycasting. I also wanted to offer much more control to the developer to decide the balance on accuracy, speed and performance.

Example GIF

So without further ado, here is “TouchedPlus”, name undecided. It’s a performant, no fuss collision detection module for cuboids and BaseParts:

Accurate Collision Detection for BaseParts, compatible with all primitive objects but made for cuboids.

In The Future I may expand the scope of what this can handle but have no plans as of yet, was more of a fun experiment. If you find any issues do let me know but uh - hope you enjoy!

Examples:

Adds Touched & Touch Ended for one BasePart.
local TouchedPlus = require(game:GetService("ReplicatedStorage").TouchedPlus)
local partDetection = TouchedPlus.new(script.Parent, 10) 
--object, precision, optional [delay] (otherwise automatically determined based on precision to balance performance)

partDetection.Touched:Connect(function(obj)
	print("touched: ", obj.Name)
end)

partDetection.TouchEnded:Connect(function(lastObj)
	print("touch ended on: ", lastObj.Name)
end)
Playground Uncopylocked

https://www.roblox.com/games/6159210678/

*The Above example uses Knit Components for the Coins and references Knit for the Modules. Knit is a framework by @Sleitnick. If you don’t use Knit you can grab the module, dependencies included, below:

https://www.roblox.com/library/6159538577/TouchedPlus

Documentation:

To setup a collision you just create a new class, like so: local variableName = TouchedPlus.new()
It accepts three parameters, with two advised:

  • Object BasePart [Required]
  • Integer Accuracy [Optional]
  • Boolean Dynamic [Optional]
  • Float Speed/Delay [Optional - else automatically calculated]

I’ve tried to mimic robloxs API as closely as possible for this, to detect collisions we have .Touched & .TouchEnded, for cleaning up the class and removing detection :Destroy().

Any questions, drop them below and i’ll do my best :blush:

48 Likes

Have you considered this?


I see, I didn’t know that Zone+ has problems and Interesting that you added that flexibility.

Thank you for sharing your resources @JedDevs


So for someone who is already using Zone+ is it worth the transition to use this Module?

  • If Touched+ is better than Zone+ how can we be sure?
  • If so by how much?
  • Using what as a benchmark?

or Maybe we can’t compare the two like Apples and Apples and it all comes down to preference and faith?

5 Likes

I’d likely put this in the category where the workarounds for Touched aren’t feasable (or you don’t want to bother) or Zone+ doesn’t apply. Whether this be:

  • Detecting Non Characters (Zone+ Limitation)
  • Large Areas (Zone+ Limitation - Performance)
  • Server Detection (Touched Workaround)
  • etc.

Really i’d suggest it instead of your clever workaround for stuff like obbies (for cuboids) just so you don’t have to add a part around the entire player or just rely on the HRP. Stuff like Safe Zones would also apply.

It’s really open ended, just a (hopefully) helpful module for whoever likes it and all under the MIT license so feel free to modify it to your hearts desire.


As it stands i’m not expecting this to be used on mass as it really is a simple module with a limited scope. If this does gain any steam (or I get bored) I may make an implementation that can handle Models, Irregular objects etc. - especially as i’ve already got the maths drawn up.

Some other Open Source modules i’ve made (If you’re interested):
The later is still being actively developed so keep your eyes peeled :eyes:

7 Likes

UPDATE LOG:

I’ve updated the module in response to some feedback (thanks guys :blush:).


Performance nerds: Added optional dynamic parameter which allows static objects with detections more performance as we’re not fetching and updating their positions.

Bug Squashing: Previously, the system would only detect the first .Touched until .TouchEnded was called for that object. It can now handle multiple objects*. And no, not sure how I didn’t notice that bug XD.

Control Freaks: Precision is now optional and will be automatically determined if you decide to take it easy.


*WARNING: With this change you may get multiple .Touched for different player character parts, you’ll have to deal with this yourself - I can help if you drop a comment or have any question!

1 Like

Does this detect sideways contact? Or does it only detect it if the object has entered another object?

It behaves identically to .Touched and .TouchEnded in terms of what you should expect - except more reliably. When two objects intersect it’ll fire .Touched when they stop .TouchEnded.

Hope that answered your question :grinning:

Ahh makes sense, I saw the raycastdown function and instantly jumped to conclusions. It seems like an interesting alternative to .Touched.

1 Like

You can test it out here, the place is also uncopylocked. The place uses a Knit Component for all the coins.

1 Like

Your GitHub repository seems to be missing some ModuleScripts under the main module, specifically the ones you referenced here:

I made a pull request on your repo that fixes this by following Rojo’s file structure:

1 Like

I’ll take a look at that tomorrow, thanks for raising that - as a temporary workaround feel free to use the model asset I linked which should have the dependencies attached.

1 Like

UPDATE LOG:

I’ve updated the module in response to some feedback (thanks guys :blush:).


The Github Repo is now following the format of Rojo thanks to @goldenstein64. Which should make those who use Github (thereby likely rojo) easilier to integrate it into their work.

If you don’t use Rojo you can still pick up the module here:

https://www.roblox.com/library/6159538577/TouchedPlus

The Documentation here and on the github repo are now up to date and reflect the current state of the module.

2 Likes

Thanks so much for this! This was a big help!

1 Like

There must be a blacklist right? Since its a raycast

It’s not publically exposed but indeed. Currently we only blacklist the Object you want collision for:

self.raycastParams = RaycastParams.new()
self.raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
self.raycastParams.FilterDescendantsInstances = {object}

If you have any suggestions please add an issue or PR to the Github Repo

1 Like

How do you apply this module to cars?

I notice this module is extremely inaccurate on the server and not the client

Thank you so much the module works well as an alternative for the Touched event which was causing issues for me.

1 Like

We are throttled on the server to about half the number of checks but this shouldn’t have any real-world affect, is it possible you’re experiencing high network congestion/low-speed leading to noticeable lag?