Tokamak Rope Demo

Blitz3D Forums/Blitz3D Programming/Tokamak Rope Demo

I was just reminded of a little rope demo that Constantine, Botbuilder, saad, and myself did a while back in the blitz-tokamak forum.
Here's the post
http://playerfactory.proboards25.com/index.cgi?board=tokamak2&action=display&num=1090192420&start=0
Here's what I think is the most stable version (may have been adjusted by me a bit later and not posted on the thread). It isn't a full rope building function, but it is fun to play with to learn how to make a nice rope sim. A full function would use triginometry for the section placement and fun stuff like that. Feel free to add to this however you want - maybe we can make this perfect:
Also, there's an odd glitch where the rope makes the box shoot upwards often (you'll see when you run it)
;Tokamak Rope demo
;V. 3
;By Constantine,Botbuilder, Mr. Picklesworth, and Saad

Const FPS=60
;=========================
Const number = 40 ;Number of rope points
Const Ydistance = 3 ;This also defines the resolution of the rope. \\ Adjust the height of the mesh and RidigBody scale manually if you change it.

Global Lastnumber = number


;=========================
Graphics3D 800,600,0,2
SetBuffer BackBuffer()

WireFrame False

campiv=CreatePivot()

camera = CreateCamera(campiv)
PositionEntity camera,0,2,-170
RotateEntity camera,30,0,0
CameraClsColor camera,82,134,172
light=CreateLight()
PositionEntity light,7,15,-5

TOKSIM_CreateSimulator(0,-20,0)
;========================

Dim mesh (number)
Dim rb (number)
Dim joint(number)

;create the hook
hookPivot = CreatePivot()
PositionEntity hookpivot,0,20,0

hook = CreateSphere()
PositionEntity hook,EntityX#(hookPivot),EntityY#(hookPivot),EntityZ#(hookPivot)
EntityAlpha hook,.5

;========================

TOKSIM_SetRigidBodiesCount number+1
TOKSIM_SetAnimatedBodiesCount 1
TOKSIM_SetGeometriesCount number+2
TOKSIM_CreateSimulator(0,-10,0)

;ground material
TOKSIM_SetMaterial 0,0.8,0

For i=1 To number - 1

    mesh(i) = CreateCylinder()
    ScaleEntity mesh(i),1,1.7,1
    EntityColor mesh(i),196,118,2
    EntityPickMode mesh(i),2
    NameEntity mesh(i),i

    rb(i) = TOKRB_Create()
    TOKRB_AddCylinder rb(i),2.14,1
    TOKRB_SetPosition rb(i),EntityX#(hookPivot),EntityY#(hookPivot)-(i*Ydistance),EntityZ#(hookPivot)
    TOKRB_SetLinearDamping rb(i),0.0001
    TOKRB_SetAngularDamping rb(i),0.0
    TOKRB_SetMass rb(i),20
    TOKRB_SetCylinderInertiaTensor rb(i),2.14,1,20

    TOKRB_CollideConnected rb(i),True

	TOKRB_SetCollisionID rb(i),0

    If i=1
        joint(i) = TOKJOINT_Create(1,rb(i),0)
        TOKJOINT_SetPositionAndRotationWorld(joint(i),0.0,EntityY#(hookPivot)-(i*(Ydistance/2)),0.0,0,0,90)
    Else
        joint(i) = TOKJOINT_Create(2,rb(i-1),rb(i))
        TOKJOINT_SetPositionAndRotationWorld(joint(i),0.0,TOKRB_GetY(rb(i-1))-1.5,0.0,0,0,90)
    EndIf

    TOKJOINT_SetType(joint(i),1)
    TOKJOINT_SetLowerLimit joint(i),3.14*0.0
    TOKJOINT_SetUpperLimit joint(i),3.14*0.8
    TOKJOINT_EnableLimit joint(i),True
    TOKJOINT_Enable(joint(i),True)
    TOKJOINT_SetDampingFactor joint(i),80.0

Next

GroundA=TOKAB_Create()
GroundGeom = TOKAB_AddBox(GroundA,200,1,200)
TOKGEOM_SetMaterialIndex GroundGeom,0
TOKAB_SetPosition GroundA,0,-200,0
gr=CreateCube()
ScaleEntity gr,100,.5,100
PositionEntity gr,0,-200,0

;the last section that it is holding (test)
i = number

mesh(i) = CreateCube()
ScaleEntity mesh(i),20,10,15,True
EntityColor mesh(i),255,0,0
EntityPickMode mesh(i),2
NameEntity mesh(i),i

rb(i) = TOKRB_Create()
TOKRB_AddBox rb(i),40,20,30
TOKRB_SetPosition rb(i),EntityX#(hookPivot),EntityY#(hookPivot)-(i*Ydistance),EntityZ#(hookPivot)
TOKRB_SetBoxInertiaTensor rb(i),40.0,20.0,30.0,200
TOKRB_SetMass rb(i),200
TOKRB_CollideConnected rb(i),True

TOKRB_SetLinearDamping rb(i),0.01
TOKRB_SetAngularDamping rb(i),0.01

TOKRB_SetCollisionID rb(i),1

joint(i) = TOKJOINT_Create(2,rb(i-1),rb(i))
TOKJOINT_SetPositionAndRotationWorld(joint(i),0.0,TOKRB_GetY(rb(i-1))-1.5,0.0,0,0,90)

TOKJOINT_SetType(joint(i),1)
TOKJOINT_SetLowerLimit joint(i),3.14*0.0
TOKJOINT_SetUpperLimit joint(i),3.14*0.8
TOKJOINT_EnableLimit joint(i),True
TOKJOINT_Enable(joint(i),True)
TOKJOINT_SetDampingFactor joint(i),80.0

;TOKSIM_SetCollisionResponse 0,1,0

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

; MainLoop
While Not KeyHit(1)
    If Not KeyDown(57) Then TurnEntity campiv,0,.1,0
    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.9/FPS,2)
        UpdateWorld
    Next
        
    ; destroying sections
    If MouseDown(1) And SAllowed = True Then
        CameraPick(camera,MouseX(),MouseY())
        If Not PickedEntity() = False Then
            Sallowed = False
            Jnumber = EntityName(PickedEntity())
            If Jnumber = number
                TOKRB_ApplyImpulse2 rb(Jnumber),0,0,.1,PickedX() * 2,PickedY() * 2,PickedZ() * 2
                Lastnumber = JNumber - 1
            Else
                TOKRB_ApplyImpulse2 rb(Jnumber),0,0,.1 ,PickedX() * 2,PickedY() * 2,PickedZ() * 2
                If Not joint(JNumber) = False
                    TOKJOINT_Enable joint(Jnumber),False
                    Lastnumber = JNumber - 1
                EndIf
            EndIf
        EndIf
    Else
        If Not MouseDown(1) Then SAllowed = True
    EndIf
    
    ;bouncing sections
    If MouseDown(2)
        CameraPick(camera,MouseX(),MouseY())
        If Not PickedEntity() = False
            Jnumber = EntityName(PickedEntity())
            TOKRB_ApplyImpulse2 rb(Jnumber),0,0,10 ,PickedX() * 2,PickedY() * 2,PickedZ() * 2
        EndIf
    EndIf
        
    For i=1 To number
        PositionEntity mesh(i),TOKRB_GetX#(rb(i)),TOKRB_GetY#(rb(i)),TOKRB_GetZ#(rb(i))
        RotateEntity mesh(i),TOKRB_GetPitch#(rb(i)),TOKRB_GetYaw#(rb(i)),TOKRB_GetRoll#(rb(i))
        
        If KeyDown( 208 )=True Then TOKRB_ApplyImpulse rb(Lastnumber),0,0,-.1
        If KeyDown( 200 )=True Then TOKRB_ApplyImpulse rb(Lastnumber),0,0,.1
        If KeyDown( 203 )=True Then TOKRB_ApplyImpulse rb(Lastnumber),-.1,0,0
        If KeyDown( 205 )=True Then TOKRB_ApplyImpulse rb(Lastnumber),.1,0,0
    Next
    
    RenderWorld tween	
    
    Text 0,0,"Physics Time:"+TOKSIM_GetPhysicsTime()*1000.0+ " milliseconds"
    Text 0,10,"Render Time:"+Str(elapsed)+ " milliseconds"
    Flip False

Wend

TOKSIM_DestroySimulator()

End


Note: You need the latest tokamak wrapper and dll.