Dividing by a number between 0, and 1 causes my vehicle to disappear

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 = {}

It prints this:

When the script is this

	local speed = 0
	for i, v in pairs(speeds) do
		print(v / dt)
		speed = speed + (v)
	end
	speed = speed / #speeds

	speeds = {}

It prints this:

Here is the file in case you want it.
Jeep2.rbxl (392.2 KB)

Can someone please help me get my issue fixed?

1 Like

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.

3 Likes

For some reason, when I do this. I still get a divide by zero error symptom. I even removed the (v / dt), and it still did this.

EDIT: I even tried this local speed = car.Chassis.CFrame:PointToObjectSpace(car.Chassis.Velocity).Magnitude. But to no avail :frowning: .

EDIIT2: The funny thing is, when I use these two lines of code:

print(v / dt)
speed = speed + (v)

The print never prints “nan”.

But when I use these 2 lines of code:

print(v / dt)
speed = speed + (v / dt)

It does end up printing “nan”, and the car breaks too.

1 Like

Wouldn’t you want to multiply by delta time instead of divide to get the modified time?

speed = speedPerSecond * dt

Also why would you want your speed to be filtered by delta time? Typically people don’t do that.

I’m trying to get the speed of the car in Studs per second, if I multiply by dt, then I will be getting the Speed of the car in Studs per dt seconds.

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.

At the end of the code, I set speeds to an empty table. So there are no other values in it.