tokamak physics thread 4

Blitz3D Forums/Blitz3D Programming/tokamak physics thread 4

Ok. Here's two:

;RopeTok Demo v.5
;Rope tokamak demo originally by Souled, modified by dillpickle79 and Bot_builder

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.1


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

    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,-120,0
gr=CreateCube()
ScaleEntity gr,100,.5,100
PositionEntity gr,0,-120,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_SetLinearDamping rb(i),0.0001
TOKRB_SetAngularDamping rb(i),0.0
TOKRB_SetMass rb(i),100
TOKRB_SetBoxInertiaTensor rb(i),20.0,10.0,15.0,100
TOKRB_SetMass rb(i),200
TOKRB_CollideConnected rb(i),True

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

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

; MainLoop
While Not KeyHit(1)
    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_Free joint(Jnumber)
                    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


And here's one by me: http://blitzstuff.250free.com/zips/Tokamak_intro.zip (234 KB)

Its a wrecking ball demo where the ball nocks over the letters "TOKAMAK". No user interaction, so its suitable for a game intro or somthing. Also uses lotus particle system (included, with intact liscence.txt), so at the end "Using lotus" and "tokamak" logos fade in.