Hello. I need to simulate carriage moving from the slope. It consists from several parts, say ,main part, some details, two bogie and four wheels. All these parts have own rigid bodies. All rigid bodies use gravity. Wheels and bogies use HingeJoints. It moves smothly under the gravity force. But when i want to calculate its acceleration, i get strange values. Some of them are positive, some of them negative.
Here is the script which calculates current rigidbody acceleration.
/// <summary>
/// Компонет определающий скорость GameObject к которому он подсоединён
/// </summary>
public class Speed : MonoBehaviour {
/// <summary>
/// Твердое тело объекта
/// </summary>
private Rigidbody rigidBody;
/// <summary>
/// Gets the speed ms.
/// </summary>
/// <value>The speed ms.</value>
public float SpeedMs { get { return rigidBody.velocity.z * -1f; } }
/// <summary>
/// Gets the speed kmh.
/// </summary>
/// <value>The speed kmh.</value>
public float SpeedKmh { get { return rigidBody.velocity.z * -3.6f; } }
/// <summary>
/// Gets the acceleration.
/// </summary>
/// <value>The acceleration.</value>
public float Acceleration { get { return acceleration.z * -1f; } }
private Vector3 lastVelocity;
private Vector3 acceleration;
void Start () {
rigidBody = GetComponent<Rigidbody>();
lastVelocity = rigidBody.velocity;
}
/// <summary>
/// Просчёт физики
/// </summary>
void FixedUpdate() {
acceleration = (rigidBody.velocity - lastVelocity) / Time.fixedDeltaTime;
lastVelocity = rigidBody.velocity;
Debug.LogFormat("accel {0}", acceleration.z);
}
}
And when carriage is moving on the track (and no difference whether it is on the plain track or slope with some angle) its acceleration always jittering. I’ve attached screenshot with log output.
So how to get smooth acceleration value?
I tryed to disable all extra threads and timers in the project, unfortunally the effect remained the same.
I tryed to make calculations in Update instead of FixedUpdate. Nothing changed.
I have to simulate physics quite preciously. Unfortunally i get strange results.
Your acceleration calculation looks correct to me. If you’re receiving those values, then surely the velocity change in the rigidbody is not smooth. Check out how are you actually modifying the velocity of your object. For example, if the joints have some spring effect, then this will be added to the acceleration.
Also, note that your code computes the acceleration in world space. So it will show the proper acceleration as long as the object is moving along the world Z axis.
Spring effects are disabled. I add some focre in the beginning, and then carrige moves by inertion from the slope. When it moved on the slope, its velocity and acceleration are prerry smooth. But when it begins moving on the flat terrain, these parameters begin jitter. Firstly i tryed to change physics material of wheels, carriage and terrain. Nothing helped though. Then i tryed to play with hinge joint parameters. The same effect. But when i added simple cylinder with collider and rigidbody, it seemed that parameters were smoother. Unfortunally jitterring was present too.
I’ve put current speed and previous speed logging in FixedUpdate, and sometimes previous speed is greater than current, some time is less. I don’t understand why does it happen. In theory, when object is deacellerating, it’s speed must decrease too. But i see that the speed of rigid body is jittering on the flat surface.
If you’re measuring the speed in the topmost rigidbody, then it’s velocity will accumulate the errors and “springy” effects of the joints and the other rigidbodies. Joints in Unity are no “solid joints”, but there’s always some amount of spring effect. The velocity variations you observe are likely caused by the physics resolving the system while it decelerates.
I can imagine a situation like this: one of the wheels decreases its speed a bit more than the others, reducing the general velocity. But then the other wheels “push” the first one which “releases” the energy from the joint spring, causing a small sudden acceleration. This may happen many times along all the wheels while the vehicle is decelerating.
So how to solve this problem? Well, i see, that some spring effect is present in joints. When i put the carriage several meters above the terrain and than release, it falls down, and at the collide with terrain moment the biggest and most heavy part shows some spring through the bogies. I supposed these effects, but i need hinge joints to operate wheels on axis, and bogies attached to main part to simulate real carriage which can follow rails with branches. My colleage desided try to solve this problem, but i think it is because of unity engine.
The cause is not Unity but the underlying physics engine, PhysX, which works that way. A possible solution to the problem would be applying some smoothing to the received values, like using the average of the last n velocity values received. This would induce some lag but the results would be smoother.