[bb] Simple car physics by Jeppe Nielsen [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:38

Previous topic - Next topic

BlitzBot

Title : Simple car physics
Author : Jeppe Nielsen
Posted : 1+ years ago

Description : Drive a cube around in a spherical world

Code :
Code (blitzbasic) Select
; Simple car physics by Jeppe Nielsen 2003


Const gravity#=-0.01 ;gravity constant


Graphics3D 640,480,16,2

Const car_col=1
Const world_col=2

Collisions car_col,world_col,2,2


light=CreateLight(1)

RotateEntity light,30,20,0

plane=CreatePlane()

EntityType plane,world_col
EntityColor plane,255,0,0

For n=1 To 100

If Rnd(10)<5

sphere=CreateSphere(16)

Else

sphere=CreateCube()

EndIf

EntityType sphere,world_col
PositionEntity sphere,Rnd(-40,40),Rnd(2),Rnd(-40,40)
EntityColor sphere,Rnd(255),Rnd(255),Rnd(255)

Next

sp=CreateSphere()
ScaleEntity sp,100,100,100
FlipMesh sp

camera=CreateCamera()
CameraClsColor camera,0,0,255


car.car=carnew(0,5,0)

Repeat
TFormPoint 0,3,-5,care,0

dx#=(TFormedX()-EntityX(camera))*.1
dy#=(TFormedY()-EntityY(camera))*.1
dz#=(TFormedZ()-EntityZ(camera))*.1

TranslateEntity camera,dx,dy,dz

PointEntity camera,care

carcontrol()
carupdate()

RenderWorld()

Flip


Until KeyDown(1)
End


Type car

Field e ;entity

Field x#,y#,z# ; position in 3d-space

Field vx#,vy#,vz# ; velocity

Field ax#,ay#,az# ; acceleration

End Type


Function carnew.car(x#,y#,z#)

c.car=New car

cx#=x#
cy#=y#
cz#=z#

ce=CreateCube()
cube=CreateCube()
ScaleEntity cube,0.3,0.3,0.3
PositionEntity cube,0,0,1
EntityParent cube,ce

EntityType ce,car_col
EntityRadius ce,1

PositionEntity ce,cx,cy,cz

Return c

End Function

Function carupdate()

For c.car=Each car

cvy#=cvy#+gravity#

cvx#=cvx#+cax#
cvy#=cvy#+cay#
cvz#=cvz#+caz#

cx#=EntityX(ce)
cy#=EntityY(ce)
cz#=EntityZ(ce)

TranslateEntity ce,cvx,cvy,cvz

Next

UpdateWorld()

For c.car=Each car

;correct velocity if collided
cvx=(EntityX(ce)-cx)
cvy=(EntityY(ce)-cy)
cvz=(EntityZ(ce)-cz)


;slow down due to friction
If EntityCollided(ce,world_col)

cvx#=cvx*0.98
cvy#=cvy*0.98
cvz#=cvz*0.98

EndIf

cax#=0
cay#=0
caz#=0

Next

End Function


Function carcontrol()

For c.car=Each car

If KeyDown(200)

TFormVector 0,0,0.02,ce,0

cax#=TFormedX()
cay#=TFormedY()
caz#=TFormedZ()

EndIf

If KeyDown(208)

cvx=cvx*0.99
cvy=cvy*0.99
cvz=cvz*0.99

EndIf

If KeyDown(57)

TFormVector 0,0.05,0,ce,0

cax#=cax+TFormedX()
cay#=cay+TFormedY()
caz#=caz+TFormedZ()


EndIf

If KeyDown(203)

TurnEntity ce,0,2,0

EndIf

If KeyDown(205)

TurnEntity ce,0,-2,0

EndIf


Next



End Function


Comments :


T-Head(Posted 1+ years ago)

 Hey, thats good stuff!


CyBeRGoth(Posted 1+ years ago)

 Holy crap T-head i suggest you remove that 'sig'