From 87950c9a94881fb84ba306b8a8faab5b728a2983 Mon Sep 17 00:00:00 2001 From: hiker Date: Thu, 9 Mar 2017 08:56:01 +1100 Subject: [PATCH] Adjust up vector of kart slowly so that the kart is upright (even when it was not upright at the start of the cannon). --- src/karts/cannon_animation.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/karts/cannon_animation.cpp b/src/karts/cannon_animation.cpp index fafaa65de..05b5bd95d 100644 --- a/src/karts/cannon_animation.cpp +++ b/src/karts/cannon_animation.cpp @@ -157,14 +157,27 @@ void CannonAnimation::update(float dt) // Get the current kart orientation Vec3 forward = m_kart->getTrans().getBasis().getColumn(2); forward.normalize(); - - // Only adjust the heading. I tried to also adjust pitch, - // but that adds a strong roll to the kart on some cannons + + // Only adjust the heading. + // ------------------------ + // I tried to also adjust pitch at the same time, but that adds a strong + // roll to the kart on some cannons Vec3 v1(tangent), v2(forward); v1.setY(0); v2.setY(0); btQuaternion q = m_kart->getRotation()*shortestArcQuatNormalize2(v2, v1); - m_kart->setRotation(q); + // While start and end line have to have the same 'up' vector, karts can + // sometimes be not parallel to them. So slowly adjust this over time + Vec3 up = m_kart->getTrans().getBasis().getColumn(1); + up.normalize(); + Vec3 gravity = -m_kart->getBody()->getGravity(); + gravity.normalize(); + // Adjust only 5% towards the real up vector. This will smoothly + // adjust the kart. + Vec3 target_up_vector = (gravity*0.05f + up*0.95f).normalize(); + btQuaternion q_up = shortestArcQuat(up, target_up_vector); + + m_kart->setRotation(q_up * q); // Then compute the new location of the kart // -----------------------------------------