weird...
;# TOKAMAK DEMO v0.0017 (bradford6)
; Credits:
; SWEENIE for creating the TOKAMAK WRAPPER
; www.tokamakphysics.com to get SDK
; note:
; you must put TOKAMAK.DLL (from SDK) in system32
; must put sweenies tokamak files in blitz3d/userlibs directory
; this is rev -1 so it is very early and probably buggy,
; constants
Const ENTS=100 ; TOTAL NUMBER OF TOKAMAKPHYSICS ENTITIES
Const FPS=50
Const w=17, s=31, a=30, d=32, space=57
; globals
Global midw, midh ,world, campiv , camera , light, pickmarker
Global pictentity,pnx#,pny#,pnz#,pcx#,pcy#,pcz#
Global latspeed#,speed#,MX#,MY#
Global cubetex ,spheretex ,cyltex ,worldtex ,animtex
Global TOKS , period, time, imptime
Global bigfont , smallfont
Dim obj(ENTS)
Dim rb(ENTS)
Dim joint(ENTS)
SeedRnd MilliSecs()
Type tokrb
Field tokid,entity,texture,pickmode
Field name$
Field posx#,posy#,posz#,scx#,scy#,scz#,pitch#,yaw#,roll#
Field tokx#,toky#,tokz#,tokwidth#,tokheight#,tokdepth#,tokmass#
Field lindamp#,angdamp#
Field inert_width#,inert_depth#,inert_height#,inert_mass#
End Type
TOKSIM_CreateSimulator(ENTS,1,0,-10,0)
period=500/FPS
time=MilliSecs()-period
imptime=MilliSecs()
create3d_scene(800,600,16,2)
; animated bodies
anWidth# = 10 anHeight#=4 anDepth#=10
anx#=0 anyY#=20 anzZ#=0
angdamp# = 0.002
lindamp# = 0.001
mass# = 2.0
inertia_tensor_mass# = 4.0
inertia_tensor_diam# = .1
AnimBody = TOKAB_Create()
animbodymesh = CreateCube() : ScaleEntity animbodymesh,anwidth#/2,anheight#/2,andepth#/2
EntityTexture animbodymesh,animtex : EntityPickMode animbodymesh,2
PositionEntity animbodymesh,anx#,anyY#,anzZ#
TOKAB_AddBox(AnimBody,anWidth#,anHeight#,anDepth#)
TOKAB_SetPosition(AnimBody,anx#,anyY#,anzZ#)
;create_TOK_BALL(12,4,anx#-(anwidth#/2),any#+6,anz#-(andepth#/2),angdamp#,lindamp#,mass#,inertia_tensor_mass#,inertia_tensor_diam#,cubetex,"ball")
;create_TOK_BALL(12,4,anx#-(anwidth#/2),any#+6,anz#-(andepth#/2),angdamp#,lindamp#,mass#,inertia_tensor_mass#,inertia_tensor_diam#,cubetex,"ball")
For j = 1 To 20
create_TOK_BOX(j/2,j/2,j/2,j*2.5,10,0,0.001,0.02,2,2,2,2,2,cubetex,"anim")
TOKRB_CollideConnected(rb(toks),True)
If j > 1
joint(j) = TOKJOINT_Create(3,rb(toks),rb(toks-1))
TOKJOINT_SetType(joint(j),3)
TOKJOINT_SetPositionAndRotationWorld(joint(j),j*2.5,10,0,0,0,0)
TOKJOINT_Enable(joint(j),True)
;TOKJOINT_SetJointLength(joint(j),3)
;TOKJOINT_SetLowerLimit2 joint(j),-3.14*0.1 ; (-45 degrees)
;TOKJOINT_SetUpperLimit2 joint(j),3.14*0.1 ; (+45 degrees)
;TOKJOINT_EnableLimit2 joint(j),True
EndIf
Next
;TOKAB_SetRotation(AnimBody,anPitch#,anYaw#,anRoll#)
; +++ MAIN LOOP START ++++++ MAIN LOOP START ++++++ MAIN LOOP START ++++++ MAIN LOOP START +++
.mainloop
While Not KeyHit(1)
Repeat
elapsed=MilliSecs()-time
Until elapsed
ticks=elapsed/period
tween#=Float(elapsed Mod period)/Float(period)
For k=1 To ticks
time=time+period
If k=ticks Then CaptureWorld
TOKSIM_Advance(1.75/FPS,1)
move_camera()
UpdateWorld
For i=1 To TOKS
If TOKRB_IsIdle(rb(i)) Then
EntityAlpha obj(i),0.5
Else
EntityAlpha obj(i),1.0
TOKRB_Active(rb(i),True)
EndIf
If EntityName(obj(i)) = "ball"
; do stuff
EndIf
If EntityName(obj(i)) = "box"
;If MouseDown(2) = 1 Then TOKRB_ApplyTwist(rb(i),0,150,0)
EndIf
PositionEntity obj(i),TOKRB_GetX#(rb(i)),TOKRB_GetY#(rb(i)),TOKRB_GetZ#(rb(i))
RotateEntity obj(i),TOKRB_GetPitch#(rb(i)),TOKRB_GetYaw#(rb(i)),TOKRB_GetRoll#(rb(i)),False
Next
Next
; Not sure wether to put this here or inside the for/next above...
.carmovement
animyaw# = animyaw# + 1
If animyaw# = 360 Then animyaw# = 1
TOKAB_SetRotation(Animbody,0,animYaw#,0) : RotateEntity animbodymesh,0,animyaw#,0
;accel# = 0.01
;force# = force# * .99
;TOKRB_ApplyImpulse rb(1),-(Sin(EntityYaw(obj(1)))*force#),-(Sin(EntityPitch(obj(1)))*force#),Cos(EntityYaw(obj(1)))*force#
;ok
;TOKRB_ApplyImpulse rb(2),-(Sin(EntityYaw(obj(1)))*force#),-(Sin(EntityPitch(obj(1)))*force#),Cos(EntityYaw(obj(1)))*force#
;TOKRB_ApplyImpulse rb(4),-(Sin(EntityYaw(obj(1)))*force#),-(Sin(EntityPitch(obj(1)))*force#),Cos(EntityYaw(obj(1)))*force#
;TOKRB_SetTorque rb(2),-(Cos(EntityYaw(obj(1)))*force#)/2,0,-(Sin(EntityYaw(obj(1)))*force#)/2
;TOKRB_ApplyTwist rb(3),-(Cos(EntityYaw(obj(1)))*force#)/2,0,-(Sin(EntityYaw(obj(1)))*force#)/2
;TOKRB_ApplyTwist rb(2),0,-(Sin(EntityYaw(obj(1)))*force#),0
carpitch# = EntityPitch(obj(1))
caryaw# = EntityYaw(obj(1))
carroll# = EntityRoll(obj(1))
caryaw_cos# = Cos(caryaw#)
caryaw_sin# = Sin(caryaw#)
If KeyDown(200) = 1
If force# < 0 Then force#=2
force# = force# + .001
If force# > 80 Then force = 80
;TOKRB_ApplyImpulse rb(1),-(Sin(EntityYaw(obj(1)))*force#),-(Sin(EntityPitch(obj(1)))*force#),Cos(EntityYaw(obj(1)))*force#
EndIf
If KeyDown(208) = 1
If force# > 0 Then force#=-2
force# = force# - 0.001
If force# < -25 Then force = -25
EndIf
;TOKRB_SetAngularMomentum(rb(6),caryaw_cos#*force#,0,caryaw_sin#*force#)
;TOKRB_SetAngularMomentum(rb(7),caryaw_cos#*force#,0,caryaw_sin#*force#)
ncaryaw# = caryaw# + turn#
If KeyDown(203) = 1
turn# = turn# + 0.05
If turn#>40 Then turn# = 40
EndIf
If KeyDown(205) = 1
turn# = turn# - 0.05
If turn#<-40 Then turn# = -40
EndIf
;TOKRB_SetRotation(rb(2),carpitch#,ncaryaw#,carroll#)
;TOKRB_SetRotation(rb(4),carpitch#,ncaryaw#,carroll#)
If MouseDown(2) = 1 Then place_rb(cubetex)
If MouseDown(1)=1 Then push_rigid_body()
RenderWorld tween
Color 255,255,255
Text 0,0,"Physics Time:"+TOKSIM_GetPhysicsTime()*1000.0+ " milliseconds"
Text 0,10,"Render Time:"+Str(elapsed)+ " milliseconds"
Text 0,20,"pnx"+pnx#+" pny "+pny#+" pnz "+pnz#
;Text 0,30,"Render Time:"+Str(elapsed)+ " milliseconds"
Text 0,40,"force: "+force#
;Text 0,50,"Render Time:"+Str(elapsed)+ " milliseconds"
Text 0,50,"camera X : "+EntityX(campiv)+" Y : "+EntityY(campiv)+" Z : "+EntityZ(campiv)
Color 255,0,0
Rect midw-4,midh-4,8,8,0
Flip False
Wend
; == MAIN LOOP END ==== MAIN LOOP End ==== MAIN LOOP End ==== MAIN LOOP End ==== MAIN LOOP End ==
TOKSIM_DestroySimulator()
End
;==================================================================================
Function curvevalue#(newvalue#,oldvalue#,increments# )
If increments>1 Then oldvalue#=oldvalue#-(oldvalue#-newvalue#)/increments
If increments<=1 Then oldvalue=newvalue
Return oldvalue#
End Function
;==================================================================================
Function move_camera()
; Movement controls
If KeyDown(w)=1 Then speed# = speed#+.03
If KeyDown(a)=1 Then latspeed# = latspeed# - .02
If KeyDown(s)=1 Then speed# = speed# -.03
If KeyDown(d)=1 Then latspeed# = latspeed# + .02
latspeed#=latspeed#*.98
speed#=speed#*.98
MY#=curvevalue#(MouseYSpeed(),MY#,3 )
MX#=curvevalue#(MouseXSpeed(),MX#,3 )
TurnEntity campiv,MY#,-MX#,0 ; turn camera up and down
;TurnEntity campiv,0,-MX#,0 ; turn nnn left --right
RotateEntity campiv,EntityPitch(campiv),EntityYaw(campiv),0
If EntityPitch(campiv)>88 Then RotateEntity campiv,88,EntityYaw(campiv),EntityRoll(campiv)
If EntityPitch(campiv)<-88 Then RotateEntity campiv,-88,EntityYaw(campiv),EntityRoll(campiv)
MoveMouse 100,100
MoveEntity campiv,latspeed#,0,speed#
End Function
;==================================================================================
Function create_cube_texture()
tex = CreateTexture(64,64)
ScaleTexture tex,.5,.5
SetBuffer TextureBuffer(tex)
Color 20,20,200
Rect 0,0,64,64
Color 255,255,255
Rect 0,0,32,32
Rect 32,32,64,64
Color 0,0,0
Rect 0,0,64,64,0
SetBuffer BackBuffer()
Return tex
End Function
Function create_anim_texture()
tex = CreateTexture(256,256)
;ScaleTexture tex,.5,.5
SetBuffer TextureBuffer(tex)
Color 240,240,241 : Rect 5,5,250,250
Color 150,150,50
Rect 5,5,128,128
Color 200,200,100
Rect 5,5,128,128
Rect 128,128,250,250
;Color 200,200,0
;Rect 5,5,128,128,0
Color 5,5,5
SetFont bigfont
Text 128+3,90+3,"TOKAMAK",1,1
Text 128+3,128+3,"Animated Body",1,1
Color 255,0,0
SetFont bigfont
Text 128,90,"TOKAMAK",1,1
Color 0,0,255
Text 128,128,"Animated Body",1,1
SetFont smallfont
SetBuffer BackBuffer()
Return tex
End Function
Function create_world_texture()
tex = CreateTexture(128,128)
ScaleTexture tex,.25,.25
SetBuffer TextureBuffer(tex)
Color 100,100,100
Rect 0,0,128,128
Color 50,50,50
Rect 0,0,64,64
Rect 64,64,128,128
Color 200,200,0
Rect 0,0,128,128,0
SetBuffer BackBuffer()
Return tex
End Function
;==================================================================================
Function create_sphere_texture()
tex = CreateTexture(128,128)
SetBuffer TextureBuffer(tex)
Color 0,200,0
Rect 0,0,128,128
Color 255,255,0
Rect 0,0,64,64
Rect 64,64,128,128
Color 0,0,0
Rect 0,0,64,64,0
Color 0,0,0
Rect 64,64,128,128,0
Color 0,0,0
Rect 0,0,128,128,0
ScaleTexture tex,.5,.5
SetBuffer BackBuffer()
Return tex
End Function
;==================================================================================
Function create_cyl_texture()
tex = CreateTexture(128,128)
SetBuffer TextureBuffer(tex)
Color 0,0,0
Rect 0,0,128,128
Color 255,0,0
Rect 0,0,64,64
Rect 64,64,128,128
SetBuffer BackBuffer()
Color 255,255,0 ; YELLOW
Return tex
End Function
;==================================================================================
Function campick()
pictentity = 0
CameraPick(camera,midw,midh)
pictentity = PickedEntity()
If Pictentity<>0
;EntityAlpha pictentity,.7
pnx# = PickedNX#()
pny# = PickedNY#()
pnz# = PickedNZ#()
pcx# = PickedX#()
pcy# = PickedY#()
pcz# = PickedZ#()
EndIf
End Function
Function create_TOK_BOX(xs#,ys#,zs#,xp#,yp#,zp#,damp#,angdamp#,mass#,iw#,ih#,id#,imass#,texture,name$)
TOKS = TOKS+1
If TOKS>ENTS
TOKS = ENTS
Return
EndIf
obj(TOKS) = CreateCube()
NameEntity obj(toks),name$
ScaleEntity obj(TOKS),xs#/2,ys#/2,zs#/2
EntityPickMode obj(TOKS),2
EntityTexture obj(TOKS),texture
rb(TOKS) = TOKRB_Create()
TOKRB_SetSleepingParameter(rb(TOKS),-100)
TOKRB_AddBox rb(TOKS),xs#,ys#,zs#
TOKRB_SetPosition(rb(TOKS),xp#,yp#,zp#)
TOKRB_SetLinearDamping rb(TOKS),damp#
TOKRB_SetAngularDamping rb(TOKS),angdamp#
TOKRB_SetMass rb(TOKS),mass#
TOKRB_SetBoxInertiaTensor rb(TOKS),iw#,ih#,id#,imass#
End Function
Function create_TOK_BALL(segs,sc#,xp#,yp#,zp#,angdamp#,lindamp#,mass#,IT1#,IT2#,texture,name$)
TOKS = TOKS+1
If TOKS>ENTS
TOKS = ENTS
Return
EndIf
obj(TOKS) = CreateSphere(segs)
NameEntity obj(toks),name$
ScaleEntity obj(TOKS),sc#/2,sc#/2,sc#/2
EntityPickMode obj(TOKS),2
EntityTexture obj(TOKS),texture
rb(TOKS) = TOKRB_Create()
TOKRB_AddSphere rb(TOKS),sc#
TOKRB_SetPosition(rb(TOKS),xp#,yp#,zp#)
TOKRB_SetAngularDamping rb(TOKS),angdamp#
TOKRB_SetLinearDamping rb(TOKS),lindamp#
TOKRB_SetMass rb(TOKS),mass#
TOKRB_SetSphereInertiaTensor rb(TOKS),IT1#,IT2#
End Function
Function create_TOK_CYL(segs,diameter#,height#,xp#,yp#,zp#,angdamp#,lindamp#,mass#,Id#,Ih#,Imass#,texture,name$)
TOKS = TOKS+1
If TOKS>ENTS
TOKS = ENTS
Return
EndIf
obj(TOKS) = CreateCylinder(segs)
EntityPickMode obj(TOKS),2
EntityTexture obj(TOKS),texture
rb(TOKS) = TOKRB_Create()
TOKRB_AddCylinder rb(TOKS),diameter#,height#
ScaleEntity obj(TOKS),diameter#/1.95,height#,diameter#/1.95
TOKRB_SetPosition(rb(TOKS),xp#,yp#,zp#)
TOKRB_SetAngularDamping rb(TOKS),angdamp#
TOKRB_SetLinearDamping rb(TOKS),lindamp#
TOKRB_SetMass rb(TOKS),mass#
TOKRB_SetCylinderInertiaTensor rb(TOKS),Id#,Ih#,Imass#
TOKRB_Active(rb(toks),True)
;TOKRB_CollideConnected(rb(toks),True)
End Function
;==================================================================================
Function place_rb(tex)
campick()
If pictentity>0
PositionEntity pickmarker,pcx#,pcy#,pcz#
;create_TOK_BOX(xs#,ys#,zs#,xp#,yp#,zp#,damp#,angdamp#,mass#,iw#,ih#,id#,imass#,texture,name$)
create_TOK_BOX(4,4,4,pcx#,pcy#+2,pcz#,0.001,0.002,2,4,4,4,2,tex,"foo")
Repeat : Until MouseDown(2) = 0
EndIf
FlushMouse()
End Function
;==================================================================================
;==================================================================================
Function push_rigid_body()
campick()
For n = 1 To TOKS
If pictentity = obj(n)
i = n
If KeyDown(29) Or KeyDown(157)
TOKRB_ApplyImpulse2 rb(i),-pnx#*15,-pny#*15,-pnz#*15,pcx#,pcy#,pcz#
Else
TOKRB_ApplyImpulse2 rb(i),-pnx#,-pny#,-pnz#,pcx#,pcy#,pcz# ;TOKRB_ApplyImpulse rb(i),-pnx#,pny#,-pnz#
EndIf
EndIf
Next
End Function
;==================================================================================
Function create3d_scene(gwidth,gheight,gdepth,gmode)
Graphics3D gwidth,gheight,gdepth,gmode
midw = GraphicsWidth()/2
midh =GraphicsHeight()/2
SetBuffer BackBuffer()
HidePointer
WireFrame False
bigfont=LoadFont("Arial",32,True)
smallfont=LoadFont("Arial",12,True)
; camera
campiv = CreatePivot()
camera = CreateCamera(campiv)
CameraClsColor(camera,0,0,0)
centerpivot = CreatePivot()
PositionEntity centerpivot ,0,10,0
PositionEntity campiv,0,30,-20
PointEntity campiv,centerpivot
; light
light=CreateLight()
PositionEntity light,-20,10,-20
FlushMouse()
; create some textures
animtex = create_anim_texture()
cubetex = create_cube_texture()
spheretex = create_sphere_texture()
cyltex = create_cyl_texture()
worldtex = create_world_texture()
pickmarker = CreateSphere(4) : EntityAlpha pickmarker,.75 : EntityTexture pickmarker,spheretex
; create (or load) TOKAMAK STATIC MESH
world = CreateCube()
;world = LoadMesh("roadmap2.3ds")
EntityPickMode world,2
sc# = 50
ScaleMesh world,sc#,sc#,sc# : PositionMesh world,0,sc#,0 : FlipMesh world : EntityTexture world,worldtex
obstacle = CreateCube() : EntityTexture obstacle,cyltex : ScaleMesh obstacle,5,10,10 : PositionMesh obstacle,-10,16,0
RotateMesh obstacle ,0,0,65 : AddMesh obstacle,world
obstacle2 = CreateSphere(6) : EntityTexture obstacle2,cubetex : ScaleMesh obstacle2,10,4,10 : PositionMesh obstacle2,32,-1,30
RotateMesh obstacle2 ,0,0,0 : AddMesh obstacle2,world
; send the woeld mesh to TOKAMAK
maketokcollider(world)
; CREATE some stacked TOKAMAK boxes
For columns = 1 To 4
For rows = 1 To 4
bxs#= 4 bys#= 4 bzs#=4
bdamp# = 0.001
bangdamp# =0.0002
bmass# = 4
biw# = 4 bih# = 4 bid# = 4
bimass# = 4
create_TOK_BOX(bxs#,bys#,bzs#,(columns*4)+20,(rows*4)+1,16,bdamp#,bangdamp#,bmass#,biw#,bih#,bid#,bimass#,cubetex,"box")
;create_TOK_BOX(4,4,4,pcx#,pcy#+2,pcz#,0.001,0.002,2,4,4,4,2,tex,"foo")
;create_TOK_BOX(4,4,4,(columns*4)+20,rows*4,16,0.001,0.001,1.5,4,4,4,2,cubetex,"car")
;create_TOK_BOX(4,4,4,(columns*4)+20,(rows*4)+1,16,0.02,0.001,4,4,4,4,2,cubetex,"car")
Next
Next
End Function
Function MakeTokCollider(mesh)
scount=CountSurfaces(mesh)
For ind=1 To scount
surface=GetSurface(mesh,ind)
ttltris=ttltris+CountTriangles(surface)
ttlvert=ttlvert+CountVertices(surface)
Next
vertices=CreateBank(16*ttlvert)
triangles=CreateBank(24*ttltris)
offsetv=0
offsett=0
For ind=1 To scount
surface = GetSurface(mesh,ind)
ctr=CountTriangles(surface)
tric=tric+cvt
cvt=CountVertices(surface)
;fill bank with vertices
For v=0 To cvt-1
PokeFloat vertices,offsetv,VertexX#(surface,v)
PokeFloat vertices,offsetv+4,VertexY#(surface,v)
PokeFloat vertices,offsetv+8,VertexZ#(surface,v)
PokeFloat vertices,offsetv+12,0.0
offsetv=offsetv+16
Next
;fill bank with triangles
For v=0 To ctr-1
PokeInt triangles,offsett,tric+TriangleVertex(surface,v,0)
PokeInt triangles,offsett+4,tric+TriangleVertex(surface,v,1)
PokeInt triangles,offsett+8,tric+TriangleVertex(surface,v,2)
PokeInt triangles,offsett+12,0
PokeInt triangles,offsett+16,0
PokeInt triangles,offsett+20,0
offsett=offsett+24
Next
Next
;Hand over the terrain data to Tokamak
TOKSIM_SetStaticMesh vertices,ttlvert,triangles,ttltris
; Now we can free the banks as Tokamak has copied all data
FreeBank vertices
FreeBank triangles
End Function