Advertisement

Raycast suspension at high physics time step problem.

Started by June 14, 2019 09:02 AM
3 comments, last by NoizullI 5 years, 7 months ago

I am creating a simple raycast car, it work fine at 0.01 fixed time step but the suspension is very unstable at the higher time step, i want it to work well at 0.05.
Does anyone have ideas to solve this?

You can take smaller sub-steps or use implicit integration.

Advertisement

You could also add some additional damping to your suspension to avoid the instability, if that works for your application.

Thank for your help, but i am finding a way to make it work the same at high time step, not to change the property to adapt the time step.
And here is my current suspension code.
 


private void SuspensionUpdate() {
		spring.preOverflow = spring.overflow;
		spring.overflow = 0f;
		if (grounded && Vector3.Dot(hit.normal, transform.up) > 0.1f) {
			spring.bottomedOut = spring.overExtended = false;

			spring.length = transform.position.y - hit.point.y;

			if (spring.length < 0f) {
				spring.overflow = -spring.length;
				spring.length = 0f;
				spring.bottomedOut = true;
			} else if (spring.length > spring.maxLength) {
				grounded = false;
				spring.length = spring.maxLength;
				spring.overExtended = true;
			}
		} else {
			spring.length = Mathf.Lerp(spring.length, spring.maxLength, Time.fixedDeltaTime * 8f);
		}

		spring.velocity = (spring.length - spring.preLength) / Time.fixedDeltaTime;
		spring.compression = (spring.maxLength - spring.length) / spring.maxLength;
		spring.force = spring.maxForce * spring.compression;

		spring.overflowVelocity = 0f;
		if (spring.overflow > 0) {
			spring.overflowVelocity = (spring.overflow - spring.preOverflow) / Time.fixedDeltaTime;
			spring.bottomedOutForce = parentRigidbody.mass * -Physics.gravity.y * Mathf.Clamp(spring.overflowVelocity, 0f, Mathf.Infinity) * 0.0225f;
			parentRigidbody.AddForceAtPosition(spring.bottomedOutForce * transform.up, transform.position, ForceMode.Impulse);
		} else {
			damper.force = spring.length <= spring.preLength ? damper.bumpForce * spring.velocity : -damper.reboundForce * spring.velocity;
		}

		spring.preLength = spring.length;
	}

 

This topic is closed to new replies.

Advertisement