Craft spring unstable

Anyone got any ideas on how I can fix my code so that my spring suspension doesn’t go haywire every time the car spins?

spring = class()

function spring:init(body, right, forward, min, max, rest, stiff, damper)
    self.body = body
    self.right = right
    self.forward = forward
    self.restLength = rest or 1.5
    self.springTravel = 1
    self.springStiffness = stiff or 30
    self.damperStiffness = damper or 10
    
    self.minLength = self.restLength - self.springTravel
    self.maxLength = self.restLength + self.springTravel
    
    self.springLength = self.maxLength
    
    self.wheelRadius = 0.5
    
end

function spring:draw()
    
    self.rayPos = (self.body.position + (self.body.forward * self.forward) + (self.body.right * -self.right))
    
    self.raycast = scene.physics:raycast(self.rayPos, -self.body.up, self.maxLength + self.wheelRadius)

    self.lastLength = self.springLength 
    
    if self.raycast then
        self.springLength = ((self.maxLength + self.wheelRadius) *  self.raycast.fraction) - self.wheelRadius
        
    else
        self.springLength = self.lastLength
    end
    
    --self.springLength = math.max(self.minLength, math.min(self.maxLength, self.springLength))
    
    self.springVelocity = (self.lastLength - self.springLength) / DeltaTime
    
    self.springOffset =  self.restLength - self.springLength
    
    self.springForce = self.springStiffness * self.springOffset

    self.damperForce = self.damperStiffness * self.springVelocity
    
    self.suspensionForce = self.springForce + self.damperForce
    
    if self.raycast then
        self.body:get(craft.rigidbody):applyForce(vec3(0,self.suspensionForce,0), vec3(-self.right, 0, self.forward))
    end
    
    return self.rayPos, self.springLength, self.springOffset, self.suspensionForce, self.springVelocity
end


Here’s a video for reference:
https://streamable.com/0z2s1l

@Exyon How are you getting the rect to tilt and eventually go crazy. I have a rect but applying a force to one corner raises the whole rect at once. I’m not sure how to tilt the rect. Maybe if I can get it to tilt, I can figure out why yours goes crazy.

I have my code posted above

@Exyon That code doesn’t help. How are you defining the rectangle that you’re applying the force to the 4 corners.

@dave1707 its just a scene entity with forces being applied at the 4 corners.
Anyways I narrowed it down to the entity being rotated past 45 degrees. It can move in any direction without instability, but when rotated clock-wise or counter clock-wise past 45 degrees, it becomes unstable.

Fixed it.
When using entity.forward or entity.right, if you want it to be 2 units back, you must use positive numbers for multiplication or else it breaks.
For example:
Before

entity.forward * -2

After

-entity.forward * 2

the reason for this is when the forward vector would reach negative, it would be multiplied by another negative number, making it positive again, thus breaking where the force is to be applied.

Now to implement tire physics!