How to calculate force against slope

I’m trying to calculate the a force against the slope.


Ignore the weird objects flying, this may be my 13th time trying to resolve this issue.

I’ve desperately tried to use Mathematical and Physical Formulas to fix this however it either shakes like crazy or just continues slipping.

Code

local rightVector = attCF.RightVector
local lookVector = attCF.LookVector

local normal = Raycast.Normal
local hit = Raycast.Position
local contactPart = Raycast.Instance

local projectedForward = (lookVector - lookVector:Dot(normal) * normal).Unit

local right = projectedForward:Cross(normal).Unit
local forward = normal:Cross(right).Unit

local rot = CFrame.fromMatrix(Vector3.zero, right, normal, forward)

FloorAtt.WorldCFrame = CFrame.new(hit) * rot

local displacement = math.max(SpringLength - Raycast.Distance, SpringMin)
local velocity = RootVelocity:Dot(normal)

-- Suspension

local springForce = displacement * SpringStiffness
local dampingForce = SpringDampening * velocity
local netForce = Mass * (springForce - dampingForce)

local platformVel = contactPart and contactPart:GetVelocityAtPosition(hit) or Vector3.zero
local rawVel = Root:GetVelocityAtPosition(hit)
local rawVelLocal = attCF:VectorToObjectSpace(rawVel)
local relVel = rawVel - platformVel
local relVelLocal = attCF:VectorToObjectSpace(relVel)

-- Friction

local floorAttCF = FloorAtt.WorldCFrame

local floorRawVelLocal = floorAttCF:VectorToObjectSpace(rawVel)
local floorRelVelLocal = floorAttCF:VectorToObjectSpace(relVel)

local worldUp = Vector3.yAxis
local slopeAngle = math.acos(math.clamp(normal:Dot(worldUp), -1, 1))
local slopeFixAngle = math.abs(math.rad(90 - math.deg(slopeAngle)))
local slopeDeg = math.deg(slopeAngle)

local normalForce = netForce
local moveForce = Mass * workspace.Gravity * math.sin(slopeDeg)
local fallForce = Mass * workspace.Gravity * math.cos(slopeDeg)

local lateralForce = -floorRelVelLocal.X * Mass
local longitudinalForce = floorRelVelLocal.Z * Mass

local floorRightVector = floorAttCF.RightVector
local floorLookVector = floorAttCF.LookVector

local F = ((floorRightVector * lateralForce) + (floorLookVector * longitudinalForce))/No_Attachments

Use Vector Projection/Rejection, with it you can calculate the exact 3D Vector of gravity sliding along the surface regardless of the way the object is facing

When gravity hits a slope it splits into 2 vectors, normal force and tangential force, if you want to get the “force against slope” (which i presume is to stop sliding), you need an equal and opposite to the tangential force

Ok I see, say that in code for me?

I did this a while ago, using a similar approach however its very close to working but its still not there yet.

local worldUp = Vector3.yAxis
local slopeAngle = math.acos(math.clamp(normal:Dot(worldUp), -1, 1))
local slopeFixAngle = math.abs(math.rad(90 - math.deg(slopeAngle)))
local slopeDeg = math.deg(slopeAngle)

local normalForce = netForce
local eff = math.abs(math.min(slopeFixAngle, 1))

local lateralForce = -floorRelVelLocal.X * normalForce * eff
local longitudinalForce = floorRelVelLocal.Z * normalForce

Try this

local gravityVec = Vector3.new(0, -workspace.Gravity * Mass, 0)
local gravNormalForce = gravityVec:Dot(normal) * normal
local gravSlideForce = gravityVec - gravNormalForce
local antiGravityGlobal = -gravSlideForce 
local antiGravityLocal = floorAttCF:VectorToObjectSpace(antiGravityGlobal)
local normalForce = netForce

local gripStiffness = 10 

local lateralForce = (-floorRelVelLocal.X * gripStiffness * normalForce) + antiGravityLocal.X
local longitudinalForce = (-floorRelVelLocal.Z * gripStiffness * normalForce) + antiGravityLocal.Z
1 Like

Working a little better still quite inaccurate, on slopes.

local gripStiffness = 1 -- 10 IS TOO BIG (Coefficient)

local lateralForce = (-floorRelVelLocal.X * gripStiffness * normalForce) + antiGravityLocal.X
local longitudinalForce = (floorRelVelLocal.Z * gripStiffness * normalForce) + antiGravityLocal.Z
-- FOWARD SHOULD BE NEGATIVE

It sits quite nicely under lower angles though, BUT it forcefully pushes its self up till equilibrium even on the slightest angle difference due to the ‘AniGravityForce’