- Home /
Calculate Distance using WheelColliders
I created a 4WD , i want to calculate the distance travelled by the car using only wheel colliders , based on the motor torque applied to the wheels.
Answer by Edy · Jul 14, 2021 at 06:29 PM
You may convert the angular velocity of the wheel into linear velocity using the radius and accumulate it:
float distance = 0.0f;
void FixedUpdate ()
{
float angularVelocity = myWheelCollider.rpm * (2.0f * Mathf.PI) / 60.0f;
distance += angularVelocity * myWheelCollider.radius * Time.deltaTime;
}
Note that this solution doesn't have the wheel slip into account. If the wheel is slipping in place, then the distance will still be increased as per the distance traveled by the surface of the wheel.
thanks for the solution , i tried it and it works for smaller distance and as distance increases the error increases , how do i counter wheel slip to calculate distance accurately.
Answer by gabgab06 · Jul 14, 2021 at 03:51 PM
I'm not sure but :
float _distanceTraveled;
Vector3 _lastPosition;
void Start()
{
_lastPosition = transform.position;
}
void Update()
{
_distanceTraveled += Vector3.Distance(transform.position, _lastPosition);
_lastPosition = transform.position;
}
@gabgab06 thanks for reply. But im looking to find it using circumference and using that the distance travelled.
is there a reason youre trying to do it based off the circumference? gabgab06 answer will give you the total distance traveled, it just wont be calculated based off the circumference. in order to calculate the distance traveled using circumference you need to know when a revolution of the wheel happens.
think of it like taking a tire, cutting it and laying it flat (inco$$anonymous$$g sick ascii art)
O -> ___
The circumference of the tire is equal to the distance traveled for each revolution of the wheel. so for example if your tire has a circumference of 2m in 20 revolutions youve gone a 40m distance
circumference = 2* pi * radius
i want it in that specific way, i got an idea to manually calculate the circumference of the wheel and based on that input calculate the distance travelled by the wheel . I will post the solution if anything comesup . Meanwhile, thank you @b1gry4n
Answer by harish_unity986 · Jul 17, 2021 at 04:09 AM
Solution above
It's exactly the same as $$anonymous$$e xD (it only returns abs distance instead of signed distance)
yo @Edy my bad man , i used your solution but was experimenting with wheels , so got wrong results , your answer is very accurate , thanks a lot.