Boxcasting/Spherecasting Request

Boxcasting / Spherecasting

As a Roblox developer, it is currently too hard to boxcast/spherecast in an efficient manner

If Roblox were to make a boxcasting/spherecasting function, it would allow me to create better, less dodgy custom physics for spaceships, planes, etc which rely purely on CFrame animation.

While I can currently boxcast in a dodgy way, which one method can be seen below, I feel as though there should be built in functions for this so that we do not have to rely on said dodgy methods. It would be greatly appreciated by myself and I’m sure many other developers if we had these functions built in.

-- Scarious
-- Box Casting Function

local cf		=CFrame.new
local ray		=Ray.new
local raycast	        =workspace.FindPartOnRayWithIgnoreList
local insert	        =table.insert
local pi		=math.pi

local function boxcast(t1,d,s,l)
	local td=t1*d
	local m0=(t1.p-td).magnitude
	local t2=t1*cf(0,0,-m0)
	local x0,y0=s.X,s.Y
	local x1,y1=x0/2,y0/2
	local n0,n1,n2,n3=cf(-x1,-y1,0),cf(-x1,y1,0),cf(x1,-y1,0),cf(x1,y1,0)
	local b0,b1,b2,b3=cf(x1,0,0),cf(0,y1,0),cf(-x1,0,0),cf(0,-y1,0)
	local i0,i1,i2,i3,i4,i5,i6,i7=t1*n0,t1*n1,t1*n2,t1*n3,t1*b0,t1*b1,t1*b2,t1*b3
	local c0,c1,c2,c3,c4,c5,c6,c7=t2*n0,t2*n1,t2*n2,t2*n3,t2*b0,t2*b1,t2*b2,t2*b3
	local ol={t1.p,i0.p,i1.p,i2.p,i3.p,i4.p,i5.p,i6.p,i7.p}
	local tl={t2.p,c0.p,c1.p,c2.p,c3.p,c4.p,c5.p,c6.p,c7.p}
	local a=t1.lookVector
	local ax,ay,az=a.x,a.y,a.z
	local il=l or {}
	local h,p,n,c
	for i=1,#ol do
		local o=ol[i]
		for j=1,#tl do
			local t=tl[j]
			local to=t-o
			local m,u=to.magnitude,to.unit
			local um=u*m
			local h0,p0,n0=raycast(workspace,ray(o,um),il)
			if h0 and p0 and n0 then
				local bx,by,bz=p0.x,p0.y,p0.z
				local ab=ax*bx+ay*by+az*bz
				local sc=(c and (c>ab and true or false)) or true
				if sc then
					h,p,n,c=h0,p0,n0,ab
				end
			end
		end
	end
	return h,p,n
end
57 Likes

Yeah totally. Unity has this and it’s really handy

13 Likes

Could you explain what this feature is used for? I looked it up on Google, but didn’t find helpful information.

1 Like

This feature is similar to raycasting, but rather than using just rays, it also uses boxes/other shapes in the cast, “casting a box along a ray and returning detailed information on what was hit”, allowing for greater precision in game development.

You can view Unity’s documentation below as an example of usage, etc:
https://docs.unity3d.com/ScriptReference/Physics.BoxCast.html
https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html

Edit:
I noticed you were doing leg r15 inverse kinematics which had issues with the foots and how the surface normal might not be the best when firing down from the middle of the foot, a solution to this would be boxcasting, in which a box is fired along with the ray using the size of the foot, allowing for more accurate precision.

15 Likes

I feel this feature request would come in handy, especially for projectiles. Instead of having a very tiny hit box for a projectile, you could have one that encircles the entire projectile, or the space in front of it, making the width & height match that of the projectile.

Would love to see this implemented someday!

11 Likes

Bumping this, while the functions: GetPartBoundsInBox and GetPartBoundsInRadius or GetPartsInPart can get parts, they unfortunately dont output geometry based information such as returned Normal or exact point of intersection. Would be very powerful to have a feature like this.

3 Likes

Bumping this. As aforementioned, would be extremely useful for projectiles.

1 Like

These are already features workspace:GetPartBoundsInBox() and workspace:GetPartBoundsInRadius()

Two posts above yours literally explain why those aren’t the same as what the OP asked for.

Educate yourself before you post.

1 Like

You can get the point of touching for CSG parts with raycasting after a workspace:GetPartBoundsInBox() orworkspace:GetPartBoundsInRadius() call.

To get the detailed raycast info for the afformentioned functions (if the object is not a meshpart or union) you can simply use a raycast with the only whitelisted part being the part that the two afformentioned functions found.

That’s a work around, not a feature included within the two functions you just mentioned.

And as you said, it doesn’t work with mesh parts or unions which a sphere/box cast would.

2 Likes

Sometimes workarounds have to be used. Doing such is completely valid.

Not every possible in the book can be added as too many features can’t exist.

Well it does but you’d have to make it use multiple raycasts instead of one.

Almost every work around is valid as long as there isn’t a feature that streamlines the process and makes it more performant.

“too many features” however, isn’t a valid reason why Boxcasting/Spherecasting shouldn’t become a feature.

2 Likes

To anyone coming to this thread with a plea for why the bounds and/or area of complex & simple shapes should be Raycast-able, I have good news!

Shapecasting wasn’t given much spotlight at RDC 2022, but it was shown in a slide at the very bottom of a list on a slideshow of potential (but not promised) engine releases for 2023 at the tech presentation event. Boxcasting and spherecasting fall under this feature’s umbrella. At least one engineer, @subcritical, is known to be working on the feature. Of course, I can’t speak for Roblox, but we can be optimistic that it’s coming! - will just take some time. :slightly_smiling_face:

The amount of effort, resources, and time shapecasting will save us is pretty enormous, since, as a technique, raycasting is used universally in almost all features in all games everywhere- it’s important that they take the time and effort needed to do things right.

(i can’t seem to find any media actually showing the slide i’m referencing at the RDC22 presentation :confused: if anyone can find it please post it)

2 Likes

This is happening, and sooner rather than later.

26 Likes

So much good news consecutively, this feels like a pipe dream :drooling_face: :partying_face: