EntityX, Y, and Z return NaN

Blitz3D Forums/Blitz3D Beginners Area/EntityX, Y, and Z return NaN

ColeE(Posted 2015) [#1]
This function that I wrote seems fine to me and I've been looking at it for hours and I can't find the problem. I have an opponent ship in a game I'm working on and this function is supposed to be used to have it rotate and maneuver toward the player ship. However when using it, the opponent ship is nowhere to be found, so, just now, I decided to display its coordinated on the screen, and I got NaN for each of them, which I find very confusing (This does not occur if the function is not called just to clarify). So, here it is... and sorry it's so ugly looking... hopefully you can infer enough about the types and variable to understand it...

[code]
Function EnemyControl()
TurnEntity enemyShip, (DeltaPitch(enemyShip, myShip)/Abs(DeltaPitch(enemyShip, myShip)))*NPCShip(currentEnemyShip%)\turnSpeed#, (DeltaYaw(enemyShip, myShip)/Abs(DeltaYaw(enemyShip, myShip)))*NPCShip(currentEnemyShip%)\turnSpeed#, (DeltaYaw(enemyShip, myShip)/Abs(DeltaYaw(enemyShip, myShip)))*NPCShip(currentEnemyShip%)\turnSpeed#
If Abs(EntityRoll(enemyShip))>Abs(NPCShip(currentEnemyShip%)\maxTurnAngle%) Then RotateEntity enemyShip, EntityPitch(enemyShip), EntityYaw(enemyShip), EntityRoll(enemyShip)/Abs(EntityRoll(enemyShip))*NPCShip(currentEnemyShip%)\maxTurnAngle%
If Abs(EntityPitch(enemyShip))>Abs(NPCShip(currentEnemyShip%)\maxTurnAngle%) Then RotateEntity enemyShip,EntityPitch(enemyShip)/Abs(EntityPitch(enemyShip))*NPCShip(currentEnemyShip%)\maxTurnAngle%, EntityYaw(enemyShip), EntityRoll(enemyShip)
If EntityDistance(enemyShip, myShip) > 24 Then MoveEntity enemyShip, 0, 0, NPCShip(currentEnemyShip%)\speed#
End Function


Stevie G(Posted 2015) [#2]
You will get a division by zero on the first Turnentity if the deltapitch or deltayaw is zero. This would cause a Nan which will then cause the entity position to become a Nan. Do you pick anything up when running in debug mode? You could easily test for and catch this scenario.

It may be unrelated but I have found that if you use a global helper pivot and rotate and position this to calculate things hundred of times each frame it will eventually result in NAN's. The solution for me was to reset the position and rotation of this pivot each frame, i.e. Rotateentity Pivot, 0,0,0, PositionEntity Pivot,0,0,0 and scaleentity Pivot,0,0,0.

Stevie


ColeE(Posted 2015) [#3]
Thanks, very helpful