RagDolls Ogre and Physics

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 202
Joined: Mon Jun 09, 2003 8:30 am

RagDolls Ogre and Physics

Post by bobobo »

Code: Select all

;
;
;
; ------------------------------------------------------------
;
;   PureBasic - RagDoll
;
;    (c) 2002 - Fantaisie Software , bobobo
; OpenGL scheitert bei mir, das mag am Treiber liegen (Laptop)
; das Programm sollte im Verzeichnis  \Examples\3D liegen
; weil es auf den Screenrequester und andere Daten dort zugreift
; alles ohne Gewähr
; ------------------------------------------------------------
; ApplyEntityImpulse without ÜPostion (causes the 3dEngine to stop v5.70)


#CameraSpeed = 0.5

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY ,rott, moveit

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "/Examples/3D/" +"Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "/Examples/3D/" +"Data/Packs/desert.zip", #PB_3DArchive_Zip)
  Parse3DScripts()
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
     EnableWorldPhysics(#True)
     EnableWorldCollisions(#True)
    WorldShadows(#PB_Shadow_Additive  )
    SkyBox("desert07.jpg")
    
    camera=CreateCamera(#PB_Any,0,0,100,100)
    MoveCamera(camera,0, 260 ,15, #PB_Absolute)
    
    texture1t=CreateTexture(#PB_Any,256,256)
    StartDrawing(TextureOutput(texture1t))
    
    Box(0, 0, 256, 256, RGBA(222,222,255, 200))
    For l=0 To 128 
      Circle(Random(256),Random(256),Random(4)+1,RGBA(0,0,255,255))
    Next l
    StopDrawing()
    
    texture1=CreateMaterial(#PB_Any, TextureID(texture1t))
    ScaleMaterial(texture1,100,100)
    
    texture2= CreateTexture(#PB_Any,256,256)
    StartDrawing(TextureOutput(texture2))
    DrawingMode(#PB_2DDrawing_AllChannels )
    Box(0,0,256,256,RGBA(80,80,80,18))
    
    For l=0 To 400
      Circle(Random(256),Random(256),Random(3)+1,RGBA(Random(5)+100,Random(5)+100,Random(5)+100,180))
    Next l
    
    DrawText(0,0,"ragdoll ®",RGBA(255,0,0,255),RGBA(80,80,80,222))
    StopDrawing()
    texture2=CreateMaterial(#PB_Any, TextureID(texture2))
    
    Structure components
      ball.i
      limb.i
      texture.i
      Bodymesh.i
      Body.i
      HeadMesh.i
      Head.i
      arml.i
      armlu.i
      armr.i
      armru.i
      legl.i
      leglu.i
      legr.i
      legru.i
      ct_bodyhead.i
      ct_arml.i
      hinge_arml.i
      ct_armr.i
      hinge_armr.i
      hinge_legl.i
      hinge_leglu.i
      hinge_legr.i
      hinge_legru.i
      posx.i
      posy.i
      posz.i
    EndStructure
    
    Define ragdoll.components
    
    NewList ragdolls.components()
    
    For r=1 To 3 ;3 Dinger
      
      AddElement(ragdolls())
      ragdolls()\posx=r*2-2
      ragdolls()\posy=260
      ragdolls()\posz=0
      
      ragdolls()\texture=texture2;CreateMaterial(#PB_Any, TextureID(texture2))
      
      ;bodyparts
      ragdolls()\Bodymesh=CreateCube(#PB_Any,1)
      ragdolls()\Body=CreateEntity(#PB_Any,MeshID(ragdolls()\Bodymesh),MaterialID(ragdolls()\texture),ragdolls()\posx,ragdolls()\posy,ragdolls()\posz)
      ScaleEntity(ragdolls()\Body,1.0,1,0.25)
      
      ragdolls()\HeadMesh = CreateSphere(#PB_Any,0.5)
      ragdolls()\limb   = CreateCylinder(#PB_Any,0.1,0.80)
      ragdolls()\Head = CreateEntity(#PB_Any,MeshID(ragdolls()\HeadMesh),MaterialID(ragdolls()\texture),  ragdolls()\posx,ragdolls()\posy+1,ragdolls()\posz)
      
      ragdolls()\arml  = CreateEntity(#PB_Any,MeshID(ragdolls()\limb),MaterialID(ragdolls()\texture)  ,ragdolls()\posx-0.6,ragdolls()\posy,ragdolls()\posz)
      ragdolls()\armlu = CreateEntity(#PB_Any,MeshID(ragdolls()\limb),MaterialID(ragdolls()\texture) ,ragdolls()\posx-0.6,ragdolls()\posy-1,ragdolls()\posz)
      
      ragdolls()\armr  = CreateEntity(#PB_Any,MeshID(ragdolls()\limb),MaterialID(ragdolls()\texture)  ,ragdolls()\posx+0.6,ragdolls()\posy,ragdolls()\posz)
      ragdolls()\armru = CreateEntity(#PB_Any,MeshID(ragdolls()\limb),MaterialID(ragdolls()\texture) ,ragdolls()\posx+ 0.6,ragdolls()\posy-1,ragdolls()\posz)
      
      ragdolls()\legl = CreateEntity(#PB_Any,MeshID(ragdolls()\limb),MaterialID(ragdolls()\texture) ,ragdolls()\posx-0.3,ragdolls()\posy-1,ragdolls()\posz)
      ragdolls()\legr = CreateEntity(#PB_Any,MeshID(ragdolls()\limb),MaterialID(ragdolls()\texture) ,ragdolls()\posx+ 0.3,ragdolls()\posy-1,ragdolls()\posz)
      
      ragdolls()\leglu = CreateEntity(#PB_Any,MeshID(ragdolls()\limb),MaterialID(ragdolls()\texture),ragdolls()\posx-0.3,ragdolls()\posy-2,ragdolls()\posz)
      ragdolls()\legru = CreateEntity(#PB_Any,MeshID(ragdolls()\limb),MaterialID(ragdolls()\texture),ragdolls()\posx+ 0.3,ragdolls()\posy-2,ragdolls()\posz)
      ;bodyPhysics
      CreateEntityBody(ragdolls()\Body,#PB_Entity_BoxBody  ,10, 0,1)
      EntityRenderMode(ragdolls()\Body, #PB_Entity_CastShadow)
      
      EntityRenderMode(ragdolls()\Head, #PB_Entity_CastShadow)
      CreateEntityBody(ragdolls()\Head, #PB_Entity_SphereBody, 5, 0.25, 1)
      
      EntityRenderMode(ragdolls()\arml, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\arml, #PB_Entity_CylinderBody,  1, 0 ,1)
      EntityRenderMode(ragdolls()\armlu, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\armlu, #PB_Entity_CylinderBody, 1, 0, 1)
      
      EntityRenderMode(ragdolls()\armr, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\armr, #PB_Entity_CylinderBody,  1, 0, 1)
      EntityRenderMode(ragdolls()\armru, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\armru, #PB_Entity_CylinderBody, 1, 0,1)
      
      EntityRenderMode(ragdolls()\legl, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\legl, #PB_Entity_CylinderBody, 1, 0,1)
      EntityRenderMode(ragdolls()\leglu, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\leglu, #PB_Entity_CylinderBody,1, 0,1)
      
      EntityRenderMode(ragdolls()\legr, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\legr, #PB_Entity_CylinderBody, 1, 0,1)
      EntityRenderMode(ragdolls()\legru, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\legru, #PB_Entity_CylinderBody,1, 0,1)
      
      ;joints
      ragdolls()\ct_bodyhead=ConeTwistJoint(#PB_Any,EntityID(ragdolls()\Body)  , 0, 0.7,0,EntityID(ragdolls()\Head) ,0,-0.4,0)
      
      ragdolls()\ct_arml     = ConeTwistJoint(#PB_Any,EntityID(ragdolls()\Body),-0.6,  0.5 ,0,EntityID(ragdolls()\arml)       , 0, 0.4,0)
      ragdolls()\hinge_arml  = HingeJoint(#PB_Any,EntityID(ragdolls()\arml)    ,   0, -0.45,0,1,0,0,EntityID(ragdolls()\armlu), 0,0.45,0,1,0,0)
      
      ragdolls()\ct_armr     = ConeTwistJoint(#PB_Any,EntityID(ragdolls()\Body), 0.6, 0.5,0,EntityID(ragdolls()\armr),        0, 0.4,0)
      ragdolls()\hinge_armr  = HingeJoint(#PB_Any,EntityID(ragdolls()\armr)    , 0  ,-0.45,0,1,0,0,EntityID(ragdolls()\armru), 0,0.45,0,1,0,0)
      
      ragdolls()\hinge_legl  = HingeJoint(#PB_Any,EntityID(ragdolls()\Body)    ,-0.3,-0.60,0,1,0,0,EntityID(ragdolls()\legl) , 0,0.40,0,1,0,0)
      ragdolls()\hinge_legr  = HingeJoint(#PB_Any,EntityID(ragdolls()\Body)    , 0.3,-0.60,0,1,0,0,EntityID(ragdolls()\legr) , 0,0.40,0,1,0,0)
      ragdolls()\hinge_leglu = HingeJoint(#PB_Any,EntityID(ragdolls()\legl)    ,   0,-0.45,0,1,0,0,EntityID(ragdolls()\leglu), 0,0.45,0,1,0,0)
      ragdolls()\hinge_legru = HingeJoint(#PB_Any,EntityID(ragdolls()\legr)    ,   0,-0.45,0,1,0,0,EntityID(ragdolls()\legru), 0,0.45,0,1,0,0)
      
      ;joints attributes       
      SetJointAttribute(ragdolls()\ct_bodyhead,#PB_ConeTwistJoint_SwingSpan,0.25)
      SetJointAttribute(ragdolls()\ct_bodyhead,#PB_ConeTwistJoint_SwingSpan2,0)
      SetJointAttribute(ragdolls()\ct_bodyhead,#PB_ConeTwistJoint_TwistSpan,0.25)
      ;       ;          
      SetJointAttribute(ragdolls()\ct_arml,#PB_ConeTwistJoint_SwingSpan ,0.2);seitl
      SetJointAttribute(ragdolls()\ct_arml,#PB_ConeTwistJoint_SwingSpan2,1);dreh
      SetJointAttribute(ragdolls()\ct_arml,#PB_ConeTwistJoint_TwistSpan ,2);vorzurück
      ;       ;          
      SetJointAttribute(ragdolls()\hinge_arml,#PB_HingeJoint_LowerLimit,0)
      SetJointAttribute(ragdolls()\hinge_arml,#PB_HingeJoint_UpperLimit,120)
      SetJointAttribute(ragdolls()\hinge_armr,#PB_HingeJoint_LowerLimit,0)
      SetJointAttribute(ragdolls()\hinge_armr,#PB_HingeJoint_UpperLimit,120)
      
      SetJointAttribute(ragdolls()\ct_armr,#PB_ConeTwistJoint_SwingSpan,0.2);seitlich
      SetJointAttribute(ragdolls()\ct_armr,#PB_ConeTwistJoint_SwingSpan2,1);dreh
      SetJointAttribute(ragdolls()\ct_armr,#PB_ConeTwistJoint_TwistSpan,2);vorzurück
      
      SetJointAttribute(ragdolls()\hinge_legl,#PB_HingeJoint_LowerLimit,-1)
      SetJointAttribute(ragdolls()\hinge_legl,#PB_HingeJoint_UpperLimit,120)
      SetJointAttribute(ragdolls()\hinge_legr,#PB_HingeJoint_LowerLimit,-1)
      SetJointAttribute(ragdolls()\hinge_legr,#PB_HingeJoint_UpperLimit,120)
      
      SetJointAttribute(ragdolls()\hinge_leglu,#PB_HingeJoint_LowerLimit,-120)
      SetJointAttribute(ragdolls()\hinge_leglu,#PB_HingeJoint_UpperLimit,1)
      SetJointAttribute(ragdolls()\hinge_legru,#PB_HingeJoint_LowerLimit,-120)
      SetJointAttribute(ragdolls()\hinge_legru,#PB_HingeJoint_UpperLimit,1)
    Next 
    ;
    
    
    
    plate=CreateCube(#PB_Any,1)
    ScaleMaterial(texture1,0.5,0.5)
    ;Kartons (50kg)
;     NewList plates()
;     For xp=0 To 10
;       For yp=0 To 10
;         AddElement(plates())
;         plates()=CreateEntity(#PB_Any,MeshID(plate),MaterialID(texture1),35+0.25*Random(100)-50,290,35+0.25*Random(100)-50)
;         ScaleEntity(plates(),1,0.5+Random(1)/4,1)
;         EntityRenderMode(plates(), #PB_Entity_CastShadow)
;         EntityPhysicBody(plates(),#PB_Entity_BoxBody,50,0.3,0.5)
;       Next yp
;     Next xp
    
    platte=CreateEntity(#PB_Any,MeshID(plate),MaterialID(texture1),0,0,0)
    ScaleEntity(platte,50,0.01,50)
    MoveEntity(platte,0,256,0)
    EntityRenderMode(platte, #PB_Entity_CastShadow)
    CreateEntityBody(platte,#PB_Entity_StaticBody,1000,1,0.5)
    
    sun=CreateLight(#PB_Any,RGB(238, 173, 148),17830,12860,10930,#PB_Light_Point)
    
    CameraLookAt(camera,EntityX(ragdolls()\Head),EntityY(ragdolls()\Head),EntityZ(ragdolls()\Head))
    dance=1
    hands=1
    Repeat
      Screen3DEvents()
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If hands=1
        hands=2
        Debug hands  
        ;Händchenhalten    
        sp=-0.5
        tau.f=0
        damp.f=0.5
        SelectElement(ragdolls(),0)
        jx=EntityX(ragdolls()\armru)
        
        jy=EntityY(ragdolls()\armru)
        jz=EntityZ(ragdolls()\armru)
        id=EntityID(ragdolls()\armru)
        SelectElement(ragdolls(),1)
        jx1=EntityX(ragdolls()\armlu)
        jy1=EntityY(ragdolls()\armlu)
        jz1=EntityZ(ragdolls()\armlu)
        id1=EntityID(ragdolls()\armlu)
        knupf1=PointJoint(#PB_Any,id,0,sp,0,id1,0,sp,0)
        SetJointAttribute(knupf1,#PB_PointJoint_Damping,damp)
        SetJointAttribute(knupf1,#PB_PointJoint_Tau,tau)
        
        jx=EntityX(ragdolls()\armru)
        jy=EntityY(ragdolls()\armru)
        jz=EntityZ(ragdolls()\armru)
        id=EntityID(ragdolls()\armru)
        SelectElement(ragdolls(),2)
        jx1=EntityX(ragdolls()\armlu)
        jy1=EntityY(ragdolls()\armlu)
        jz1=EntityZ(ragdolls()\armlu)
        id1=EntityID(ragdolls()\armlu)
        knupf2=PointJoint(#PB_Any,id,0,sp,0,id1,0,sp,0)
        SetJointAttribute(knupf2,#PB_PointJoint_Damping,damp)
        SetJointAttribute(knupf2,#PB_PointJoint_Tau,tau)
        
        
      EndIf
      If hands=3
        Debug hands  
        hands=0
        If knupf1
          FreeJoint(knupf1)
          knupf1=0
        EndIf
        If knupf2
          FreeJoint(knupf2)
          knupf2=0
        EndIf
        If knupf3
          FreeJoint(knupf3)
          knupf3=0
        EndIf
        
      EndIf
      
      If dance=1
        ForEach ragdolls()
          If EntityY(ragdolls()\Head)<259.5
            ApplyEntityImpulse(ragdolls()\Head  ,0, 9,0)
            ApplyEntityImpulse(ragdolls()\legru  ,0, -5,0)
            ApplyEntityImpulse(ragdolls()\leglu  ,0, -5,0)
            If EntityY(ragdolls()\leglu)<256.8
              ApplyEntityImpulse(ragdolls()\leglu,Random(2)-1,Random(8),Random(2)-1)
            EndIf
            If EntityY(ragdolls()\legru)<256.8
              ApplyEntityImpulse(ragdolls()\legru,Random(2)-1,Random(8),Random(2)-1)
            EndIf
            ApplyEntityImpulse(ragdolls()\armlu,Random(2)-1,Random(2)-1,Random(2)-1)
            ApplyEntityImpulse(ragdolls()\armru,Random(2)-1,Random(2)-1,Random(2)-1)
          EndIf
        Next          
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_P) Or KeyboardReleased(#PB_Key_R)
          px.f=0
          py.f=0
          pz.f=0
          ForEach ragdolls()
            px+EntityX(ragdolls()\Body)
            py+EntityY(ragdolls()\Body)
            pz+EntityZ(ragdolls()\Body)
          Next
          px=px/ListSize(ragdolls())
          py=py/ListSize(ragdolls())
          pz=pz/ListSize(ragdolls())
          
          CameraLookAt(camera,px,py,pz)
        EndIf
        
        If KeyboardPushed(#PB_Key_S) And shoot=0
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\Body,0,0,-100,Random(8)-4,Random(8)-4,Random(8)-4)
            shoot=1
          Next
        EndIf
        
        If KeyboardReleased(#PB_Key_S)
          shoot=0
        EndIf
        
        If KeyboardPushed(#PB_Key_R)
          ForEach ragdolls()
            MoveEntity(ragdolls()\Head,ragdolls()\posx,ragdolls()\posy,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\Body,ragdolls()\posx,ragdolls()\posy-1,ragdolls()\posz,#PB_Absolute)
            RotateEntity(ragdolls()\Body,0,0,0,#PB_Absolute)
            MoveEntity(ragdolls()\legr,ragdolls()\posx+0.3,ragdolls()\posy-2,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\legl,ragdolls()\posx-0.3,ragdolls()\posy-2,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\legru,ragdolls()\posx+0.3,ragdolls()\posy-2.9,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\leglu,ragdolls()\posx-0.3,ragdolls()\posy-2.9,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\armr,ragdolls()\posx+0.6,ragdolls()\posy-1,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\arml,ragdolls()\posx-0.6,ragdolls()\posy-1,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\armru,ragdolls()\posx+0.6,ragdolls()\posy-2,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\armlu,ragdolls()\posx-0.6,ragdolls()\posy-2,ragdolls()\posz,#PB_Absolute)
          Next
        EndIf
        
        If KeyboardReleased(#PB_Key_V) 
          Select hands
            Case 0
              hands=1
            Case 2
              hands=3
          EndSelect
        EndIf
        
        If KeyboardPushed(#PB_Key_T)
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\armlu ,0,-1,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\armru ,0,-1,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\leglu,0,-3,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\legru,0,-3,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\Head  ,0, 10,0,0,1,0)
          Next
        EndIf
        
        If KeyboardPushed(#PB_Key_J)
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\Body,0,10,0)
          Next
        EndIf
        
        If KeyboardPushed(#PB_Key_U) And dance=0
          dance=1
        EndIf
        
        If KeyboardPushed(#PB_Key_I)
          dance=0
        EndIf
        
        If KeyboardPushed(#PB_Key_F)
          ForEach ragdolls()
            If EntityY(ragdolls()\Body)<259
              ApplyEntityImpulse(ragdolls()\Body,0, 3,0,0,-10,0)
              ApplyEntityImpulse(ragdolls()\armlu  ,0, 3,0,0,-10,0)
              ApplyEntityImpulse(ragdolls()\armru  ,0, 3,0,0,-10,0)
              ApplyEntityImpulse(ragdolls()\leglu ,0, 3,0,0,-10,0)
              ApplyEntityImpulse(ragdolls()\legru ,0, 3,0,0,-10,0)
              ApplyEntityImpulse(ragdolls()\Head   ,0,6,0,0,0  ,0)
            EndIf
          Next
        EndIf
        
        If KeyboardPushed(#PB_Key_H)
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\armlu,0,1,0,0,-2,0)
            ApplyEntityImpulse(ragdolls()\armru,0,1,0,0,-2,0)
            ApplyEntityImpulse(ragdolls()\legl,0,-4,0,0,-20,0)
            ApplyEntityImpulse(ragdolls()\legr,0,-4,0,0,-20,0)
            ApplyEntityImpulse(ragdolls()\Head,0,5,0,0,0,0)
            ApplyEntityImpulse(ragdolls()\Body,0,4,0,0,10,0)
          Next
        EndIf
        
        If KeyboardPushed(#PB_Key_Add)
          wg+1
          WorldGravity(wg)
        EndIf
        
        If KeyboardPushed(#PB_Key_Subtract)
          wg-1
          WorldGravity(wg)
        EndIf
        
        If KeyboardPushed(#PB_Key_G)
          ;CameraLookAt(camera,EntityX(ragdolls()\Body),EntityY(ragdolls()\Body),EntityZ(ragdolls()\Body))
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\Head   ,0, 2,0)
            ApplyEntityImpulse(ragdolls()\legl , 0, Random(4),0,0,-6,0)
            ApplyEntityImpulse(ragdolls()\legr , 0, Random(4),0,0,-6,0)
            ApplyEntityImpulse(ragdolls()\armlu , 0, Random(1),0,0,6,0)
            ApplyEntityImpulse(ragdolls()\armru , 0, Random(1),0,0,6,0)
            
            ApplyEntityImpulse(ragdolls()\Body,0,-6,0,0,0,0)
          Next
        EndIf
        
;         If KeyboardPushed(#PB_Key_N)
;           ForEach plates()
;             ApplyEntityImpulse(plates(),0,8.5,0)
;           Next
;         EndIf
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
      EndIf
      
      RotateCamera(camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (camera, KeyX, 0, KeyY)
      
      RenderWorld()
      
      If IsSprite(out) ;freesprite if exists
        FreeSprite(out)
      EndIf
      
      out=CreateSprite(#PB_Any,ScreenWidth(),20)
      
      StartDrawing(SpriteOutput(out))
      If hands
        hao.s=" V:Hands Off,"
      Else
        hao.s=" V:Hands On,"
      EndIf
      
      If dance
        ho.s=" I:IdiotDance OFF,"
        ;DrawText(0,0,"R:reset, "+ho+" P:LookatAll,")
      Else
        ho.s=" U:IdiotDance ON, "
        
      EndIf
      DrawText(0,0,"R:reset, T:upright, H:hail ,G:strange ,T: upright, F:forceup (near plate),"+ho+" J:Up,"+hao+"P:LookatAll")
      StopDrawing()
      DisplayTransparentSprite(out,0,0)
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
End
Last edited by bobobo on Thu Dec 06, 2018 11:33 am, edited 2 times in total.
사십 둘 .
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: RagDolls Ogre and Physics

Post by applePi »

extraordinary demo and a resource for infos, thank you very much.
it works with dx and opengl
marc_256
Enthusiast
Enthusiast
Posts: 743
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: RagDolls Ogre and Physics

Post by marc_256 »

hi bobobo,

Thanks for the example, nice work, love it.
Good to learn some stuff.

Marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: RagDolls Ogre and Physics

Post by Fred »

Very nice !
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: RagDolls Ogre and Physics

Post by davido »

Brilliant!!!
DE AA EB
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: RagDolls Ogre and Physics

Post by Andre »

Very cool demo! :D

And to be able to save the source everywhere I just added following code in front of the IncludeFile / Add3DArchive() calls:

Code: Select all

#PB_Compiler_Home + "/Examples/3D/" + 
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
bmon
User
User
Posts: 54
Joined: Sat May 24, 2008 8:51 pm
Location: U.S.

Re: RagDolls Ogre and Physics

Post by bmon »

That is nice! I haven't worked with joints that much but it definitely something I need to investigate more. The only thing I would change is to move the line ... out=CreateSprite(#PB_Any,ScreenWidth(),20) ... from the main loop since that sprite really only needs to be created once. Otherwise, if you play with it for a while, you are creating a whole bunch of sprites that aren't needed. Other than that ... it's pretty cool!
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: RagDolls Ogre and Physics

Post by IdeasVacuum »

.........So that's how I look when I dance :shock:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: RagDolls Ogre and Physics

Post by Comtois »

Splendide :!: Nice work.

I saw them in a video, look at 1:30 :P
Please correct my english
http://purebasic.developpez.com/
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 202
Joined: Mon Jun 09, 2003 8:30 am

Re: RagDolls Ogre and Physics

Post by bobobo »

Thanks for advices .. new optimized version
(holding hands on-off is now optional)
사십 둘 .
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 202
Joined: Mon Jun 09, 2003 8:30 am

Re: RagDolls Ogre and Physics

Post by bobobo »

a more bloody Version

take it as a demo

Code: Select all

;
;
; ------------------------------------------------------------
;
;   PureBasic - RagDoll
;
;    (c) 2002 - Fantaisie Software , bobobo
; OpenGL scheitert bei mir, das mag am Treiber liegen (Laptop)
; das Programm sollte im Verzeichnis  \Examples\3D liegen
; weil es auf den Screenrequester und andere Daten dort zugreift
; alles ohne Gewähr
; ------------------------------------------------------------
; ApplyEntityImpulse without ÜPostion (causes the 3dEngine to stop v5.70)

#CameraSpeed = 0.5

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY ,rott, moveit
Define.i Knupf1,knupf2, Knupf3 , hands 

Structure components
  nodeid.i
  ball.i
  limb.i
  texture.i
  Bodymesh.i
  Body.i
  HeadMesh.i
  Head.i
  Nose.i
  eyer.i
  eyel.i
  mouth.i
  earl.i
  earr.i
  arml.i
  armlu.i
  armr.i
  armru.i
  legl.i
  leglu.i
  legr.i
  legru.i
  footl.i
  footr.i
  ct_bodyhead.i
  ct_arml.i
  hinge_arml.i
  ct_armr.i
  hinge_armr.i
  hinge_legl.i
  hinge_leglu.i
  hinge_legr.i
  hinge_legru.i
  posx.i
  posy.i
  posz.i
EndStructure

Define ragdoll.components
Global NewList ragdolls.components()


Structure blood
  id.i
  time.i
EndStructure

bloodygameheight=340
NewList blood.blood()

Procedure killjoints()
  
  ForEach ragdolls()
    FreeJoint(#PB_All)
  Next
  
EndProcedure

Procedure makejoints()
  ;joints
  ForEach ragdolls()
    RotateEntity(ragdolls()\Head,0,0,0,#PB_Absolute)
    RotateEntity(ragdolls()\body,0,0,0,#PB_Absolute)
    RotateEntity(ragdolls()\arml,0,0,0,#PB_Absolute)
    RotateEntity(ragdolls()\armlu,0,0,0,#PB_Absolute)
    RotateEntity(ragdolls()\armr,0,0,0,#PB_Absolute)
    RotateEntity(ragdolls()\armru,0,0,0,#PB_Absolute)
    RotateEntity(ragdolls()\legl,0,0,0,#PB_Absolute)
    RotateEntity(ragdolls()\leglu,0,0,0,#PB_Absolute)
    RotateEntity(ragdolls()\legr,0,0,0,#PB_Absolute)
    RotateEntity(ragdolls()\legru,0,0,0,#PB_Absolute)
    
    
    ragdolls()\ct_bodyhead=ConeTwistJoint(#PB_Any,EntityID(ragdolls()\Body)  , 0, 0.7,0,EntityID(ragdolls()\Head) ,0,-0.4,0)
    ;ragdolls()\ct_bodyhead=PointJoint(#PB_Any,EntityID(ragdolls()\Body)  , 0, 0.7,0,EntityID(ragdolls()\Head) ,0,-0.4,0)
    
    ragdolls()\ct_arml     = ConeTwistJoint(#PB_Any,EntityID(ragdolls()\Body),-0.6,  0.5 ,0,EntityID(ragdolls()\arml)       , 0, 0.4,0)
    ragdolls()\hinge_arml  = HingeJoint(#PB_Any,EntityID(ragdolls()\arml)    ,   0, -0.45,0,1,0,0,EntityID(ragdolls()\armlu), 0,0.45,0,1,0,0)
    
    ragdolls()\ct_armr     = ConeTwistJoint(#PB_Any,EntityID(ragdolls()\Body), 0.6, 0.5,0,EntityID(ragdolls()\armr),        0, 0.4,0)
    ragdolls()\hinge_armr  = HingeJoint(#PB_Any,EntityID(ragdolls()\armr)    , 0  ,-0.45,0,1,0,0,EntityID(ragdolls()\armru), 0,0.45,0,1,0,0)
    
    ragdolls()\hinge_legl  = HingeJoint(#PB_Any,EntityID(ragdolls()\Body)    ,-0.3,-0.60,0,1,0,0,EntityID(ragdolls()\legl) , 0,0.40,0,1,0,0)
    ragdolls()\hinge_legr  = HingeJoint(#PB_Any,EntityID(ragdolls()\Body)    , 0.3,-0.60,0,1,0,0,EntityID(ragdolls()\legr) , 0,0.40,0,1,0,0)
    ragdolls()\hinge_leglu = HingeJoint(#PB_Any,EntityID(ragdolls()\legl)    ,   0,-0.45,0,1,0,0,EntityID(ragdolls()\leglu), 0,0.45,0,1,0,0)
    ragdolls()\hinge_legru = HingeJoint(#PB_Any,EntityID(ragdolls()\legr)    ,   0,-0.45,0,1,0,0,EntityID(ragdolls()\legru), 0,0.45,0,1,0,0)
    
    ;joints attributes       
    SetJointAttribute(ragdolls()\ct_bodyhead,#PB_ConeTwistJoint_SwingSpan,0.5)
    SetJointAttribute(ragdolls()\ct_bodyhead,#PB_ConeTwistJoint_SwingSpan2,0.5)
    SetJointAttribute(ragdolls()\ct_bodyhead,#PB_ConeTwistJoint_TwistSpan,0.5)
    ;       ;          
    SetJointAttribute(ragdolls()\ct_arml,#PB_ConeTwistJoint_SwingSpan ,0.2);seitl
    SetJointAttribute(ragdolls()\ct_arml,#PB_ConeTwistJoint_SwingSpan2,1);dreh
    SetJointAttribute(ragdolls()\ct_arml,#PB_ConeTwistJoint_TwistSpan ,2);vorzurück
    ;       ;          
    SetJointAttribute(ragdolls()\hinge_arml,#PB_HingeJoint_LowerLimit,0)
    SetJointAttribute(ragdolls()\hinge_arml,#PB_HingeJoint_UpperLimit,120)
    SetJointAttribute(ragdolls()\hinge_armr,#PB_HingeJoint_LowerLimit,0)
    SetJointAttribute(ragdolls()\hinge_armr,#PB_HingeJoint_UpperLimit,120)
    
    SetJointAttribute(ragdolls()\ct_armr,#PB_ConeTwistJoint_SwingSpan,0.2);seitlich
    SetJointAttribute(ragdolls()\ct_armr,#PB_ConeTwistJoint_SwingSpan2,1);dreh
    SetJointAttribute(ragdolls()\ct_armr,#PB_ConeTwistJoint_TwistSpan,2);vorzurück
    
    SetJointAttribute(ragdolls()\hinge_legl,#PB_HingeJoint_LowerLimit,-1)
    SetJointAttribute(ragdolls()\hinge_legl,#PB_HingeJoint_UpperLimit,120)
    SetJointAttribute(ragdolls()\hinge_legr,#PB_HingeJoint_LowerLimit,-1)
    SetJointAttribute(ragdolls()\hinge_legr,#PB_HingeJoint_UpperLimit,120)
    
    SetJointAttribute(ragdolls()\hinge_leglu,#PB_HingeJoint_LowerLimit,-120)
    SetJointAttribute(ragdolls()\hinge_leglu,#PB_HingeJoint_UpperLimit,1)
    SetJointAttribute(ragdolls()\hinge_legru,#PB_HingeJoint_LowerLimit,-120)
    SetJointAttribute(ragdolls()\hinge_legru,#PB_HingeJoint_UpperLimit,1)
  Next
EndProcedure

If InitEngine3D()
;  Ergebnis = WorldDebug(#PB_World_DebugBody)
  
  Add3DArchive(#PB_Compiler_Home + "/Examples/3D/" +"Data/Textures", #PB_3DArchive_FileSystem)
  
  Add3DArchive(#PB_Compiler_Home + "/Examples/3D/" +"Data/Packs/desert.zip", #PB_3DArchive_Zip)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
     EnableWorldPhysics(#True)
     EnableWorldCollisions(#True)
    WorldShadows(#PB_Shadow_Additive,512)
    
    SkyBox("desert07.jpg")
    
    camera=CreateCamera(#PB_Any,0,0,100,100)
    ;CameraRenderMode(camera, #PB_Camera_Plot)

    MoveCamera(camera,0, 260 ,15, #PB_Absolute)
    
    texture1t=CreateTexture(#PB_Any,512,512)
    font=LoadFont(#PB_Any,"",14,#PB_Font_HighQuality)
    StartDrawing(TextureOutput(texture1t))
    DrawingFont(FontID(font))
    Box(0, 0, 512, 512, RGBA(222,222,255, 200))
    For l=0 To 128 
      Circle(Random(512),Random(512),Random(4)+1,RGBA(128,128,255,255))
    Next l
    DrawingMode(#PB_2DDrawing_Transparent)
    
    FrontColor(#Black)
    ;BackColor(#Black)
    th=TextHeight("X")
    ty=0
    DrawText(0, 0,"V:Hands OFF/ON (**)")
    ty+th
    DrawText(0,ty,"U:IdiotDance OFF/ON (**)")
    ty+th
    DrawText(0,ty,"R:reset (*)")
    ty+th
    DrawText(0,ty,"B:BloodyGame (**)")
    ty+th
    DrawText(0,ty,"BloodyGameHeight N:increase , M: decrease (*)")
    ty+th
    DrawText(0,ty,"T:upright (*)")
    ty+th
    DrawText(0,ty,"H:hail (*), G:strange (*)")
    ty+th
    DrawText(0,ty,"T: upright (*)")
    ty+th
    DrawText(0,ty,"F:forceup (near plate) (trampoline) (*)")
    ty+th
    DrawText(0,ty,"J:forceup (*)")
    ty+th
    DrawText(0,ty,"P:LookatAll (*)")
    ty+th
    DrawText(0,ty,"S:Shoot (**)")
    ty+th
    DrawText(0,ty,"L:Disassemble , O: Assemble (**)")
    ty+th
    DrawText(0,ty,"1,2,3,4,6,8 turns head (*)")
    ty+th
    DrawText(0,ty,"Add:Gravitiy decrease")
    ty+th
    DrawText(0,ty,"Sub:Gravitiy increase")
    ty+th
    DrawText(0,ty,"(*) while pressed,  (**) when release key")
    ;Debug ty
    StopDrawing()
    
    texture1=CreateMaterial(#PB_Any, TextureID(texture1t))
    
    texture2=CreateTexture(#PB_Any,256,256)
    StartDrawing(TextureOutput(texture2))
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    Box(0,0,256,256,RGBA(0,0,0,0))
    
    DrawingFont(FontID(font))
    
    ;FrontColor(RGBA(0,0,0,255))
    DrawText(0,0,"Press B and have fun",RGBA(255,255,0,255),RGBA(0,0,0,0))


    DrawText(0,50,"Hold N for a time and press B",RGBA(255,255,0,255),RGBA(0,0,0,0))
    DrawText(0,70,"and have even more fun",RGBA(255,255,0,255),RGBA(0,0,0,0))
    StopDrawing()
    infomat=CreateMaterial(#PB_Any,TextureID(texture2))
    MaterialBlendingMode(infomat,#PB_Material_AlphaBlend)
    
    texture2= CreateTexture(#PB_Any,256,256)
    StartDrawing(TextureOutput(texture2))
    Box(0,0,256,256,RGBA(80,80,80,180))
    
    For l=0 To 400
      Circle(Random(256),Random(256),Random(3)+1,RGBA(Random(5)+100,Random(5)+100,Random(5)+100,180))
    Next l
    
    DrawText(0,0,"ragdoll ®",RGBA(255,0,0,255),RGBA(80,80,80,222))
    StopDrawing()
    
    
    texture2=CreateMaterial(#PB_Any, TextureID(texture2))
    
    textureblue = CreateTexture(#PB_Any,256,256)
    StartDrawing(TextureOutput(textureblue))
    Box(0,0,256,256,#White)
    Ellipse(  0,128,25,50,RGB(30, 144, 255))
    Ellipse(256,128,25,50,RGB(30, 144, 255))
    Ellipse(  0,128,10,20,RGB(0 ,   0,   0))
    Ellipse(256,128,10,20,RGB(0 ,   0,   0))
    StopDrawing()
    MatEye = CreateMaterial(#PB_Any,TextureID(textureblue))
    
    texturedead = CreateTexture(#PB_Any,256,256)
    StartDrawing(TextureOutput(texturedead))
    Box(0,0,256,256,#White)
    For de=0 To 10
      Line(0,128+de,256,1,#Black)
      Line(0,128-de,256,1,#Black)
    Next
    
    For de=0 To 10
      Line(de,0,1,256,#Black)
      Line(256-de,0,1,256,#Black)
      
    Next
    
    
    
    StopDrawing()
    MatDeadEye = CreateMaterial(#PB_Any,TextureID(texturedead))
    
    textureflesh = CreateTexture(#PB_Any,256,256)
    StartDrawing(TextureOutput(textureflesh))
    Box(0,0,256,256,RGB(255, 218, 185))
    For f=0 To 512
      Circle(Random(255),Random(255),Random(2),RGB(243, 208, 161))
    Next
    
    StopDrawing()
    MatFlesh = CreateMaterial(#PB_Any,TextureID(textureflesh))
    
    texturemouth = CreateTexture(#PB_Any,256,256)
    StartDrawing(TextureOutput(texturemouth))
    Box(0,0,256,256,#White)
    For t=0 To 256 Step 16
      Line(t,0,1,256,#Black)
      Line(t-1,0,1,256,#Black)
      Line(t-2,0,1,256,#Black)
      Line(t+1,0,1,256,#Black)
      Line(t+2,0,1,256,#Black)
    Next t
    
    StopDrawing()
    MatMouth = CreateMaterial(#PB_Any,TextureID(texturemouth))
    
    texturekontakt= CreateTexture(#PB_Any,256,256)
    
    StartDrawing(TextureOutput(texturekontakt))
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    Box(0,0,256,256,RGBA(0,0,0,0))
    For c=1 To 32
      bx=64+Random(128)
      by=64+Random(128)
      br=Random(32)
      Circle(bx,by,br,RGBA(255,0,0,br*4))
    Next c
    
    StopDrawing()
    
    MatKontakt = CreateMaterial(#PB_Any,TextureID(texturekontakt))
    
    MaterialBlendingMode(MatKontakt,#PB_Material_AlphaBlend)
    RotateMaterial(MatKontakt,0.005,#PB_Material_Animated)
    AddMaterialLayer(MatKontakt,TextureID(texturekontakt),#PB_Material_Add)
    
    Bodymesh = CreateCube(#PB_Any,1)
    LimbMesh = CreateCylinder(#PB_Any, 0.1 ,0.80)
    
    HeadMesh  = CreateSphere(#PB_Any , 0.5); ,12,12)
    NoseMesh  = CreateSphere(#PB_Any , 0.25, 3, 4)
    EyeMeshl  = CreateSphere(#PB_Any , 0.10);,8,8)
    EyeMeshr  = CreateSphere(#PB_Any , 0.10);,8,8)
    MouthMesh = CreateSphere(#PB_Any , 0.25,15,15)
    EarMeshl =  CreateSphere(#PB_Any , 0.25);,8,8)
    EarMeshr =  CreateSphere(#PB_Any , 0.25);,8,8)
    FootMeshl = CreateSphere(#PB_Any , 0.25,6,6)
    FootMeshr = CreateSphere(#PB_Any , 0.25,6,6)
    
    kontaktMesh= CreatePlane(#PB_Any,0.25,0.25,1,1,1,1)
    TransformMesh(kontaktMesh,0,0.05,0,1,1,1,0,0,0)
    kontakt=CreateEntity(#PB_Any,MeshID(kontaktMesh),MaterialID(MatKontakt))
    EntityRenderMode(kontakt, #PB_Entity_CastShadow)
    
    
    TransformMesh(NoseMesh , 0.00, 0.00, 0.50, 1.00, 1.00, 1.00,  0.00,  0.00, 0.00)
    TransformMesh(EyeMeshl ,-0.35, 0.20, 0.25, 1.00, 1.00, 1.00,  0.00,  0.00, 0.00)
    TransformMesh(EyeMeshr , 0.35, 0.20, 0.25, 1.00, 1.00, 1.00,  0.00,  0.00, 0.00)
    TransformMesh(MouthMesh, 0.00,-0.15, 0.22, 1.50, 0.45, 1.00, 40.00,  0.00,  0.00)
    TransformMesh(EarMeshl ,-0.50,-0.10,-0.20, 1.00, 1.00, 0.25,  0.00,  0.00,  0.00)
    TransformMesh(EarMeshr , 0.50,-0.10,-0.20, 1.00, 1.00, 0.25,  0.00,  0.00,  0.00)
    TransformMesh(FootMeshl,-0.02,-0.38, 0.15, 0.50, 0.25, 1.00, 10.00, 20.00,  0.00)
    TransformMesh(FootMeshr, 0.02,-0.38, 0.15, 0.50, 0.25, 1.00, 10.00,-20.00,  0.00)
    
    For r=1 To 8 ;3 Dinger
      
      AddElement(ragdolls())
      
      ragdolls()\posx=r*2-2
      ragdolls()\posy=260
      ragdolls()\posz=0
      
      ragdolls()\texture=texture2
      
      ;bodyparts
      
      ragdolls()\Body=CreateEntity(#PB_Any,MeshID(Bodymesh),MaterialID(ragdolls()\texture),ragdolls()\posx,ragdolls()\posy,ragdolls()\posz)
      ScaleEntity(ragdolls()\Body,1.0,1,0.25)
      
      ragdolls()\Nose  = CreateEntity(#PB_Any,MeshID(NoseMesh) ,MaterialID(MatFlesh))
      ragdolls()\eyel  = CreateEntity(#PB_Any,MeshID(EyeMeshl) ,MaterialID(MatEye))
      ragdolls()\eyer  = CreateEntity(#PB_Any,MeshID(EyeMeshr) ,MaterialID(MatEye))
      ragdolls()\mouth = CreateEntity(#PB_Any,MeshID(MouthMesh),MaterialID(MatMouth))
      ragdolls()\earl  = CreateEntity(#PB_Any,MeshID(EarMeshl) ,MaterialID(MatFlesh))
      ragdolls()\earr  = CreateEntity(#PB_Any,MeshID(EarMeshr) ,MaterialID(MatFlesh))
      
      
      ragdolls()\Head = CreateEntity(#PB_Any,MeshID(HeadMesh),MaterialID(MatFlesh),  ragdolls()\posx,ragdolls()\posy+1,ragdolls()\posz)
      AttachEntityObject(ragdolls()\Head,"",EntityID(ragdolls()\Nose) ,0,0,0,0,0,0)
      AttachEntityObject(ragdolls()\Head,"",EntityID(ragdolls()\eyel) ,0,0,0,0,0,0)
      AttachEntityObject(ragdolls()\Head,"",EntityID(ragdolls()\eyer) ,0,0,0,0,0,0)
      AttachEntityObject(ragdolls()\Head,"",EntityID(ragdolls()\mouth),0,0,0,0,0,0)
      AttachEntityObject(ragdolls()\Head,"",EntityID(ragdolls()\earl) ,0,0,0,0,0,0)
      AttachEntityObject(ragdolls()\Head,"",EntityID(ragdolls()\earr) ,0,0,0,0,0,0)
      
      
      ragdolls()\arml  = CreateEntity(#PB_Any,MeshID(LimbMesh),MaterialID(ragdolls()\texture) ,ragdolls()\posx-0.6,ragdolls()\posy,ragdolls()\posz)
      ragdolls()\armlu = CreateEntity(#PB_Any,MeshID(LimbMesh),MaterialID(ragdolls()\texture) ,ragdolls()\posx-0.6,ragdolls()\posy-1,ragdolls()\posz)
      ragdolls()\armr  = CreateEntity(#PB_Any,MeshID(LimbMesh),MaterialID(ragdolls()\texture) ,ragdolls()\posx+0.6,ragdolls()\posy,ragdolls()\posz)
      ragdolls()\armru = CreateEntity(#PB_Any,MeshID(LimbMesh),MaterialID(ragdolls()\texture) ,ragdolls()\posx+ 0.6,ragdolls()\posy-1,ragdolls()\posz)
      
      ragdolls()\legl = CreateEntity(#PB_Any,MeshID(LimbMesh),MaterialID(ragdolls()\texture)  ,ragdolls()\posx-0.3,ragdolls()\posy-1,ragdolls()\posz)
      ragdolls()\legr = CreateEntity(#PB_Any,MeshID(LimbMesh),MaterialID(ragdolls()\texture)  ,ragdolls()\posx+ 0.3,ragdolls()\posy-1,ragdolls()\posz)
      
      ragdolls()\leglu = CreateEntity(#PB_Any,MeshID(LimbMesh),MaterialID(ragdolls()\texture) ,ragdolls()\posx-0.3,ragdolls()\posy-2,ragdolls()\posz)
      ragdolls()\legru = CreateEntity(#PB_Any,MeshID(LimbMesh),MaterialID(ragdolls()\texture) ,ragdolls()\posx+ 0.3,ragdolls()\posy-2,ragdolls()\posz)
      ragdolls()\footl = CreateEntity(#PB_Any,MeshID(FootMeshl),MaterialID(ragdolls()\texture))
      
      AttachEntityObject(ragdolls()\leglu,"",EntityID(ragdolls()\footl) ,0,0,0,0,0,0)
      ragdolls()\footr = CreateEntity(#PB_Any,MeshID(FootMeshr),MaterialID(ragdolls()\texture))
      
      AttachEntityObject(ragdolls()\legru,"",EntityID(ragdolls()\footr) ,0,0,0,0,0,0)
      
      ;rendermode=#PB_Entity_CastShadow
      ;rendermode=#PB_Entity_CastShadow|#PB_Entity_DisplaySkeleton
      
      ;bodyPhysics
      CreateEntityBody(ragdolls()\Body,#PB_Entity_BoxBody  ,10, 0,1)
      EntityRenderMode(ragdolls()\Body, #PB_Entity_CastShadow)
      
      EntityRenderMode(ragdolls()\Head, #PB_Entity_CastShadow)
      CreateEntityBody(ragdolls()\Head, #PB_Entity_ConvexHullBody, 5, 0.25, 1)
      
      EntityRenderMode(ragdolls()\arml, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\arml, #PB_Entity_CylinderBody,  1, 0 ,1)
      EntityRenderMode(ragdolls()\armlu, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\armlu, #PB_Entity_CylinderBody, 1, 0, 1)
      
      EntityRenderMode(ragdolls()\armr, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\armr, #PB_Entity_CylinderBody,  1, 0, 1)
      EntityRenderMode(ragdolls()\armru, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\armru, #PB_Entity_CylinderBody, 1, 0,1)
      
      EntityRenderMode(ragdolls()\legl, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\legl, #PB_Entity_CylinderBody, 1, 0,1)
      EntityRenderMode(ragdolls()\leglu, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\leglu, #PB_Entity_CylinderBody,1, 0,1)
      
      EntityRenderMode(ragdolls()\legr, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\legr, #PB_Entity_CylinderBody, 1, 0,1)
      EntityRenderMode(ragdolls()\legru, #PB_Entity_CastShadow) 
      CreateEntityBody(ragdolls()\legru, #PB_Entity_CylinderBody,1, 0,1)
    
    Next 
    
    makejoints()  
    ;
    
    
    plate=CreateCube(#PB_Any,1)
    ScaleMaterial(texture1,1,1)
    RotateMaterial(texture1,90,#PB_Material_Fixed)
    
    platte=CreateEntity(#PB_Any,MeshID(plate),MaterialID(texture1),0,0,0)
    
    ScaleEntity(platte,70,0.01,70)
    MoveEntity(platte,0,256,0)
    EntityRenderMode(platte, #PB_Entity_CastShadow)
    CreateEntityBody(platte,#PB_Entity_StaticBody,1000,1,0.5)
    
    info=CreateEntity(#PB_Any,MeshID(plate),MaterialID(infomat),0,0,0)
    ScaleEntity(info,10,0.01,10)
    MoveEntity(info,5,260,0)
    RotateEntity(info,90,0,0)
    EntityRenderMode(info,  #PB_Entity_DisplaySkeleton )
    
    suncolor=#White
    
    sun=CreateLight(#PB_Any,suncolor,17830,12860,10930,#PB_Light_Point)
    
    CameraLookAt(camera,EntityX(ragdolls()\Head),EntityY(ragdolls()\Head),EntityZ(ragdolls()\Head))
    dance=1
    hands=1
    
    
    Repeat
      ;Debug hands
      
      ;Debug "************"
      
      Screen3DEvents()
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If hands=1
        hands=2
        ;Debug hands  
        ;Händchenhalten    
        sp=-0.5
        tau.f=0
        damp.f=0.5
        SelectElement(ragdolls(),0)
        jx=EntityX(ragdolls()\armru)
        
        jy=EntityY(ragdolls()\armru)
        jz=EntityZ(ragdolls()\armru)
        id=EntityID(ragdolls()\armru)
        SelectElement(ragdolls(),1)
        jx1=EntityX(ragdolls()\armlu)
        jy1=EntityY(ragdolls()\armlu)
        jz1=EntityZ(ragdolls()\armlu)
        id1=EntityID(ragdolls()\armlu)
        knupf1=PointJoint(#PB_Any,id,0,sp,0,id1,0,sp,0)
        SetJointAttribute(knupf1,#PB_PointJoint_Damping,damp)
        SetJointAttribute(knupf1,#PB_PointJoint_Tau,tau)
        
        jx=EntityX(ragdolls()\armru)
        jy=EntityY(ragdolls()\armru)
        jz=EntityZ(ragdolls()\armru)
        id=EntityID(ragdolls()\armru)
        SelectElement(ragdolls(),2)
        jx1=EntityX(ragdolls()\armlu)
        jy1=EntityY(ragdolls()\armlu)
        jz1=EntityZ(ragdolls()\armlu)
        id1=EntityID(ragdolls()\armlu)
        knupf2=PointJoint(#PB_Any,id,0,sp,0,id1,0,sp,0)
        SetJointAttribute(knupf2,#PB_PointJoint_Damping,damp)
        SetJointAttribute(knupf2,#PB_PointJoint_Tau,tau)
        
        
      EndIf
      
      If hands=3
        ;Debug hands  
        hands=0
        If knupf1
          FreeJoint(knupf1)
          knupf1=0
        EndIf
        If knupf2
          FreeJoint(knupf2)
          knupf2=0
        EndIf
        If knupf3
          FreeJoint(knupf3)
          knupf3=0
        EndIf
        
      EndIf
      
      If dance=1
        ;dance=2
        ForEach ragdolls()
          If EntityY(ragdolls()\Head)<259.5
            ApplyEntityImpulse(ragdolls()\Head  ,0, 9,0)
            ApplyEntityImpulse(ragdolls()\legru  ,0, -5,0)
            ApplyEntityImpulse(ragdolls()\leglu  ,0, -5,0)
            If EntityY(ragdolls()\leglu)<256.8
              ApplyEntityImpulse(ragdolls()\leglu,Random(2)-1,Random(8),Random(2)-1)
            EndIf
            If EntityY(ragdolls()\legru)<256.8
              ApplyEntityImpulse(ragdolls()\legru,Random(2)-1,Random(8),Random(2)-1)
            EndIf
            ApplyEntityImpulse(ragdolls()\armlu,Random(2)-1,Random(2)-1,Random(2)-1)
            ApplyEntityImpulse(ragdolls()\armru,Random(2)-1,Random(2)-1,Random(2)-1)
          EndIf
        Next          
      EndIf
      
      KeyboardMode(#PB_Keyboard_International)
      
      If ExamineKeyboard()
        If KeyboardReleased(#PB_Key_R)  Or KeyboardReleased(#PB_Key_E)
          px.f=0
          py.f=0
          pz.f=0
          ForEach ragdolls()
            px+EntityX(ragdolls()\Body)
            py+EntityY(ragdolls()\Body)
            pz+EntityZ(ragdolls()\Body)
          Next
          px=px/ListSize(ragdolls())
          py=py/ListSize(ragdolls())
          pz=pz/ListSize(ragdolls())
          MoveCamera(camera,0, 260 ,15, #PB_Absolute)
          CameraLookAt(camera,px,py,pz)
        EndIf
        If KeyboardPushed(#PB_Key_P) 
          px.f=0
          py.f=0
          pz.f=0
          ForEach ragdolls()
            px+EntityX(ragdolls()\Body)
            py+EntityY(ragdolls()\Body)
            pz+EntityZ(ragdolls()\Body)
          Next
          px=px/ListSize(ragdolls())
          py=py/ListSize(ragdolls())
          pz=pz/ListSize(ragdolls())
          CameraLookAt(camera,px,py,pz)
        EndIf
        
        If KeyboardPushed(#PB_Key_N)
          bloodygameheight+1
        EndIf
        If KeyboardPushed(#PB_Key_M)
          bloodygameheight-1
          If bloodygameheight<290
            bloodygameheight=290
          EndIf
          
        EndIf
        If KeyboardReleased(#PB_Key_O)
          killjoints()
          hands=0
          makejoints()
          ForEach ragdolls()
            SetEntityMaterial(ragdolls()\eyel, MaterialID(MatEye))
            SetEntityMaterial(ragdolls()\eyer, MaterialID(MatEye))
          Next
          
        EndIf
        If KeyboardReleased(#PB_Key_L)
          killjoints()
          hands=0
          ForEach ragdolls()
            SetEntityMaterial(ragdolls()\eyel, MaterialID(MatDeadEye))
            SetEntityMaterial(ragdolls()\eyer, MaterialID(MatDeadEye))
          Next
          

        EndIf
        
        If KeyboardReleased(#PB_Key_B)
          px.f=0
          py.f=0
          pz.f=0
          ForEach ragdolls()
            px+EntityX(ragdolls()\Body)
            py+EntityY(ragdolls()\Body)
            pz+EntityZ(ragdolls()\Body)
          Next
          px=px/ListSize(ragdolls())
          py=py/ListSize(ragdolls())
          pz=pz/ListSize(ragdolls())
          ForEach(ragdolls())
            ApplyEntityImpulse(ragdolls()\Body,Random(20)-10,0,Random(20)-10)
            ApplyEntityImpulse(ragdolls()\Head,Random(20)-10,0,Random(20)-10)
          Next
          MoveCamera(camera,px,py+10,pz,#PB_Absolute)
          CameraLookAt(camera,0,260,0)
        EndIf
        If KeyboardPushed(#PB_Key_S) And shoot=0
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\Body,0,0,-100)
            shoot=1
          Next
        EndIf
        
        If KeyboardReleased(#PB_Key_S)
          shoot=0
        EndIf
        
        If KeyboardPushed(#PB_Key_R)
          ForEach ragdolls()
            MoveEntity(ragdolls()\Head,ragdolls()\posx,ragdolls()\posy,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\Body,ragdolls()\posx,ragdolls()\posy-1,ragdolls()\posz,#PB_Absolute)
            RotateEntity(ragdolls()\Body,0,0,0,#PB_Absolute)
            MoveEntity(ragdolls()\legr,ragdolls()\posx+0.3,ragdolls()\posy-2,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\legl,ragdolls()\posx-0.3,ragdolls()\posy-2,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\legru,ragdolls()\posx+0.3,ragdolls()\posy-2.9,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\leglu,ragdolls()\posx-0.3,ragdolls()\posy-2.9,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\armr,ragdolls()\posx+0.6,ragdolls()\posy-1,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\arml,ragdolls()\posx-0.6,ragdolls()\posy-1,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\armru,ragdolls()\posx+0.6,ragdolls()\posy-2,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\armlu,ragdolls()\posx-0.6,ragdolls()\posy-2,ragdolls()\posz,#PB_Absolute)
          Next
        EndIf
        
        If KeyboardPushed(#PB_Key_B)
          ForEach ragdolls()
            MoveEntity(ragdolls()\Head,ragdolls()\posx,bloodygameheight,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\Body,ragdolls()\posx,bloodygameheight-1,ragdolls()\posz,#PB_Absolute)
            RotateEntity(ragdolls()\Body,0,0,0,#PB_Absolute)
            MoveEntity(ragdolls()\legr,ragdolls()\posx+0.3,bloodygameheight-2,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\legl,ragdolls()\posx-0.3,bloodygameheight-2,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\legru,ragdolls()\posx+0.3,bloodygameheight-2.9,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\leglu,ragdolls()\posx-0.3,bloodygameheight-2.9,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\armr,ragdolls()\posx+0.6,bloodygameheight-1,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\arml,ragdolls()\posx-0.6,bloodygameheight-1,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\armru,ragdolls()\posx+0.6,bloodygameheight-2,ragdolls()\posz,#PB_Absolute)
            MoveEntity(ragdolls()\armlu,ragdolls()\posx-0.6,bloodygameheight-2,ragdolls()\posz,#PB_Absolute)
          Next
        EndIf
        
        If KeyboardReleased(#PB_Key_V) 
          Select hands
            Case 0
              hands=1
            Case 2
              hands=3
          EndSelect
        EndIf
        
        
        If KeyboardPushed(#PB_Key_T)
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\armlu ,0,-1,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\armru ,0,-1,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\leglu,0,-3,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\legru,0,-3,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\Head  ,0, 10,0,0,1,0)
            ;ApplyEntityForce(ragdolls()\HEAD,(Random(10)+5)*-1,0,(Random(10)+5)*-1) ;nearly ParachuteEffect
          Next
        EndIf
        
        If KeyboardPushed(#PB_Key_J)
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\Head,0,10,0)
          Next
        EndIf
        If KeyboardPushed(#PB_Key_K)
          SelectElement(ragdolls(),1)
            ApplyEntityImpulse(ragdolls()\Head,0,Random(10)+9,0)
          
        EndIf
        If KeyboardPushed(#PB_Key_I)
          I_f+1
          SelectElement(ragdolls(),0)
            ApplyEntityImpulse(ragdolls()\armlu,0,Random(10)+10+I_f,0)
          
        EndIf
        If KeyboardReleased(#PB_Key_I)
          i_f=0
        EndIf
        
        If KeyboardReleased(#PB_Key_U)
          Select dance
            Case 1
              dance=0
            Case 0
              dance=1
          EndSelect
          
        EndIf
        
        
        If KeyboardPushed(#PB_Key_F)
          ForEach ragdolls()
            If EntityY(ragdolls()\Body)<259
              ApplyEntityImpulse(ragdolls()\Body,0, 3,0)
              ApplyEntityImpulse(ragdolls()\armlu  ,0, 3,0)
              ApplyEntityImpulse(ragdolls()\armru  ,0, 3,0)
              ApplyEntityImpulse(ragdolls()\leglu ,0, 3,0)
              ApplyEntityImpulse(ragdolls()\legru ,0, 3,0)
              ApplyEntityImpulse(ragdolls()\Head   ,0,6,0)
            EndIf
          Next
        EndIf
        
        If KeyboardPushed(#PB_Key_H)
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\armlu,0,1,0)
            ApplyEntityImpulse(ragdolls()\armru,0,1,0)
            ApplyEntityImpulse(ragdolls()\legl,0,-4,0)
            ApplyEntityImpulse(ragdolls()\legr,0,-4,0)
            ApplyEntityImpulse(ragdolls()\Head,0,5,0)
            ApplyEntityImpulse(ragdolls()\Body,0,4,0)
          Next
        EndIf
        
        If KeyboardPushed(#PB_Key_Add)
          wg+1
          WorldGravity(wg)
        EndIf
        
        If KeyboardPushed(#PB_Key_Subtract)
          wg-1
          WorldGravity(wg)
        EndIf
        
        If KeyboardPushed(#PB_Key_G)
          
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\Head   ,0, 2,0)
            ApplyEntityImpulse(ragdolls()\legl , 0, Random(4),0,0,-6,0)
            ApplyEntityImpulse(ragdolls()\legr , 0, Random(4),0,0,-6,0)
            ApplyEntityImpulse(ragdolls()\armlu , 0, Random(1),0,0,6,0)
            ApplyEntityImpulse(ragdolls()\armru , 0, Random(1),0,0,6,0)
            
            ApplyEntityImpulse(ragdolls()\Body,0,-6,0)
          Next
        EndIf
        
        If KeyboardPushed(#PB_Key_Pad6)
          ForEach ragdolls()
            RotateEntity(ragdolls()\Head,0,10,0,#PB_Relative)
          Next
        EndIf
        If KeyboardPushed(#PB_Key_Pad4)
          ForEach ragdolls()
            RotateEntity(ragdolls()\Head,0,-10,0,#PB_Relative)
          Next
        EndIf
        If KeyboardPushed(#PB_Key_Pad1)
          ForEach ragdolls()
            RotateEntity(ragdolls()\Head,0,0,-5,#PB_Relative)
          Next
        EndIf
        If KeyboardPushed(#PB_Key_Pad3)
          ForEach ragdolls()
            RotateEntity(ragdolls()\Head,0,0,5,#PB_Relative)
          Next
        EndIf
        If KeyboardPushed(#PB_Key_Pad8)
          ForEach ragdolls()
            RotateEntity(ragdolls()\Head,-10,0,0,#PB_Relative)
          Next
        EndIf
        If KeyboardPushed(#PB_Key_Pad2)
          ForEach ragdolls()
            RotateEntity(ragdolls()\Head,10,0,0,#PB_Relative)
          Next
        EndIf
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
      EndIf
      
      RotateCamera(camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (camera, KeyX, 0, KeyY)
      
      ;Make it Bloody
      ExamineWorldCollisions(#True)
      While NextWorldCollision()
        
        If platte=FirstWorldCollisionEntity() Or platte=SecondWorldCollisionEntity()
          WorldCollisionAppliedImpulse()
          
          impy=GetY()
          If impy>666
            killjoints()
            hands=0
          EndIf
          If impy>100
            dead=1
            ForEach ragdolls()
            SetEntityMaterial(ragdolls()\eyel, MaterialID(MatDeadEye))
            SetEntityMaterial(ragdolls()\eyer, MaterialID(MatDeadEye))
          Next
        EndIf
        
          If impy>20 ; blood with heavier impulses
            If impy>100
              impy=100
            EndIf
            
            WorldCollisionContact()
            AddElement(blood())
            blood()\id=CopyEntity(kontakt,#PB_Any)
            ScaleEntity(blood()\id,impy/5,1,impy/5,#PB_Relative)
            
            blood()\time=ElapsedMilliseconds()+(impy*impy/2)
            MoveEntity(blood()\id,GetX(),GetY(),GetZ(),#PB_Absolute)
            
          EndIf
          
          
        EndIf
        
      Wend
      ForEach blood()
        If ElapsedMilliseconds()>blood()\time+5000
          FreeEntity(blood()\id)
          DeleteElement(blood())
          If dead
            ForEach ragdolls()
              SetEntityMaterial(ragdolls()\eyel, MaterialID(MatEye))
              SetEntityMaterial(ragdolls()\eyer, MaterialID(MatEye))
              
            Next
            dead=0
          EndIf
          
        EndIf
      Next
      
      lre=RenderWorld()

      Debug lre
      Debug Engine3DStatus(#PB_Engine3D_CurrentFPS)
      Debug "*******"
      FlipBuffers()


    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
End
Last edited by bobobo on Thu Dec 06, 2018 11:39 am, edited 2 times in total.
사십 둘 .
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: RagDolls Ogre and Physics

Post by applePi »

i like the situation when they have equilibrium in the air like this
Image
they could have also parachutes to land safely from high altitudes, but then there is the problem of letting the parachute float like in the air, may be by manipulating gravity with some random fluctuations we can simulate a floating parachute.
thanks for RagDolls examples, i remember someone have asked too much about it in the forum.
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 202
Joined: Mon Jun 09, 2003 8:30 am

Re: RagDolls Ogre and Physics

Post by bobobo »

You can hold "T" for the parachuteEffect (without the security that there will be no blood :twisted:)
.. (without the real parachute Entity)

winddeviations can be made with ApplyForce i think

here a "T"-Loop with some wind

Code: Select all

 
..
...
       If KeyboardPushed(#PB_Key_T)
          ForEach ragdolls()
            ApplyEntityImpulse(ragdolls()\armlu ,0,-1,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\armru ,0,-1,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\leglu,0,-3,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\legru,0,-3,0,0,-1,0)
            ApplyEntityImpulse(ragdolls()\Head  ,0, 10,0,0,1,0)
            ApplyEntityForce(ragdolls()\HEAD,(Random(100)+50),0,(Random(100)+50)) ; 
          Next
        EndIf
...
..
사십 둘 .
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 202
Joined: Mon Jun 09, 2003 8:30 am

Re: RagDolls Ogre and Physics

Post by bobobo »

those joints have only a few parameters which are not selfexplaining (or i'm to lowbrained to understand the help :oops: ).
the joints were made with Trial and Error.

a "boned" character is on my future-list. i'll see how far it is "handy" with the PhysicEngine and the manual bone movements.
사십 둘 .
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 202
Joined: Mon Jun 09, 2003 8:30 am

Re: RagDolls Ogre and Physics

Post by bobobo »

updated code in the postings above due a behaviour concerning ApplyEntityImpulse when using Postitions in PB 5.70 Beta
사십 둘 .
Post Reply