Kright / MotorcyclePhysics

Physics model of motocycle in Unity3d
19 stars 11 forks source link

Moto is falling to the side #1

Open jbelonstark opened 5 years ago

jbelonstark commented 5 years ago

Hello , I've been testing your project, it's kinda cool but the moto falls to the sides very often, how can you avoid this behaviour? for example a physics constraint would do the trick?

thanks

Kright commented 5 years ago

Hi, I use countersteering only, same as on real motorcycles. https://en.wikipedia.org/wiki/Countersteering

My code trying to maintain unstable balance by steering. If motorcycle falls left, wheel turns left and tries to raise motorcycle back to vertical.

It's working quite good on middle speeds, but steering isn't enough on small speed and errors in physics leads to instability at high speed. Constants in steering algorithm is a subject of tuning. Decreasing time step of physics helps too. I wrote this code three years ago, may be unity physics was changed :)

It was my attempt to achieve motorcycle stability with real physics without any hacks. What do you want to do?

jbelonstark commented 5 years ago

Hello Kright, thanks for your replied, I wanted the moto to never fall so I ended up using another approach, not yours. I used a lean angle to compensate the fall to the side effect. Just changing the center of mass of the bike in the center bottom of the 2 wheels.

if (_transform.localEulerAngles.z < 70f)
                    angleLeanCompensate = _transform.localEulerAngles.z / 30f;
centerOfMass.localPosition = new Vector3(centerOfMass.localPosition.x,
                    (-(1f - _baseDistance / 2.8f) - angleLeanCompensate) , centerOfMass.localPosition.z);
bzn7 commented 4 years ago

Hello Kright, thanks for your replied, I wanted the moto to never fall so I ended up using another approach, not yours. I used a lean angle to compensate the fall to the side effect. Just changing the center of mass of the bike in the center bottom of the 2 wheels.

if (_transform.localEulerAngles.z < 70f)
                    angleLeanCompensate = _transform.localEulerAngles.z / 30f;
centerOfMass.localPosition = new Vector3(centerOfMass.localPosition.x,
                    (-(1f - _baseDistance / 2.8f) - angleLeanCompensate) , centerOfMass.localPosition.z);

Hello jbelonstark, could you please explain what the _baseDistance is? Do I need any algorithm to calculate it?

Edit: I guess it is "centerOfMass.localPosition.y" Thank you very much for this simple and proper solution.