I am using a wheelcollider to model suspension on an aircraft.
I would like to be able to simulate different types of terrain under the wheels, such as smooth runway vs grass, without having to model mesh colliders to reflect this.
Does anyone have some idea as to how this could be achieved. I figured setting the suspension length manually and randomly would do it but this doesn’t seem to be something that can be achieved out of the box. Do I need to roll my own raycast suspension?
The WCs need to stay in contact with the ground or it will look weird (I’m assuming that the grass looks flat, you just want the pilot to experience a bumpy landing.
I would not try to change the suspension configuration, instead apply forces to the aircraft rigidbody (RB) at the locations of the WCs using RB.AddForceAtPosition().
Assuming you use sensible forces (dependent on suspension stiffness, travel and RB mass) the suspension can be additionally loaded up / unloaded and allow some wobble of the RB.
Or if just first person and single player, you could shake the camera.
The RB.AddForceAtPosition() will force the plane up or down on the WC spring. If you limit the force peaks (use some sort of sinusoidal function with some velocity scaling) to enough to just give you 20-30 mm spring compression, the plane will appear to bounce around a little. If the forces are added out of sync then you will introduce a little pitch and roll.
The plane will appear to be dipping down towards the wheels or lifting up away from them, I believe the trick is make the additional suspension travel small so that it does not look crazy. If you check out this
@5:30+ you can see the whole landing is fairly smooth; the body of the plane can be seen moving relative to the ground @ 5:43. Inside the cockpit you get a little bit of a feel for the shaking.
I think at this point, you either introduce some unevenness to the terrain and let the WCs sort it out or you fake it, and this is the best faking it I can offer.
Note: the youtube clip is nothing to do with me, it’s just the first grass landing I found, I’ve not heard the sound, so hopefully there is nothing inappropriate!
I also simulate such terrain effects using direct forces at the wheel’s contact point. You may use Perlin noise (Mathf.PerlinNoise) for simulating irregular but continuous and repeatable randomness.
However, have in mind that by applying such forces upwards you’re effectively lifting the wheel with an external force. Thus, the grip of the wheel will decrease accordingly and the wheel may likely slide in conditions where it should keep adherent. You should have some way to account for that grip reduction while applying a force upwards. For example, increase the wheel’s friction coefficient proportionally to the force applied (Disclaimer: I haven’t tested this solution in the WheelCollider).
Something similar will happen when applying the forces downward: the wheel will gain a grip that may make it stay adherent in conditions where it should slide.
Of course, forgot about the potential grip delta issue, I will give it a go and see if that is noticeable. It would be nice to think that this inconsistent grip actually lends realism to the effect as rough dirt vs smooth runway does in real life.
So I did some experiments and realised that applying force to rigidbody has a flaw in that I am trying to move an object of relatively large mass quickly which doesn’t give me the motion I need.
When running over rough terrain it is the wheel that moves with a high frequency and the aggregation of these forces is then passed to rigidbody.
Seems like if I could inject an offset into the hit.distance within the collider, that would simulate a fast changing terrain under wheel and the spring physics would do the rigidbody motion for me.
Unfortunately this looks like it needs a custom raycast solution as I can’t modify the initial number going into the wheelcollider…
So I used a third party wheel collider that allowed me to intercept the raycasthit and modify its point property with an offset driven by perlin noise and it appears to be working as intended now.
The raycast thinks it is closer or further away than it really is and sends all usual physics up through spring into rigid body.
Thanks a lot to you both for taking time to comment.
@OJDee May I ask, why you try force such behaviour, using physics?
I doubt you need such realistic behaviour, with physics acting on each wheel.
You will have lots of issues with setting it properly. Specially, when you change type of wheels etc.
If you need anything else than just pilot shaking, like indeed the aircraft, I would just add noise to the model position, in respect to speed while in touchdown. Just model, for visuals.
You could apply also noise only to visual part of wheels (models), while physics wheel colliders actually move on smooth surface, without additional forced noise.
I understand what you are saying about smoke and mirrors, but when I am dealing with an exclusively external camera looking at an entirely physics driven vehicle, anything to do with motion just has to be based in physics or it will just look artificial.
My experience of moving a transform outside of applying forces to rigidbody has always been bad.
If there is already a system in place for suspension, it seems to make sense to leverage this if possible and get all the benefits.
I will try and capture some video in the next few days and see if you agree with me