Not getting correct position of closest point along box

I’m currently trying to calculate the closest point along a box, however the values i’m getting are offset as if the boxes Y position is lower, I’m also having a weird issue where my code doesnt seem to play with an Oriented Box despite converting positions to Object Space.

The values shown on the point is the correct value however it is not being interpreted correctly by my code

The point (in red) is supposed to be the closest point along the ramp clamped to its dimensions

So far ive tried using different methods of World to Local/Local to World conversion but nothing seems to be working correctly

function RPhysics.Box_Sphere_Collision(b1: SharedTypes.BoxCollider, s1: SharedTypes.SphereCollider): SharedTypes.CollisionResult
	local depth: number = 0
	local normal: Vector3 =
	local max: Vector3 = b1.size * 0.5
	local min: Vector3 = -b1.size * 0.5
	local maxX, minX = * max, * min
	local maxY, minY = * max, * min
	local maxZ, minZ = * max, * min
	local s1_center_LocalSpace: CFrame =
	local x: number = math.clamp(s1_center_LocalSpace.X, -minX.Magnitude, maxX.Magnitude)
	local y: number = math.clamp(s1_center_LocalSpace.Y, -minY.Magnitude, maxY.Magnitude)
	local z: number = math.clamp(s1_center_LocalSpace.Z, -minZ.Magnitude, maxZ.Magnitude)
	local closestPoint_LocalSpace: Vector3 =, y, z)
	local closestPoint_WorldSpace: Vector3 =
	Gizmo.drawText(closestPoint_WorldSpace, tostring(closestPoint_WorldSpace))
	Gizmo.setColor("Bright red").Name)


b1 is simply a data struct using CFrame to handle position and rotation and in this case at the start of the game each object that is a box is assigned a BoxCollider which is fed the appropriate data

1 Like