Tokamak Ragdoll

Blitz3D Forums/Blitz3D Userlibs/Tokamak Ragdoll

srcoder(Posted 2007) [#1]
I got bored working with this and thought I'd post it for your use. Dont expect me to answer any forum questions and a lot of the code is borrowed. Have fun!! Left mouse fires a ragdoll, right mouse fires a high mass cube.


;///////////////////////////TOKOMAK BASIC FILE//////////////////////////////////////////////////




Const Rigid_Bodies=500,Animated_Bodies=5
TOKSIM_SetRigidBodiesCount Rigid_Bodies
TOKSIM_SetAnimatedBodiesCount Animated_Bodies
TOKSIM_SetRigidParticleCount 0
TOKSIM_SetControllersCount 0
TOKSIM_SetGeometriesCount Rigid_Bodies+Animated_Bodies ;Assuming each one only has one geometry. Not true with more complex rigid bodies and animated bodies.
TOKSIM_CreateSimulator(0,-10,0)

Global vertices=CreateBank(0),triangles=CreateBank(0)
Global offsett,offsetv,tric,ttlvert,ttltris

Dim tname$(20),tMindex(20)
Global cur=0


Graphics3D 640,480
SeedRnd(MilliSecs())

;Setup The scene

timer = CreateTimer(60)

Global player = CreatePivot()
Global campitch# = 0.0
Global camera = CreateCamera(player)
HidePointer()
MoveMouse 320,240
PositionEntity(player,0,5,-300)


Local light = CreateLight()
RotateEntity(light,45,45,0)


ground = CreateCube()
ScaleMesh(ground,500,1,500)

;create some textures for the ground: No media required!!
Global grasstex = CreateTexture(64,64)
SetBuffer TextureBuffer(grasstex)
For x = 0 To 64
For y = 0 To 64
Color 0,Rnd(100,255),0
Plot x,y
Next
Next

ScaleTexture(grasstex,0.1,0.1)
EntityTexture(ground,grasstex)

gab = TOKAB_Create()
TOKAB_AddBox(gab,1000,2,1000)



Const FPS = 60
period=1000/FPS
time=MilliSecs()-period

imptime=MilliSecs()
; MainLoop
While Not KeyHit(1)

UpdatePlayer()

;Position graphical representation
UpdateTokCubes()
UpdateTokSpheres()

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(2.0/FPS,1)
Next



RenderWorld()
Flip
WaitTimer(timer)
Wend

TOKSIM_DestroySimulator
End

Function UpdatePlayer()
y# = -MouseXSpeed() * 0.5
TurnEntity(player,0,y#,0)
campitch = campitch + MouseYSpeed() * 0.5
If campitch > 80.0 Then campitch = 80.0
If campitch < -80.0 Then campitch = -80.0
RotateEntity(camera,campitch,0,0)
TFormVector(0,0,1,camera,0)
If(KeyDown(17)) Then TranslateEntity(player,TFormedX(),TFormedY(),TFormedZ())
If KeyDown(31) Then TranslateEntity(player,-TFormedX(),-TFormedY(),-TFormedZ())
If KeyDown(30) Then MoveEntity player,-0.5,0,0
If KeyDown(32) Then MoveEntity player,0.5,0,0
If MouseHit(1)
r.ragdoll = CreateRagdoll(EntityX(player),EntityY(player),EntityZ(player),EntityPitch(camera,True)+90,EntityYaw(camera,True),EntityRoll(camera,True))
TOKRB_SetVelocity(r\body\rb,TFormedX()* 300 ,TFormedY()* 300,TFormedZ()*300)
EndIf
If MouseHit(2)
s.tokcube = CreateTokCube(EntityX(player),EntityY(player),EntityZ(player),1,1,1)
TOKRB_SetVelocity(s\rb,TFormedX()*100,TFormedY()*100,TFormedZ()*100)
TOKRB_SetMass(s\rb,100)
EndIf

MoveMouse 320,240
End Function


Function PositionAndRotate(obj,rb)
PositionEntity obj,TOKRB_GetX(rb),TOKRB_GetY(rb),TOKRB_GetZ(rb)
RotateEntity obj,TOKRB_GetPitch(rb),TOKRB_GetYaw(rb),TOKRB_GetRoll(rb)
End Function

Type toksphere
Field entity,rb,geom
End Type

Function CreateTokSphere.toksphere(x#,y#,z#,radius#,parent = 0)
Local s.toksphere = New toksphere
s\entity = CreateSphere(8,parent)
ScaleMesh(s\entity,radius#,radius#,radius#)
PositionEntity(s\entity,x#,y#,z#)
EntityColor(s\entity,200,200,200)
s\rb = TOKRB_Create()
s\geom = TOKRB_AddSphere(s\rb,2.0*radius#)
;TOKGEOM_SetMaterialIndex c\geom,Rnd(1,3)
TOKRB_SetPosition s\rb,EntityX(s\entity,True),EntityY(s\entity,True),EntityZ(s\entity,True)
TOKRB_SetRotation s\rb,EntityPitch(s\entity,True),EntityYaw(s\entity,True),EntityRoll(s\entity,True)
TOKRB_SetLinearDamping s\rb,0.00001
TOKRB_SetAngularDamping s\rb,0.0
TOKRB_SetMass s\rb,3.14*(radius*radius*radius)
TOKRB_SetSphereInertiaTensor s\rb,2.0,2.0
EntityParent(s\entity,0)
Return s
End Function

Function UpdateTokSpheres()
For s.toksphere = Each toksphere
PositionEntity(s\entity,TOKRB_GetX(s\rb),TOKRB_GetY(s\rb),TOKRB_GetZ(s\rb))
RotateEntity(s\entity,TOKRB_GetPitch(s\rb),TOKRB_GetYaw(s\rb),TOKRB_GetRoll(s\rb))
Next
End Function


Type tokcube
Field entity,rb,geom
End Type

Function CreateTokCube.tokcube(x#,y#,z#,width#,height#,depth#, parent = 0)
Local c.tokcube = New tokcube
c\entity = CreateCube(parent)
ScaleMesh(c\entity,width#,height#,depth#)
PositionEntity(c\entity,x#,y#,z#)
EntityColor(c\entity,200,200,200)
c\rb = TOKRB_Create()
c\geom = TOKRB_AddBox(c\rb,width#*2.0,height#*2.0,depth#*2.0)
;TOKGEOM_SetMaterialIndex c\geom,Rnd(1,3)
TOKRB_SetPosition c\rb,EntityX(c\entity,True),EntityY(c\entity,True),EntityZ(c\entity,True)
TOKRB_SetRotation c\rb,EntityPitch(c\entity,True),EntityYaw(c\entity,True),EntityRoll(c\entity,True)
TOKRB_SetLinearDamping c\rb,0.00001
TOKRB_SetAngularDamping c\rb,0.0
mass# = Sqr((width#*width#)+(height#*height#)+(depth#*depth#))
TOKRB_SetMass c\rb,mass#
TOKRB_SetBoxInertiaTensor c\rb,widtth#*2.0,height#*2.0,depth#*2.0,mass#
EntityParent(c\entity,0)
Return c
End Function

Function UpdateTokCubes()
idlecount = 0
For c.tokcube = Each tokcube
PositionEntity(c\entity,TOKRB_GetX(c\rb),TOKRB_GetY(c\rb),TOKRB_GetZ(c\rb))
RotateEntity(c\entity,TOKRB_GetPitch(c\rb),TOKRB_GetYaw(c\rb),TOKRB_GetRoll(c\rb))
If TOKRB_IsIdle(c\rb)
idlecount = idlecount+1
End If
Next
End Function



Function TOK_SetTextureMaterial(fil$,Mindex)
tname$(cur)=fil$
tMindex(cur)=Mindex
cur=cur+1
End Function

Function TOK_AddMesh(mesh,recurse=0,mat=-1,textureindex=0)
scount=CountSurfaces(mesh)
For ind=1 To scount
surface=GetSurface(mesh,ind)
ttltris=ttltris+CountTriangles(surface)
ttlvert=ttlvert+CountVertices(surface)
Next
ResizeBank triangles,ttltris*24
ResizeBank vertices,ttlvert*16
For ind=1 To scount
surface = GetSurface(mesh,ind)
bru=GetSurfaceBrush(surface)
tex=GetBrushTexture(bru,textureindex)
nam$=StripPath$(TextureName$(tex))
fo=0
For a=0 To cur-1
If tname$(a)=nam$ Then mindex=tMindex(a):fo=1:Exit
Next
If fo=0 Then mindex=0
FreeBrush bru
ctr=CountTriangles(surface)
tric=tric+cvt
cvt=CountVertices(surface)
;add vertices to bank
For v=0 To cvt-1
TFormPoint VertexX#(surface,v),VertexY#(surface,v),VertexZ#(surface,v),mesh,0
PokeFloat vertices,offsetv,TFormedX()
PokeFloat vertices,offsetv+4,TFormedY()
PokeFloat vertices,offsetv+8,TFormedZ()
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)
If mat=-1 Then PokeInt triangles,offsett+12,mindex Else PokeInt triangles,offsett+12,mat
PokeInt triangles,offsett+16,0
PokeInt triangles,offsett+20,0
offsett=offsett+24
Next
Next
If recurse Then
children=CountChildren(mesh)
If children>0 Then
For childcount = 1 To children
child = GetChild(entity,childcount)
TOK_AddMesh child,recurse,mat
Next
EndIf
EndIf
End Function

Function TOK_SetMesh()
;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

Function StripPath$(file$)
If Len(file$)>0
For i=Len(file$) To 1 Step -1
mi$=Mid$(file$,i,1)
If mi$="\" Or mi$="/" Then Return name$ Else name$=mi$+name$
Next
EndIf
Return name$
End Function

Function TOK_ReInitializeBanks()
vertices=CreateBank(0)
triangles=CreateBank(0)
offsett=0
offsetv=0
tric=0
ttlvert=0
ttltris=0
End Function

Type ragdoll
Field body.tokcube
Field head.toksphere
Field larm.tokcube,l4arm.tokcube
Field rarm.tokcube,r4arm.tokcube
Field lthi.tokcube,lshin.tokcube
Field rthi.tokcube,rshin.tokcube
End Type

Function CreateRagdoll.ragdoll(x#,y#,z#,pitch#,yaw#,roll#)
Local r.ragdoll = New ragdoll
r\body = CreateTokCube(x+0,y+1,z+0,1,2,1)
;rotate the body and make the rest of objects as children to this
RotateEntity(r\body\entity,pitch#,yaw#,roll#)
TOKRB_SetRotation(r\body\rb,pitch,yaw,roll)
;now the heed
r\head = CreateTokSphere(0,3,0,1,r\body\entity)
Local neck = TOKJOINT_Create(2,r\head\rb,r\body\rb)
TOKJOINT_SetType(neck,1)
; TOKRB_CollideConnected(body\rb,True)
TOKJOINT_SetPositionAndRotationFrameA(neck,0,-0.5,0,0,0,90)
TOKJOINT_SetPositionAndRotationFrameB(neck,0,2.5,0,0,0,90)
TOKJOINT_SetLowerLimit neck,-3.14*0.0
TOKJOINT_SetUpperLimit neck,3.14*0.2
TOKJOINT_EnableLimit neck,True
TOKJOINT_Enable neck,True
TOKJOINT_SetDampingFactor neck,0.1
;now the arm
r\rarm = CreateTokCube(2.0,1.5,0,1.0,0.5,0.5,r\body\entity)
rshoulder = TOKJOINT_Create(2,r\rarm\rb,r\body\rb)
TOKJOINT_SetType(rshoulder,1)
TOKJOINT_SetPositionAndRotationFrameA(rshoulder,-0.75,0.0,0,0,0,0)
TOKJOINT_SetPositionAndRotationFrameB(rshoulder,1.25,1.5,0,0,0,0)
TOKJOINT_SetLowerLimit rshoulder,3.14*0.0 ; (0 degrees)
TOKJOINT_SetUpperLimit rshoulder,3.14*0.5 ; (+90 degrees)
TOKJOINT_EnableLimit rshoulder,True
TOKJOINT_Enable rshoulder,True
TOKJOINT_SetDampingFactor rshoulder,0.1
r\larm = CreateTokCube(-2.0,1.5,0,1,0.5,0.5,r\body\entity)
lshoulder = TOKJOINT_Create(2,r\larm\rb,r\body\rb)
TOKJOINT_SetType(lshoulder,1)
; TOKJOINT_SetPositionAndRotationWorld(lshoulder,x-1.5,y+2.0,z+0,0,0,0)
TOKJOINT_SetPositionAndRotationFrameA(lshoulder,0.75,0.0,0,0,0,0)
TOKJOINT_SetPositionAndRotationFrameB(lshoulder,-1.25,1.5,0,0,0,0)
TOKJOINT_SetLowerLimit lshoulder,3.14*0.0 ; (0 degrees)
TOKJOINT_SetUpperLimit lshoulder,3.14*0.4 ; (+90 degrees)
TOKJOINT_EnableLimit lshoulder,True
TOKJOINT_Enable lshoulder,True
TOKJOINT_SetDampingFactor lshoulder,0.1
r\r4arm = CreateTokCube(4.0,1.5,0,1.0,0.5,0.5,r\body\entity)
relbow = TOKJOINT_Create(2,r\r4arm\rb,r\rarm\rb)
TOKJOINT_SetType(relbow,3)
; TOKJOINT_SetPositionAndRotationWorld(relbow,x+3.0,y+1.5,z+0,0,0,0)
TOKJOINT_SetPositionAndRotationFrameA(relbow,-1.0,0,0,0,0,0)
TOKJOINT_SetPositionAndRotationFrameB(relbow,1,0,0,0,0,0)
TOKJOINT_SetLowerLimit relbow,3.14*-0.1 ; (0 degrees)
TOKJOINT_SetUpperLimit relbow,3.14*0.5 ; (+90 degrees)
TOKJOINT_EnableLimit relbow,True
TOKJOINT_Enable relbow,True
TOKJOINT_SetDampingFactor relbow,0.1
r\l4arm = CreateTokCube(-4.0,1.5,0,1.0,0.5,0.5,r\body\entity)
lelbow = TOKJOINT_Create(2,r\l4arm\rb,r\larm\rb)
TOKJOINT_SetType(lelbow,3)
; TOKJOINT_SetPositionAndRotationWorld(lelbow,x-3.0,y+1.5,z+0,0,0,0)
TOKJOINT_SetPositionAndRotationFrameA(lelbow,1.0,0,0,0,0,0)
TOKJOINT_SetPositionAndRotationFrameB(lelbow,-1,0,0,0,0,0)
TOKJOINT_SetLowerLimit lelbow,3.14*-0.1 ; (0 degrees)
TOKJOINT_SetUpperLimit lelbow,3.14*0.5 ; (+90 degrees)
TOKJOINT_EnableLimit lelbow,True
TOKJOINT_Enable lelbow,True
TOKJOINT_SetDampingFactor lelbow,0.1
r\rthi = CreateTokCube(0.75,-3.0,0.0,0.5,1.0,0.5,r\body\entity)
rhip = TOKJOINT_Create(2,r\rthi\rb,r\body\rb)
TOKJOINT_SetType(rhip,1)
; TOKJOINT_SetPositionAndRotationWorld(rhip,x+0.6,y-2.0,z+0,0,0,-90)
TOKJOINT_SetPositionAndRotationFrameA(rhip,0,1,0,0,0,-90)
TOKJOINT_SetPositionAndRotationFrameB(rhip,0.75,-2.0,0,0,0,-90)
TOKJOINT_SetLowerLimit rhip,3.14*0.0 ; (0 degrees)
TOKJOINT_SetUpperLimit rhip,3.14*0.3 ; (+90 degrees)
TOKJOINT_EnableLimit rhip,True
TOKJOINT_Enable rhip,True
TOKJOINT_SetDampingFactor rhip,0.1
r\lthi = CreateTokCube(-0.75,-3.0,0.0,0.5,1.0,0.5,r\body\entity)
lhip = TOKJOINT_Create(2,r\lthi\rb,r\body\rb)
TOKJOINT_SetType(lhip,1)
; TOKJOINT_SetPositionAndRotationWorld(lhip,x-0.6,y-2.0,z+0,0,0,-90)
TOKJOINT_SetPositionAndRotationFrameA(lhip,0,1,0,0,0,-90)
TOKJOINT_SetPositionAndRotationFrameB(lhip,-0.75,-2.0,0,0,0,-90)

TOKJOINT_SetLowerLimit lhip,3.14*0.0 ; (0 degrees)
TOKJOINT_SetUpperLimit lhip,3.14*0.3 ; (+90 degrees)
TOKJOINT_EnableLimit lhip,True
TOKJOINT_Enable lhip,True
TOKJOINT_SetDampingFactor lhip,0.1
r\rshin = CreateTokCube(0.75,-5.0,0.0,0.5,1.0,0.5,r\body\entity)
rknee = TOKJOINT_Create(2,r\rshin\rb,r\rthi\rb)
TOKJOINT_SetType(rknee,3)
TOKJOINT_SetPositionAndRotationFrameA(rknee,0,1,0,0,0,-90)
TOKJOINT_SetPositionAndRotationFrameB(rknee,0,-1,0,0,0,-90)
TOKJOINT_SetLowerLimit rknee,3.14*-0.1 ; (0 degrees)
TOKJOINT_SetUpperLimit rknee,3.14*0.6 ; (+90 degrees)
TOKJOINT_EnableLimit rknee,True
TOKJOINT_Enable rknee,True
TOKJOINT_SetDampingFactor rknee,0.1
r\lshin = CreateTokCube(-0.75,-5.0,0.0,0.5,1.0,0.5,r\body\entity)
lknee = TOKJOINT_Create(2,r\lshin\rb,r\lthi\rb)
TOKJOINT_SetType(lknee,3)
; TOKJOINT_SetPositionAndRotationWorld(lknee,x-0.6,y-4.0,z+0,0,0,-90)
TOKJOINT_SetPositionAndRotationFrameA(lknee,0,1,0,0,0,-90)
TOKJOINT_SetPositionAndRotationFrameB(lknee,0,-1,0,0,0,-90)
TOKJOINT_SetLowerLimit lknee,3.14*-0.1 ; (0 degrees)
TOKJOINT_SetUpperLimit lknee,3.14*0.6 ; (+90 degrees)
TOKJOINT_EnableLimit lknee,True
TOKJOINT_Enable lknee,True
TOKJOINT_SetDampingFactor lknee,0.1


;make the rigid bodies inactive
; TOKRB_Active r\body\rb,False
; TOKRB_Active r\head\rb,False
; TOKRB_Active r\larm\rb,False
; TOKRB_Active r\rarm\rb,False
; TOKRB_Active r\l4arm\rb,False
; TOKRB_Active r\r4arm\rb,False
; TOKRB_Active r\lthi\rb,False
; TOKRB_Active r\rthi\rb,False
; TOKRB_Active r\lshin\rb,False
; TOKRB_Active r\rshin\rb,False
Return r
End Function

Function ActivateRagdoll(r.ragdoll)
TOKRB_Active r\body\rb,True
TOKRB_Active r\head\rb,True
TOKRB_Active r\larm\rb,True
TOKRB_Active r\rarm\rb,True
TOKRB_Active r\l4arm\rb,True
TOKRB_Active r\r4arm\rb,True
TOKRB_Active r\lthi\rb,True
TOKRB_Active r\rthi\rb,True
TOKRB_Active r\lshin\rb,True
TOKRB_Active r\rshin\rb,True
End Function

Function DeactivateRagdoll(r.ragdoll)
;make the rigid bodies inactive
TOKRB_Active r\body\rb,False
TOKRB_Active r\head\rb,False
TOKRB_Active r\larm\rb,False
TOKRB_Active r\rarm\rb,False
TOKRB_Active r\l4arm\rb,False
TOKRB_Active r\r4arm\rb,False
TOKRB_Active r\lthi\rb,False
TOKRB_Active r\rthi\rb,False
TOKRB_Active r\lshin\rb,False
TOKRB_Active r\rshin\rb,False
End Function



;~IDEal Editor Parameters: