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
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.