Basic Physics Example

Blitz3D Forums/Blitz3D Programming/Basic Physics Example

Chroma(Posted 2004) [#1]
Here's a basic physics example I threw together using my vector.bb library from the code archives.

It encompasses how to do gravity and apply force to an object. Be sure to snag the vector.bb file from the archives before you run this.

Press the up arrow key to apply upward force.

;// Basic Physics Demo utilizing Vector.bb
Graphics3D 800,600,16,1
SetBuffer BackBuffer()

AppTitle "Basic Physics Demo - using Vector.bb from the code archives."

Include "vector.bb"		

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

;// Ball Parameters
ball = CreateSphere()
mass = 10

;// Set Initial Forces and Vectors
force.vector = vector()				;force vector
acc.vector = vector()				;acceleration vector
vel.vector = vector()				;velocity vector
pos.vector = vector(0,20,30)		        ;position vector

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

;// Camera and Lights
camera = CreateCamera()
light  = CreateLight()

;// 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 force

;// Calculate Gravity
force\y = gravity * mass

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

;// Calc New Acceleration, Velocity, and Position
Vector_DivideScalar acc,force,mass
Vector_AddTimeStep vel,acc,delta
Vector_AddTimeStep pos,vel,delta

;// Position the Ball
Vector_PositionEntity ball,pos

UpdateWorld
RenderWorld

Text 5,5,"FPS=" + FPS
Vector_Show(force,5,100,"Force")
Vector_Show(acc,5,175,"Acc")
Vector_Show(vel,5,250,"Vel")
Vector_Show(pos,5,325,"Pos")

Flip
Wend
End



Chroma(Posted 2004) [#2]
Bah! I put this is the wrong forum...can you move it to the Tutorials section?


wizzlefish(Posted 2004) [#3]
It's not exactly a tutorial. And it's still "Blitz3D Programming"


Chroma(Posted 2004) [#4]
Optomistic...it's a tutorial on how to use the vector.bb lib and apply force and gravity. kthxbye