getPhysicsRotation() returns (NaN, NaN, NaN, NaN)

Hello,



My ship simulator application runs fine, except when I try to start simulating waves on a ship. The ships starts rolling as it should, but suddenly the screen turns black. Below the code which I use to apply forces on the ship to simulate wave rolling:



[java]

// Simulate waves:

public void simulateWaves()

{

// Simulate roll:

if (angles[0] > -maxRoll && angles[2] < maxRoll && rollDir == 0)

{

rollDir = 1;

}

if (angles[0] > maxRoll && rollDir == 1)

{

rollDir = -1;

}

if (angles[0] < -maxRoll && rollDir == -1)

{

rollDir = 1;

}

rollDirection.x = 0.0f;

rollDirection.y = 0.0f;

rollDirection.z = rollDirrollForce;

ship_phy.getPhysicsRotation().multLocal(rollDirection);

ship_phy.applyForce(rollDirection, waveForce);



// Simulate Pitch:

if (angles[2] > -maxPitch && angles[2] < maxPitch && pitchDir == 0)

{

pitchDir = 1;

}

if (angles[2] > maxPitch && pitchDir == -1)

{

pitchDir = 1;

}

if (angles[2] < -maxPitch && pitchDir == 1)

{

pitchDir = -1;

}

pitchDirection.x = pitchDir
pitchForce;

pitchDirection.y = 0.0f;

pitchDirection.z = 0.0f;

System.out.println(ship_phy.getPhysicsRotation());

ship_phy.getPhysicsRotation().multLocal(pitchDirection);

//System.out.println(angles[2]);

//System.out.println(pitchDirection);

ship_phy.applyForce(pitchDirection, waveForce);

}

[/java]



When I check the values of ‘System.out.println(ship_phy.getPhysicsRotation())’ it starts with numbers, but as soon as the value of angles[2] swaps from 0 to -0.5*PI, the ‘System.out.println(ship_phy.getPhysicsRotation())’ returns (NaN, NaN, NaN, NaN). In angles[], I store the rotation of the ship over the 3 axis with:

[java]

ship_phy.getPhysicsRotation(shipsRot); // Get the actual rotation of the ship.

shipsRot.toAngles(angles);

[/java]



When I disable the code for pitch simulation, it works okay, so it has to do with the pitch, not with the rolling.



I have been changing some things in my code the last couple of days, but I haven’t changed this part and I am almost positive it worked a few days ago. Has anything changed which causes this, or am I missing something?



Basically, what I am trying to do is apply a force on the ship to make it roll (pitch and roll) to simulate the wave movement. It has worked a few days ago, but again, maybe I am missing something, although I don’t know how getPhysicsRotation() can return ‘Not a Number’.



Well, I hope this makes any sense to somebody here, but considering all the good sollutions, I am sure someone can make something out of this :slight_smile:



If required, I can post all the code, but I don’t think that would be necessary at this point.



With kind regards,



Husky

Its applyForce(force, location), where location is local to the RigidBody, you probably want to do

[java]

ship_phy.applyForce(pitchDirection.add(waveForce), Vector3f.ZERO);

[/java]

Uhm… no. I think I picked the wrong names:



waveForce is a Vector3f:

waveForce = new Vector3f(0.0f, oceanState*1000000.0f, 0.0f); // Power and direction of the waves.

Basically, just a vector high up in the sky where the force of the wave has to apply to make the ship roll.



rollDirection and pitchDirection are Vector3fs as well to define the axis around which the ship has to roll:

Vector3f rollDirection = new Vector3f(0,0,1);

Vector3f pitchDirection = new Vector3f(1,0,0);



So, I use pitchDirection only to define a force over the x-direction and the force applies on vector waveForce (high above the ship, to generate some momentum).



I can imagine this is not the most common way, but for now it works, except for the NaN. Here is some output from the console showing the result of ‘System.out.println(ship_phy.getPhysicsRotation())’:



(-0.0032529563, -6.0847236E-5, -0.006843114, 0.9999713)

(-0.002952665, -6.3312975E-5, -0.006803149, 0.99997246)

(-0.0026469284, -6.579732E-5, -0.0067623835, 0.9999736)

(-0.0023425866, -6.82573E-5, -0.0067217667, 0.99997467)

(-0.002032906, -7.073388E-5, -0.0066803657, 0.9999756)

(-0.0017113315, -7.326579E-5, -0.006637272, 0.9999765)

(-0.0013915715, -7.577042E-5, -0.006594389, 0.9999773)

(-0.0010668808, -7.828703E-5, -0.006550782, 0.99997795)

(NaN, NaN, NaN, NaN)

(NaN, NaN, NaN, NaN)

(NaN, NaN, NaN, NaN)

(NaN, NaN, NaN, NaN)

(NaN, NaN, NaN, NaN)

(NaN, NaN, NaN, NaN)



As you can see, it first returns the correct values, but then it starts to return NaNs and the screen turns blank.



Husky

1000000.0f ? Thats a bit too high for proper physics calculation, try not to go above 100.000 units.

Well… I use that high values since my ships mass is 2500000 kilos (it is a WW2 destroyer)… I haven’t scaled it, but according to what you are saying, it might be wise to scale everything? My ship is 114 meters long (in JME that is 114 WU). I also propell it with about 11500000 [N].

Any guidelines for proper scaling in this case?



But anyway, shouldn’t getPhysicsRotation() always return values? I don’t understand how it can return NaN? The object always has a rotation right? And since it works fine in the other direction, there must be something which I oversee, I guess.



Regards,



Husky

7 significant numbers, is what flaot gives you, try to stay in 6 rang eand yo are fine,

11 500 000 → 115 000



Basically depending on what else you simulate even weights and foreces around 100-5000 are good enough, and will safe you from a few problems, as long as you scale the forces accordingly it should behave exaclty the same.



NAN might happen if a flaot reachs + - infinity if I’m not mistaken.

Yeah, mind that due to the calculations the internal numbers can get magnitudes smaller or larger than the values you already use.

Okay, thank you for these answers! I will scale everything and see what happends. This forum is really valuable for a happy-hobby-programmer like myself :slight_smile:



I wasn’t aware of this float and its 7 significant numbers shame



Husky

Actually if you come ot the conclusion that you are unable to do your project in float space for some reason, I can send you a 64bit double version of jbullet, however that one is not integrated in the jme core and you need to call everything manually without the binding norman created.



(I use it for space simulation in a jmelless server, jme is ‘only’ used for displaying the local data around the player on the client.)

Thank you, EmpirePhoenix. I will first try to scale everything to see if I can stay within floats reach. If that will not work, I will certainly consider your offer.



My first attempt was to simulate an entire spherical globe, but I gave up on that as soon as I discovered the positioning of the ship was not accurate enough which caused it to ‘jump around’ on the earth surface. So I decided to go for the flat world (makes a lot of things a LOT easier, but I had to simulate the earth radius when it comes to ‘see other ships over the horizon’).