"Glue" 2 or more entities to one Object?

Everything related to 3D programming
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

"Glue" 2 or more entities to one Object?

Post by Bananenfreak »

Heyho,

is here anybody with knowledge about Joints (Not the Smoking ones :mrgreen: ).
I will create an testexample for my Problem, but here´s it in words:
I have different Entities with Physicsbody (Not the static one, hmm... complex so it´s good for every Situation).
Those should be connected, altogether they are one Object. This Connection has to be fix. Fixed Rotation, fixed everything.
AttachEntityObject() is not working, entity with physicsbody (except static Body) will fall down and is not fixed at its Position and Rotation.

EDIT:
This code here Shows my Problem:

Code: Select all

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;>>                             >>
;>>  Name: Jointtest            >>
;>>                             >>
;>>  Author: Bananenfreak       >>
;>>                             >>
;>>  Date: 24.06.2014           >>
;>>                             >>
;>>  OS: Windows                >>
;>>                             >>
;>>                             >>
;>>                             >>
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
EnableExplicit


Define.f KeyX, KeyY, MouseX, MouseY
Define nx.f, nz.f, Boost.f = 1, Yaw.f, Pitch.f
Define.i Quit, boden, cyl, count
#kam_0 = 0
#window = 0
#plane = 0
#planent = 0
#CylHeight = 3
#cylMat = 0

Dim cyls.i(#CylHeight - 1)

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  OpenWindow(#window, 0, 0, 1800, 1000, "Jointtest", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(#window), 10, 10, 2000, 2000, 0, 10, 10, #PB_Screen_SmartSynchronization)
  
  WorldShadows(#PB_Shadow_TextureAdditive, 200, RGB(255 * 0.2, 255 * 0.2, 255 * 0.2), 4096)
  
  AmbientColor(RGB(255 * 0.2, 255 * 0.2, 255 * 0.2))
  
  CreatePlane(#plane, 100, 100, 100, 100, 100, 100)
  boden = GetScriptMaterial(#PB_Any, "Scene/GroundBlend")
  CreateEntity(#planent, MeshID(#plane), MaterialID(boden), 0, 0, 0)
  EntityPhysicBody(#planent, #PB_Entity_StaticBody, 1000)
  
  ;-Camera
  CreateCamera(#kam_0, 0, 0, 100, 100)
  MoveCamera(#kam_0, 0, 20, 0, #PB_Absolute)
  CameraLookAt(#kam_0, 20, 0, 20)
  CameraRange (#kam_0, 2, 5000)
  CameraFOV   (#kam_0, 90)
  CameraBackColor(#kam_0, RGB(0, 0, 0))
  
  
  cyl = CreateCylinder(#PB_Any, 1, 10)
  CreateMaterial(#cylMat, LoadTexture(1, "Wood.jpg"))
  
  For count = 0 To #CylHeight - 1
    cyls(count) = CreateEntity(#PB_Any, MeshID(cyl), MaterialID(#cylMat), 0, count * 10 + 5, 0)
    EntityPhysicBody(cyls(count), #PB_Entity_ConvexHullBody, 10, 0.4, 0.1)
  Next count
  
  PointJoint(#PB_Any, EntityID(cyls(0)), 0, 5, 0, EntityID(cyls(1)), 0, -5, 0)
  
  
  Repeat
    Repeat
    Until WindowEvent() = 0
    
    
    If ExamineMouse()
      Yaw   = -MouseDeltaX() * 0.05
      Pitch = -MouseDeltaY() * 0.05
    EndIf
    
    If ExamineKeyboard()
      
      If KeyboardPushed(#PB_Key_Up)    
        MoveCamera(0,  0, 0, -1 * Boost)
      ElseIf KeyboardPushed(#PB_Key_Down)
        MoveCamera(0,  0, 0,  1 * Boost)
      EndIf 
      
      If KeyboardPushed(#PB_Key_Left)  
        MoveCamera(0, -1 * Boost, 0, 0) 
      ElseIf KeyboardPushed(#PB_Key_Right)
        MoveCamera(0,  1 * Boost, 0, 0)
      EndIf 
      
    EndIf
    
    RotateCamera(0, Pitch, Yaw, 0, #PB_Relative)
    
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Greets,

Bananenfreak
Last edited by Bananenfreak on Thu Jun 26, 2014 5:12 pm, edited 2 times in total.
Image
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Anyone knows a lot about joints?

Post by applePi »

not a lot, but i have made tests with it
if you means that the 3rd cylinder fall alone then you need also to attach the third cylinder to the second cylinder with point joint.

Code: Select all

PointJoint(#joint1, EntityID(cyls(0)), 0, 5, 0, EntityID(cyls(1)), 0, -5, 0)
  PointJoint(#joint2, EntityID(cyls(1)), 0, 5, 0, EntityID(cyls(2)), 0, -5, 0)
in the following code press space key to free the second joint and see the 3rd cylinder will detach, you can also apply force
if you want the 3 cylinders to stay vertical just make its radius thicker (such as 5)
i have used a stretched cube instead of a plane because it seems the cylinders are more stable with it.
for more info about point joints look the official example "Bridge.pb" in the 3D section also look my example "another way to move Objects" http://purebasic.fr/english/viewtopic.php?f=36&t=58858

Code: Select all

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;>>                             >>
;>>  Name: Jointtest            >>
;>>                             >>
;>>  Author: Bananenfreak       >>
;>>                             >>
;>>  Date: 24.06.2014           >>
;>>                             >>
;>>  OS: Windows                >>
;>>                             >>
;>>                             >>
;>>                             >>
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
EnableExplicit


Define.f KeyX, KeyY, MouseX, MouseY
Define nx.f, nz.f, Boost.f = 1, Yaw.f, Pitch.f
Define.i Quit, boden, cyl, count
Define joint2
#kam_0 = 0
#window = 0
#plane = 0
#planent = 0
#CylHeight = 3
#cylMat = 0
#joint1=1
#joint2=2
#joint3=3

Dim cyls.i(#CylHeight - 1)

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  OpenWindow(#window, 0, 0, 800, 600, "Jointtest, , press space to detach cylinder 3", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(#window), 10, 10, 800, 600, 0, 10, 10, #PB_Screen_SmartSynchronization)
  
  ;WorldShadows(#PB_Shadow_TextureAdditive, 200, RGB(255 * 0.2, 255 * 0.2, 255 * 0.2), 4096)
  
  AmbientColor(RGB(255 * 1, 255 * 1, 255 * 1))
  
  ;CreatePlane(#plane, 100, 100, 100, 100, 100, 100)
  boden = GetScriptMaterial(#PB_Any, "Scene/GroundBlend")
  ;CreateEntity(#planent, MeshID(#plane), MaterialID(boden), 0, 0, 0)
  ;EntityPhysicBody(#planent, #PB_Entity_StaticBody, 1000)
  
  CreateCube(#planent,1)
    CreateEntity(#planent,MeshID(#planent),MaterialID(boden), 0, -5, 0)
    ScaleEntity(#planent,100,10,100)
    EntityRenderMode(#planent, 0) 
    EntityPhysicBody(#planent, #PB_Entity_StaticBody, 0, 1, 1) 
  
  ;-Camera
  CreateCamera(#kam_0, 0, 0, 100, 100)
  MoveCamera(#kam_0, 0, 20, 30, #PB_Absolute)
  CameraLookAt(#kam_0, 0, 20, 20)
  CameraRange (#kam_0, 2, 5000)
  CameraFOV   (#kam_0, 90)
  CameraBackColor(#kam_0, RGB(0, 0, 0))
  
  
  cyl = CreateCylinder(#PB_Any, 3, 10)
  CreateMaterial(#cylMat, LoadTexture(1, "Wood.jpg"))
  
  For count = 0 To #CylHeight - 1
    cyls(count) = CreateEntity(#PB_Any, MeshID(cyl), MaterialID(#cylMat), 0, count * 10 + 7 , 0)
    ;EntityPhysicBody(cyls(count), #PB_Entity_CylinderBody, 1, 0.4, 0.1)
    EntityPhysicBody(cyls(count), #PB_Entity_ConvexHullBody, 1, 0.02, 0.1)
  Next count
 
  
  PointJoint(#joint1, EntityID(cyls(0)), 0, 5, 0, EntityID(cyls(1)), 0, -5, 0)
  PointJoint(#joint2, EntityID(cyls(1)), 0, 5, 0, EntityID(cyls(2)), 0, -5, 0)
  PointJoint(#joint3, EntityID(cyls(0)), 0, -5, 0, EntityID(#planent), 0, 5, 0)
  
  ;WorldDebug(#PB_World_DebugEntity)
  
  Repeat
    Repeat
    Until WindowEvent() = 0
    
    
    If ExamineMouse()
      Yaw   = -MouseDeltaX() * 0.05
      Pitch = -MouseDeltaY() * 0.05
    EndIf
    
    If ExamineKeyboard()
      
      If KeyboardPushed(#PB_Key_Up)    
        MoveCamera(0,  0, 0, -1 * Boost)
      ElseIf KeyboardPushed(#PB_Key_Down)
        MoveCamera(0,  0, 0,  1 * Boost)
      EndIf 
      
      If KeyboardPushed(#PB_Key_Left)  
        MoveCamera(0, -1 * Boost, 0, 0) 
      ElseIf KeyboardPushed(#PB_Key_Right)
        MoveCamera(0,  1 * Boost, 0, 0)
      EndIf 
      
      If KeyboardReleased(#PB_Key_Space )
        
        FreeJoint(#joint2)
        ApplyEntityForce(cyls(2), 0,0,-1,0,0,1)
        ApplyEntityImpulse(cyls(2), 0,0,-1,0,0,1)
        
      EndIf  
      
            
    EndIf
    
    RotateCamera(0, Pitch, Yaw, 0, #PB_Relative)
    
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
Edit:
added a joint between cylinder 0 and the ground (the box with thickness 10, so the joint are placed 5 units above it, and 5 units below the cylinder 0 center)
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Anyone knows a lot about joints?

Post by Bananenfreak »

Yes, my code works, but there are some things not good enough.

There's an opening between the cylinders, I want them in contact. If my 2 connected cylinders fall, they‘re not really connected, it's like a (uh,
I translated this word to english, it's joint ^^) joint.
Hmm, I think I asked the wrong way.

Second atempt:
I need a way to connect 2 or more entities with physics body. They should be like cemented or glued and so act like one Object.
I want one Object with opportunity to detect more physicevents.
Is there a possibility to do that?
Image
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: "Glue" 2 or entities to one Object?

Post by Bananenfreak »

So, Joints would be one solution, if there isn´t such a big gap between the entities. This gap is hardcoded... There is no possibility to set this.
For big dimensions (~10WU = 1m) you don´t see anything, but for smaller dimensions it´s a big gap (Official WU to meters of OGRE is 1WU = 1m).

In this code, you don´t see a gap:

Code: Select all

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;>>                             >>
;>>  Name: Jointtest            >>
;>>                             >>
;>>  Author: Bananenfreak       >>
;>>                             >>
;>>  Date: 24.06.2014           >>
;>>                             >>
;>>  OS: Windows                >>
;>>                             >>
;>>                             >>
;>>                             >>
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
EnableExplicit


Define.f KeyX, KeyY, MouseX, MouseY
Define nx.f, nz.f, Boost.f = 1, Yaw.f, Pitch.f
Define.i Quit, boden, cyl, count, joint
#kam_0 = 0
#window = 0
#plane = 0
#planent = 0
#CylHeight = 3
#cylMat = 0

Dim cyls.i(#CylHeight - 1)

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  OpenWindow(#window, 0, 0, 1800, 1000, "Jointtest", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(#window), 10, 10, 2000, 2000, 0, 10, 10, #PB_Screen_SmartSynchronization)
  
  WorldShadows(#PB_Shadow_TextureAdditive, 200, RGB(255 * 0.2, 255 * 0.2, 255 * 0.2), 4096)
  
  AmbientColor(RGB(255 * 0.2, 255 * 0.2, 255 * 0.2))
  
  CreatePlane(#plane, 100, 100, 100, 100, 100, 100)
  boden = GetScriptMaterial(#PB_Any, "Scene/GroundBlend")
  CreateEntity(#planent, MeshID(#plane), MaterialID(boden), 0, 0, 0)
  EntityPhysicBody(#planent, #PB_Entity_StaticBody, 1000)
  
  ;-Camera
  CreateCamera(#kam_0, 0, 0, 100, 100)
  MoveCamera(#kam_0, 20, 20, 20, #PB_Absolute)
  CameraLookAt(#kam_0, 20, 0, 20)
  CameraRange (#kam_0, 2, 5000)
  CameraFOV   (#kam_0, 90)
  CameraBackColor(#kam_0, RGB(0, 0, 0))
  
  
  cyl = CreateCylinder(#PB_Any, 10, 100)
  CreateMaterial(#cylMat, LoadTexture(1, "Wood.jpg"))
  
  For count = 0 To #CylHeight - 1
    cyls(count) = CreateEntity(#PB_Any, MeshID(cyl), MaterialID(#cylMat), 0, count * 100 + 50, 0)
    EntityPhysicBody(cyls(count), #PB_Entity_ConvexHullBody, 10, 0.4, 0.1)
  Next count
  
  ;-opening too big
;   joint = PointJoint(#PB_Any, EntityID(cyls(0)), 0, 5, 0, EntityID(cyls(1)), 0, -5, 0)
;   SetJointAttribute(joint, #PB_PointJoint_Tau, 0)       ;damping decrement
;   SetJointAttribute(joint, #PB_PointJoint_Damping, 2)       ;Speed-Up-Value
  ;-opening too big
  joint = ConeTwistJoint(#PB_Any, EntityID(cyls(0)), 0, 50, 0, EntityID(cyls(1)), 0, -50, 0)
  SetJointAttribute(joint, #PB_ConeTwistJoint_SwingSpan, 0)
  SetJointAttribute(joint, #PB_ConeTwistJoint_SwingSpan2, 0)
  SetJointAttribute(joint, #PB_ConeTwistJoint_TwistSpan, 0)
;-Wobbling and opening too big
; joint = SliderJoint(#PB_Any, EntityID(cyls(0)), 0, 5, 0, EntityID(cyls(1)), 0, -5, 0)
; SetJointAttribute(joint, #PB_SliderJoint_LowerLimit, 0)
; SetJointAttribute(joint, #PB_SliderJoint_UpperLimit, 0)
  
  
  Repeat
    Repeat
    Until WindowEvent() = 0
    
    
    If ExamineMouse()
      Yaw   = -MouseDeltaX() * 0.05
      Pitch = -MouseDeltaY() * 0.05
    EndIf
    
    If ExamineKeyboard()
      
      If KeyboardPushed(#PB_Key_Up)    
        MoveCamera(0,  0, 0, -1 * Boost)
      ElseIf KeyboardPushed(#PB_Key_Down)
        MoveCamera(0,  0, 0,  1 * Boost)
      EndIf 
      
      If KeyboardPushed(#PB_Key_Left)  
        MoveCamera(0, -1 * Boost, 0, 0) 
      ElseIf KeyboardPushed(#PB_Key_Right)
        MoveCamera(0,  1 * Boost, 0, 0)
      EndIf 
      
    EndIf
    
    RotateCamera(0, Pitch, Yaw, 0, #PB_Relative)
    
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
In this code, gap is big!

Code: Select all

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;>>                             >>
;>>  Name: Jointtest            >>
;>>                             >>
;>>  Author: Bananenfreak       >>
;>>                             >>
;>>  Date: 24.06.2014           >>
;>>                             >>
;>>  OS: Windows                >>
;>>                             >>
;>>                             >>
;>>                             >>
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
EnableExplicit


Define.f KeyX, KeyY, MouseX, MouseY
Define nx.f, nz.f, Boost.f = 1, Yaw.f, Pitch.f
Define.i Quit, boden, cyl, count, joint
#kam_0 = 0
#window = 0
#plane = 0
#planent = 0
#CylHeight = 3
#cylMat = 0

Dim cyls.i(#CylHeight - 1)

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  OpenWindow(#window, 0, 0, 1800, 1000, "Jointtest", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(#window), 10, 10, 2000, 2000, 0, 10, 10, #PB_Screen_SmartSynchronization)
  
  WorldShadows(#PB_Shadow_TextureAdditive, 200, RGB(255 * 0.2, 255 * 0.2, 255 * 0.2), 4096)
  
  AmbientColor(RGB(255 * 0.2, 255 * 0.2, 255 * 0.2))
  
  CreatePlane(#plane, 100, 100, 100, 100, 100, 100)
  boden = GetScriptMaterial(#PB_Any, "Scene/GroundBlend")
  CreateEntity(#planent, MeshID(#plane), MaterialID(boden), 0, 0, 0)
  EntityPhysicBody(#planent, #PB_Entity_StaticBody, 1000)
  
  ;-Camera
  CreateCamera(#kam_0, 0, 0, 100, 100)
  MoveCamera(#kam_0, 20, 20, 20, #PB_Absolute)
  CameraLookAt(#kam_0, 20, 0, 20)
  CameraRange (#kam_0, 2, 5000)
  CameraFOV   (#kam_0, 90)
  CameraBackColor(#kam_0, RGB(0, 0, 0))
  
  
  cyl = CreateCylinder(#PB_Any, 1, 10)
  CreateMaterial(#cylMat, LoadTexture(1, "Wood.jpg"))
  
  For count = 0 To #CylHeight - 1
    cyls(count) = CreateEntity(#PB_Any, MeshID(cyl), MaterialID(#cylMat), 0, count * 10 + 5, 0)
    EntityPhysicBody(cyls(count), #PB_Entity_ConvexHullBody, 10, 0.4, 0.1)
  Next count
  
  ;-opening too big
;   joint = PointJoint(#PB_Any, EntityID(cyls(0)), 0, 5, 0, EntityID(cyls(1)), 0, -5, 0)
;   SetJointAttribute(joint, #PB_PointJoint_Tau, 0)       ;damping decrement
;   SetJointAttribute(joint, #PB_PointJoint_Damping, 2)       ;Speed-Up-Value
  ;-opening too big
  joint = ConeTwistJoint(#PB_Any, EntityID(cyls(0)), 0, 5, 0, EntityID(cyls(1)), 0, -5, 0)
  SetJointAttribute(joint, #PB_ConeTwistJoint_SwingSpan, 0)
  SetJointAttribute(joint, #PB_ConeTwistJoint_SwingSpan2, 0)
  SetJointAttribute(joint, #PB_ConeTwistJoint_TwistSpan, 0)
;-Wobbling and opening too big
; joint = SliderJoint(#PB_Any, EntityID(cyls(0)), 0, 5, 0, EntityID(cyls(1)), 0, -5, 0)
; SetJointAttribute(joint, #PB_SliderJoint_LowerLimit, 0)
; SetJointAttribute(joint, #PB_SliderJoint_UpperLimit, 0)
  
  
  Repeat
    Repeat
    Until WindowEvent() = 0
    
    
    If ExamineMouse()
      Yaw   = -MouseDeltaX() * 0.05
      Pitch = -MouseDeltaY() * 0.05
    EndIf
    
    If ExamineKeyboard()
      
      If KeyboardPushed(#PB_Key_Up)    
        MoveCamera(0,  0, 0, -1 * Boost)
      ElseIf KeyboardPushed(#PB_Key_Down)
        MoveCamera(0,  0, 0,  1 * Boost)
      EndIf 
      
      If KeyboardPushed(#PB_Key_Left)  
        MoveCamera(0, -1 * Boost, 0, 0) 
      ElseIf KeyboardPushed(#PB_Key_Right)
        MoveCamera(0,  1 * Boost, 0, 0)
      EndIf 
      
    EndIf
    
    RotateCamera(0, Pitch, Yaw, 0, #PB_Relative)
    
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
Time for a futurerequest?
Image
Post Reply