Simple Game Physics Question

Blitz3D Forums/Blitz3D Programming/Simple Game Physics Question

Ok, sorry I'm so dense on this issue...3D math is not my strongest point. I'm just going to post the code I'm using for bouncing objects (I found this somewhere else on the forums and modified it to add gravity, but even that doesn't work too well). If someone can please tell me how to make them start moving in the direction they are pointing, I'd be very grateful. Also, if someone can tell me where I'm going wrong on gravity...when balls run out of y velocity they stick to the ground like glue, even though they should have some x and z left in them.

Graphics3D 640,480,16,1

SeedRnd(MilliSecs)

Const C_BALL = 1
Const C_CUBE = 2
Const BALLS = 2
Const SPEED# = 2

Global camera,cube
Global light =CreateLight()

Type ball
	Field entity
	Field xvel#,yvel#,zvel#
End Type

Collisions C_BALL, C_BALL,1,1
Collisions C_BALL, C_CUBE,2,1

MakeStuff()

While Not KeyDown(1)
	
	If KeyHit(57) Then
		For b.ball = Each Ball
			b\xvel = Rnd(-SPEED,SPEED)
			b\yvel = Rnd(SPEED,(SPEED*5))
			b\zvel = Rnd(-SPEED,SPEED)
		Next
	EndIf
	UpdateBalls()
	UpdateWorld()
	RenderWorld()
	Flip
	
Wend

;==============================================
;==============================================
;==============================================

Function MakeStuff()
	
	;cube
	cube = CreateCube()
	ScaleMesh cube,30,30,30
	FlipMesh cube
	EntityColor cube,50,50,200
	EntityType cube,C_CUBE
	UpdateNormals cube
	
	cube2 = CreateCube()
	ScaleMesh(cube2,5,5,5)
	PositionEntity(cube2,0,-10,0)
	EntityColor(cube2,255,0,0)
	EntityType(cube2,C_CUBE)
	UpdateNormals(cube2)
	
	;camera
	camera = CreateCamera()
	PositionEntity camera,0,0,60
	PointEntity camera, cube
	
	;template for balls
	temp = CreateSphere()
	EntityType temp,C_BALL
	UpdateNormals temp
	EntityShininess temp,1
	HideEntity temp
		
	;balls
	For l = 1 To BALLS
		b.ball = New ball
		b\xvel = Rnd(-SPEED,SPEED)
		b\yvel = Rnd(-SPEED,SPEED)
		b\zvel = Rnd(-SPEED,SPEED)
		b\entity = CopyEntity( temp )
		PositionEntity b\entity,Rnd(-30,30),Rnd(-30,30), Rnd(-30,30)
		EntityColor b\entity,Rand(100,255),Rand(100,255),Rand(100,255)
		radius# = Rnd( 1,4 )
		ScaleEntity b\entity,radius,radius,radius
		EntityRadius b\entity,radius
	Next

End Function

;==============================================
;==============================================
;==============================================

Function UpdateBalls()

	For b.ball = Each ball
	
		For i = 1 To CountCollisions(b\entity)
			HandleBounce(b,i)
		Next
		
		b\yvel = b\yvel - 0.1
		
		TranslateEntity b\entity,b\xvel,b\yvel,b\zvel
		
	Next

End Function

;==============================================
;==============================================
;==============================================

Function HandleBounce(b.ball,i)
	
	; Get the normal of the surface collided with. 
	Nx# = CollisionNX(b\entity, i) 
	Ny# = CollisionNY(b\entity, i) 
	Nz# = CollisionNZ(b\entity, i) 
			
	; Compute the dot product of the ball's motion vector and the normal of the surface collided with. 
	VdotN# = b\xvel*Nx + b\yvel*Ny + b\zvel*Nz 
	
	; Calculate the normal force. 
	NFx# = -2.0 * Nx# * VdotN 
	NFy# = -2.0 * Ny# * VdotN 
	NFz# = -2.0 * Nz# * VdotN 
	
	; Add the normal force to the motion vector. 
	b\xvel = b\xvel + NFx
	b\yvel = (b\yvel + NFy) * 0.8
	b\zvel = b\zvel + NFz
	
End Function