Fast Minkowski Shapecast

Overview

We already know that Roblox has a native solution for shapecasting, however, as many of us may have experienced problems with it, like, being inaccurate or having no margins, well, I came up with an old algorithm I made a few years ago that solves these problems.


Installation

You can get the repo here


Basic usage

local ReplicatedStorage = game:GetService 'ReplicatedStorage'

local World = require(ReplicatedStorage:WaitForChild 'FMShapecast')() --// Creates a new geometry world

local sphereShape = World.createShape 'Ball' --// Creates a sphere shape
sphereShape.Transform = CFrame.new(0, 50, 0)
sphereShape.Radius = 1
sphereShape.Margin = .01

local overlapParams = OverlapParams.new()
local shapecastResult = World:Shapecast(sphereShape, Vector3.new(0, -999, 0), overlapParams)
if shapecastResult then
    print('Distance', shapecastResult.Distance)
    print('Instance', shapecastResult.Instance)
    print('Material', shapecastResult.Material)
    print('Position', shapecastResult.Position)
    print('Normal', shapecastResult.Normal)
end

How does it work?

This algorithm is based on GJK and MPR, it tries to raycast the minkowski difference then find the time of impact, contact point and surface normal.


Known limitations

I don’t expect to be this very popular, so there are few limitations to consider before using this library in real production:

  • Doesn’t work with meshes or union operations.
  • Doesn’t work with terrain.

Example place

Bouncy Spheres


Thoughts

If the community consider this useful, I will consider adding support for meshes and terrains (maybe union operations too). Anyways, I hope some people finds this fast algorithm useful for some cases.

25 Likes

Really useful resource. Thanks for sharing this!

9 Likes

I’ve been using Roblox shapecast from time to time, I’ll keep this in mind next time I need it. Thanks for the contribution!

2 Likes

I’ve almost never used shape casting in my life… But after watching this post, I definitely want to learn how to use it

Thank you for this resource, this is very useful!

2 Likes

This is quite frankly, one of the best things I’ve seen all day.

2 Likes