SetEntityCollisionFilter two applications

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

SetEntityCollisionFilter two applications

Post by applePi »

when Realizimo posted his "entity inside entity with SetEntityCollisionFilter":
http://purebasic.fr/english/viewtopic.php?f=36&t=60530 i thought it is a useless and absurd project. but now i think we can design some toys
the first example is a robot which are walking like a man walks on crutches, the example are using almost Realizimo code with some changes, the operating instructions on the title bar:

Code: Select all

#material = 1
#CameraSpeed = 0.5

InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"PgUp/ PgDown :to start the robot .... Z/X : rotate Robot left/right.... mouse+arrows for Camera",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
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/GUI", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
;- -------------  Material  -----------------------------------------
  CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
  CreateMaterial(1, LoadTexture(1, "Wood.jpg"))
  
;- -------------  Camera  -----------------------------------------
  #camera =0
  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, 0, 12, 15, #PB_Absolute)
  CameraLookAt(0, 0, 0, 0)
;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 1560, 900, 500)
  AmbientColor($330000)
;- -------------  World  -------------------------------------------
  WorldGravity(-9)
  SkyBox("desert07.jpg")
;- -------------  meshes  -----------------------------------------
  CreateCube(0, 1.0)
;- -------------  entitys  -------------------------------------------
  CreateEntity(0, MeshID(0), MaterialID(0), 0, 0, 0)
  ScaleEntity(0, 3, 4.3, 0.5)
  CreateEntity(1, MeshID(0), MaterialID(1), 3, 0, 0)
  ScaleEntity(1, 2, 4, 2)     
;- -------------  PhysicBody  -----------------------------------
  EntityPhysicBody(0, #PB_Entity_BoxBody, 1, 0.1,5)
  EntityPhysicBody(1, #PB_Entity_BoxBody, 1, 0.1, 0.1)    
;- -------------  CollisionFilter  ---------------------------------
  SetEntityCollisionFilter(0      , 4 , 3 ) 
  SetEntityCollisionFilter(1      , 4 , 3 ) 
 ;SetEntityCollisionFilter(#Entity, CollisionGroup, CollisionMask)

;- -------------  joints  ---------------------------------------------
  HingeJoint(0, EntityID(0),
             0, 0, 0,
             1, 0, 0, 
             EntityID(1),
             0, 0, 0, 
             0, 0, 1)
  SetJointAttribute(0, #PB_HingeJoint_LowerLimit, 0)    
;- -------------  not sleep  ---------------------------------------
  SetEntityAttribute(0, #PB_Entity_LinearSleeping, 0)
  SetEntityAttribute(1, #PB_Entity_LinearSleeping, 0)  
;- -------------  Loop  ----------------------------------------------

Define.f KeyX, KeyY, MouseX, MouseY
CreateMaterial(#material, LoadTexture(#material, "MRAMOR6X6.jpg"))
CreatePlane(500, 100, 100, 20, 20, 2, 2)
CreateEntity(500, MeshID(500), MaterialID(#material), 0,-2.5,0)
EntityPhysicBody(500, #PB_Entity_StaticBody, 1, 0.1, 1)
CreateCube(200, 0.5)
CreateEntity(200,MeshID(200),#PB_Material_None ,10,5,-10)
EntityPhysicBody(200, #PB_Entity_BoxBody, 0.1, 0.5, 1)

Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate camera and fly in/out
  ExamineKeyboard()
  ;EnableHingeJointAngularMotor(#Joint, Enable, TargetVelocity, MaxMotorImpulse)
  If KeyboardPushed(#PB_Key_PageUp): EnableHingeJointAngularMotor(0,1,-1,2): EndIf
  If KeyboardPushed(#PB_Key_PageDown): EnableHingeJointAngularMotor(0,1,1,2): EndIf
    
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Z)
          RotateEntity(1, 0, 1, 0, #PB_Relative)
        ElseIf KeyboardPushed(#PB_Key_X)
          RotateEntity(1, 0, -1, 0, #PB_Relative)
        EndIf
        
   
  ;RotateEntity(0, 0, 0.5, 0,#PB_Relative)
  
  RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (0, KeyX, 0, KeyY)
  
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
the second code is a 4 blades gear (made from 2 scaled cubes, one of them are merged to thin cylinder ), (operation instructions on the title bar.

Code: Select all

Enumeration
#material = 3
#Joint
#stone
#axisCube
#Brake
#BrakeHinge
EndEnumeration
#CameraSpeed = 0.5
Declare CreateMatrix()
Declare dropStone(stn.l)
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global Dim stone(2000)

InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"Space :to drop stones .... R rotate Gear with RotateEntity.... T: Energize the Gear motor.... ..Mouse + arrow: for the Camera",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
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/GUI", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

 ;-------------  Materials  -----------------------------------------
  CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
  CreateMaterial(1, LoadTexture(1, "wood.jpg"))
  CreateMaterial(2, LoadTexture(2, "nskinrd.jpg"))
 ;--------------  Camera  -----------------------------------------
  #camera =0
  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, 8, 12, 5, #PB_Absolute)
  CameraLookAt(0, 0, 7, 0)
;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 1560, 900, 500)
  AmbientColor(RGB(255,255,255))
;- -------------  World  -------------------------------------------
  WorldGravity(-9)
  SkyBox("desert07.jpg")
  ;----------------- meshes -----------------------------------------

  CreateMatrix(); to make the scaled cube wich have a cylinder in the middle as an axis
  CreateEntity(0, MeshID(0), MaterialID(0), -3, 11.5, 0)
  
  CreateCube(1, 1)
  CreateEntity(1, MeshID(1), MaterialID(1), -9, 11.5, 0)
  ScaleEntity(1, 2.8, 4, 0.2)
  ;---------------- PhysicBody -----------------------------------
  EntityPhysicBody(0, #PB_Entity_ConvexHullBody, 1, 0.1,1)
  EntityPhysicBody(1, #PB_Entity_ConvexHullBody, 1, 0.1, 1)

  ;----------------- CollisionFilter -----------------------------
  SetEntityCollisionFilter(0      , 4 , 3 ) 
  SetEntityCollisionFilter(1      , 4 , 3 ) 

  ;SetEntityCollisionFilter(#Entity, CollisionGroup, CollisionMask)
  CreateCube(#axisCube, 0.5)
  CreateEntity(#axisCube, MeshID(#axisCube), #PB_Material_None , 1.5, 8, 0)
  EntityPhysicBody(#axisCube, #PB_Entity_StaticBody)
 
  ;------------------joints  ---------------------------------------------
 ;HingeJoint(#Joint, EntityID, PivotX, PivotY, PivotZ, AxisX, AxisY, AxisZ, EntityID2, PivotX2, PivotY2, PivotZ2, AxisX2, AxisY2, AxisZ2)

  
  ;SetJointAttribute(0, #PB_HingeJoint_LowerLimit, 0) 
  
  ;joints 0 and 1 just to fix entities 0 and ent 1 together
  HingeJoint(0, EntityID(0),
             0, 0, 0,
             1, 0, 0, 
             EntityID(1),
             0, 0, 0, 
             0, 0, 1)
  
  HingeJoint(1, EntityID(0),
             0, -1, 0,
             1, 0, 0, 
             EntityID(1),
             0, -1, 0, 
             0, 0, 1)
  
  ; the joint between entityID(0) and the entityID(#axisCube)
  HingeJoint(#Joint, EntityID(0),
             0, 3, 0,
             0, 1, 0, 
             EntityID(#axisCube),
             -0.5, 0, 0, 
             1, 0, 0)
 ;----------------- not sleep  ---------------------------------------
  SetEntityAttribute(0, #PB_Entity_LinearSleeping, 0)
  SetEntityAttribute(1, #PB_Entity_LinearSleeping, 0)  
 ;-------------  Loop  ----------------------------------------------

Define.f KeyX, KeyY, MouseX, MouseY
CreateMaterial(#material, LoadTexture(#material, "MRAMOR6X6.jpg"))
CreatePlane(500, 30, 30, 5, 5, 2, 2)
CreateEntity(500, MeshID(500), MaterialID(#material), 0,-2.5,0.0)
EntityPhysicBody(500, #PB_Entity_StaticBody, 1, 0.1, 1)


CreateCube(#stone, 0.2)
  For i = 1 To 2000
  stone(i) = CreateEntity(#PB_Any, MeshID(#stone),#PB_Material_None)
    
Next

CreateEntity(#Brake, MeshID(#axisCube), MaterialID(2) , 0.5, 8, -1.3)
ScaleEntity(#Brake, 1,5,1)
EntityPhysicBody(#Brake, #PB_Entity_BoxBody, 1, 0.1, 1)
CreateEntity(#BrakeHinge, MeshID(#axisCube), MaterialID(3) , 0.5, 10, -1.3)
EntityPhysicBody(#BrakeHinge, #PB_Entity_StaticBody)

HingeJoint(3, EntityID(#Brake),
             0, 2.6, 0,
             0, 0, 1, 
             EntityID(#BrakeHinge),
             0, -0.26, 0, 
             1, 0, 0)

;HideEntity(#Brake,1)

Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate camera and fly in/out
  ExamineKeyboard()
  ;EnableHingeJointAngularMotor(#Joint, Enable, TargetVelocity, MaxMotorImpulse)
    
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf
If KeyboardPushed(#PB_Key_R)
  RotateEntity(0, 0,2,0,#PB_Relative)
  
EndIf 
If KeyboardPushed(#PB_Key_T)
  
  EnableHingeJointAngularMotor(#Joint,1,6,10)
  
EndIf 

  
  If KeyboardPushed(#PB_Key_Space)
      stn+1
      If stn > 1000: stn=1: EndIf
      dropStone(stn)
  EndIf
  If KeyboardReleased(#PB_Key_V)
    
    EnableHingeJointAngularMotor(#Joint,1,0.5,10)
    
  EndIf

  
  RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (0, KeyX, 0, KeyY)
  
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

Procedure dropStone(stn.l)
  
  MoveEntity(stone(stn), -3,15, 1)
  ;RotateEntity(stone(stn), 0, 0,-135)
  EntityPhysicBody(stone(stn),#PB_Entity_BoxBody,1)
    
  
EndProcedure

Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  
  CreateCube(30,1)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(30,0,0,0,  2.8, 4, 0.2, 0,0,0)
  GetMeshData(30,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate , 0, MeshVertexCount(30)-1)
  GetMeshData(30,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(30, 0)-1)
  ArrSize = ArraySize(MeshData())
  
   ;main mesh
   For c=0 To ArrSize
      
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
         
   Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   For i=0 To ArrSizeInd Step 3
     MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
   Next
   
   FreeMesh(30)
   
   ;;;99999999999999999999999999999999999999999999999999999999999999
   
  AddSubMesh(#PB_Mesh_TriangleList)
   
  CreateCylinder(30, 0.2,1)
  TransformMesh(30,0, 0.1, 0,  1,6,1, 0,90,0)
  GetMeshData(30,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(30)-1)
  GetMeshData(30,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(30, 0)-1)
  
  FreeMesh(30)
  ArrSize = ArraySize(MeshData())
  
  For c=0 To ArrSize
      
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
      
   Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   For i=0 To ArrSizeInd Step 3
     MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
   Next    
   
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(0),0)

  
EndProcedure
  
the Gear demo are trying to mimic concave geometry using SetEntityCollisionFilter + Joints
but i wish the addition of dynamic compound shapes:
Bullet Reference Manual wrote:
The btCompoundShape allows to store multiple other btCollisionShapes This allows for moving concave collision objects.
http://purebasic.fr/english/viewtopic.php?f=3&t=55916
http://purebasic.fr/english/viewtopic.php?f=36&t=59062

for more info: look SetEntityCollisionFilter.pb example