[bb] Ball rolling example by Jeppe Nielsen [ 1+ years ago ]

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

Previous topic - Next topic

BlitzBot

Title : Ball rolling example
Author : Jeppe Nielsen
Posted : 1+ years ago

Description : Shows how you can make a ball roll

Code :
Code (blitzbasic) Select
;Ball rolling physics example by Jeppe Nielsen 2003

Const gravity#=-0.04;-0.02 ;gravity constant

Graphics3D 800,600,16,1

Const ball_col=1
Const world_col=2

Collisions ball_col,world_col,2,2

Global texture

texture=CreateTexture(16,16)

SetBuffer TextureBuffer(texture)

Color 255,255,255
Rect 0,0,16,16,1

Color 255,0,0
Rect 0,0,8,8,1
Rect 8,8,8,8,1

ScaleTexture texture,0.1,0.1


Global groundtexture=CreateTexture(32,32,1+8)

SetBuffer TextureBuffer(groundtexture)

Color 0,0,0
Rect 0,0,32,32,1

Color 0,0,255
Rect 0,0,32,32,0

ScaleTexture groundtexture,2,2

SetBuffer BackBuffer()
Color 255,255,255

plane=CreatePlane()
EntityTexture plane,groundtexture
EntityType plane,world_col
EntityAlpha plane,0.5

mirror=CreateMirror()




light=CreateLight(1)
RotateEntity light,30,20,0


obj=CreateCube()

EntityType obj,world_col
PositionEntity obj,0,0,0
ScaleEntity obj,10,10,10
RotateEntity obj,45,0,0



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


ball.ball=ballnew(0,4,-120,2,0.8)


zoom#=14

Repeat

zoom#=zoom#-MouseZSpeed()
ballcamera(camera,ball,0,zoom#,-zoom#,0,5,0)


ballcontrol()
ballupdate()

RenderWorld()

Text 10,10,"Arrow keys to control ball"
Text 10,30,"Scroll on mouse to zoom"


Flip


Until KeyDown(1)
End


Type ball

Field e ;entity

Field sphere

Field pivot

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

Field vx#,vy#,vz# ; velocity

Field ax#,ay#,az# ; acceleration

Field size#

Field bounce# ; bounce factor

Field vel#

Field vx2#,vy2#,vz2# ; temp velocity

End Type


Function ballnew.ball(x#,y#,z#,size#=1,bounce#=0.9)

c.ball=New ball

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

csize=size

counce#=bounce#

ce=CreatePivot()

csphere=CreateSphere(64)

cpivot=CreatePivot()

EntityType ce,ball_col
EntityRadius ce,csize

PositionEntity ce,cx,cy,cz
ScaleEntity csphere,csize,csize,csize

EntityTexture csphere,texture

Return c

End Function

Function ballupdate()

For c.ball=Each ball


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.ball=Each ball

;correct velocity if collided
cvx2=(EntityX(ce)-cx)
cvy2=(EntityY(ce)-cy)
cvz2=(EntityZ(ce)-cz)

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

PositionEntity csphere,cx,cy,cz
PositionEntity cpivot,cx,cy,cz

If EntityCollided(ce,world_col)

For i = 1 To CountCollisions(ce)
; Get the normal of the surface which the entity collided with.
Nx# = CollisionNX(ce, i)
Ny# = CollisionNY(ce, i)
Nz# = CollisionNZ(ce, i)

; Compute the dot product of the entity's motion vector and the normal of the surface collided with.
VdotN# = cvx#*Nx# + cvy#*Ny# + cvz#*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 direction vector.
cvx# = cvx# + NFx# * counce#
cvy# = cvy# + NFy# * counce#
cvz# = cvz# + NFz# * counce#

avx#=EntityPitch(csphere)
avy#=EntityYaw(csphere)
avz#=EntityRoll(csphere)

;Rotate stuff
;Get vector from center to collision
dx1#=(CollisionX(ce,i)-cx)
dy1#=(CollisionY(ce,i)-cy)
dz1#=(CollisionZ(ce,i)-cz)

dx2#=cvx
dy2#=cvy
dz2#=cvz

;Cross product
cx# = ( dy1 * dz2 ) - ( dz1 * dy2 )
cy# = ( dz1 * dx2 ) - ( dx1 * dz2 )
cz# = ( dx1 * dy2 ) - ( dy1 * dx2 )

AlignToVector cpivot,cx,cy,cz,1

Next

Nx# = CollisionNX(ce, 1)
Ny# = CollisionNY(ce, 1)
Nz# = CollisionNZ(ce, 1)

AlignToVector ce,Nx#,Ny#,Nz#,2,0.5

cvel#=Sqr(cvx2*cvx2+cvy2*cvy2+cvz2*cvz2)

;slow down due to friction
cvx#=cvx*0.98
cvy#=cvy*0.98
cvz#=cvz*0.98


EndIf


EntityParent csphere,cpivot

TurnEntity cpivot,-cvel#*(180/Pi)/csize#,0,0

EntityParent csphere,0

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

Next

End Function


Function ballcontrol()

For c.ball=Each ball

If KeyDown(200)

TFormVector 0,0,0.03,ce,0

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

EndIf

If KeyDown(208)

cvx=cvx*0.94
cvy=cvy*0.94
cvz=cvz*0.94

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

Function ballcamera(camera,b.ball,camx#,camy#,camz#,aimx#=0,aimy#=0,aimz#=0,smoothcam#=0.1,roll#=0)

TFormPoint camx#,camy#,camz#,be,0

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

TranslateEntity camera,dx,dy,dz

TFormPoint aimx#,aimy#,aimz#,be,0

dx# = EntityX(camera)-TFormedX()
dy# = EntityY(camera)-TFormedY()
dz# = EntityZ(camera)-TFormedZ()
dist#=Sqr#((dx#*dx#)+(dz#*dz#))
pitch#=ATan2(dy#,dist#)
yaw#=ATan2(dx#,-dz#)

RotateEntity camera,pitch#,yaw#,roll#

End Function


Comments :


Q(Posted 1+ years ago)

 My current project involves controlling a ball, this has pointed me in the right direction.  Thanks! :o)


Jeepster(Posted 1+ years ago)

 cool. I just have one question:what part(s) of the code make an object solid? when I try to add another cube, the ball rolls right through it!


Jeepster(Posted 1+ years ago)

 wait, never mind, I got itanyway, good show. I also have a project that involves controlling a ball


puki(Posted 1+ years ago)

 This is great.


Curtastic(Posted 1+ years ago)

 This is awesome. I thought the ball was jerking but it was just the camera. For now something like this can be used to make it smooth.
Function ballcamera(camera,b.ball,camx#,camy#,camz#,aimx#=0,aimy#=0,aimz#=0,smoothcam#=0.1,roll#=0)
TFormPoint camx#,camy#,camz#,be,0

dx#=(TFormedX()-EntityX(camera))*smoothcam#
dz#=(TFormedZ()-EntityZ(camera))*smoothcam#

TranslateEntity camera,dx,0,dz
PositionEntity camera,EntityX(camera),EntityY(be)+15,EntityZ(camera)
PointEntity camera,be
End Function



Rook Zimbabwe(Posted 1+ years ago)

 This is cool also! OK Jieppe Gets my vote for BLitzgy of the year!!!


Captain Wicker (crazy hillbilly)(Posted 1+ years ago)

 This code may be almost a decade old, but it still rocks!


GaryV(Posted 1+ years ago)

 R.I.P. Rook Zimbabwe :( [/i]