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 = Vector3.zero
	
	local max: Vector3 = b1.size * 0.5
	local min: Vector3 = -b1.size * 0.5
	
	local maxX, minX = b1.center.RightVector * max, b1.center.RightVector * min
	local maxY, minY = b1.center.UpVector * max, b1.center.UpVector * min
	local maxZ, minZ = b1.center.LookVector * max, b1.center.LookVector * min
	
	local s1_center_LocalSpace: CFrame = b1.center:PointToObjectSpace(s1.center.Position)
	
	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 = Vector3.new(x, y, z)
	local closestPoint_WorldSpace: Vector3 = b1.center:PointToWorldSpace(closestPoint_LocalSpace)
	
	Gizmo.setScale(3)
	Gizmo.drawText(closestPoint_WorldSpace, tostring(closestPoint_WorldSpace))
	Gizmo.setColor(BrickColor.new("Bright red").Name)
	Gizmo.drawPoint(closestPoint_WorldSpace)
	Gizmo.reset()

    ...
end

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