Double pendulum

Everything related to 3D programming
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Double pendulum

Post by Comtois »

Adapted from @Fig's demo

Using PointJoint

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts" , #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ; First create materials
    ;
    CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
    CreateMaterial(1, LoadTexture(1, "Dirt.jpg"))
    GetScriptMaterial(2, "Examples/LightRibbonTrail")
    
    ; Meshes
    ;
    CreateCube(0, 1)
    CreateSphere(1, 0.5)
    
    ; Entities
    ;
    CreateEntity(0, MeshID(0), MaterialID(0), 0,  0, 0)
    CreateEntity(1, MeshID(0), MaterialID(0), 0, -5.1, 0)
    CreateEntity(2, MeshID(1), MaterialID(1), 0, -5.1, 0)
    CreateEntity(3, MeshID(1), MaterialID(1), 0, 0, 0)
    
    ScaleEntity(0,1,10,1)
    ScaleEntity(1,1,10,1)   
    
    ;Ribbon
    
    CreateRibbonEffect(0, MaterialID(2), 1, 2800, 180)
    RibbonEffectColor(0, 0, RGBA(0, 255*0.8, 255*0.8, 255), RGBA(0, 255, 0, 5))
    RibbonEffectWidth(0, 0, 0.5, 0.2) 
    AttachRibbonEffect(0, EntityParentNode(3))
    
    ; Bodies
    ;
    CreateEntityBody(0, #PB_Entity_BoxBody   , 1.0)
    CreateEntityBody(1, #PB_Entity_BoxBody   , 1.0)
    CreateEntityBody(2, #PB_Entity_SphereBody, 1.0)
    CreateEntityBody(3, #PB_Entity_SphereBody, 0.00001)   
    
    ; PointJoint
    ;
    PointJoint(0, EntityID(0), 0, 5.5, 0)
    PointJoint(1, EntityID(0),  0, -5.5, 0, EntityID(2), 0, 0, 0)
    PointJoint(2, EntityID(1),  0, 5.5, 0, EntityID(2), 0, 0, 0)
    PointJoint(3, EntityID(1),  0, -5.5, 0, EntityID(3), 0, 0, 0)   
        
    For i=0 To 3
      EntityLinearFactor(i, 1, 1, 0)
    Next
        
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 50, #PB_Absolute)
    
    ;- Light
    ;
    AmbientColor(RGB(25, 25, 25))
    CreateLight(0, RGB(215, 190, 40), 75, 75, 75)
    
    ; GO
    ;
    ApplyEntityImpulse(1,  10, 0, 0)
    
    Repeat
      Screen3DEvents()
      ExamineKeyboard()
      
      RenderWorld()
      
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
Using HingeJoint

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

If InitEngine3D()
 
  InitSprite()
  InitKeyboard()
  InitMouse()
 
  If Screen3DRequester()
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts" , #PB_3DArchive_FileSystem)
    Parse3DScripts()
   
    ; First create materials
    ;
    CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
    CreateMaterial(1, LoadTexture(1, "Dirt.jpg"))
    GetScriptMaterial(2, "Examples/LightRibbonTrail")
   
    ; Meshes
    ;
    CreateCube(0, 1)
    CreateSphere(1, 0.5)
   
    ; Entities
    ;
    CreateEntity(0, MeshID(0), MaterialID(0), 0,  0, 0)
    CreateEntity(1, MeshID(0), MaterialID(0), 0, -5.1, 0)
    CreateEntity(2, MeshID(1), MaterialID(1), 0, -5.1, 0)
    CreateEntity(3, MeshID(1), MaterialID(1), 0, 0, 0)
   
    ScaleEntity(0,1,10,1)
    ScaleEntity(1,1,10,1)   
   
    ;Ribbon
   
    CreateRibbonEffect(0, MaterialID(2), 1, 2800, 180)
    RibbonEffectColor(0, 0, RGBA(0, 255*0.8, 255*0.8, 255), RGBA(0, 255, 0, 5))
    RibbonEffectWidth(0, 0, 0.5, 0.2)
    AttachRibbonEffect(0, EntityParentNode(3))
   
    ; Bodies
    ;
    CreateEntityBody(0, #PB_Entity_BoxBody   , 1.0)
    CreateEntityBody(1, #PB_Entity_BoxBody   , 1.0)
    CreateEntityBody(2, #PB_Entity_SphereBody, 1.0)
    CreateEntityBody(3, #PB_Entity_SphereBody, 0.00001)   
   
    ; HingeJoint
    ;
    HingeJoint(0, EntityID(0), 0,  5.5, 0, 0, 0, 1,         -2 , 0, 0, 0, 0, 0, 0)   
    HingeJoint(1, EntityID(0), 0, -5.5, 0, 0, 0, 1, EntityID(2), 0, 0, 0, 0, 0, 1)
    HingeJoint(2, EntityID(1), 0,  5.5, 0, 0, 0, 1, EntityID(2), 0, 0, 0, 0, 0, 1)
    HingeJoint(3, EntityID(1), 0, -5.5, 0, 0, 0, 1, EntityID(3), 0, 0, 0, 0, 0, 1)   
      
    ;SetJointAttribute(0,  #PB_HingeJoint_LowerLimit, -45)
    ;SetJointAttribute(0,  #PB_HingeJoint_UpperLimit,  45)
    
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 50, #PB_Absolute)
   
    ;- Light
    ;
    AmbientColor(RGB(25, 25, 25))
    CreateLight(0, RGB(215, 190, 40), 75, 75, 75)
   
    ; GO
    ;
    ApplyEntityImpulse(1,  10, 0, 0)
   
    Repeat
      Screen3DEvents()
      ExamineKeyboard()
     
      RenderWorld()
     
      Screen3DStats()
      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 Comtois on Wed Aug 15, 2018 9:03 pm, edited 3 times in total.
Please correct my english
http://purebasic.developpez.com/
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Double pendulum

Post by DK_PETER »

So simple and nice.
Thx. :-)
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Double pendulum

Post by Fig »

So simple and elegant ! :shock:
What about a third pendulum ?
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
IceSoft
Addict
Addict
Posts: 1616
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Double pendulum

Post by IceSoft »

Fig wrote:What about a third pendulum ?
Thats not so much interessting as using only double.
Belive!
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Double pendulum

Post by RSBasic »

Nice
Image
Image
dige
Addict
Addict
Posts: 1247
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Double pendulum

Post by dige »

Amazing! :o Especially with so few lines of code.
"Daddy, I'll run faster, then it is not so far..."
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Double pendulum

Post by Denis »

Slt Comtois,

excellent !
A+
Denis
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Double pendulum

Post by applePi »

Thanks Comtois and Fig for these great demos, i thought before that CreateLine3D is ugly and static while this demo show that we can use it to simulate a thread.
let me hijack the Comtois demo, to look at the pendulum (the Gymnast) in a more 3D sensation by rotating the camera around the pendulum and to put references such as a ground , a wall, and a tree . so we have more experience of the pendulum in the space.
in short we can use PB 2D/3D Graphics in Art and science show

Code: Select all

    #MaxHistoricNumber=200
    Structure Vector3
      x.f
      y.f
      z.f
    EndStructure

    NewList trace.Vector3()

    Declare Draw(List ptr.Vector3())

    IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

    If InitEngine3D()
     
      InitSprite()
      InitKeyboard()
      InitMouse()
     
      If Screen3DRequester()
        Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
        Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
       
        ; First create materials
        ;
        CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
        CreateMaterial(1, LoadTexture(1, "Dirt.jpg"))
        CreateMaterial(2, LoadTexture(2, "ground_diffuse.png"))
        CreateMaterial(3, LoadTexture(3, "MRAMOR6X6.jpg"))
        CreateMaterial(4, LoadTexture(4, "RustyBarrel.png"))
       
        ; Meshes
        ;
        CreateCube(0, 1)
        CreateSphere(1, 0.5)
       
        ; Entities
        ;
        CreateEntity(0, MeshID(0), MaterialID(0), 0,  0, 0)
        CreateEntity(1, MeshID(0), MaterialID(0), 0, -5.1, 0)
        CreateEntity(2, MeshID(1), MaterialID(1), 0, -5.1, 0)
        CreateEntity(3, MeshID(1), MaterialID(1), 0, 0, 0)
       
        ScaleEntity(0,1,10,1)
        ScaleEntity(1,1,10,1)   
       
        ; Bodies
        ;
        CreateEntityBody(0, #PB_Entity_BoxBody   , 1.0)
        CreateEntityBody(1, #PB_Entity_BoxBody   , 1.0)
        CreateEntityBody(2, #PB_Entity_SphereBody, 1.0)
        CreateEntityBody(3, #PB_Entity_SphereBody, 0.00001)   
       
        ; PointJoint
        ;
        PointJoint(0, EntityID(0), 0, 5.5, 0)
        PointJoint(1, EntityID(0),  0, -5.5, 0, EntityID(2), 0, 0, 0)
        PointJoint(2, EntityID(1),  0, 5.5, 0, EntityID(2), 0, 0, 0)
        PointJoint(3, EntityID(1),  0, -5.5, 0, EntityID(3), 0, 0, 0)   
       
       
        For i=0 To 3
          EntityLinearFactor(i, 1, 1, 0)
        Next
       
       
        ; Camera
        ;
        CreateCamera(0, 0, 0, 100, 100)
        MoveCamera(0, 0, 0, 50, #PB_Absolute)
       
        ;- Light
        ;
        AmbientColor(RGB(25, 25, 25))
        CreateLight(0, RGB(215, 250, 260), 75, 75, 75)
        CreateLight(1, RGB(215, 250, 260), 0, 75, -20)
        LightLookAt(1,0,0,0)
       
        ; GO
        ;
        ApplyEntityImpulse(1,  10, 0, 0)
        
        Plane = CreateEntity(#PB_Any, MeshID(CreatePlane(#PB_Any, 1000, 1000, 100, 100, 10, 10)), MaterialID(2))
        MoveEntity(Plane, 0,-100,0)
        CopyEntity(Plane, 30)
        MoveEntity(30, 0,0,-200)
        RotateEntity(30, 90,0,0)
        SetEntityMaterial(30, MaterialID(3))
        
       
        CreateCapsule(5, 4,70)
        CreateEntity(5, MeshID(5), MaterialID(4), 30,-70,120)
        CreateSphere(6, 20)
        CreateEntity(6, MeshID(6), MaterialID(1), 30,-30,120)
        ScaleEntity(6,1,0.1,1)
        
        
        CameraBackColor(0, RGB(19, 34, 49))
        ;glLineWidth_(3)
        
        Repeat
          Screen3DEvents()
          ExamineKeyboard()
          angle.f + #PI*1/1000
          x.f=Cos(angle)*60
          z.f=Sin(angle)*60
          MoveCamera(0, x, 30, z, #PB_Absolute)
          CameraLookAt(0, 0,0,0)
         
          Draw(trace())
         
          RenderWorld()
         
          Screen3DStats()
          FlipBuffers()
        Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
      EndIf
     
    Else
      MessageRequester("Error", "The 3D Engine can't be initialized", 0)
    EndIf

    End

    Procedure Draw(List ptr.Vector3())
      If FirstElement(ptr())
        xold.f=ptr()\x
        yold.f=ptr()\y
        zold.f=ptr()\z
       
        If ListSize(ptr())>#MaxHistoricNumber
          DeleteElement(ptr())
        EndIf
        LastElement(ptr())
      EndIf
     
      AddElement(ptr())
      ptr()\x=EntityX(3)
      ptr()\y=EntityY(3)
      ptr()\z=EntityZ(3) 
     
      n=10
     
      ForEach ptr()
        colorR.i=ListIndex(ptr())*255/ListSize(ptr())
        CreateLine3D(n, ptr()\x,ptr()\y,ptr()\z,RGB(colorR,0,0),xold,yold,zold,RGB(colorR,0,0))
        xold=ptr()\x
        yold=ptr()\y
        zold=ptr()\z
        n + 1
      Next 
    EndProcedure
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Double pendulum

Post by idle »

8)
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Double pendulum

Post by Comtois »

With your demo you can comment these lines

Code: Select all

   For i=0 To 3
      EntityLinearFactor(i, 1, 1, 0)
   Next
i thought before that CreateLine3D is ugly and static while this demo show that we can use it to simulate a thread.
I used CreateLine3D because i wasnt able to use Ribbon. Since i found goods parameters i changed my code (see first post)
Please correct my english
http://purebasic.developpez.com/
Post Reply