Summing floats went terribly wrong
Im calculating torque applied to spaceship if one of its engines is damaged. Engines are not aligned with center of mass, so it makes sence. Since torque equals force*sin() *radiusVector I use precalculated ratio to simplify calculations and only thing that changes is force, which is multiplied on engine effectiveness. So, I hope I managed to explain that part, because now things became complicated. Left engine has ratio of -0.678 and right has 0.678. They have equal force and on full effectiveness (1.0f) each engine must result in equal torques, which in sum must give me 0. but they dont. When I calculate them inside of Debug.Log they are equal, but when I use += I get a difference around 0.5*10^-7. Alright - thats may be just an error in floats. But when ship moves in other way I add backwards ratio and get a resulting torque of ~9, which is totally wrong. In attached code: Engine is a class holding thrust force values in vector3 axisThrust = (75,0,0), precalculated torqueRatio and cashed Module.cs with GetEffectiveness which returns a float, in that case always 1. thrustForward1D is an input value, 1 for forward and -1 for backwards. Debugs perform same calculations but give different results and I cant understand why
void CheckTorque()
{
differenceTorque = 0;
foreach (Engine engine in engines)
{
if (thrustForward1D != 0)
{
differenceTorque += engine.engineTorqueRatio * engine.axisThrust.x * engine.module.GetEffectiveness();
if (thrustForward1D < 0)
{
differenceTorque *= engine.backwardForceRatio;
Debug.Log(engine.engineTag + " " + engine.engineTorqueRatio * engine.axisThrust.x * engine.module.GetEffectiveness() * engine.backwardForceRatio);
}
else
{
Debug.Log(engine.engineTag + " " + engine.engineTorqueRatio * engine.axisThrust.x * engine.module.GetEffectiveness());
}
}
}
}
Your answer
Follow this Question
Related Questions
SetSpeed float C# 1 Answer
Mathf.Round ? [C#] 2 Answers
Car is Flying instead of driving 1 Answer
subtracting from float in a user made function 0 Answers
I need to synchronize 2 float values of each character to another script, 0 Answers