Page 1 of 1

Compound Shape

Posted: Sat Apr 19, 2014 6:55 pm
by Bananenfreak
Heyho,

I have many complex Meshes and for a few of them, true Center of mass isn´t in Origin.

Here is something from another side, but I don´t understand those answers ^^:
http://www.bulletphysics.org/Bullet/php ... f=9&t=2209

So here is my Problem:
I have a tri mesh, for simplicity let's say it's a cube 2x2x2 units in size, with one corner at the origin. Bullet treats this body as if the center of mass is at the origin corner. I'd like to tell Bullet that the center of mass is at (1,1,1) -- in the center of the 2x2x2 cube.
Simplest way is to move cube in 3D-Editor to Origin, but it isn´t a solution for me, it would make things hard (Rotation for example).

The biggest Problem is, let´s say I changed Position of Center of mass to (1,1,1) and I rotate Entity by 90°, I have 2 different boxes. 1 Box is the box you´ll see, the other is the physics box.
So I read the only solution is "Compound Shape". But what is this? Can we do this with PB?

Here´s a Picture that Shows my Problem (More or less):
Image
If you have an alternative idea let me know.

Re: Compound Shape

Posted: Sat Apr 19, 2014 9:06 pm
by applePi
i see that the posts in the bullet forum have no explicit solutions.
not sure if the following will be useful in your problem. here: http://www.purebasic.fr/english/viewtop ... 15#p417666 Comtois provided a procedure ChangeMesh() to center a mesh. the mesh here is a pyramid. if we comment the call to ChangeMesh() in line 54 we will see the pyramid does not rotate around its own center, but with ChangeMesh() it will rotate around its center. note that the mesh are not in 0,0,0 position as in the modified code below. if we uncomment line 55 EntityPhysicBody(0,...) the pyramid will fall under gravity.
i don't understand the second question, but try to center the tree parts if this will correct the positions. i suggest to post a small working prototype of the problem may be someone will find a solution.

Code: Select all

Enumeration
  #MESH
  #LIGHT
  #CAMERA
  #mainwin
EndEnumeration
Structure Vector3
  x.f
  y.f
  z.f
EndStructure
Define.Vector3 v1, v2, v3

Global x.f = 0
Global y.f = 10
Global z.f = -30
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "F2: wireframe. F3: solid ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  
  Define.f KeyX, KeyY
  Declare ChangeMesh()
  Declare CreateMatrix()
  Declare DrawMatrix()
  
  If InitEngine3D()
    
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
    Add3DArchive("/", #PB_3DArchive_FileSystem)
    
    InitSprite()
    InitKeyboard()
    OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)
    
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
    CreateMatrix()
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 120, #PB_Absolute)
    CameraFOV(0, 70)
    CameraBackColor(0, $330000)
    CameraLookAt(0,0,0,0)
    
    CreateLight(0, RGB(255,255,255), 10, 60, -10)
    AmbientColor(RGB(90, 90, 90))
    
    ChangeMesh()
    ;EntityPhysicBody(0, #PB_Entity_ConvexHullBody , 1, 0.5, 1)
    
    Repeat
      Event = WindowEvent()
      
      If ExamineKeyboard()
        
        If KeyboardReleased(#PB_Key_F2)
          MaterialShadingMode(0, #PB_Material_Wireframe)
        ElseIf KeyboardReleased(#PB_Key_F3)
          MaterialShadingMode(0, #PB_Material_Solid)
        EndIf
        
     
        
      EndIf
      rot.f+0.6
      RotateEntity(0,0,rot,0)
      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 ChangeMesh()
  Protected.f minx, miny, minz, maxx, maxy, maxz
  Protected.f corx, cory, corz
  minx =  999999
  miny =  999999
  minz =  999999
  maxx = -999999
  maxy = -999999
  maxz = -999999
  
  For i=0 To ArraySize(MeshData())
    If MeshData(i)\x > maxx
      maxx = MeshData(i)\x
    EndIf
    If MeshData(i)\x < minx
      minx = MeshData(i)\x
    EndIf    
    If MeshData(i)\y > maxy
      maxy = MeshData(i)\y
    EndIf
    If MeshData(i)\y < miny
      miny = MeshData(i)\y
    EndIf   
    If MeshData(i)\z > maxz
      maxz = MeshData(i)\z
    EndIf
    If MeshData(i)\z < minz
      minz = MeshData(i)\z
    EndIf       
  Next  
  corx = (-maxx - minx) /2.0
  cory = (-maxy - miny) /2.0
  corz = (-maxz - minz) /2.0
  
  MessageRequester("", "minx = " + StrF(minx,2) + Chr(10) +
                       "miny = " + StrF(miny,2) + Chr(10) +
                       "minz = " + StrF(minz,2) + Chr(10) +
                       "maxx = " + StrF(maxx,2) + Chr(10) +
                       "maxy = " + StrF(maxy,2) + Chr(10) +
                       "maxz = " + StrF(maxz,2) + Chr(10) +
                       "Correction x = " + StrF(corx,2) + Chr(10) +
                       "Correction y = " + StrF(cory,2) + Chr(10) +
                       "Correction z = " + StrF(corz,2), #PB_MessageRequester_Ok )
  For i=0 To ArraySize(MeshData())
    MeshData(i)\x + corx
    MeshData(i)\y + cory
    MeshData(i)\z + corz     
  Next  
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
EndProcedure


Procedure DrawMatrix()
  MeshVertexPosition(-20, -20, -20) ;vertex 0
  MeshVertexColor(RGB(255, 0, 0))
  MeshVertexPosition(20, -20, -20)  ; vertex 1
  MeshVertexColor(RGB(0, 255, 0))
  MeshVertexPosition(0, -20, -40)   ; vertex 2
  MeshVertexColor(RGB(255, 220, 0))
  
  MeshVertexPosition(0, 10,-30)     ; vertex 3
  MeshFace(0, 1, 2)
  MeshFace(1, 2, 3)
  MeshFace(1, 0, 3)
  MeshFace(0, 3, 2)
  
EndProcedure 

Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_TriangleList, #True)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(0))
  
  CreateEntity(0, MeshID(0), #PB_Material_None, 45,15,-30)
  
  ScaleEntity(0, 2, 2, 2)
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
EndProcedure

Re: Compound Shape

Posted: Sun Apr 20, 2014 7:39 am
by Bananenfreak
Thank you, applePi.
This would be an possible solution, but this Procedure doesn´t find the physical Center of mass.
I want to code a program which Returns the physical Center of mass (But this is pretty difficult) in my spare time.

But the other question is still out there; What is CompoundShape?!

Re: Compound Shape

Posted: Sun Apr 20, 2014 8:34 am
by applePi
compound shape , "Multiple convex shapes can be combined into a composite or compound shape, using the btCompoundShape This is a concave shape made out of convex sub parts, called child shapes. Each child shape has its own local offset transform, relative to the btCompoundShape. ." Bullet user manual

seems not possible in purebasic 3D now, since btCompoundShape not implemented by PB Ogre/Bullet, look my request here http://purebasic.fr/english/viewtopic.p ... it=concave to add dynamic concave shapes, Olby replied that what i need is a dynamic compound object. ((The btCompoundShape allows to store multiple other btCollisionShapes This allows for moving concave collision objects)) Bullet Reference Manual
the dynamic compound object seems to me natural to add to the PB 3D engine, this will allow to use and design gears and all sorts of funny machines.
i have checked the Engine3D.dll with notepad i saw this: ImpactConcaveShape !!.