Hello. I’m currently working on a car chassis, and I’m trying to implement gears / a transmission in my car chassis, and when I get the RPM of the car I have to get the speed of the wheels, and put the speed through a formula to get the engine RPM.
There is one problem though. Cars have four wheels, not one. So I decided to get the average speed of all 4 wheels, by adding them all up, and dividing them by the amount of wheels on the car (Let me know if my math isn’t correct).
The problem is, all my speeds are multiplied by dt (dt stands for delta time). dt Is basically how long it is between each frame (I’m doing some other calculations every physics frame). So to fix this, I divide by dt in order to get the original speed. Aaaaand… my car get’s the symptoms of a divide by 0 error (Things start disappearing, can’t move camera, car teleports around, etc…).
But when I print speed / dt it never prints nan a single time. But when I update my formula so it divides by dt, and then print speed / dt I get nan occasionally.
Here is what it prints:
When the script is this
local speed = 0
for i, v in pairs(speeds) do
print(v / dt)
speed = speed + (v / dt)
end
speed = speed / #speeds
speeds = {}
I’ve heard somewhere that roblox cannot support fast rotating parts like spinning wheels because of game engine limitations, so unfortunately that might not be possible.
Thing is you already are measuring speed by the thrusters .Velocity property which represents the “virtual wheel” which is odd. When you multiple speed by dt you will get distance traveled and not speed.
Just remove the dt from the initial multiplication which is unnecessary?
table.insert(speeds, #speeds + 1, thruster.Velocity:Dot(thruster.CFrame.LookVector))--no dt here
Moreover, dt is a very small number if you have a good laptop and hence is prone to NAN errors as it’s close to zero if you have a good laptop or are even using an fps unlocker, plus floating point number truncation is most likely the case here 0.001 basically becomes 0 due to how the numbers truncate I believe.
speed is just math.abs(velocity.Magnitude) btw since speed is a scalar and not a vector with a direction.
I think it’s because of the speeds table. Because you are swapping the variable speeds with a new table the original table for speeds in the update thruster is still inserting into the original speeds table and not the new one, that’s just my hypothesis though.
Hence nothing is inserted in the speeds table and divide by zero Nan error occurs.
Edit: Nvm, tested it out in studio still remains 4 so it should be ok.