Car physics joints (with suspension)

Hiya 😀!


I have some experience in SKPhysics, so it's been frustrating now as I'm struggling to make a basic car with spring suspension for the wheels. I've been able to make a vehicle where there's a "parent" point that represents the chassis and two wheel points. When these three points are connected with pin joints, SKPhysicsJointPin, the pins force the wheels to stay in place in relation to the chassis, but allow rotation around their own anchor points. So that works nicely as a car. However, I would like to add suspension to the wheels, so they have some vertical freedom of movement based on physics, as car suspension would in real life.


The problem is if I replace the pins with springs, SKPhysicsJointSpring, the springs don't stay vertical. I've tried adding sliding joints, SKPhysicsJointSliding, to them, too, to force the springs to stay in a vertical area. This seems to just make stuff go haywire. So at this point when I'm juggling three different joint types and a bunch of physicsBody, anchorPoint and axis variables, I wanted to check if someone else would know what to do. Essentially I want the same setup as in games like Hill Climb Racing.


. chassis

/ \

/ \

. . axles

| |

. . wheels


How do I implement the suspension springs?


Here's the SKPhysicsJoint reference: https://developer.apple.com/reference/spritekit/skphysicsjoint


Here's my code, with simple pin joints:

let suspensionLeft = SKPhysicsJointPin.joint(withBodyA: chassis.physicsBody!, bodyB: wheelLeft.physicsBody!, anchor: chassis.physicsBody!)
let suspensionRight = SKPhysicsJointPin.joint(withBodyA: chassis.physicsBody!, bodyB: wheelRight.physicsBody!, anchor: chassis.physicsBody!)

physicsWorld.add(suspensionLeft)
physicsWorld.add(suspensionRight)


Any help really appreciated, thanks!

I don't know, but man, kudos for the diagram above. Well done!


Maybe see if you can set allowsRotation to false.

Thanks, I take much pride in my ASCII 😀.


I should try allowsRotation, thanks a lot. I hadn't thought of that!


I got a bit further now. With the last setup (as seen on the previous diagram) I adjusted where the anchor points were and it really helped. I got a sort of beach buggy or motorcycle approach where the wheels would move farther and closer to each other. Of course this wouldn't work for a normal car, so I realized I have to put the chassis and stuff all at the same vertical height. Also, I realized that the wheels have to be separate from the suspension, since they should only care that they're rotating around their own center.


This means I now have four hierarchy elements: 1) chassis which simply is the parent of each element, 2) back and front axles which are at a distance fixed to the chassis, 3) wheel disks that represent the tips of the axles and 4) wheels themselves. The axles are connected to the chassis with fixed joints, which means they always move with the chassis. The axles are connected to the disks with both sliding and spring joints to produce the suspension. Finally the disks are connected to the wheels, which are simply pin jointed to the same position of the disks.


____ ____

/ . \_________._________/ . \

\_____/ \_____/ ._______________.________________.________________.


back axle chassis front axle chassis axle disk wheel

back disk front disk fixed joint sliding joint pin joint

back wheel front wheel spring joint


So it kinda works now, but the issue is still that the sliding joint just doesn't stay in place and the springs seem to rotate pretty freely. This results in what you might imagine: two wheels with the chassis dragging on the ground. I'll definitely try out playing with allowsRotation, but if someone has any insight into this, please let me know. I have a slight feeling that I may be overthinking this somehow and that there's some clear solution that I'm just not seeing.

Car physics joints (with suspension)
 
 
Q