connect any two points with a cylinder instead of a Line

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

connect any two points with a cylinder instead of a Line

Post by applePi »

the idea is to use the native PB cylinder as it is a thick line but with usability like that in CreateLine3D function. so to make a new function like :
CylinderFromTo(cylRadius, x1, y1, z1, x2, y2, z2)
cylinders is better than thin unnoticeable lines
before we do that we need to know:
1- the distance between the two points
2- make a cylinder = that distance
3- shift the cylinder rotational center to one of its edges using the TransformMesh(...) function
4-create an entity from that cylinder mesh and move it to the first point, the new center of the cylinder will be on the first point (remember we shifted the cylinder center to the cylinder base)
5- now what we need is to apply trigonometry rules to rotate the cylinder twice: horizontal and vertical until it fits between the 2 points
the code is documented
i have used this picture to imagine the situation:
Image

Image

if you find errors or have another version of the code please post and thanks in advance.

tested with PB5.42 beta4 windows xp/32

Code: Select all

Declare CylinderFromTo(cylRadius.f, x1.f, y1.f, z1.f, x2.f, y2.f, z2.f)
InitEngine3D()
InitKeyboard()
InitSprite()
InitMouse()

Define.f KeyX, KeyY, MouseX, MouseY
Global Window, Event
Global x1.f, y1.f, z1.f, x2.f, y2.f, z2.f
Global camera, quit
Global Texture, Material
Global cyl, sphereOrNot=1
#cameraSpeed = 0.5
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
OpenWindow(0,0,0,DesktopW, DesktopH,"press 'Space' to stop/rotate the camera ",#PB_Window_ScreenCentered| #PB_Window_MinimizeGadget | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,DesktopW, DesktopH)

Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home+"examples/3d/Data/Packs/Sinbad.zip", #PB_3DArchive_Zip)

Parse3DScripts()

SkyBox("desert07.jpg")
AmbientColor(RGB(150, 150, 150))

CreateLight(#PB_Any,RGB(170, 170, 150), 700, 100, 600)
WorldShadows(#PB_Shadow_Additive)

CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
MaterialCullingMode(5, #PB_Material_NoCulling)
CreateMaterial(6, LoadTexture(6, "clouds.jpg"))
MaterialCullingMode(6, #PB_Material_NoCulling)

 CreateLine3D(0, 0, 0.2, 0, RGB(255,   0,   0), 10,  0.2,  0, RGB(255,   0,   0))  ; Axis X
 CreateLine3D(1, 0, 0, 0, RGB(  0, 255,   0),  0, 10,  0, RGB(  0, 255,   0))  ; Axis Y
 CreateLine3D(2, 0, 0.2, 0, RGB(  0,   0, 255),  0,  0.2, 10, RGB(  0,   0, 255))  ; Axis Z
        
Camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(Camera, 0, 20, 50, #PB_Absolute)
CameraLookAt(Camera,  0,10,0)

;Ground
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "snow_1024.jpg")))
Entity = CreateEntity(#PB_Any, MeshID(CreatePlane(#PB_Any, 1000, 1000, 100, 100, 10, 10)), MaterialID(Material))

Fog(RGB(139, 100, 19), 1, 100, 200)

Euclid = CreateEntity(#PB_Any, MeshID(LoadMesh(#PB_Any, "Sinbad.mesh")), #PB_Material_None, 0, 5, -25)

For i=1 To 12 ; read the data for points with spheres on
  Read.f x1: Read.f y1: Read.f z1: Read.f x2: Read.f y2: Read.f z2
  CylinderFromTo(0.3, x1, y1, z1,   x2, y2, z2)    
Next
sphereOrNot=0
For i=1 To 4 ; read data for points without spheres on it
  Read.f x1: Read.f y1: Read.f z1: Read.f x2: Read.f y2: Read.f z2
  CylinderFromTo(0.7, x1, y1, z1,   x2, y2, z2)    
Next

Global camRot=1
Repeat
  Repeat   
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
      EndSelect
  Until Event = 0
      
      If ExamineMouse()
        MouseX = -MouseDeltaX()/10
        MouseY = -MouseDeltaY()/10
      EndIf
                
      If ExamineKeyboard()
        
      ;moving the camera ================================================
        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
        
    If KeyboardPushed (#PB_Key_Escape)
     Break
    EndIf
    

    If KeyboardReleased (#PB_Key_Space)
        camRot !1
    EndIf
      
    If camRot
    count.f + 0.003
    MoveCamera(camera, Sin(count) * 60, 15, Cos(count) * 60, #PB_Absolute)
    CameraLookAt(camera,0,5,0)
     Else
       RotateCamera(camera, MouseY, MouseX, 0, #PB_Relative)
       MoveCamera(camera, KeyX, 0, KeyY)
     EndIf
 
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1

    End
    Procedure CylinderFromTo(cylRadius.f, x1.f, y1.f, z1.f, x2.f, y2.f, z2.f)
    CreateLine3D(3, 0, 0, 0, RGB(  140,   250, 255),  x2,  y2, z2, RGB(  140,   250, 255))   
    NbBaseSegments = 16
    ; the lenght of the cylinder = distance between the 2 points
    cylLenght.f = Sqr((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1))
    
    CreateCylinder(5,cylRadius,cylLenght, NbBaseSegments, 2,0) 
    ;change the cylinder rotation center from its center to its edge
    TransformMesh(5, cylLenght/2,0,0,1,1,1, 0, 0,90)
    UpdateMeshBoundingBox(5)
    CreateEntity(cyl, MeshID(5), MaterialID(5),  x1, y1, z1)
    
    If sphereOrNot ; just to draw or not to draw spheres over the points
    sphere = CreateSphere(#PB_Any, 1)
    CreateEntity(#PB_Any, MeshID(sphere), MaterialID(6),  x1, y1, z1)
    secondPoint = CreateEntity(#PB_Any, MeshID(sphere), MaterialID(6),  x2, y2, z2)
  EndIf
    
    angleSin.f=(y2-y1)/cylLenght ; to find the vertical angle needed for cylinder vertical rotation
    angle.f = Degree(ASin(angleSin))
         
    angXZ.f = ATan2(x2-x1, z2-z1) ; horizontal angle between x and the projection of the line between the 2 points over the plane
    angXZ = Degree(angXZ)
   
    RotateEntity(cyl, 0,-angXZ, 0, #PB_Absolute)
    RotateEntity(cyl, 0,0, angle, #PB_Relative) 
          
    cyl+1
          
      
    EndProcedure
    
    DataSection
     points:
     Data.f -10,1,10, 10,1,10
     Data.f 10,1,10,  10,1,-10
     Data.f 10,1,-10, -10,1,-10
     Data.f-10,1,-10, -10,1,10
     
     Data.f 10,22,10, 10,22,-10
     Data.f 10,22,-10, -10,22, -10
     Data.f -10,22,-10, -10,22,10
     Data.f -10,22,10, 10,22,10
     
     Data.f 10,1,10, 10,22,10
     Data.f 10,1,-10, 10,22,-10
     Data.f -10,1,-10, -10,22,-10
     Data.f -10,1,10, -10,22,10
     
     
     Data.f -5,6,10, 5,6,10
     Data.f 5,14,10, -5,14,10
     Data.f 2.5,14,10, -5,6,10
     Data.f -4.0,10,10, 1.5,10,10
   EndDataSection
   
      
Last edited by applePi on Sun Feb 28, 2016 11:34 am, edited 1 time in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: connnect any two points with a cylinder instead of a Lin

Post by applePi »

another proof that shifting the cylinder center to its base side is useful so we can use the cylinder as it is a line like CylinderFromTo(cylRadius.f, x1.f, y1.f, z1.f, x2.f, y2.f, z2.f)
is this 2D tree which is an Ogre version with cylinders of the opengl version with thick lines posted here http://purebasic.fr/english/viewtopic.php?f=36&t=60998
Image
note: it is possible to use a robot.mesh instead of the cylinder so we get a tree made from robots, but because we must shift the rotation center to its bottom its animation are distorted, more tests needed about the robots tree.

tested with PB5.42 beta4 winxp/32

Code: Select all

Declare CylinderFromTo(cylRadius.f, x1.f, y1.f, z1.f, x2.f, y2.f, z2.f)

Enumeration
   #camera
   #Plane
      
EndEnumeration
 
#cameraspeed = 4
Declare tree(depth.l, x1.f, y1.f, angle.f)

Define.f KeyX, KeyY, MouseX, MouseY

depth = 9 ; height of the tree
Global cyl

ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "'Space' stop/rotate .... mouse & Arrows: move and rotate the Camera ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If InitEngine3D()
  
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts",#PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  OpenWindowedScreen(WindowID(0), 0, 20, WindowWidth(0)-50, WindowHeight(0)-60, 0, 0, 0) 
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 0, 0, 600, #PB_Absolute)
    CameraBackColor(#camera, RGB(250,250,250))
    CameraLookAt(#camera,0,0,0)
        
    CreateLight(0, RGB(255,255,255), 0, 0, 20)
    AmbientColor(RGB(200, 200, 200))

    CreateMaterial(5, LoadTexture(5, "ground_diffuse.png"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
    
    angle.f=0
    
    sphere = CreateSphere(#PB_Any, 10)
    ;the object we want to attach every tree twig, not neccessary action but to be able to rotate all the tree at once
    Global spheros = CreateEntity(#PB_Any, MeshID(sphere), MaterialID(5),  0, -200, 0)
    
    tree(depth, 0, 0, 90) ; call the tree proc 
   
    angle = 0: rot=1
    Repeat
      Event = WindowEvent()
      Repeat   
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
      EndSelect
    Until Event = 0
    
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
          
      If ExamineKeyboard()
                  
    
        If KeyboardReleased(#PB_Key_P)  
          rot ! 1
        EndIf
                
        ;moving the camera ================================================
        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
        ;==================================================================
        ; wire or solid frame
        If KeyboardReleased(#PB_Key_Space)
         rot ! 1
        EndIf
               
        
      EndIf
      
       
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#camera, KeyX, 0, KeyY)
      
      RotateEntity(spheros,0,rot,0,#PB_Relative) ; rotate all the tree as one
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End


Procedure tree(d.l, x1.f, y1.f, angle.f)
  
  x2.f: y2.f
   If d > 0
    
    x2 = x1 + Cos(Radian(angle)) * d * Random(12,5)
    y2 = y1 + Sin(Radian(angle)) * d * Random(12,5)
    
    CylinderFromTo(d, x1,y1,0,x2,y2,0)
               
    d-1
    
    tree(d, x2, y2, angle-20)
    tree(d, x2, y2, angle+20)
    
  EndIf
    
EndProcedure  

Procedure CylinderFromTo(cylRadius.f, x1.f, y1.f, z1.f, x2.f, y2.f, z2.f)
    
    NbBaseSegments = 8
    ; the lenght of the cylinder = distance between the 2 points
    cylLenght.f = Sqr((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1))
        
    CreateCylinder(5,cylRadius,cylLenght, NbBaseSegments, 2,0) 
    ;change the cylinder rotation center from its center to its edge
    TransformMesh(5, cylLenght/2,0,0,1,1,1, 0, 0,90)
    UpdateMeshBoundingBox(5)
    
    CreateEntity(cyl, MeshID(5), MaterialID(5),  x1, y1, z1)
    
    
    angleSin.f=(y2-y1)/cylLenght ; to find the vertical angle needed for cylinder vertical rotation
    angle.f = Degree(ASin(angleSin))
         
    angXZ.f = ATan2(x2-x1, z2-z1) ; horizontal angle between x and the projection of the line between the 2 points over the plane
    angXZ = Degree(angXZ)
    
       
    RotateEntity(cyl, 0,-angXZ, 0, #PB_Absolute)
    RotateEntity(cyl, 0,0, angle, #PB_Relative) 
    
    
    AttachEntityObject(spheros,"", EntityID(cyl))
          
    cyl+1
          
      
    EndProcedure
    
        
        
        
        
        
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5524
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: connect any two points with a cylinder instead of a Line

Post by Kwai chang caine »

Like always, very very nice :shock:
Works perfectly
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
Hades
Enthusiast
Enthusiast
Posts: 188
Joined: Tue May 17, 2005 8:39 pm

Re: connect any two points with a cylinder instead of a Line

Post by Hades »

I hate you! Seeing this makes me want to experiment with L-Systems again! :evil:

Just joking, really nice work. :D
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: connect any two points with a cylinder instead of a Line

Post by applePi »

Thank you Kwai chang caine and Hades
another application of the function CylinderFromTo (which its name tell what it is) is to make a helical tube made from many cylinders.
** line 85 y2.f + 3 raise the cylinder base (or top) a little by 3 so to make a helical shape. change y2.f + 3 to y2.f + 1 and will get compressed helix
** if you want a short helix change line 80: For j=1 To 10 to as an example For j=1 To 1 or For j=1 To 2 if you can't wait for the sphere to hit Sinbad.
wait the sphere until it hits sinbad, and once hitting sinbad he will sadly stop animation
** i have increase the WorldGravity twice .
** what to do : move/rotate the camera up to see the big spiral in the sky, move closer to see the red sphere falling in the tubes
wait the sphere until it hits sinbad, and once hitting sinbad he will sadly stop animation

Edit: the purpose of the function CylinderPlus is nothing to do with helix , it is just to build a cylinder which have different radius in its base and its top, and it is using the official PB Cylinder which i think is great enough. it is expressed here as the blue conic cylinder on the ground .
Image

Code: Select all

Declare CylinderFromTo(cylRadius.f, x1.f, y1.f, z1.f, x2.f, y2.f, z2.f)
Declare CylinderPlus(radius.f, bottomRadius.f, leng.f, NbBaseSegments)
;CylinderPlus: to make different cylinder base and top radius

InitEngine3D()
InitKeyboard()
InitSprite()
InitMouse()

Global Dim MeshData.PB_MeshVertex(0) ;array for loading and

Define.f KeyX, KeyY, MouseX, MouseY
Global Window, Event
Global x1.f, y1.f, z1.f, x2.f, y2.f, z2.f
Global camera, quit
Global Texture, Material
Global cyl, sphereOrNot=1
#cameraSpeed = 2

ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
OpenWindow(0,0,0,DesktopW, DesktopH,"arrows and mouse to move/rotate the camera ",#PB_Window_ScreenCentered| #PB_Window_MinimizeGadget | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,DesktopW, DesktopH)

Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home+"examples/3d/Data/Packs/Sinbad.zip", #PB_3DArchive_Zip)

Parse3DScripts()

SkyBox("desert07.jpg")
AmbientColor(RGB(150, 150, 150))

CreateLight(#PB_Any,RGB(170, 170, 150), 700, 100, 600)
WorldShadows(#PB_Shadow_Additive)

CreateMaterial(5, LoadTexture(5, "soil_wall.jpg"))
MaterialCullingMode(5, #PB_Material_NoCulling)
MaterialBlendingMode(5, #PB_Material_AlphaBlend)
SetMaterialColor(5, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 120))

CreateMaterial(6, LoadTexture(6, "ground_diffuse.png"))
MaterialCullingMode(6, #PB_Material_NoCulling)
MaterialBlendingMode(6, #PB_Material_AlphaBlend)
SetMaterialColor(6, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 120))

CreateMaterial(7, LoadTexture(7, "Geebee2.bmp"))
MaterialCullingMode(7, #PB_Material_NoCulling)


 CreateLine3D(0, 0, 0.2, 0, RGB(255,   0,   0), 10,  0.2,  0, RGB(255,   0,   0))  ; Axis X
 CreateLine3D(1, 0, 0, 0, RGB(  0, 255,   0),  0, 10,  0, RGB(  0, 255,   0))  ; Axis Y
 CreateLine3D(2, 0, 0.2, 0, RGB(  0,   0, 255),  0,  0.2, 10, RGB(  0,   0, 255))  ; Axis Z
        
Camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(Camera, 0, 100, 200, #PB_Absolute)
CameraLookAt(Camera,  0,10,0)

;Ground
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "snow_1024.jpg")))
MaterialCullingMode(Material, #PB_Material_NoCulling)

Entity = CreateEntity(#PB_Any, MeshID(CreatePlane(#PB_Any, 1000, 1000, 100, 100, 10, 10)), MaterialID(Material))
CreateEntityBody(Entity, #PB_Entity_StaticBody, 1, 0.5, 10)

Sinbad = LoadMesh(#PB_Any, "Sinbad.mesh")
Sinbad = CreateEntity(#PB_Any, MeshID(Sinbad), #PB_Material_None, 20, 10, -25)
ScaleEntity(Sinbad, 2,2,0.05)
UpdateMeshBoundingBox(Sinbad)

CreateEntityBody(Sinbad, #PB_Entity_ConvexHullBody ) 
StartEntityAnimation(Sinbad, "IdleTop")

angle.f: rad.f = 20: nSides = 20
circum.f = 2*rad*#PI
leng.f = circum / nSides
x1.f = rad: y1.f =2: z1.f = 0
sphereOrNot = 0 ; whether we want spheres at the ends of each cylinder
For j=1 To 10 ; how many full turn of the tubes; one turn = 360 degree ie 2*#Pi
    For i=1 To nSides
      angle.f + #PI*2/nSides
      x2.f=rad * Cos(angle) ; circle formula
      z2.f=rad * Sin(angle) ; // //
      y2.f + 3 ; here we rais the cylinder end 3 units so at the end it forms helical shape

      CylinderFromTo(2, x1, y1, z1,   x2, y2, z2)
     
      x1=x2: z1=z2 : y1=y2
    Next
 Next
    
    
NbBaseSegments = 16   
CylinderPlus(30,0.3,50, NbBaseSegments)
Global sphere = CreateSphere(#PB_Any, 1.5)
sphere = CreateEntity(#PB_Any, MeshID(sphere), MaterialID(7) , x1,y1,z1)
CreateEntityBody(sphere, #PB_Entity_ConvexHullBody , 10, 0.5, 1 )

WorldGravity(-20)
Repeat
  Repeat   
    Event  = WindowEvent()
  Until Event = 0
      
      If ExamineMouse()
        MouseX = -MouseDeltaX()/10
        MouseY = -MouseDeltaY()/10
      EndIf
                
      If ExamineKeyboard()
        
      ;moving the camera ================================================
        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
        
    If KeyboardPushed (#PB_Key_Escape)
     Break
    EndIf
    

     
     RotateCamera(camera, MouseY, MouseX, 0, #PB_Relative)
     MoveCamera(camera, KeyX, 0, KeyY)
    
     ;if collison happened between the sphere and sinbad the animation stop
     If EntityCollide(Sinbad, sphere)
       ;Debug "collision"
       StopEntityAnimation(Sinbad, "IdleTop")

     EndIf
     
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1

    End
    Procedure CylinderFromTo(cylRadius.f, x1.f, y1.f, z1.f, x2.f, y2.f, z2.f)
    CreateLine3D(3, 0, 0, 0, RGB(  140,   250, 255),  x2,  y2, z2, RGB(  140,   250, 255))   
    NbBaseSegments = 16
    ; the lenght of the cylinder = distance between the 2 points
    cylLenght.f = Sqr((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1))
    
    cylinder = CreateCylinder(#PB_Any,cylRadius,cylLenght, NbBaseSegments, 2,0) 
    ;change the cylinder rotation center from its center to its edge
    TransformMesh(cylinder, cylLenght/2,0,0,1,1,1, 0, 0,90)
    UpdateMeshBoundingBox(cylinder)
    CreateEntity(cyl, MeshID(cylinder), MaterialID(5),  x1, y1, z1)
    
    If sphereOrNot ; just to draw or not to draw spheres over the points
      sphere = CreateSphere(#PB_Any, 2.5)
      CreateEntity(#PB_Any, MeshID(sphere), MaterialID(6),  x1, y1, z1)
      secondPoint = CreateEntity(#PB_Any, MeshID(sphere), MaterialID(6),  x2, y2, z2)
      
    EndIf
    
    angleSin.f=(y2-y1)/cylLenght ; to find the vertical angle needed for cylinder vertical rotation
    angle.f = Degree(ASin(angleSin))
         
    angXZ.f = ATan2(x2-x1, z2-z1) ; horizontal angle between x and the projection of the line between the 2 points over the plane
    angXZ = Degree(angXZ)
   
    RotateEntity(cyl, 0,-angXZ, 0, #PB_Absolute)
    RotateEntity(cyl, 0,0, angle, #PB_Relative) 
    CreateEntityBody(cyl,#PB_Entity_StaticBody )
    
    cyl+1
           
      
  EndProcedure
  
  Procedure CylinderPlus(radius.f, bottomRadius.f, leng.f, NbBaseSegments)
    
    conicCylinder = CreateCylinder(#PB_Any,radius.f,leng.f, NbBaseSegments, 1,0) 
           
    GetMeshData(conicCylinder,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(conicCylinder)-1)
    ;building the cylinder with hole in the bottom
    angle.f=0: ;radius.f = 1
    For i=0 To NbBaseSegments
      x.f=MeshData(i)\x 
      z.f=MeshData(i)\z 
      ;circle formula:
      x2.f=bottomRadius * radius * Cos(angle)
      z2.f=bottomRadius * radius * Sin(angle)
      y2.f = -5
      ;filling the array with the new vertices coordinates
      MeshData(i)\x = x2: MeshData(i)\z = z2: MeshData(i)\y = y2
      If i=0
      ;to save the coordinates of the last vertex in the circle to equal -->
      ;the coordinates of the first vertex in the circle  
        x3.f=x2: y3.f=y2: z3.f=z2
      EndIf
      
      angle.f + #PI*2/NbBaseSegments
    Next
    
    MeshData(NbBaseSegments)\x = x3
    MeshData(NbBaseSegments)\y = y3
    MeshData(NbBaseSegments)\z = z3
    
    SetMeshData(conicCylinder, 0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(conicCylinder)-1)
    UpdateMeshBoundingBox(conicCylinder)
    TransformMesh(conicCylinder, 0,0,0,1,1,1, 0, 0,-90)
    TransformMesh(conicCylinder, leng/2,0,0,1,1,1, 0, 0,0)
    
    conicCylinder = CreateEntity(#PB_Any, MeshID(conicCylinder), MaterialID(6), -40,0,-25)
    
    RotateEntity(conicCylinder,0,0,90)
  EndProcedure
  

    
   
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5524
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: connect any two points with a cylinder instead of a Line

Post by Kwai chang caine »

Works very well, i have waiting all the time for know what's appened to the the BAD simBAD :lol:
Now your animations, is not only animation...but a real movie with "hitchcock suspens" :D

Each your new animation is always a present i want try immediately like when i'm a little kid
Litle by little i begin to be interesting by the 3D, thanks to you, because always surprising to see what it is possible to do :shock:
Thanks again and again for all your 3D jewels 8)
ImageThe happiness is a road...
Not a destination
Post Reply