'how can I go about getting the linear impulse required at a position to fully stop a rotating and moving cuboid

I'm trying to find the impulse at a position that will completely stop a cuboid of a given size, mass, linear velocity, and rotational velocity. This function should give me the actual impulse which I can negate and apply to the part to stop the part in its tracks. This is all meant for Roblox which has a basepart:ApplyImpulseAtPosition(part,worldposition) function that I am trying to make use of.

Here's the function I came up with, but I am not sure if it's correct because it created glitchy, snappy, feedback looping results in my testing.

local function getImpulseAtPosition(part,pos)

    local smallNumber = 1e-10
    
    local pos = part.CFrame:Inverse()*pos
    local CFRot = CFrame.new(part.CFrame.p):Inverse()*part.CFrame
    local size = part.Size
    local mass = part.Mass
    local linearVel = part.Velocity
    local rotationalVel = part.RotVelocity
    
    local linearInertia = part.Velocity*mass
    if rotationalVel.Magnitude > smallNumber then
        local rotationalAxis = part.RotVelocity.Unit
        
        local xI,yI,zI = (size.Y^2+size.Z^2)*mass/12, (size.X^2+size.Z^2)*mass/12, (size.X^2+size.Y^2)*mass/12
        local vI = xI*math.abs(Vector3.xAxis:Dot(rotationalAxis))^0.5+yI*math.abs(Vector3.yAxis:Dot(rotationalAxis))^0.5+zI*math.abs(Vector3.zAxis:Dot(rotationalAxis))^0.5
        
        local rotationalLinearVel = -pos:Cross(rotationalVel)
        local centerDist = pos:Cross(rotationalAxis).Magnitude^2
        local rotationalInertia = centerDist > smallNumber and rotationalLinearVel*(vI/centerDist) or Vector3.zero
        
        return CFRot*rotationalInertia+linearInertia
    else
        return linearInertia
    end

end 

sorry for the messy variable names, I just threw it together while trying to use my brain at its full capacity.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source