Introducing generalized shapecasts

Hi Creators,

Today we’re releasing generalized shapecasts. These allow you to cast any arbitrary part into the world: Blocks, spheres, cylinders, wedges, CSG, MeshParts, etc.

We previously released Blockcast and Spherecast as ways to find collisions using swept shapes. These can be thought of as “raycasts with volume”.

Until today, shapecasts were limited to block and sphere shapes, which limited their usefulness. Even if you started with a block or sphere-shaped part, you couldn’t simply plug the part into the shapecast query; you had to manually copy over the CFrame/Size values.

For performance reasons, you can’t cast terrain, i.e. use Terrain as the part argument in Shapecast.

Use cases

  • Draggers: To create a furniture dragger, you previously had to cast many rays and do a bunch of math. Casting the mesh directly simplifies this and results in more accurate collisions.
  • Projectiles: To simulate a rocket’s collisions, you previously had to approximate the rocket shape with block/sphere/raycasts. You can now accurately cast the rocket directly.
  • Mesh collisions: Developers could previously only use approximations for other objects such as swords and car colliders. Casting meshes allows more accurate collisions with less code.

API

WorldRoot:Shapecast(
  part: BasePart,
  direction: Vector3,
  params: RaycastParams?
): RaycastResult?
Parameter Summary
part The part to cast.
direction The direction in which the part is cast. Direction.Magnitude is the maximum distance the part will travel to find a hit.
params RaycastParams struct for defining filters.

Usage is congruent to the existing shapecast methods, but we now take a part instead of a shape / cframe / size.

What’s coming next

Performance

Performance is acceptable for smaller cast sizes, but large casts against dense worlds can be pretty slow. We plan to optimize this with our ongoing broadphase work.

Collision Margins

A common pain point with shapecasts is that there is no collision margin (aka “skin”). This means that a shapecast starting flush with another part won’t detect that part. We’ve heard your feedback, and this is one of the next steps we will be taking for shapecasts.

Optional CFrame override

We’ll be adding an optional “cframe” param to override the part’s original CFrame later this year.

Thank you for your feedback so far! Let us know what you think and if you have any questions below.

465 Likes

This topic was automatically opened after 10 minutes.

I can’t wait to start using this, it brings lots of potential! Is there any plans for more features like this in the future?

53 Likes

Thank you for this!
Finally, we can easily make differently shaped hitboxes (for melee and projectiles).

And yes, it would be very nice indeed to have an option for a CFrame override so that the part can be used in multiple locations without having to move it manually (via code).


I do have one question in mind, can we re-use the same part for multiple casts in different locations with the overide feature?
38 Likes

Seems useful for something like a building system or item drop system. A great addition to the Roblox engine. Can’t wait to see further improvements in the future :+1:

37 Likes

Sounds really good. Finally can use something that can help me with all instances.

Question though, will this not potentially remove Blockcast and Spherecast or set as deprecated?

30 Likes

Awesome. Were the return types fixed though?

29 Likes

Blockcast and Spherecast will live on for when you don’t have a part to cast from.

37 Likes

Could someone give me an example where I’d wanna use this?

24 Likes

Thank you, we’ll get this fixed!

27 Likes

Awesome! Is this possible to use with unions?

20 Likes

This is stated in the post. Unions are part of the CSG system, so yes.

21 Likes

Could use it for an accurate melee system, where the blade of the sword is cast in the direction of the swing, to see if it hit anything

23 Likes

Now this is quite nice to see. What else are we expecting in the future regarding shapecasts aside from the ones mentioned?

18 Likes

Sounds amazing! Does this support EditableMeshes? If not, I think that should be a crucial feature since lots of us are planning on using them as much as possible. It would also make custom physics engines much more efficient since the collision logic wouldn’t be in Luau.

21 Likes

Due to the nature of the way shapecasts work. I’m guessing a cone cast is still relevent because a shapecast will make a cone shaped mesh look more like a cylinder if the direction value is used accordingly. If anyone can correct me on this that’d be helpful.

It just would be nice to have a fast method for detecting if something is within “view” of say another object, like an NPC, or a targetting system.

17 Likes

Uh so the shapecasts ignore the precise convex decomposition property of mesh and unions, it will always use the default collision, and it’s kinda annoying for projectiles, will this ever be fixed?

18 Likes

PLEASE add visualisations for raycasts for developer debugging reasons! It’s super inconvenient trying to check to see if our raycasts are being done in the correct manner.

87 Likes

Does WorldRoot:GetPartsInPart() supersede WorldRoot:GetPartBoundsInBox()?

No… alright, so no.

17 Likes

They use whatever the current decomposition geometry is. Precise mode should work - can you provide a repro?

18 Likes