How to detect if a projectile is on a flat non sloped/tilted surface

Hello, I’m not sure how to word this so the topic title might sound weird, I’ve been working on a droplet system and wanted a feature where if it lands on walls or sloped objects it’ll drip down and once it is no longer on the surface it drops once again. Right now, the system I made for it doing this is very scuffed. Here’s the code I did.

local Hitbox = RaycastHB.new(Model)
Hitbox.RaycastParams = Params
Hitbox.DetectionMode = RaycastHB.DetectionMode.PartMode
Hitbox.Visualizer = Visualizer
Hitbox:HitStart()

Hitbox.OnHit:Connect(function(hit, blank, raycastResult)
	
	if hit.Orientation == Vector3.new(0,0,0) then
		
		Hitbox:HitStop()
		ResultFound = true
		Model.Anchored = true
		
		module.FinishDroplet(Model, raycastResult, true)
	end
end)

if Model and Result then
	
	task.spawn(function()
		
		while ResultFound == false do
			
			wait()
			
			if Model then
				
				Model.Position += Vector3.new(0, -1, 0)
			else
				return
			end
		end
	end)
end

As I said its very scuffed at the moment. So, what’s the best way to detect if the object the projectile is on is sloped and can slide down the object. Also, what would be the best way of sliding the droplet down in the direction of the slope.

Another question is how I would go by making it slide down things like spheres correctly, without it colliding through it.

Sorry if this is somewhat confusing, I’m not sure what terms to use. Any help or feedback would be appreciated, and here’s a video of what it currently looks like.

And here’s an image to show my goals if I didn’t explain it well enough

You’ll most likely need a custom physics solution. I’m not necessarily the greatest at it but, in my head I imagine you would have the initial velocity paired with acceleration due to gravity. You would then need to cast a ray based on the drops (current - prev) velocity to ensure it doesn’t clip through anything. You’ll utilize the rays normal to calculate the vector aligned to the surface hit so you can adjust the drops current velocity to start going in the slopes direction. Of course, if you detect a splat within a certain range, handle it accordingly so the droplets don’t start sliding on near horizontal planes.

I was assuming I wouldn’t have to make it physics based since I saw another post where someone made a similar droplet system, The droplets slid down in a choppy manor which makes me think he didn’t use physics to do the sliding part. But I could be wrong as I’m not too experienced in coding.

The post i saw: Procedural Water System (technically droplets) - Help and Feedback / Creations Feedback - Developer Forum | Roblox

try using the raycast normal. If the raycast normal is the same as Vector3.new(0,1,0) then it’s a flat surface. Othewise, it’s sloped and you can make it do whatever. Here’s a snippet from my 3d particle system:

local slope = (raycast.Normal - Part:GetAttribute("Speed").Unit).Magnitude
Part.Position = (raycast.Position + ((raycast.Normal * Part.Size.X))) + ((((source:GetAttribute("Acceleration") or acceleration) + (raycast.Normal * (slope^4))).Unit*((source:GetAttribute("AddSlideSpeed") or AddSlideSpeed) + math.clamp(slope - 2,0,3))) * Part:GetAttribute("Speed").Magnitude)
Part:SetAttribute("Speed",Part:GetAttribute("Speed") + ((raycast.Normal * XZplane).Unit * 0.4))

acceleration is just which way it’s going, addslidespeed tells it how much faster to go with more slope, and part is the particle.
edit: XZ plane is Vector3.new(1,0,1)

So first you cast a ray and get its normal vector, then the direction the droplet is sliding downwards will be:
Slide_dir = normal:Cross(Vector.new(0, -1, 0)):Cross(normal)
Then you move the droplet in that direction * speed * time. But on uneven surfaces like a sphere this would place the droplet above the actual surface of the sphere, which would be curving away from it. So you cast another ray from the new position, in the opposite direction of the previous ray’s normal, and then at that position the new ray strikes you finally place the actual droplet.


At this point if the surface normal is pointing downwards you can make it drip down, or if it’s close enough to being on a flat surface, do nothing.
The angle can be gotten via math.asin(normal.Y) * math.deg(1) where 90° will mean it’s on a perfectly flat surface, 0° means it’s sticking to a vertical wall, and a negative angle will mean it’s on an overhead surface.

the correct solution is raycasting vertically down
but how many droplets are you planning to raycast in such a way, sir?
your place will stutter and hang at approximately 200k droplets, i can assume
fluid simulation is very tedious with engines not designed for it

local TweenService = game:GetService("TweenService")

local MaxPool = 200
local Pool = {}
local ActiveDrops = {}
local DropSize = Vector3.new(0.5,0.25,0.5)
local DropSpeed = 50
local DropRayDistance = 2
local PuddleSize = Vector3.new(1,0.25,1)
local PuddleColor = Color3.fromRGB(115,110,255)

local Emitter = workspace:WaitForChild("DropletEmitter")

for i=1,MaxPool do
	local Part = Instance.new("Part")
	Part.Size = DropSize
	Part.Transparency = 1
	Part.CanCollide = true
	Part.Anchored = false
	Part.Parent = nil
	table.insert(Pool, Part)
end

local function FaceVelocity(part)
	local vel = part.Velocity
	if vel.Magnitude < 0.01 then return end
	part.CFrame = CFrame.lookAt(part.Position, part.Position + vel.Unit, Vector3.new(0,1,0))
end

local function SpawnPuddle(Position)
	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	rayParams.FilterDescendantsInstances = {workspace.Droplets}
	local hit = workspace:Raycast(Position + Vector3.new(0,1,0), Vector3.new(0,-5,0), rayParams)
	if hit then Position = hit.Position end
	local Puddle = Instance.new("Part")
	Puddle.Parent = workspace.Droplets
	Puddle.Anchored = true
	Puddle.CanCollide = false
	Puddle.Size = Vector3.new(0,PuddleSize.Y,0)
	Puddle.Transparency = 1
	Puddle.Color = PuddleColor
	Puddle.Shape = Enum.PartType.Block
	Puddle.CFrame = CFrame.new(Position)
	local grow = TweenService:Create(Puddle, TweenInfo.new(0.2), {Size=PuddleSize, Transparency=0})
	grow:Play()
	grow.Completed:Wait()
	local shrink = TweenService:Create(Puddle, TweenInfo.new(0.2), {Size=Vector3.new(0,PuddleSize.Y,0), Transparency=1})
	shrink:Play()
	shrink.Completed:Wait()
	Puddle:Destroy()
end

local function FakeDestroyDrop(Drop,i)
	Drop.Parent = nil
	Drop.Transparency = 1
	table.remove(ActiveDrops,i)
	table.insert(Pool,Drop)
end

local function SpawnDrop(Origin,Velocity)
	local Drop = table.remove(Pool,1)
	if Drop then
		Drop.Parent = workspace.Droplets
		Drop.Position = Origin
		Drop.Velocity = Velocity
		Drop.Transparency = 0
		table.insert(ActiveDrops,Drop)
	end
end

RunService.Stepped:Connect(function()
	for i=#ActiveDrops,1,-1 do
		local Drop = ActiveDrops[i]
		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Blacklist
		rayParams.FilterDescendantsInstances = ActiveDrops
		local DownRay = workspace:Raycast(Drop.Position, Vector3.new(0,-DropRayDistance,0), rayParams)
		if DownRay then
			local N = DownRay.Normal
			local globalDown = Vector3.new(0,-1,0)
			local tangent = (Drop.Velocity - N*Drop.Velocity:Dot(N)).Unit
			local downward = (globalDown - N*globalDown:Dot(N)).Unit
			if downward.Y < -0.01 then
				Drop.Velocity = downward * Drop.Velocity.Magnitude
			else
				Drop.Velocity = tangent * Drop.Velocity.Magnitude
			end
			if N.Y > 0.9 then
				task.spawn(SpawnPuddle, Drop.Position)
				FakeDestroyDrop(Drop,i)
			end
		end
		FaceVelocity(Drop)
	end
end)

local function Drop()
	local frontCF = Emitter.CFrame
	local size = Emitter.Size
	local offsetX = (math.random()-0.5)*size.X
	local offsetY = (math.random()-0.5)*size.Y
	local frontPos = frontCF.Position + frontCF.LookVector*(size.Z/2) + frontCF.RightVector*offsetX + frontCF.UpVector*offsetY
	local frontDir = frontCF.LookVector
	task.spawn(SpawnDrop, frontPos, frontDir*DropSpeed)
end

RunService.Stepped:Connect(function()
	for _=1,5 do Drop() end
end)

this seemed to work for me
the code for the droplet physics is in the renderstepped loop.

i got it to trail down more by making these changes:

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local MaxPool = 200
local Pool = {}
local ActiveDrops = {}
local DropSize = Vector3.new(0.5,0.25,0.5)
local DropSpeed = 0
local DropRayDistance = 4
local PuddleSize = Vector3.new(1,0.25,1)
local PuddleColor = Color3.fromRGB(115,110,255)

local Emitter = workspace:WaitForChild("DropletEmitter")

for i=1,MaxPool do
	local Part = Instance.new("Part")
	Part.Size = DropSize
	Part.Transparency = 1
	Part.CanCollide = true
	Part.Anchored = false
	Part.Parent = nil
	table.insert(Pool, Part)
end

local function FaceVelocity(part)
	local vel = part.Velocity
	if vel.Magnitude < 0.01 then return end
	part.CFrame = CFrame.lookAt(part.Position, part.Position + vel.Unit, Vector3.new(0,1,0))
end

local function SpawnPuddle(Position)
	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	rayParams.FilterDescendantsInstances = {workspace.Droplets}
	local hit = workspace:Raycast(Position + Vector3.new(0,1,0), Vector3.new(0,-5,0), rayParams)
	if hit then Position = hit.Position end
	local Puddle = Instance.new("Part")
	Puddle.Parent = workspace.Droplets
	Puddle.Anchored = true
	Puddle.CanCollide = false
	Puddle.Size = Vector3.new(0,PuddleSize.Y,0)
	Puddle.Transparency = 1
	Puddle.Color = PuddleColor
	Puddle.Shape = Enum.PartType.Block
	Puddle.CFrame = CFrame.new(Position)
	local grow = TweenService:Create(Puddle, TweenInfo.new(0.2), {Size=PuddleSize, Transparency=0})
	grow:Play()
	grow.Completed:Wait()
	local shrink = TweenService:Create(Puddle, TweenInfo.new(0.2), {Size=Vector3.new(0,PuddleSize.Y,0), Transparency=1})
	shrink:Play()
	shrink.Completed:Wait()
	Puddle:Destroy()
end

local function FakeDestroyDrop(Drop,i)
	Drop.Parent = nil
	Drop.Transparency = 1
	table.remove(ActiveDrops,i)
	table.insert(Pool,Drop)
end

local function SpawnDrop(Origin,Velocity)
	local Drop = table.remove(Pool,1)
	if Drop then
		Drop.Parent = workspace.Droplets
		Drop.Position = Origin
		Drop.Velocity = Velocity
		Drop.Transparency = 0
		Drop.Material = Enum.Material.Neon
		Drop.Color = Color3.fromRGB(115,110,255)
		table.insert(ActiveDrops,Drop)
	end
end

RunService.Stepped:Connect(function()
	for i=#ActiveDrops,1,-1 do
		local Drop = ActiveDrops[i]
		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Blacklist
		rayParams.FilterDescendantsInstances = ActiveDrops
		local DownRay = workspace:Raycast(Drop.Position, Vector3.new(0,-DropRayDistance,0), rayParams)
		if DownRay then
			local N = DownRay.Normal
			local globalDown = Vector3.new(0,-1,0)
			local tangent = (Drop.Velocity - N*Drop.Velocity:Dot(N)).Unit
			local downward = (globalDown - N*globalDown:Dot(N)).Unit
			if downward.Y < -0.001 then
				Drop.Velocity = downward * Drop.Velocity.Magnitude
			else
				Drop.Velocity = tangent * Drop.Velocity.Magnitude
			end
			if N.Y > .99 then
				task.spawn(SpawnPuddle, Drop.Position)
				FakeDestroyDrop(Drop,i)
			end
		end
		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Blacklist
		rayParams.FilterDescendantsInstances = ActiveDrops

		local DropletDownRay = workspace:Raycast(Drop.Position, Drop.Velocity.Unit * .5, rayParams)
		if DropletDownRay then
			local N = DropletDownRay.Normal

			-- tangent along the surface
			local tangent = (Drop.Velocity - N * Drop.Velocity:Dot(N)).Unit

			-- maintain speed along tangent
			Drop.Velocity = tangent * Drop.Velocity.Magnitude
		end

		FaceVelocity(Drop)
	end
end)

local function Drop()
	local frontCF = Emitter.CFrame
	local size = Emitter.Size
	local offsetX = (math.random()-0.5)*size.X
	local offsetY = (math.random()-0.5)*size.Y
	local frontPos = frontCF.Position + frontCF.LookVector*(size.Z/2) + frontCF.RightVector*offsetX + frontCF.UpVector*offsetY
	local frontDir = frontCF.LookVector
	task.spawn(SpawnDrop, frontPos, frontDir*DropSpeed)
end

RunService.Stepped:Connect(function()
	for _=1,20 do Drop() end
end)


You will need some form of fake-physics to achieve what you’re wanting to do. I read through all these posts and they seem to be fairly solid however, you SHOULD NOT be casting vertically down as that doesn’t respect the droplets AS PROJECTILES. If you were to only raycast downward, your droplets will only work when falling straight down. I’m assuming this is not what you want. The reason I’m suggesting a custom physics approach is due to the fact you can utilize workspace:BulkMoveTo() for droplet updates which will improve performance. You could optionally utilize Actors to, potentially, improve performance even more.
You may be able to do this with roblox’s physics engine and fake the droplets based on the current move vector. This would allow you to just raycast downwards and avoid a bunch of vector math but, a custom solution will probably be best.
If I ever get the time, I’ll throw together a prototype for one droplet and post it here.

That’s nice! I didn’t know that was a thing.

works great and isn’t too complicated at least for me, thank you so much.

Thanks, I’ll try to use the workspace:BulkMoveTo() thing you mentioned. And I’ll try and research more about the things you mentioned to get a better idea on them

notice this whole idea of detecting lack of movement when the droplet lays on top of horizontal plain surface is unrequired
you can detect a droplet being motionless by polling AssemblyLinearVelocity once in, say, 200 ms

I’m gonna try that right now thats a good idea and less costly.

I tested this, but there are some issues.
if you use this, the droplet always is going to bounce, since the velocity doesn’t just go to 0 after it hits the ground. it only puddles after the part comes to a complete stop.
a solution to this is detecting if the parts Y velocity is 0 instead of the entire velocity.
BUT, this comes with its own issue. the droplets will sometimes puddle right when they spawn, and if they don’t, they puddle at the peak of their jump when the height of the drop peaks.
lmk if you have a solution

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local workspace = game:GetService("Workspace")

local MaxPool = 670
local Pool, PuddlePool, ActiveDrops = {}, {}, {}
local DropSize = Vector3.new(0.5, 0.75, 0.5)
local DropSpeed = 100
local DropPollDelay = .02
local DropRayDistance = 2
local PuddleSize = Vector3.new(0.1, 2, 2)
local PuddleColor = Color3.fromRGB(154, 28, 30)
local Emitter = workspace:WaitForChild("DropletEmitter")

local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
RayParams.FilterDescendantsInstances = {workspace.Droplets, Emitter}

for i = 1, MaxPool do
	local p = Instance.new("Part")
	p.Size = DropSize
	p.Transparency = 1
	p.CanCollide = true
	p.Anchored = false
	p.Parent = nil
	Pool[i] = p
end

for i = 1, MaxPool do
	local p = Instance.new("Part")
	p.Anchored = true
	p.CanCollide = false
	p.Transparency = 0.6
	p.Color = PuddleColor
	p.Shape = Enum.PartType.Cylinder
	p.Material = Enum.Material.SmoothPlastic
	p.Parent = nil
	PuddlePool[i] = p
end

local function FaceVelocity(part)
	local vel = part.Velocity
	if vel.Magnitude < 0.01 then return end
	part.CFrame = CFrame.lookAt(part.Position, part.Position + vel.Unit, Vector3.yAxis)
end

local function SpawnPuddle(pos)
	local hit = workspace:Raycast(pos + Vector3.new(0, 1, 0), Vector3.new(0, -5, 0), RayParams)
	if hit then pos = hit.Position end
	local Puddle = table.remove(PuddlePool, 1)
	if not Puddle then return end
	Puddle.Size = Vector3.zero
	Puddle.Parent = workspace.Droplets
	Puddle.CFrame = CFrame.Angles(0, math.rad(90), math.rad(90)) + pos
	local grow = TweenService:Create(Puddle, TweenInfo.new(0.2, Enum.EasingStyle.Back), {Size = PuddleSize, Transparency = 0})
	grow:Play()
	grow.Completed:Wait()
	wait(0.2)
	local shrink = TweenService:Create(Puddle, TweenInfo.new(0.2), {Size = Vector3.zero, Transparency = 1})
	shrink:Play()
	shrink.Completed:Wait()
	Puddle.Parent = nil
	table.insert(PuddlePool, Puddle)
end

local function FakeDestroyDrop(Drop, i)
	Drop.Parent = nil
	Drop.Transparency = 1
	table.remove(ActiveDrops, i)
	table.insert(Pool, Drop)
end

local function SpawnDrop(origin, velocity)
	local Drop = table.remove(Pool, 1)
	if Drop then
		Drop.Parent = workspace.Droplets
		Drop.Position = origin
		Drop.Velocity = velocity
		Drop.Transparency = 0.4
		Drop.Material = Enum.Material.SmoothPlastic
		Drop.Color = Color3.fromRGB(241, 78, 81)
		if not Drop:GetAttribute("LastDir") then
			Drop:SetAttribute("LastDir", velocity.Unit)
		end
		table.insert(ActiveDrops, Drop)
	end
end
local LastPoll
RunService.Heartbeat:Connect(function()
	if not LastPoll then LastPoll = tick() end
	if tick() - LastPoll < DropPollDelay then return end
	LastPoll = tick()
	for i = #ActiveDrops, 1, -1 do
		local Drop = ActiveDrops[i]
		local pos, vel = Drop.Position, Drop.Velocity
		local downRay = workspace:Raycast(pos, Vector3.new(0, -DropRayDistance, 0), RayParams)
		if downRay then
			local N = downRay.Normal
			local globalDown = Vector3.new(0, -1, 0)
			local tangent = (vel - N * vel:Dot(N)).Unit
			local downward = (globalDown - N * globalDown:Dot(N)).Unit
			if downward.Y < -0.001 then
				Drop.Velocity = downward * vel.Magnitude
			else
				Drop.Velocity = tangent * vel.Magnitude
			end
			if N.Y > 0.99 then
				task.spawn(SpawnPuddle, pos)
				FakeDestroyDrop(Drop, i)
				continue
			end
		end
		local ray = workspace:Raycast(pos, vel.Unit * 0.5, RayParams)
		if ray then
			local N = ray.Normal
			Drop.Velocity = (vel - N * vel:Dot(N)).Unit * vel.Magnitude
		end
		local lastDir = Drop:GetAttribute("LastDir")
		if not lastDir then
			Drop:SetAttribute("LastDir", vel.Unit)
		elseif lastDir:Dot(vel.Unit) < 0.98 then
			FaceVelocity(Drop)
			Drop:SetAttribute("LastDir", vel.Unit)
		end
	end
end)

local function Drop()
	local cf, size = Emitter.CFrame, Emitter.Size
	local offsetX, offsetY = (math.random() - 0.5) * size.X, (math.random() - 0.5) * size.Y
	local frontPos = cf.Position + cf.LookVector * (size.Z / 2) + cf.RightVector * offsetX + cf.UpVector * offsetY
	local frontDir = cf.LookVector
	SpawnDrop(frontPos, frontDir * DropSpeed)
end

RunService.Heartbeat:Connect(function()
	for _ = 1, 2 do Drop() end
end)

i added a polling delay
by changing the polling delay and the raycast length you can kind of fine tune it.
it also makes it less laggy.

The system u made works great, is there any way to tweak the speed of the droplets, I tried by dividing the droplets velocity and I broke it lol.

Change the workspace gravity property to make them fall at diffferent speeds. I also added a drop speed setting at the top I think
Also try messing with the parts density

local physProperties = PhysicalProperties.new(DENSITY, FRICTION, ELASTICITY, FRICTION_WEIGHT, ELASTICITY_WEIGHT)

part.CustomPhysicalProperties = physProperties

Also you should replace the renderstepped loop for the drop physics with a while loop, idk why I was thinking with the polling thing, but it does not need to update every physics simulations, not even ever heartbeat.