help with entity controls

Blitz3D Forums/Blitz3D Beginners Area/help with entity controls

Aussie(Posted 2009) [#1]
Gday i have a problem & was hopeing someone could help.

I have a model that i am trying get to bank/roll left & right like a jet would. I have got it to bank left & right, the only problem is that the model will sometimes, very slightly keep moving in one direction. Here is the function if someone could take a look at it & see if they can find why it would be greatly appreciated.
Function update_player()
	For pl.player = Each player
		EntityType pl\p_piv,player_col
		EntityRadius pl\p_piv,8
		pvz#=-(pl\z_vel#*200)
		pl\z_vel#=pl\z_vel-.01
		
		
		If KeyDown(200) pl\z_vel = pl\z_vel +.1
		If pl\z_vel > 2 pl\z_vel= 2
	If KeyDown(208) pl\z_vel = pl\z_vel - .1
		If pl\z_vel < .5 pl\z_vel = .5
	
	
	If KeyDown (203) pl\x_dir#=pl\x_dir# -.1
	If KeyDown (205) pl\x_dir#=pl\x_dir# +.1
	
	If KeyDown(30) MoveEntity pl\p_piv,0,.5,0
	If KeyDown(44) MoveEntity pl\p_piv,0,-.5,0
	
	If KeyDown(56) If MilliSecs() > timer + rate_of_fire Then
		create_rocket()
		timer = MilliSecs() 
	EndIf

	
	
	If pl\x_dir# > 1.5 pl\x_dir = 1.5
	If pl\x_dir# < -1.5 pl\x_dir = -1.5
	
	
	If pl\x_dir# > 0 pl\x_dir = pl\x_dir - .05
	If pl\x_dir# < 0 pl\x_dir = pl\x_dir + .05
	If pl\x_dir# = 0 pl\x_dir = 0

		
	pl\z_roll#=pl\x_dir#*-30
	RotateEntity pl\p_mesh,0,0,pl\z_roll#

	TranslateEntity pl\p_piv,pl\x_dir,0,pl\z_vel
	
	PositionEntity camera,EntityX(pl\p_piv),EntityY(pl\p_piv)+30,EntityZ(pl\p_piv)-50
	IgnitionEffectVelocity fire,0,0,pvz ,0,0,pvz
Next
End Function



Ross C(Posted 2009) [#2]
I think THINK the problem may lie here:

	If pl\x_dir# > 0 pl\x_dir = pl\x_dir - .05
	If pl\x_dir# < 0 pl\x_dir = pl\x_dir + .05
	If pl\x_dir# = 0 pl\x_dir = 0


You should always be extra careful when dealing with floating point number, as they won't stay the way you want them. For instance, it's a bad idea to check to see if a floating point number = 0. There's a good chance it;ll end up being 0.000001 or something.

So:
	If pl\x_dir# > 0
             pl\x_dir = pl\x_dir - .05
  	     If pl\x_dir# < 0 pl\x_dir = 0             
	ElseIf pl\x_dir# < 0
             pl\x_dir = pl\x_dir + .05
             If pl\x_dir# < 0 pl\x_dir = 0
        ElseIf pl\x_dir# = 0
             pl\x_dir = 0
        End If


If after subtracting the value, if become less than zero (or greater than zero after adding), you need to check, and set the value to 0. This will cover all possiblities. Try it and see if it does make any difference.

What happens if you don't nest the If statements is this:

        If pl\x_dir# > 0 pl\x_dir = pl\x_dir - .05
	If pl\x_dir# < 0 pl\x_dir = pl\x_dir + .05
	If pl\x_dir# = 0 pl\x_dir = 0


The code will execute every If statement in order, regardless of the last. So, if pl\x_dir = 0.00001, then it is above 0 and meets the condition. So the code takes off the 0.05, taking the number to -0.499999.

Now the next line gets executed. pl\x_dir now meets the next condition of being less than 0. So, it adds the 0.05 back on. In theory, it shouldn't really change, but the floating point aspect may very well, be changing this number slightly.

Hope that helps, as i can't see anything else really...


Aussie(Posted 2009) [#3]
Thanks Ross. That was the problem & thanks for the explanation, it helped me out quite a bit & something i will have to remember for future codeing.
Cheers :)


Ross C(Posted 2009) [#4]
No worries man. Caught me out many a time.


Aussie(Posted 2009) [#5]
Gday all. I have just been playing around with the possibility of using the mouse to controll the player in the game i am working on.
I have got it to work but the movement is way to jerky. Was wandering if someone could have a look at the example & possibly give me a few ideas on how to smoth out the movement.
Cheers. :)
Graphics3D 800,600,0,2

SetBuffer BackBuffer()



tex=CreateTexture(32,32,11)
	ScaleTexture tex,5,5
	SetBuffer TextureBuffer (tex)
	Color 0,50,0:Rect 0,0,32,32
	Color 0,255,0:Rect 0,0,32,32,False
SetBuffer BackBuffer()

plane=CreatePlane()
	EntityTexture plane,tex
	PositionEntity plane,0,-1,0
	EntityFX plane,1
	EntityType plane,ground_col
	
cube= CreateCube()
	ScaleEntity cube,2,.2,1

camera= CreateCamera()
	

z_vel# = 0
z_roll# = 0

HidePointer

While Not KeyDown (1)


x_vel# = MouseXSpeed()/2
y_vel# = MouseYSpeed()/2
	MoveMouse 800/2,600/2


	If KeyDown(200) z_vel = z_vel + .05
	
	If z_vel < .5 z_vel = .5
	If x_vel < -1 x_vel = -1
	If x_vel > 1 x_vel = 1
	If y_vel < -.5 y_vel = -.5
	If y_vel > .5 y_vel = .5
	
	z_vel = z_vel - .03
	z_roll = x_vel *-30


RotateEntity cube,0,0,z_roll

TranslateEntity cube,x_vel,-y_vel,z_vel
PositionEntity camera,EntityX(cube),EntityY(cube)+3,EntityZ(cube)-10





UpdateWorld
RenderWorld
Color 255,255,255
	Text 0,0,"Use mouse to move left, right, up & down."
	Text 0,15,"Use Csr up to accelerate." 
Flip
Wend

End



Ross C(Posted 2009) [#6]
You might be best starting a new thread for this. I usually don't read past the part of the thread where the person gets help, so you might get fewer responses. Sorry i can't help right now, i'm in work :)


Aussie(Posted 2009) [#7]
No worries. I probably should have posted in Blitz3d Programming. :)
http://www.blitzbasic.com/Community/posts.php?topic=86170