Particle Collisions - prevent particles from going through walls! And easily create particle collisions!

Source Code
---  BIack1st
local module = {}
local Blacklisted = {}
local CollisionsService = game:GetService("PhysicsService")
function module.new(StarterPart:Part, Particles:ParticleEmitter)
	local function CheckGroups(Group1:string, Group2:string)
		
	return CollisionsService:CollisionGroupsAreCollidable(Group1, Group2)
	end
	local GroupId = StarterPart.CollisionGroupId
	local GroupName = CollisionsService:GetCollisionGroupName(GroupId)
	local function GetDirection(Part:BasePart, Particles:ParticleEmitter)
		local ParticleDirection = Particles.EmissionDirection
		if ParticleDirection == Enum.NormalId.Top then
			return Part.CFrame.UpVector
		elseif ParticleDirection == Enum.NormalId.Back then
			return -Part.CFrame.LookVector
		elseif ParticleDirection == Enum.NormalId.Front then
			return Part.CFrame.LookVector
		elseif ParticleDirection == Enum.NormalId.Bottom then
			return -Part.CFrame.UpVector
		elseif ParticleDirection == Enum.NormalId.Left then
			return -Part.CFrame.RightVector
		elseif ParticleDirection == Enum.NormalId.Right then
			return Part.CFrame.RightVector



		end
	end
	
	
		
		local Direction = GetDirection(StarterPart, Particles)
		local ResultRay = workspace:Raycast(StarterPart.Position, Direction * 5000)

		if ResultRay then
		local RayInstance = ResultRay.Instance
		

				local Pos = ResultRay.Position

		Particles.Lifetime = NumberRange.new((StarterPart.Position - Pos).Magnitude * (5.3 / Particles.Speed.Max / 2  ) / 2.75 )
	else

		Particles.Lifetime = NumberRange.new(math.huge)
		
			end
	
			
	
		

		


end
return module

Download: Particle Collisions - Roblox

Hello, so yesterday I made this post. I decided that I’m going to make what I made into a module for anyone’s use! This module allows you to create particle collisions easily. So no more particles going through walls! This module supports, emission direction, speed, and rotation! Heres how it looks in action!


For Parts, If you want to use this with an attachment then look below

First of all, make sure you have the module installed. If you don’t have it, then download it here.
Once you’ve downloaded it. Go to toolbox → inventory and insert the module into replicated stroage.

If you already have a particle emitter then you can skip this part. After that, insert a particle emitter into your desired part. Customize however much you like.

Once you’re done customizing your particles. Insert a script into your part and type this. Make sure to edit the script to make it work in your situation.

local Module = require(game.ReplicatedStorage.ParticleCollisions)
	Module.new(script.Parent, script.Parent.ParticleEmitter)

Making it repeat
Well, we finished doing the steps. The only issue is. It’ll calculate once. Which is a huge issue. Because if the part moves then it’ll give us in-accurate results. To fix this do:

game:GetService('RunService').Heartbeat:Connect(function()
	local Module = require(game.ReplicatedStorage.ParticleCollisions)
	Module.new(script.Parent, script.Parent.ParticleEmitter)
end)

Attachments
First of all, make sure you have the module installed. If you don’t have it, then download it here.

Once you’ve downloaded it. Go to toolbox → inventory and insert the module into replicated storage.

If you already have am attachment and particles then you can skip this part. After that, insert an attachment into your desired part. Then, insert a particle emitter into the attachment. Customize it to your liking.

After you finished customizing the particles, insert a script into the attachment. Once you’ve done that, type this into your script. Edit it to work in your situation.

game:GetService('RunService').Heartbeat:Connect(function()
	local Module = require(game.ReplicatedStorage.ParticleCollisions)
	Module.FromAttactment(script.Parent, script.Parent.ParticleEmitter)
end)

I hope this module will help you! If you find any bugs, have a suggestion, or have feedback then feel free to say below! I’m open to any criticism!

88 Likes

The module is epic but i checked inside it and there seems to be unused code…

	local function CheckGroups(Group1:string, Group2:string)
		return CollisionsService:CollisionGroupsAreCollidable(Group1, Group2)
	end
	
	local GroupId = StarterPart.CollisionGroupId
	local GroupName = CollisionsService:GetCollisionGroupName(GroupId)

maybe make sure to remove or put that into a use
except that its a really nice module

2 Likes

Thank you! That function is there for a future update.

now I can make particles with shields properly, thanks!

1 Like

No problem! Glad I could help!

1 Like

I have a question if you don’t mind me asking, does this work with attachments or will that be implemented later?

I don’t know why I didn’t think of that. I’ll try my best to get a version that works for attachments later today, just for you!

Thank you very much! I was originally thinking you had because particle emitters only work with parts and attachments.

1 Like

Version 1.1, Support for attachments!
Thanks to @rottendogDkR for suggesting this update!
New version yay! In this version we’re introducing support for attachments! This is done using the .FromAttactment function! Without further ado, lets get right into it!
image

First, install the module here if you dont have it already.

After you installed it, go to toolbox → inventory and insert the module. Then move it into replicated storage.
You can skip this part if you have an attachment and particles set up already. Next, insert a attachment into a part, then insert a particle emitter inside of the attachment. Customize it however much you’d like.

Once you’ve set up your attachment, insert a script into the attachment and do this:

game:GetService('RunService').Heartbeat:Connect(function()
	local Module = require(game.ReplicatedStorage.ParticleCollisions)
	Module.FromAttactment(script.Parent, script.Parent.ParticleEmitter)
end)

image

And thats it for the update! Hope this could help to anyone who uses attachments! If you find any bugs, or have a suggestion, or have feedback then feel free to say below! I’m open to criticism!

2 Likes

Well, theres now attachment support! Once again, thanks for the suggestion!

1 Like

Didn’t really take that long, pretty good module do you know when other updates will be released that will be useful for many people?

Thank you! I honestly don’t know when more updates will be released. Either if someone points out a flaw, or they have a suggestion. Another time I may update is if roblox add some stuff to the particle api. Which is unlikely

Speaking of flaws your module is ineffective when having acceleration on the particles because of how you cast your rays.

1 Like

Yeah… didn’t notice that. I’ll try to get a fix to acceleration soon. I just need to know how roblox calculates it.

1 Like

Im guessing you do some checks if their is acceleration and modify the direction with the acceleration, you can spilt the raycast into multiple raycast to perform a check by continuously following the acceleration until the max distance.

1 Like

Thank you, i’ll look into this further. I’ll let you know if i find a way to add acceleration support.

This module is pretty cool, though most likely Roblox will add particle collisions, since they’ve been doing some improvements to the particle system, and other systems recently.


To be honest, the source code looks pretty bad. The formatting is weird, there are massive gaps of just whitespace, and some places need newlines to make the code look more readable. For personal projects, readable and good looking code is not important, but for open source community resources, having presentable code can make it easier for other people modify it, although good looking code is purely subjective, some changes have to be made.

I’ve made a few changes to the source code to better align with how my code looks, which is just my opinion, write code however you want, just make it readable to everyone.

--// BIack1st

local ParticleColl = {  }
local Blacklisted = {  }

local PhysicsS = game:GetService("PhysicsService")


function ParticleColl.new(starterPart: Part, particles: ParticleEmitter)
	local function checkGroups(group1: string, group2: string)
		return PhysicsS:CollisionGroupsAreCollidable(group1, group2)
	end
	
	local groupId = starterPart.CollisionGroupId
	local groupName = PhysicsS:GetCollisionGroupName(groupId)
	
	local function getDir(part: BasePart, particles: ParticleEmitter)
		local ParticleDirection = particles.EmissionDirection
		
		if ParticleDirection == Enum.NormalId.Top then
			return part.CFrame.UpVector
		elseif ParticleDirection == Enum.NormalId.Back then
			return -part.CFrame.LookVector
		elseif ParticleDirection == Enum.NormalId.Front then
			return part.CFrame.LookVector
		elseif ParticleDirection == Enum.NormalId.Bottom then
			return -part.CFrame.UpVector
		elseif ParticleDirection == Enum.NormalId.Left then
			return -part.CFrame.RightVector
		elseif ParticleDirection == Enum.NormalId.Right then
			return part.CFrame.RightVector
		end
	end

	local dir = getDir(starterPart, particles)
	local result = workspace:Raycast(starterPart.Position, dir * 5000)

	if result then
		local rayInst = result.Instance

		local pos = result.Position

		particles.Lifetime = NumberRange.new((starterPart.Position - pos).Magnitude * (5.3 / particles.Speed.Max / 2) / 2.75)
	else
		particles.Lifetime = NumberRange.new(math.huge)
	end
end

return ParticleColl

My version was made for fun, you don’t need to use it.

Here are a few things with your code that should be improved:

  1. Big gaps of whitespace, e.g. 49 - 56.
  2. Parts of the code is super clustered together, with nearly no whitespace added to make it look nicer, e.g. 1 - 13
  3. The array with all the methods is named “module”, not something that tells the reader what it is.
  4. (Subjective-ish) Capitalized variable / parameter names. Most programmers (including myself) use capitalized names for important data, like constants, module arrays, etc. etc.
1 Like

Thank you. I’ll take this into consideration next time im coding something.

1 Like

Once Roblox finishes the particle emitter shapes system, do you think it’ll still work with your current module?

More about the new particle emitter shapes: New Procedural Particle Emitter Shapes


I only optimize and make my code readable when I’m either done with it, or when I make something open source. It doesn’t matter how good your code looks if you’re the only person looking at it.

1 Like

I really like the particle shapes. I do plan on adding it. I just need to find out the right formula to make it have collisions.

1 Like