How to get a zero

Blitz3D Forums/Blitz3D Programming/How to get a zero

What's the best way to obtain a zero value on PLAYER_CMOVESPEED#
It starts doing this 1.-e1234 things after a while.

Should floats just be avoided?

;My Code
Graphics3D 640, 480, 0, 2

CAM = CreateCamera()
MoveEntity CAM, 0, 80, 0
TurnEntity CAM, 90, 0, 0



Global PLAYER_MOVESTATE
Global PLAYER_MOVESPEED# = .75
Global PLAYER_CMOVESPEED#
Global PLAYER_MOVEACC# = .01

Global PLAYER_MOVEDIRECTION



Global PLAYER_MESH
PLAYER_MESH = CreateCube()
EntityFX PLAYER_MESH, 1

Create_Grid(32)


Repeat

	
	Move_Player()
	
	DebugLog PLAYER_CMOVESPEED#
	
	RenderWorld()
	Text 0, 0, "Speed: " + PLAYER_CMOVESPEED#
	Text 0, 20, "Move State: " + PLAYER_MOVESTATE
	Flip 

Until KeyHit(1)
End



Function Move_Player()
	If KeyDown(17) Then
		move = 1
		PLAYER_MOVEDIRECTION = 1
	EndIf
	
	If KeyDown(31) Then
		move = 1
		PLAYER_MOVEDIRECTION = -1
	EndIf
	
	
	If KeyDown(30) Then
		turn = 1
	EndIf
	
	If KeyDown(32) Then
		turn = -1
	EndIf
	
	If move = 1 Then
		If PLAYER_MOVEDIRECTION = 1 Then
			PLAYER_CMOVESPEED# = PLAYER_CMOVESPEED# + PLAYER_MOVEACC#
			If PLAYER_CMOVESPEED# > PLAYER_MOVESPEED# Then PLAYER_CMOVESPEED# = PLAYER_MOVESPEED#
		Elseif PLAYER_MOVEDIRECTION = -1
			PLAYER_CMOVESPEED# = PLAYER_CMOVESPEED# - PLAYER_MOVEACC#
			If PLAYER_CMOVESPEED# < -PLAYER_MOVESPEED# Then PLAYER_CMOVESPEED# = -PLAYER_MOVESPEED#
		EndIf
	Else
		If PLAYER_CMOVESPEED < 0 Then
			PLAYER_CMOVESPEED# = PLAYER_CMOVESPEED# + PLAYER_MOVEACC#
		ElseIf PLAYER_CMOVESPEED > 0
			PLAYER_CMOVESPEED# = PLAYER_CMOVESPEED# - PLAYER_MOVEACC#
		EndIf
	EndIf
	
	If PLAYER_CMOVESPEED# <> 0 Then
		MoveEntity PLAYER_MESH, 0, 0, PLAYER_CMOVESPEED#
	EndIf

	TurnEntity PLAYER_MESH, 0, turn, 0

End Function


Function Create_Grid(scale=1, size=256)
	
	GRIDTex = CreateTexture(size, size,1+2+8)
	SetBuffer TextureBuffer(GRIDTex)
	
	Color 0, 255, 0
	Rect 0, 0, size, size, 0
	Color 50, 50, 50
	Rect 1, 1, size-2, size-2, 1
	
	SetBuffer BackBuffer()
	ScaleTexture GRIDTex, scale, scale
	
	GRID = CreatePlane()
	EntityTexture GRID, GRIDTex
	
	EntityAlpha GRID, .75
	Color 255, 255, 255
End Function


If (PLAYER_CMOVESPEED#+1) <> 1 Then
    MoveEntity PLAYER_MESH, 0, 0, PLAYER_CMOVESPEED#
Else
    PLAYER_CMOVESPEED# = 0
EndIf
This is just one of those things, rounding error will make PLAYER_CMOVESPEED sort of wobble even when you think it should be 0. All the code Ive printed does, is make sure that if PLAYER_CMOVESPEED is sort of 0 to 6 decimels then it will be treated as 0, and set to 0

Another solution would be
if Mod (PLAYER_CMOVESPEED) < 0.0001 then....
But I dont know B3D syntax, so you may have to mess with it.

(Edit, (Ignore this comment) its really 6.22 Decimels, but dont tell anyone hahahahahahaha)

Edit: On a seperate issue the Player_CmoveSpeed should be part of a Vector called Velocity. Cos Really Speed can never be negative. And The Accel should always be added to it, that is both should have a positive value and a direction. But thats just me being pedantic. So dont take any real notice of this.


Something like this? Dodgy key choice btw ;)

;My Code
Graphics3D 640, 480, 0, 2

CAM = CreateCamera()
MoveEntity CAM, 0, 80, 0
TurnEntity CAM, 90, 0, 0

Global PLAYER_VX# , PLAYER_VZ#
Const PLAYER_DRAG# = .9866
Const PLAYER_ACC# = .01
Global PLAYER_SPEED#
;NOTE: Max Speed = PLAYER_ACC / ( 1.0 - PLAYER_DRAG )

Global PLAYER_MESH
PLAYER_MESH = CreateCone()
RotateMesh PLAYER_MESH, 90,0,0 : ScaleMesh PLAYER_MESH, 1, 1, 2
EntityFX PLAYER_MESH, 1

Create_Grid(32)


Repeat
	
	Move_Player()
	
	DebugLog PLAYER_SPEED#
	
	RenderWorld()
	Text 0, 0, "Speed: " + PLAYER_SPEED#
	Flip 

Until KeyHit(1)
End



Function Move_Player()


	Turn = KeyDown(30) - KeyDown(32)
	TurnEntity PLAYER_MESH, 0, Turn , 0

	Acceleration# = ( KeyDown(17) - KeyDown(31) ) * PLAYER_ACC
	TFormVector 0, 0, Acceleration, PLAYER_MESH, 0
		
	PLAYER_VX = ( PLAYER_VX * PLAYER_DRAG ) + TFormedX()
	PLAYER_VZ = ( PLAYER_VZ * PLAYER_DRAG ) + TFormedZ()
	PLAYER_SPEED# = Sqr( PLAYER_VX * PLAYER_VX + PLAYER_VZ * PLAYER_VZ ) 
	
	If Acceleration = 0 And PLAYER_SPEED < .001
		PLAYER_VX = 0
		PLAYER_VZ = 0
	EndIf
	
	TranslateEntity PLAYER_MESH, PLAYER_VX, 0, PLAYER_VZ

End Function


Function Create_Grid(scale=1, size=256)
	
	GRIDTex = CreateTexture(size, size,1+2+8)
	SetBuffer TextureBuffer(GRIDTex)
	
	Color 0, 255, 0
	Rect 0, 0, size, size, 0
	Color 50, 50, 50
	Rect 1, 1, size-2, size-2, 1
	
	SetBuffer BackBuffer()
	ScaleTexture GRIDTex, scale, scale
	
	GRID = CreatePlane()
	EntityTexture GRID, GRIDTex
	
	EntityAlpha GRID, .75
	Color 255, 255, 255
End Function