Current position: x=NaN y=NaN z=NaN

Blitz3D Forums/Blitz3D Programming/Current position: x=NaN y=NaN z=NaN

BlitzProg(Posted 2011) [#1]
I am not sure whether this is a Blitz3D glitch or not.

Today i've re-encountered a problem that occured twice in a row after 10 seconds of execution of my Blitz3d game. Now i'm unable to reproduce it but I quite fear its return when the game reaches a release version...

I have "re-encountered" this problem because I did experience the same glitch several YEARS ago with another program.

---

What happens:
I'm controlling my character nicely, suddenly everything vanish into darkness (or into cameraclscolor)
when debugging, I notice that all coordinates of my character changed into NaN. It can happen at any time, even if i'm not moving...

Anyone experienced something similar? And if so do you know why it happens?

Thanks :)

Edit: From what I saw this is not exactly right. debugger still indicates the old position until i move, after everything went into darkness and my camera get blind --- Right after I move, all of my coordinates become NaN.

EDIT:

Is this the case for you?


;In debug it should work fine
;In non debug OH SHIT I DIVIDED BY 0

;*****************************************************************************
;THE NAN GLITCH DETECTOR
;*****************************************************************************

Global enable_proof_glitch = True ;<= Set to true to stop the game after NaN coordinates
Function NaNCheck(entity,debugpoint)
	Zero#=0.0
	NaN#=0.0/Zero 
	If Int(NaN)=Int(EntityX(entity)) 
		While Not MouseDown (1)
			Text 20,70,"!NaN Entity! --- Entry point "+debugpoint
			Flip
		Wend
		End
	EndIf
End Function

;*****************************************************************************
;CAMERA_FUNCTIONS
;*****************************************************************************
Global dist=64
Dim trace#(dist,2)
Global current=1

Function movecam(camera,point)
	PositionEntity camera,trace(current,0),trace(current,1),trace(current,2)
	trace(current,0)=EntityX(point)
	trace(current,1)=EntityY(point)+20
	trace(current,2)=EntityZ(point)
	PointEntity camera,point
	current=((current+1) Mod dist)
End Function

Function fillcamposition(point)
	For i=0 To dist-1
		trace(i,0)=EntityX(point)
		trace(i,1)=EntityY(point)+20
		trace(i,2)=EntityZ(point)
	Next
End Function

;*****************************************************************************
;ENTITIES SETUP
;*****************************************************************************

Global demo_ship

Graphics3D 800,600,16,2
SetBuffer BackBuffer()
AmbientLight 255,255,255


demo_ship=CreateCube()
EntityType demo_ship,1
PositionEntity demo_ship,1000,640,000
TurnEntity demo_ship,45,0,0

camera=CreateCamera()
CameraRange camera,0.5,5000
fillcamposition(demo_ship)

light=CreateLight(1)
LightRange light,20

terrain=CreateTerrain(1)
EntityType terrain,2
TerrainDetail terrain,2000,True
ScaleEntity terrain,2000,1,2000
EntityColor terrain,64,32,16

Collisions 1,2,2,2

MoveMouse GraphicsWidth()/2,GraphicsHeight()/2

;*****************************************************************************
;MAIN LOOP
;*****************************************************************************

While Not MouseDown(1)
	mxs#=-MouseXSpeed()*.25
	mys#=MouseYSpeed()*.25
	
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	TurnEntity demo_ship,mys#,mxs#,0
	RotateEntity demo_ship,EntityPitch#(demo_ship),EntityYaw#(demo_ship),0

	If enable_proof_glitch Then NaNCheck(demo_ship,1)
	
	MoveEntity demo_ship,0,0,0.5+(2.5*MouseDown(2))
	If enable_proof_glitch Then NaNCheck(demo_ship,2) ; <= NaN triggered in non-Debug.
	
	movecam(camera,demo_ship)
	If enable_proof_glitch Then NaNCheck(demo_ship,3)
	
	
	UpdateWorld
	RenderWorld
	Text 20,20,"X:"+(EntityX(demo_ship))+"  Y:"+(EntityY(demo_ship))+"  Z:"+(EntityZ(demo_ship))
	Text 20,50,"Left click to quit, Right click to accelerate"
	

	
	Flip
Wend
End


Last edited 2011


Ross C(Posted 2011) [#2]
How far are you from the world origin, ie (0,0,0)? As you move further from the origin point, your accuracy drops. Past a certain point, you may get strange things happening.

Last edited 2011


BlitzProg(Posted 2011) [#3]
my world is contained inside a cube ranging from 0,0,0 to 100,100,100 approx.

I guess it's pretty far from 2147483647 I get from converting these NaN values to Integers :P


Yasha(Posted 2011) [#4]
Check your divisions. The most common cause of NaN is a zero divided by zero. Got anywhere this could occur?


Rroff(Posted 2011) [#5]
You've probably got a division by 0 somewhere and/or you need to handle the calculations as floating point even if your using integers for the actual resulting value.

Last edited 2011


BlitzProg(Posted 2011) [#6]
Yeah. Each time it happens, I get reminded that meme "I divided by 0! Oh shi-"

Considering the glitch also happened without the need of moving I find that strange...

I checked to make sure, but after all "/" signs there is no variable, only constant values. :(

When the game is running fine, The values display correctly (they are recalculated to give me the exact position inside a 3D array)


using this and hiting enter any number of time:
	divbyzero=0
	If KeyHit(28) Then PositionEntity patou,EntityX(patou)/divbyzero,EntityY(patou)/divbyzero,EntityZ(patou)/divbyzero

results in this:


However, with this:
	divbyzero=0
	If KeyHit(28) Then PositionEntity patou,Float(divbyzero)/divbyzero,Float(divbyzero)/divbyzero,Float(divbyzero)/divbyzero

I reach the exact same situation i'm tracking:


Last edited 2011


BlitzProg(Posted 2011) [#7]
More info, I've found back the original project where i got it happening.

In that one, that NaN glitch nearly immediatly occurs, but it DOESN'T when i'm on debug mode. I'm going to try to make a code out of this.

Edit: Check top post for code example.

Last edited 2011


Floyd(Posted 2011) [#8]
This makes no sense at all.

I couldn't see where you were doing any problematic divisions. But the trouble seems to happen at MoveEntity. So I tried storing the 2.5*MouseDown(2) in a variable instead of using it directly in MoveEntity. Variations on this theme produced baffling results. But easily the most confusing came from not using the stored value at all, but just making a superfluous call to MouseDown like this:

	MouseDown(2)  ;  check MouseDown, ignore result
	MoveEntity demo_ship,0,0,0.5 + 2.5*MouseDown(2)

This seems to eliminate the NaN problem. I would swear that is impossible, but there it is.

Note that I never touch the right mouse button so MouseDown(2) should always have been zero.


BlitzProg(Posted 2011) [#9]
Many thanks!! lol!

I really don't understand it either. It looks like the glitch is being circumvented by another glitch.

Again, thanks for this :)


Warner(Posted 2011) [#10]
Yes, for some reason, the formula:
spd# = 0.5+(2.5*MouseDown(2))

Seems to give "NaN" as a result sometimes.
If you want to check if a value is NaN, you can use this:
If Str$(spd) = "NaN" ..

Maybe there is an division by zero error inside MouseDown?


Ross C(Posted 2011) [#11]
What value does mousedown() actually return? If it's just true/false (which I believe it is), then just use an IF statement to determine whether the mouse is being held and set another variable to true or 1, and us that as your multplier/divisor.

Very strange error...


big10p(Posted 2011) [#12]
This is an odd one. I don't suppose expressly casting the the result of MouseDown - with Float(MouseDown(2)) - makes any difference?


Warner(Posted 2011) [#13]
Internally, MouseDown is linked to an array. The array is filled with bools by Events. MouseDown converts such an bool into an integer. So either that conversion sometimes doesn't work, or sometimes the array contains undefined information.
Either way it seems to be safer to use If MouseDown() and If KeyDown() rather than using their values in a formula.


BlitzProg(Posted 2011) [#14]
If MouseDown(2) Then MoveEntity patou,0.15,0,0

Adding another instance of mousedown(2) didn't solve the problem and it occured again! (NaN,NaN,NaN)

But thanks to this idea, I think I have discovered the problem and I have successfully reproduced the exact same effect: Non-NaN positions but still everything disapearing, Then 100% chance NaN position with Move Entity.

If KeyHit(28) Then RotateEntity patou,0,Float(divbyzero)/divbyzero,0


Rotations values become NaN. Because the camera use these values to follow my character, everything disappear.

In the game:
RotateEntity patou,0,EntityYaw(patou),EntityRoll(patou)-MouseYSpeed()/2
TurnEntity patou,0,-MouseXSpeed(),0,1 


So MouseXSpeed and MouseYSpeed could also be broken... Which would cause on some very rare occasions rotation values to become NaN.

I'm looking suspiciously at my mouse now... xD

Last edited 2011


Adam Novagen(Posted 2011) [#15]
NaN, unless I'm greatly mistaken, is short for "Not a Number," and I believe it sometimes (though not always) results from an internal Blitz glitch. I once and ONLY once encountered this, though with Yaw/Pitch/Roll values rather than X/Y/Z. In MY case, it was being generated by AlignToVector(), a native Blitz command which I'd never had problems with before. Replacing the command with a RotateEntity() formula solved the problem, yet to this day (over a year later) I have no idea why. My best guess was an extremely rare internal Blitz glitch.


BlitzProg(Posted 2011) [#16]
yes NaN is the short for Not a Number, caused by exceptionnal cases such as Zero divided by Zero or Infinity divided by infinity. It's the way for your computer to answer you by "I don't know what is the answer"

(Like when you have to solve the equation of the kind 0x=0 to find a number, you can't answer because any number can fit :D)

I'm interested in knowing what could be causing this though. And yes, i'm fairly sure this IS a blitz glitch now, since the code do not produce NaN result in debug, but it does on release. :)


Charrua(Posted 2011) [#17]
hi

You can test, anywhere in your code with:

if myFloat="Nan" then
...; probably write in a file current variables involved values and task state
else
...; what normally your program should do
end if



Juan


Serpent(Posted 2011) [#18]
I had problems with this earlier. I setup a simple "orbit camera around player" thing with something like this:
TurnEntity CameraPivot, MouseXSpeed(), MouseYSpeed(), MouseZSpeed()

or something like that anyway. I had the same problem - occasionally when turning through certain angles, the rotation values (Pitch, Yaw, Roll) ended up as Nans...
I had the same problem (I think) using
RotateEntity X, EntityPitch(X) + MouseXSpeed(), etc.

but this worked:
Local NewPitch#, NewYaw#
NewPitch = NewPitch + MouseYSpeed() * 0.1
NewYaw = NewYaw + MouseXSpeed() * -0.1
RotateEntity Camera, 0, 0, 0
TurnEntity Camera,NewPitch,NewYaw, 0


Seems to be some problem with the math in either RotateEntity or TurnEntity or both...

Last edited 2011


Adam Novagen(Posted 2011) [#19]
I guess it just comes down to workarounds then, and figuring out how to handle occurrences. Hopefully I won't have to wrestle with it again any time soon. ;P