Help with Gravity/Bounce Code...

Blitz3D Forums/Blitz3D Programming/Help with Gravity/Bounce Code...

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

No problems yet and I ran it about 12 times... same results every time...
{{EDIT}}OK after I post that I see what you mean... it starts going wonky...

It looks like the velocity is being ADDED to rather than being subtracted in a constant ratio... I changed the drop height to 15 (added a mirror plane as well for fun) and it sometimes adds and adds... it is probably something in the vector.bb... let me look...

Oh my code:
;// Gravity and Bounce Example
Graphics3D 800,600
SetBuffer BackBuffer()

Include "vector.bb"

;// Camera and Lights
camera = CreateCamera()
PositionEntity camera,0,1,-20
light=CreateLight()
RotateEntity light,90,0,0

;// Set Gravity meters per sec
g# = -9.8

;// Ball Parameters
ball = CreateSphere()
ScaleEntity ball,0.5,0.5,0.5
mass = 10

; for a bit of fun
plane=CreatePlane()
grass_tex=LoadTexture( "tmp11.jpg" )
EntityTexture plane,grass_tex
EntityAlpha plane,0.5
PositionEntity plane,0,0,-.5
; make plane a mirror
mirror=CreateMirror()

;// Set Initial Forces and Vectors
vFor.vector = vector()				;force vector
vAcc.vector = vector()				;acceleration vector
vVel.vector = vector()				;velocity vector
vPos.vector = vector(0,15,0)		        ;position vector

;// Timing 
NewTime% = MilliSecs()
OldTime% = NewTime

;// Main Loop
While Not KeyHit(1)
Cls

;// Find Delta Time
NewTime = MilliSecs()
delta# = Float (NewTime - OldTime) / 1000
OldTime = NewTime
FPS = 1 / delta

;// Reset Forces to Zero Each Cycle
Vector_Reset vFor

;// Calculate Gravity
vFor\y = g * mass

;// Collision
If vPos\y < 0 Then Collided = True
If Collided = True
	speed# = Vector_Magnitude(vVel)
	collided = False
	vFor\y = -vFor\y * (Float (speed / 2)) * mass / 2
	vVel\y = 0
EndIf

;// Up Arrow Key applies 100 force upwards
If KeyDown(200) Then vFor\y = 100

If KeyHit(57) = True Then 
	vPos.vector = vector(0,15,0)
	;Vector_PositionEntity ball,vPos
EndIf

;// Calc New Acc, Vel, and Pos
Vector_DivideScalar vAcc,vFor,mass
Vector_AddTimeStep vVel,vAcc,delta
Vector_AddTimeStep vPos,vVel,delta

;// Position the Ball
Vector_PositionEntity ball,vPos

UpdateWorld
RenderWorld

;// Show What's Happening
Text 5,5,"FPS=" + FPS
Text 5,50,"Speed=" + Int(Vector_Magnitude(vVel))
Vector_Show(vFor,5,100,"Force")
Vector_Show(vAcc,5,175,"Acc")
Vector_Show(vVel,5,250,"Vel")
Vector_Show(vPos,5,325,"Pos")

Flip
Wend
End