multitheftauto / mtasa-blue

Multi Theft Auto is a game engine that incorporates an extendable network play element into a proprietary commercial single-player game.
https://multitheftauto.com
GNU General Public License v3.0
1.4k stars 435 forks source link

getVehicleCurrentGear() returns gear 1 while driving very slow backwards #503

Open Einheit-101 opened 6 years ago

Einheit-101 commented 6 years ago

Describe the bug

As the summary says, i can drive very slow backwards but getVehicleCurrentGear does return gear 1 instead of 0.

To Reproduce

  1. Take a slow car, and drive one meter forward (gear = 1)
  2. Stop and use the handbrake (so you dont get too fast) and drive some millimeters backwars.
  3. Now open console and use runcode: crun getVehicleCurrentGear(getPedOccupiedVehicle(localPlayer))
  4. It may output "1" as current gear. If you were too fast, it does output 0.

Expected behavior This function should always return 0 when driving backwards.

Multi Theft Auto v1.5.6-release-14394

Additional context Workaround:

local matrix = getElementMatrix(veh) local vx, vy, vz = getElementVelocity(veh) local velocityDir = Vector3(vx, vy, vz) velocityDir:normalize() local forwardDir = Vector3(matrix[2][1], matrix[2][2], matrix[2][3]) local dotproduct = velocityDir:dot(forwardDir) if dotproduct > 1 then dotproduct = 1 end -- dot sometimes returns values slightly higher/lower than 1/ -1 --this seems to be a bug, so we clamp the values to avoid errors if dotproduct < -1 then dotproduct = -1 end local drivingForwards = math.acos(dotproduct) < math.pi / 2

instead of if getVehicleCurrentGear(veh) == 0 then you can now use if not drivingForwards then and there will be no bug.

From https://bugs.mtasa.com/view.php?id=8562

Pirulax commented 3 years ago

Probably related to some floating point accuracy rounding in GTA. @TheNormalnij maybe has more idea about this tho.

TheNormalnij commented 3 years ago

cTransmission::InitGearRatios at 0x6D0460

  v1->m_aGears[offsetof(cTransmission, m_aGears)].m_fChangeUpVelocity = -0.0099999998;
  v1->m_aGears[offsetof(cTransmission, m_aGears)].m_fChangeDownVelocity = v8;
  v1->m_aGears[1].m_fChangeDownVelocity = -0.0099999998;

constant -0.0099999998 can be changed