From a76a6e3508ae24a998fded9103c14165df934a8e Mon Sep 17 00:00:00 2001 From: Benau Date: Thu, 5 Jul 2018 14:22:37 +0800 Subject: [PATCH] Use smoothed transform to raycast the skidmark quads --- src/graphics/skid_marks.cpp | 57 +++++++++++++++++-------------------- src/karts/moveable.hpp | 6 ---- src/physics/btKart.cpp | 32 ++++++++------------- src/physics/btKart.hpp | 10 +++---- 4 files changed, 43 insertions(+), 62 deletions(-) diff --git a/src/graphics/skid_marks.cpp b/src/graphics/skid_marks.cpp index 497047bf4..620d0e5b7 100644 --- a/src/graphics/skid_marks.cpp +++ b/src/graphics/skid_marks.cpp @@ -21,18 +21,17 @@ #include "config/stk_config.hpp" #include "graphics/central_settings.hpp" -#include "graphics/material_manager.hpp" -#include "karts/abstract_kart.hpp" -#include "karts/skidding.hpp" -#include "modes/world.hpp" #include "graphics/material.hpp" +#include "graphics/material_manager.hpp" #include "graphics/sp/sp_dynamic_draw_call.hpp" #include "graphics/sp/sp_per_object_uniform.hpp" #include "graphics/sp/sp_shader.hpp" #include "graphics/sp/sp_shader_manager.hpp" #include "graphics/sp/sp_texture_manager.hpp" - #include "graphics/sp/sp_uniform_assigner.hpp" +#include "karts/abstract_kart.hpp" +#include "karts/skidding.hpp" +#include "modes/world.hpp" #include "physics/btKart.hpp" #include "utils/mini_glm.hpp" @@ -116,8 +115,18 @@ void SkidMarks::update(float dt, bool force_skid_marks, Vec3 raycast_right; Vec3 raycast_left; - vehicle->getVisualContactPoint(m_kart.getSkidding()->getVisualSkidRotation(), - &raycast_left, &raycast_right); + vehicle->getVisualContactPoint(m_kart.getSmoothedTrans(), &raycast_left, + &raycast_right); + + btTransform smoothed_inv = m_kart.getSmoothedTrans().inverse(); + Vec3 lc_l = smoothed_inv(raycast_left); + Vec3 lc_r = smoothed_inv(raycast_right); + btTransform skidding_rotation = m_kart.getSmoothedTrans(); + skidding_rotation.setRotation(m_kart.getSmoothedTrans().getRotation() * + btQuaternion(m_kart.getSkidding()->getVisualSkidRotation(), 0.0f, 0.0f)); + raycast_left = skidding_rotation(lc_l); + raycast_right = skidding_rotation(lc_r); + Vec3 delta = raycast_right - raycast_left; // The kart is making skid marks when it's: @@ -158,17 +167,10 @@ void SkidMarks::update(float dt, bool force_skid_marks, // but it produces good enough results float distance = (newPoint - start).length(); - Vec3 p1 = raycast_left - delta; - Vec3 p2 = raycast_left + delta; - Vec3 p3 = raycast_right - delta; - Vec3 p4 = raycast_right + delta; - m_kart.transformSmoothedPosition(&p1); - m_kart.transformSmoothedPosition(&p2); - m_kart.transformSmoothedPosition(&p3); - m_kart.transformSmoothedPosition(&p4); - - m_left.back()->add(p1, p2, m_kart.getNormal(), distance); - m_right.back()->add(p3, p4, m_kart.getNormal(), distance); + m_left.back()->add(raycast_left-delta, raycast_left+delta, + m_kart.getNormal(), distance); + m_right.back()->add(raycast_right-delta, raycast_right+delta, + m_kart.getNormal(), distance); return; } @@ -198,22 +200,15 @@ void SkidMarks::update(float dt, bool force_skid_marks, m_right.erase(m_right.begin()); } - Vec3 p1 = raycast_left - delta; - Vec3 p2 = raycast_left + delta; - Vec3 p3 = raycast_right - delta; - Vec3 p4 = raycast_right + delta; - m_kart.transformSmoothedPosition(&p1); - m_kart.transformSmoothedPosition(&p2); - m_kart.transformSmoothedPosition(&p3); - m_kart.transformSmoothedPosition(&p4); - m_left.emplace_back( - new SkidMarkQuads(p1, p2, m_kart.getNormal(), m_material, m_shader, - m_avoid_z_fighting, custom_color)); + new SkidMarkQuads(raycast_left - delta, raycast_left + delta, + m_kart.getNormal(), m_material, m_shader, m_avoid_z_fighting, + custom_color)); m_right.emplace_back( - new SkidMarkQuads(p3, p4, m_kart.getNormal(), m_material, m_shader, - m_avoid_z_fighting, custom_color)); + new SkidMarkQuads(raycast_right - delta, raycast_right + delta, + m_kart.getNormal(), m_material, m_shader, m_avoid_z_fighting, + custom_color)); m_skid_marking = true; } // update diff --git a/src/karts/moveable.hpp b/src/karts/moveable.hpp index 2c7d83431..0c629761c 100644 --- a/src/karts/moveable.hpp +++ b/src/karts/moveable.hpp @@ -171,12 +171,6 @@ public: static std::string unused("unused"); return unused; } - // ------------------------------------------------------------------------ - void transformSmoothedPosition(Vec3* position) const - { - Vec3 p = m_transform.inverse()(*position); - *position = m_smoothed_transform(p); - } }; // class Moveable diff --git a/src/physics/btKart.cpp b/src/physics/btKart.cpp index 76105d1ff..8873a0c5b 100644 --- a/src/physics/btKart.cpp +++ b/src/physics/btKart.cpp @@ -93,7 +93,7 @@ btWheelInfo& btKart::addWheel(const btVector3& connectionPointCS, btWheelInfo& wheel = m_wheelInfo[getNumWheels()-1]; - updateWheelTransformsWS( wheel , false ); + updateWheelTransformsWS(wheel, getChassisWorldTransform(), false); updateWheelTransform(getNumWheels()-1,false); m_forwardWS.resize(m_wheelInfo.size()); @@ -145,7 +145,7 @@ void btKart::updateWheelTransform(int wheelIndex, bool interpolatedTransform) { btWheelInfo& wheel = m_wheelInfo[ wheelIndex ]; - updateWheelTransformsWS(wheel,interpolatedTransform); + updateWheelTransformsWS(wheel, getChassisWorldTransform(), interpolatedTransform); btVector3 up = -wheel.m_raycastInfo.m_wheelDirectionWS; const btVector3& right = wheel.m_raycastInfo.m_wheelAxleWS; btVector3 fwd = up.cross(right); @@ -191,22 +191,22 @@ void btKart::resetSuspension() // ---------------------------------------------------------------------------- void btKart::updateWheelTransformsWS(btWheelInfo& wheel, + btTransform chassis_trans, bool interpolatedTransform, float fraction) { wheel.m_raycastInfo.m_isInContact = false; - btTransform chassisTrans = getChassisWorldTransform(); if (interpolatedTransform && (getRigidBody()->getMotionState())) { - getRigidBody()->getMotionState()->getWorldTransform(chassisTrans); + getRigidBody()->getMotionState()->getWorldTransform(chassis_trans); } wheel.m_raycastInfo.m_hardPointWS = - chassisTrans( wheel.m_chassisConnectionPointCS*fraction ); - wheel.m_raycastInfo.m_wheelDirectionWS = chassisTrans.getBasis() * + chassis_trans( wheel.m_chassisConnectionPointCS*fraction ); + wheel.m_raycastInfo.m_wheelDirectionWS = chassis_trans.getBasis() * wheel.m_wheelDirectionCS ; - wheel.m_raycastInfo.m_wheelAxleWS = chassisTrans.getBasis() * + wheel.m_raycastInfo.m_wheelAxleWS = chassis_trans.getBasis() * wheel.m_wheelAxleCS; } // updateWheelTransformsWS @@ -267,7 +267,7 @@ btScalar btKart::rayCast(unsigned int index, float fraction) m_chassisBody->getBroadphaseHandle()->m_collisionFilterGroup = 0; } - updateWheelTransformsWS( wheel,false, fraction); + updateWheelTransformsWS(wheel, getChassisWorldTransform(), false, fraction); btScalar max_susp_len = wheel.getSuspensionRestLength() + wheel.m_maxSuspensionTravel; @@ -369,7 +369,7 @@ btScalar btKart::rayCast(unsigned int index, float fraction) * \param n Index of the wheel, must be 2 or 3 since only the two rear * wheels define the visual position */ -void btKart::getVisualContactPoint(float visual_rotation, +void btKart::getVisualContactPoint(const btTransform& chassis_trans, btVector3 *left, btVector3 *right) { btAssert(m_vehicleRaycaster); @@ -387,7 +387,7 @@ void btKart::getVisualContactPoint(float visual_rotation, { // Map index 0-1 to wheel 2-3 (which are the rear wheels) btWheelInfo &wheel = m_wheelInfo[index]; - updateWheelTransformsWS(wheel, false); + updateWheelTransformsWS(wheel, chassis_trans, false); btScalar max_susp_len = wheel.getSuspensionRestLength() + wheel.m_maxSuspensionTravel; @@ -396,17 +396,9 @@ void btKart::getVisualContactPoint(float visual_rotation, // hits the ground. btScalar raylen = max_susp_len + 0.5f; btVector3 rayvector = wheel.m_raycastInfo.m_wheelDirectionWS * (raylen); - btTransform chassisTrans = getChassisWorldTransform(); - if (getRigidBody()->getMotionState()) - { - getRigidBody()->getMotionState()->getWorldTransform(chassisTrans); - } - btQuaternion q(visual_rotation, 0, 0); - btQuaternion rot_new = chassisTrans.getRotation() * q; - chassisTrans.setRotation(rot_new); btVector3 pos = m_kart->getKartModel()->getWheelGraphicsPosition(index); pos.setZ(pos.getZ()*0.9f); - btVector3 source = chassisTrans(pos); + btVector3 source = chassis_trans(pos); btVector3 target = source + rayvector; btVehicleRaycaster::btVehicleRaycasterResult rayResults; @@ -1046,7 +1038,7 @@ void btKart::adjustSpeed(btScalar min_speed, btScalar max_speed) //vehicles towards the ground at the start of a race btScalar btKart::rayCast(btWheelInfo& wheel, const btVector3& ray) { - updateWheelTransformsWS( wheel,false); + updateWheelTransformsWS(wheel, getChassisWorldTransform(), false); btScalar depth = -1; diff --git a/src/physics/btKart.hpp b/src/physics/btKart.hpp index 28ca8bfea..235817465 100644 --- a/src/physics/btKart.hpp +++ b/src/physics/btKart.hpp @@ -121,6 +121,10 @@ private: void defaultInit(); btScalar rayCast(btWheelInfo& wheel, const btVector3& ray); + void updateWheelTransformsWS(btWheelInfo& wheel, + btTransform chassis_trans, + bool interpolatedTransform=true, + float fraction = 1.0f); public: @@ -155,19 +159,15 @@ public: bool isFrontWheel); const btWheelInfo& getWheelInfo(int index) const; btWheelInfo& getWheelInfo(int index); - void updateWheelTransformsWS(btWheelInfo& wheel, - bool interpolatedTransform=true, - float fraction = 1.0f); void updateAllWheelTransformsWS(); void setAllBrakes(btScalar brake); void updateSuspension(btScalar deltaTime); virtual void updateFriction(btScalar timeStep); -public: void setSliding(bool active); void instantSpeedIncreaseTo(btScalar speed); void adjustSpeed(btScalar min_speed, btScalar max_speed); void updateAllWheelPositions(); - void getVisualContactPoint(float visual_rotation, + void getVisualContactPoint(const btTransform& chassis_trans, btVector3 *left, btVector3 *right); // ------------------------------------------------------------------------ /** Returns true if both rear visual wheels touch the ground. */