I'm having a random numeric issue with this code

Hello, my problem is that my code is sometimes working and soemtimes not, I suspect its a precision error and I would like to find a solution to it.
Here's my code:

.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <math.h>
#include <algorithm>    // std::max
#include <cmath>        // std::abs

class MyWheel
{
    public:
        MyWheel(void);
        ~MyWheel(void);

        float radius;
        float inertia;
	float drivingTorque;
	float brakingTorque;
		
        void Initialise();
        void Destroy();
        void Reset();
        float GetRotation();
        float GetOmega();
        void Update(float dT);
    private:
        float timeStep;
	float rotation;
        float omega;
	float longForce;
};

.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "MyWheel.h"

#define Deg2Rad(deg)  ((deg) * M_PI / 180)
#define Rad2Deg(rad)  ((rad) * 180 / M_PI)

template <typename T> float sgn(T val) {
    return (T(0) < val) - (val < T(0));
}

MyWheel::MyWheel(void)
{
    Initialise();
}
MyWheel::~MyWheel(void)
{
    Destroy();
}
void MyWheel::Initialise()
{
	radius = 0.3f;
    Reset();
}
void MyWheel::Destroy()
{
    Reset();
}
void MyWheel::Reset()
{
    timeStep = 0.0f;
    omega = 0.0f;
    rotation = 0.0f;
    forwardVel = 0.0f;
    absForwardVel = 0.0f;
    drivingTorque = 0.0f;
    brakingTorque = 0.0f;
}

float MyWheel::GetRotation()
{
    return Rad2Deg(rotation);
}

float MyWheel::GetOmega()
{
    return omega;
}

void MyWheel::Update(float dT)
{
    timeStep = dT;

    float drivingAngularDelta = drivingTorque * timeStep / inertia;
    float brakingAngularDelta = brakingTorque * timeStep / inertia;
	
    float roadTorque = longForce * radius;
	float roadAngularDelta = roadTorque * timeStep / inertia;

    omega -= roadAngularDelta;
    omega += drivingAngularDelta;
    if (fabs(omega) > brakingAngularDelta)
    {
        omega  -= brakingAngularDelta * sgn(omega);
    }
    else
    {
        omega = 0.0f;
    }	
    rotation += omega * timeStep;
}


The issue comes in with "omega".

I have a vector of 4 MyWheel objects.
I have a DLL that i am creating for a C#
When I Update the wheel array I sometimes(pretty often actually) get a error with omega, its 0.0 or it's something like 560000000000000000000000... or sometimes the update is completely out of sync. For example 3 wheels are working ok, but one of the wheels is updating slower or sometimes faster.

Also a funny thing I noticed is that the destructor doesnt work.
For example 3 runs work okay, from the fourth run the omega gets messed up And from that point after every run is messed up and gets worse every time. It's liek the dll is not unloading.

Please help me because I've been killing myself with these issues for 3 days allready. Thanks!
Last edited on
I tryed isnan and isinf but they didnt fix my issue. So any ideas?
Topic archived. No new replies allowed.