Page 1 of 2

EntityCollide problem

Posted: Sun Aug 03, 2014 6:59 pm
by Thorium
I am new to 3D-engines and wanted to experiment a little bit with Ogre in PB.
So my experiment is to create a very simple simulation of a 5 axis CNC machine.

Works great, however i have a problem with entity collision. I simply want to check if the tool, the toolholder (named head in the code) or the pinole collide with the workpiece and highlight the parts that collide in red.

Using EntityCollide to test the pinole against the workpiece works.
Testing the tool or toolholder against the workpiece fails, it allways returns no collision. Is that because tool and toolholder belong to a different node? And if so how do i test them for collision?

Here is the code:
Consider it as a learning project, nothing serious.
You need to change the path in the second line to the examples folder of purebasic, as it uses the GUI stuff from the examples.

Use mouse and arrow keys to move camera.
Use keys X,Y,Z,A,C to choose a axis to move.
Use keys + and - to move the chosen axis.
The workpiece is the small cube inside the machine.

Code: Select all

EnableExplicit

Global GUIPath.s = "Data\GUI"

Enumeration
  #Mesh_Staender1
  #Mesh_Staender2
  #Mesh_Querbalken
  #Mesh_Pynole
  #Mesh_Workpiece
  #Mesh_Head
  #Mesh_Tool
EndEnumeration

Enumeration
  #Entity_Staender1
  #Entity_Staender2
  #Entity_Querbalken
  #Entity_Pynole
  #Entity_Workpiece
  #Entity_Head
  #Entity_Tool
EndEnumeration

Enumeration
  #Node_Machine
  #Node_Head
EndEnumeration

Enumeration
  #AxisX
  #AxisY
  #AxisZ
  #AxisA
  #AxisC
EndEnumeration

#CameraSpeed = 0.5
#AxisSpeed   = 0.5

Enumeration
  #AxisX
  #AxisY
  #AxisZ
  #AxisA
  #AxisC
  #AxisText
  #WireFrameText
  #TransparentText
  #ShadowText
EndEnumeration

Global KeyX.f
Global KeyY.f
Global MouseX.f
Global MouseY.f
Global Quit.i
Global WireFrameActive.i
Global TransparencyActive.i
Global ShadowsActive.i = #True
Global CollisionHappened.i

Global ZeroPosX.f = -4.0
Global ZeroPosY.f =  4.5
Global ZeroPosZ.f =  1.5
Global EndPosX.f  =  4.5
Global EndPosY.f  =  0.5
Global EndPosZ.f  =  0.5
Global AxisPosX.f = ZeroPosX
Global AxisPosY.f = ZeroPosY
Global AxisPosZ.f = ZeroPosZ
Global AxisPosA.f =  0
Global AxisPosC.f =  0

Global ActiveAxis.i

Procedure WndEventHandler()
  
  Protected Event.i
  
  Repeat
    
    Event = WindowEvent()
    
    Select Event
           
      Case #PB_Event_CloseWindow
        Quit = #True
      
    EndSelect

  Until Event = #False
   
EndProcedure

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
Add3DArchive(GUIPath, #PB_3DArchive_FileSystem)

AntialiasingMode(#PB_AntialiasingMode_x4)
OpenWindow(0, 0, 0, 800, 600, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
KeyboardMode(#PB_Keyboard_International)

WorldShadows(#PB_Shadow_Additive)

CreateTexture(0, 32, 32)
StartDrawing(TextureOutput(0))
Box(0, 0, 32, 32, #White)
StopDrawing()
CreateMaterial(0, TextureID(0))
SetMaterialColor(0, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 128))

CreateTexture(1, 32, 32)
StartDrawing(TextureOutput(1))
Box(0, 0, 32, 32, #Red)
StopDrawing()
CreateMaterial(1, TextureID(1))

CreateCube(#Mesh_Staender1, 1.0)
CreateCube(#Mesh_Staender2, 1.0)
CreateCube(#Mesh_Querbalken, 1.0)
CreateCube(#Mesh_Pynole, 1.0)
CreateCube(#Mesh_Workpiece, 0.5)
CreateSphere(#Mesh_Head, 0.25)
CreateCylinder(#Mesh_Tool, 0.05, 0.2)

CreateEntity(#Entity_Staender1, MeshID(#Mesh_Staender1), MaterialID(0), 0, 0, 0)
CreateEntity(#Entity_Staender2, MeshID(#Mesh_Staender2), MaterialID(0), 5, 0, 0)
CreateEntity(#Entity_Querbalken, MeshID(#Mesh_Querbalken), MaterialID(0), 2.5, 1, -4)
CreateEntity(#Entity_Pynole, MeshID(#Mesh_Pynole), MaterialID(0), 4.5, 1.5, -4.5)
CreateEntity(#Entity_Workpiece, MeshID(#Mesh_Workpiece), MaterialID(0), 2, -0.5, -3)
CreateEntity(#Entity_Head, MeshID(#Mesh_Head), MaterialID(0), 0, 0, 0)
CreateEntity(#Entity_Tool, MeshID(#Mesh_Tool), MaterialID(0), 0, -0.3, 0)

ScaleEntity(#Entity_Staender1, 0.5, 2, 10, #PB_Relative)
ScaleEntity(#Entity_Staender2, 0.5, 2, 10, #PB_Relative)
ScaleEntity(#Mesh_Querbalken, 5, 1, 1, #PB_Relative)
ScaleEntity(#Entity_Pynole, 0.5, 2, 0.5, #PB_Relative)

CreateNode(#Node_Head, 0, 0, 0)
AttachNodeObject(#Node_Head, EntityID(#Entity_Head))
AttachNodeObject(#Node_Head, EntityID(#Entity_Tool))

CreateNode(#Node_Machine, 0, 0, 0)
AttachNodeObject(#Node_Machine, EntityID(#Entity_Staender1))
AttachNodeObject(#Node_Machine, EntityID(#Entity_Staender2))
AttachNodeObject(#Node_Machine, EntityID(#Entity_Querbalken))
AttachNodeObject(#Node_Machine, EntityID(#Entity_Pynole))
AttachNodeObject(#Node_Machine, EntityID(#Entity_Workpiece))
AttachNodeObject(#Node_Machine, NodeID(#Node_Head))

EntityPhysicBody(#Entity_Staender1, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Staender2, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Querbalken, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Pynole, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Workpiece, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Head, #PB_Entity_StaticBody)
EntityPhysicBody(#Entity_Tool, #PB_Entity_StaticBody)

EntityRenderMode(#Entity_Staender1, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Staender2, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Querbalken, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Pynole, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Workpiece, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Head, #PB_Entity_CastShadow)
EntityRenderMode(#Entity_Tool, #PB_Entity_CastShadow)

MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)

CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 10, 1, -10, #PB_Absolute)
CameraLookAt(0, 2, 0, 0)
CameraBackColor(0, RGB(19, 34, 49))

CreateLight(0, $FFFFFF, 1560, 900, -500)
AmbientColor($606060)

OpenWindow3D(0, 10, 10, 200, 215, "Info")
TextGadget3D(#AxisX, 5, 5, 190, 15, "")
TextGadget3D(#AxisY, 5, 25, 190, 15, "")
TextGadget3D(#AxisZ, 5, 45, 190, 15, "")
TextGadget3D(#AxisA, 5, 65, 190, 15, "")
TextGadget3D(#AxisC, 5, 85, 190, 15, "")
TextGadget3D(#AxisText, 5, 105, 190, 15, "")
TextGadget3D(#WireFrameText, 5, 125, 190, 15, "")
TextGadget3D(#TransparentText, 5, 145, 190, 15, "")
TextGadget3D(#ShadowText, 5, 165, 190, 15, "")
ShowGUI(128, #False)

;WorldDebug(#PB_World_DebugBody)

Repeat
  
  WndEventHandler()
  
  If ExamineMouse()
    MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
    MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
  EndIf
  
  If ExamineKeyboard()
    
    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
    
    If KeyboardReleased(#PB_Key_W)
      If WireFrameActive = #False
        MaterialShadingMode(0, #PB_Material_Gouraud | #PB_Material_Wireframe)
        WireFrameActive = #True
      Else
        MaterialShadingMode(0, #PB_Material_Gouraud | #PB_Material_Solid)
        WireFrameActive = #False
      EndIf
    EndIf
    
    If KeyboardReleased(#PB_Key_T)
      If TransparencyActive = #False
        MaterialBlendingMode(0, #PB_Material_Color)
        TransparencyActive = #True
      Else
        MaterialBlendingMode(0, #False)
        TransparencyActive = #False
      EndIf
    EndIf
    
    If KeyboardReleased(#PB_Key_S)
      If ShadowsActive = #False
        WorldShadows(#PB_Shadow_Additive)
        ShadowsActive = #True
      Else
        WorldShadows(#PB_Shadow_None)
        ShadowsActive = #False
      EndIf
    EndIf
    
    If KeyboardReleased(#PB_Key_X)
      ActiveAxis = #AxisX
    EndIf
    
    If KeyboardReleased(#PB_Key_Y)
      ActiveAxis = #AxisY
    EndIf
    
    If KeyboardReleased(#PB_Key_Z)
      ActiveAxis = #AxisZ
    EndIf
    
    If KeyboardReleased(#PB_Key_A)
      ActiveAxis = #AxisA
    EndIf
    
    If KeyboardReleased(#PB_Key_C)
      ActiveAxis = #AxisC
    EndIf
    
    If KeyboardPushed(#PB_Key_Add)
      Select ActiveAxis
        
        Case #AxisX
          AxisPosX = AxisPosX + (#AxisSpeed * 0.05)
          If AxisPosX > EndPosX
            AxisPosX = EndPosX
          EndIf
          MoveEntity(#Entity_Querbalken, 2.5, 1, AxisPosX, #PB_Absolute)
          MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
          MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
          
        Case #AxisY
          AxisPosY = AxisPosY - (#AxisSpeed * 0.05)
          If AxisPosY < EndPosY
            AxisPosY = EndPosY
          EndIf
          MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
          MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
          
        Case #AxisZ
          AxisPosZ = AxisPosZ + (#AxisSpeed * 0.05)
          If AxisPosZ > ZeroPosZ
            AxisPosZ = ZeroPosZ
          EndIf
          MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
          MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
          
        Case #AxisA
          AxisPosA = AxisPosA + #AxisSpeed
          If AxisPosA > 90
            AxisPosA = 90
          EndIf
          RotateNode(#Node_Head, 0, AxisPosC, AxisPosA, #PB_Absolute)
          
        Case #AxisC
          AxisPosC = AxisPosC + #AxisSpeed
          If AxisPosC > 180
            AxisPosC = 180
          EndIf
          RotateNode(#Node_Head, 0, AxisPosC, AxisPosA, #PB_Absolute)
          
      EndSelect
    EndIf
    
    If KeyboardPushed(#PB_Key_Subtract)
      Select ActiveAxis
        
        Case #AxisX
          AxisPosX = AxisPosX - (#AxisSpeed * 0.05)
          If AxisPosX < ZeroPosX
            AxisPosX = ZeroPosX
          EndIf
          MoveEntity(#Entity_Querbalken, 2.5, 1, AxisPosX, #PB_Absolute)
          MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
          MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
          
        Case #AxisY
          AxisPosY = AxisPosY + (#AxisSpeed * 0.05)
          If AxisPosY > ZeroPosY
            AxisPosY = ZeroPosY
          EndIf
          MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
          MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
          
        Case #AxisZ
          AxisPosZ = AxisPosZ - (#AxisSpeed * 0.05)
          If AxisPosZ < EndPosZ
            AxisPosZ = EndPosZ
          EndIf
          MoveEntity(#Entity_Pynole, AxisPosY, AxisPosZ, AxisPosX - 0.5, #PB_Absolute)
          MoveNode(#Node_Head, AxisPosY, AxisPosZ -1 , AxisPosX -0.5, #PB_Absolute)
          
        Case #AxisA
          AxisPosA = AxisPosA - #AxisSpeed
          If AxisPosA < -90
            AxisPosA = -90
          EndIf
          RotateNode(#Node_Head, 0, AxisPosC, AxisPosA, #PB_Absolute)
          
        Case #AxisC
          AxisPosC = AxisPosC - #AxisSpeed
          If AxisPosC < 0
            AxisPosC = 0
          EndIf
          RotateNode(#Node_Head, 0, AxisPosC, AxisPosA, #PB_Absolute)
          
      EndSelect
    EndIf
    
  EndIf
  
  RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera(0, KeyX, 0, KeyY)
  
  CollisionHappened = #False 
  
  If EntityCollide(#Entity_Tool, #Entity_Workpiece) = #False
    SetEntityMaterial(#Entity_Tool, MaterialID(0))
  Else
    CollisionHappened = #True
    SetEntityMaterial(#Entity_Tool, MaterialID(1))
  EndIf
  
  If EntityCollide(#Entity_Head, #Entity_Workpiece) = #False
    SetEntityMaterial(#Entity_Head, MaterialID(0))
  Else
    CollisionHappened = #True
    SetEntityMaterial(#Entity_Head, MaterialID(1))
  EndIf
  
  If EntityCollide(#Entity_Pynole, #Entity_Workpiece) = #False
    SetEntityMaterial(#Entity_Pynole, MaterialID(0))
  Else
    CollisionHappened = #True
    SetEntityMaterial(#Entity_Pynole, MaterialID(1))
  EndIf
  
  If CollisionHappened = #True
    SetEntityMaterial(#Entity_Workpiece, MaterialID(1))
  Else
    SetEntityMaterial(#Entity_Workpiece, MaterialID(0))
  EndIf
  
  SetGadgetText3D(#AxisX, "X-Axis: " + StrF((AxisPosX - ZeroPosX) * 1000, 3))
  SetGadgetText3D(#AxisY, "Y-Axis: " + StrF(-1*((AxisPosY - ZeroPosY) * 1000), 3))
  SetGadgetText3D(#AxisZ, "Z-Axis: " + StrF((AxisPosZ - ZeroPosZ) * 1000, 3))
  SetGadgetText3D(#AxisA, "A-Axis: " + StrF(AxisPosA, 3))
  SetGadgetText3D(#AxisC, "C-Axis: " + StrF(AxisPosC, 3))
  Select ActiveAxis
    Case #AxisX
      SetGadgetText3D(#AxisText, "Chosen Axis: X")
    Case #AxisY
      SetGadgetText3D(#AxisText, "Chosen Axis: Y")
    Case #AxisZ
      SetGadgetText3D(#AxisText, "Chosen Axis: Z")
    Case #AxisA
      SetGadgetText3D(#AxisText, "Chosen Axis: A")
    Case #AxisC
      SetGadgetText3D(#AxisText, "Chosen Axis: C")
  EndSelect
  If WireFrameActive = #True
    SetGadgetText3D(#WireFrameText, "Wireframe (W): yes")
  Else
    SetGadgetText3D(#WireFrameText, "Wireframe (W): no")
  EndIf
  If TransparencyActive = #True
    SetGadgetText3D(#TransparentText, "Transparency (T): yes")
  Else
    SetGadgetText3D(#TransparentText, "Transparency (T): no")
  EndIf
  If ShadowsActive = #True
    SetGadgetText3D(#ShadowText, "Shadows (S): yes")
  Else
    SetGadgetText3D(#ShadowText, "Shadows (S): no")
  EndIf
  
  RenderWorld()
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape) Or Quit = #True

End

Re: EntityCollide problem

Posted: Mon Aug 04, 2014 8:29 am
by Bananenfreak
Hey,
cool, you´re using EnableExplicit!
Coming to your Problem: I´m confused, because I thought entities marked with #PB_Entity_StaticBody can´t throw out a collisionevent.
But you´re using EntityCollide() which should be different.

In this code, spheres also work:

Code: Select all

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;>>                             >>
;>>  Name: EntityCollide()         >>
;>>                             >>
;>>  Author: Bananenfreak  >>
;>>                             >>
;>>  Date: 19.04.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, ent1 = 1, ent2 = 2
#kam_0 = 0
#window = 0
#plane = 0
#planent = 0
#mesh1 = 1
#mesh2 = 2

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, "3D-Template", #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)
  CreateCube(#plane, 100)
  TransformMesh(#plane, 0, 0, 0, 1, 0.005, 1, 0, 0, 0)
  boden = GetScriptMaterial(#PB_Any, "Scene/GroundBlend")
  CreateEntity(#planent, MeshID(#plane), MaterialID(boden), 0, 0, 0)
  EntityPhysicBody(#planent, #PB_Entity_StaticBody)
  
  CreateSphere(#mesh1, 1, 16, 16)
  CreateCube(#mesh2, 1)
  CreateEntity(ent1, MeshID(#mesh1), 0, -10, 10, 0)
  CreateEntity(ent2, MeshID(#mesh2), 0, 10, 10, 0)
  EntityPhysicBody(ent1, #PB_Entity_StaticBody)
  EntityPhysicBody(ent2, #PB_Entity_StaticBody)
  
  ;-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))
  
  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_W)
        MoveEntity(ent1, -10, 0, 0, #PB_Absolute)
        MoveEntity(ent2, 10, 0, 0, #PB_Absolute)
      EndIf
      
    EndIf
    
    If EntityCollide(#planent, ent1)
      Debug "Entity 1"
    EndIf
    If EntityCollide(#planent, ent2)
      Debug "Entity 2"
    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
So, I think there is something wrong with Nodes...

So test this one:
Without pressing "W", Engine detectes Collision with Ent 1 and 2. But there is no Oo

Code: Select all

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;>>                             >>
;>>  Name: EntityCollide()      >>
;>>                             >>
;>>  Author: Bananenfreak       >>
;>>                             >>
;>>  Date: 19.04.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, ent1 = 1, ent2 = 2, node
#kam_0 = 0
#window = 0
#plane = 0
#planent = 0
#mesh1 = 1
#mesh2 = 2

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, "3D-Template", #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)
  CreateCube(#plane, 100)
  TransformMesh(#plane, 0, 0, 0, 1, 0.005, 1, 0, 0, 0)
  boden = GetScriptMaterial(#PB_Any, "Scene/GroundBlend")
  CreateEntity(#planent, MeshID(#plane), MaterialID(boden), 0, 0, 0)
  EntityPhysicBody(#planent, #PB_Entity_StaticBody)
  
  CreateSphere(#mesh1, 1, 16, 16)
  CreateCube(#mesh2, 1)
  CreateEntity(ent1, MeshID(#mesh1), 0, -10, 0, 0)
  CreateEntity(ent2, MeshID(#mesh2), 0, 10, 0, 0)
  EntityPhysicBody(ent1, #PB_Entity_StaticBody)
  EntityPhysicBody(ent2, #PB_Entity_StaticBody)
  node = CreateNode(#PB_Any, 0, 10, 0)
  AttachNodeObject(node, EntityID(ent1))
  AttachNodeObject(node, EntityID(ent2))
  
  ;-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))
  
  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_W)
;         MoveEntity(ent1, -10, 0, 0, #PB_Absolute)
;         MoveEntity(ent2, 10, 0, 0, #PB_Absolute)
        MoveNode(node, 0, 0, 0, #PB_Absolute)
      EndIf
      
    EndIf
    
    If EntityCollide(#planent, ent1)
      Debug "Entity 1"
    EndIf
    If EntityCollide(#planent, ent2)
      Debug "Entity 2"
    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


Re: EntityCollide problem

Posted: Mon Aug 04, 2014 8:55 am
by applePi
beautiful example with a new ideas.
but i can't follow its details and intricacies , what i understand is that the entities can't register collision if it is moved by following a node move, the following simpler example show this, when the ball collide with the wall, the wall will not change its material due to the collide, while in the second example it will change the wall material when colliding with it
press space to move the ball until it reaches the wall
does not show collision, with MoveNode

Code: Select all

#CameraSpeed = 1
#wall = 223
#wall2 = 225
#node  = 230

Define.f KeyX, KeyY, MouseX, MouseY
Global z.f
Global disp.f = 0.5

If InitEngine3D()
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  ExamineDesktops()
  DesktopW=DesktopWidth(0)
  DesktopH=DesktopHeight(0)
  If OpenWindow(0, 0, 0, DesktopW, DesktopH, "press space to move the ball")
    
  If OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
    
    Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Textures",#PB_3DArchive_FileSystem)

  
  Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Scripts",#PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Packs/desert.zip", #PB_3DArchive_Zip)
  Parse3DScripts()
  
     
    WorldShadows(#PB_Shadow_Modulative, -1, RGB(127, 127, 127))
    
    ;Materials
    ;
    CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
    GetScriptMaterial(1, "SphereMap/SphereMappedRustySteel")
    CreateMaterial(2, LoadTexture(2, "Dirt.jpg"))
    GetScriptMaterial(3, "Scene/GroundBlend")
    
    ; Ground
    ;
    ;CreatePlane(3, 500, 500, 5, 5, 5, 5)
    CreateCube(3,1)
    CreateEntity(3,MeshID(3),MaterialID(3), 0, 0, 0)
    ScaleEntity(3,500,10,500)
    EntityRenderMode(3, 0) 
    EntityPhysicBody(3, #PB_Entity_BoxBody, 0, 1, 1) ; note it has weight 0
    
    CreateEntity(#wall,MeshID(3),MaterialID(3), 120, 20, 120)
    ScaleEntity(#wall,30,10,50)
    RotateEntity(#wall,-45,0,90)
    EntityPhysicBody(#wall, #PB_Entity_StaticBody, 1, 1, 1) ; note it has weight 0
    CopyEntity(#wall, #wall2)
    MoveEntity(#wall2, -60, 20, -60)
    RotateEntity(#wall2,-45,0,90)
    EntityPhysicBody(#wall2, #PB_Entity_StaticBody, 1, 1, 1) ; note it has weight 0
    ; Objects
    ;
    CreateSphere(2, 6, 6, 6)
    
    ball = CreateEntity(#PB_Any, MeshID(2), MaterialID(1),0,12,0)
    EntityPhysicBody(ball, #PB_Entity_SphereBody, 1.0, 0.3, 0.5)
    CreateNode(#node, 0, 0, 0)
    AttachNodeObject(#node, EntityID(ball))
        
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 254, 43, -137, #PB_Absolute)
    CameraLookAt(0, 0, 20,  0)
    RotateCamera(0, 0, 10,0,#PB_Relative)
    ; Skybox
    ;
    SkyBox("desert07.jpg")
    
    ; Light
    ;
    CreateLight(0, RGB(255, 255, 255), 100, 800, -500)
    AmbientColor(RGB(20, 20, 20))
    ;WorldDebug(#PB_World_DebugBody  )
    
    x.f = 0:z.f=0 
         
    Repeat
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
       If KeyboardPushed(#PB_Key_Space )
                    
           z.f+disp
           x.f+disp
           ApplyEntityImpulse(ball, 0, 0.01, 0)
           ;MoveEntity(ball,disp,0,disp)
           MoveNode(#node, disp,0,disp)
          
           If EntityCollide(ball, #wall)
           
             disp = -disp; to reverse the ball move direction
             
             SetEntityMaterial(#wall, MaterialID(1))
           ElseIf EntityCollide(ball, #wall2)
             disp = -disp
             SetEntityMaterial(#wall2, MaterialID(1))
           EndIf
 

         
       EndIf
       If KeyboardReleased(#PB_Key_C)
         Debug CameraX(0):Debug CameraY(0):Debug CameraZ(0)
       EndIf
               
        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
      
      
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)
      
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
EndIf
End
works, with MoveEntity

Code: Select all

#CameraSpeed = 1
#wall = 223
#wall2 = 225

Define.f KeyX, KeyY, MouseX, MouseY
Global z.f
Global disp.f = 20

If InitEngine3D()
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  ExamineDesktops()
  DesktopW=DesktopWidth(0)
  DesktopH=DesktopHeight(0)
  If OpenWindow(0, 0, 0, DesktopW, DesktopH, "press space to move the ball")
    
  If OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
    
    Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Textures",#PB_3DArchive_FileSystem)

  
  Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Scripts",#PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Packs/desert.zip", #PB_3DArchive_Zip)
  Parse3DScripts()
  
     
    WorldShadows(#PB_Shadow_Modulative, -1, RGB(127, 127, 127))
    
    ;Materials
    ;
    CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
    GetScriptMaterial(1, "SphereMap/SphereMappedRustySteel")
    CreateMaterial(2, LoadTexture(2, "Dirt.jpg"))
    GetScriptMaterial(3, "Scene/GroundBlend")
    
    ; Ground
    ;
    ;CreatePlane(3, 500, 500, 5, 5, 5, 5)
    CreateCube(3,1)
    CreateEntity(3,MeshID(3),MaterialID(3), 0, 0, 0)
    ScaleEntity(3,500,10,500)
    EntityRenderMode(3, 0) 
    EntityPhysicBody(3, #PB_Entity_BoxBody, 0, 1, 1) ; note it has weight 0
    
    CreateEntity(#wall,MeshID(3),MaterialID(3), 120, 20, 120)
    ScaleEntity(#wall,30,10,50)
    RotateEntity(#wall,-45,0,90)
    EntityPhysicBody(#wall, #PB_Entity_StaticBody, 1, 1, 1) ; note it has weight 0
    CopyEntity(#wall, #wall2)
    MoveEntity(#wall2, -60, 20, -60)
    RotateEntity(#wall2,-45,0,90)
    EntityPhysicBody(#wall2, #PB_Entity_StaticBody, 1, 1, 1) ; note it has weight 0
    ; Objects
    ;
    CreateSphere(2, 6, 6, 6)
    
    ball = CreateEntity(#PB_Any, MeshID(2), MaterialID(1),0,12,0)
    EntityPhysicBody(ball, #PB_Entity_SphereBody, 1.0, 0.3, 0.5)

       
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 254, 43, -137, #PB_Absolute)
    CameraLookAt(0, 0, 20,  0)
    RotateCamera(0, 0, 10,0,#PB_Relative)
    ; Skybox
    ;
    SkyBox("desert07.jpg")
    
    ; Light
    ;
    CreateLight(0, RGB(255, 255, 255), 100, 800, -500)
    AmbientColor(RGB(20, 20, 20))
    ;WorldDebug(#PB_World_DebugBody  )
    
    x.f = 0:z.f=0 
         
    Repeat
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
       If KeyboardPushed(#PB_Key_Space )
                    
           z.f+disp
           x.f+disp
           ApplyEntityImpulse(ball, 0, 0.01, 0)
           MoveEntity(ball,disp,0,disp)
           
          
           If EntityCollide(ball, #wall)
           
             disp = -disp; to reverse the ball move direction
             
             SetEntityMaterial(#wall, MaterialID(1))
           ElseIf EntityCollide(ball, #wall2)
             disp = -disp
             SetEntityMaterial(#wall2, MaterialID(1))
           EndIf
 

         
       EndIf
       If KeyboardReleased(#PB_Key_C)
         Debug CameraX(0):Debug CameraY(0):Debug CameraZ(0)
       EndIf
               
        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
      
      
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)
      
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
EndIf
End

Re: EntityCollide problem

Posted: Mon Aug 04, 2014 11:01 am
by applePi
i have found an alternative solution , is to use rayCast function which returns the entity passing between point 1 and point 2 whether it is moved by following a node or not . so in my previous example we position a ray near the wall and when the ball approach that wall the rayCast will register the ball and then changing the wall material and then reversing the ball move direction .
regarding RayCollide i was not successful , rayCast works okay.
for the cnc project , try to not using the nodes, else use the rayCast function
PS: any time press 'C" to get the x,y,z coordinates of the ball

Code: Select all

#CameraSpeed = 1
#wall = 223
#wall2 = 225
#node  = 230

Define.f KeyX, KeyY, MouseX, MouseY
Global z.f
Global disp.f = 0.5

If InitEngine3D()
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  ExamineDesktops()
  DesktopW=DesktopWidth(0)
  DesktopH=DesktopHeight(0)
  If OpenWindow(0, 0, 0, DesktopW, DesktopH, "press space to move the ball")
    
  If OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
    
    Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Textures",#PB_3DArchive_FileSystem)

  
  Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Scripts",#PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Packs/desert.zip", #PB_3DArchive_Zip)
  Parse3DScripts()
  
     
    WorldShadows(#PB_Shadow_Modulative, -1, RGB(127, 127, 127))
    
    ;Materials
    ;
    CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
    GetScriptMaterial(1, "SphereMap/SphereMappedRustySteel")
    CreateMaterial(2, LoadTexture(2, "Dirt.jpg"))
    GetScriptMaterial(3, "Scene/GroundBlend")
    
    ; Ground
    ;
    ;CreatePlane(3, 500, 500, 5, 5, 5, 5)
    CreateCube(3,1)
    CreateEntity(3,MeshID(3),MaterialID(3), 0, 0, 0)
    ScaleEntity(3,500,10,500)
    EntityRenderMode(3, 0) 
    EntityPhysicBody(3, #PB_Entity_BoxBody, 0, 1, 1) ; note it has weight 0
    
    CreateEntity(#wall,MeshID(3),MaterialID(3), 120, 20, 120)
    ScaleEntity(#wall,30,10,50)
    RotateEntity(#wall,-45,0,90)
    EntityPhysicBody(#wall, #PB_Entity_StaticBody, 1, 1, 1) ; note it has weight 0
    CopyEntity(#wall, #wall2)
    MoveEntity(#wall2, -60, 20, -60)
    RotateEntity(#wall2,-45,0,90)
    EntityPhysicBody(#wall2, #PB_Entity_StaticBody, 1, 1, 1) ; note it has weight 0
    ; Objects
    ;
    CreateSphere(2, 6, 6, 6)
    
    ball = CreateEntity(#PB_Any, MeshID(2), MaterialID(1),0,12,0)
    EntityPhysicBody(ball, #PB_Entity_SphereBody, 1.0, 0.3, 0.5)
    CreateNode(#node, 0, 1, 0)
    AttachNodeObject(#node, EntityID(ball))
        
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 254, 43, -137, #PB_Absolute)
    CameraLookAt(0, 0, 20,  0)
    RotateCamera(0, 0, 10,0,#PB_Relative)
    ; Skybox
    ;
    SkyBox("desert07.jpg")
    
    ; Light
    ;
    CreateLight(0, RGB(255, 255, 255), 100, 800, -500)
    AmbientColor(RGB(20, 20, 20))
    ;WorldDebug(#PB_World_DebugBody  )
    
    x.f = 0:z.f=0 
    EnableWorldPhysics(1)
    checked = 1 
    Repeat
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
       If KeyboardPushed(#PB_Key_Space )
                    
           z.f+disp
           x.f+disp
           ApplyEntityImpulse(ball, 0, 0.1, 0)
           

           ;MoveEntity(ball,disp,0,disp)
           MoveNode(#node, disp,0,disp)
           DisableEntityBody(ball, 0)
          
           ;If EntityCollide(ball, #wall)
           ;re = RayCollide(110, 12, 111, 110, 12, 111)
           
           re = RayCast(110, 12, 111, 110, 12, 111, -1)
           
           ;re = RayCollide(120, 20, 120, 120, 20, 100)
           ;Debug re
           If checked
           If re = ball
           
             disp = -disp; to reverse the ball move direction
             
             SetEntityMaterial(#wall, MaterialID(1))
             checked ! 1
           EndIf
           EndIf
 

         
       EndIf
       If KeyboardReleased(#PB_Key_C)
         ;Debug CameraX(0):Debug CameraY(0):Debug CameraZ(0)
         Debug EntityX(ball):Debug EntityY(ball):Debug EntityZ(ball)
       EndIf
               
        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
      
      
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)
      
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
EndIf
End

Re: EntityCollide problem

Posted: Tue Aug 05, 2014 7:10 am
by Bananenfreak
@Thorium:
I forgot this: Really cool program! :)

@applePi:
An alternative solution can´t be an real solution. I don´t know exactly where the issue is, but there is a Problem with nodes...

Re: EntityCollide problem

Posted: Tue Aug 05, 2014 8:37 pm
by Thorium
Bananenfreak wrote:I´m confused, because I thought entities marked with #PB_Entity_StaticBody can´t throw out a collisionevent.
But you´re using EntityCollide() which should be different.
Using another body type doesnt work in my code. It was the first thing i tried to change. I am sure the nodes are the problem. Collision detection inside the node works. Testing the head against the tool does work, they belong to the same node. Testing the pinole against the workpiese does work, they belong to the same node. Testing the tool or head against the workpiece does not work, they belong to different nodes.

So the solution would be to write my own node system.
But is it a bug? Should we post this as a bug report? Because, what good is a node system if you cant test for collisions?

Re: EntityCollide problem

Posted: Tue Aug 05, 2014 10:12 pm
by Samuel
I'm sure Fred and Comtois have a better understanding then I do, but I'm guessing this isn't a bug.

In OGRE we start off by creating an entity, but that entity is not part of the scene that we will visually see until it is assigned to a node.
So, we create a node that represents our world. I'm assuming Purebasic creates this "world node" by default.

When we create our own separate nodes. We are in a way creating more worlds that are also being rendered onto our screen.

Now in order to give an OGRE entity physics we need to create another entity that represents its bounds.
This physics body can be in the shape of a cube, sphere, or even as a similar shape as the original OGRE entity.

We then assign that physics body to the scene node that the OGRE entity belongs too.

Here lies the problem. Since your physics bodies are in separate worlds they can not collide with each other.
The only way around this is to put all your entities into the same scene node or just keep them in the one Purebasic creates by default.

Now this is just my understanding of how OGRE and bullet work. I could very well be wrong on some of this.
When I get some free time I think I'll look into this a bit more.

Re: EntityCollide problem

Posted: Wed Aug 06, 2014 8:24 am
by DK_PETER
EDIT: Changed a bit to code.
Added entitycollide and mouseraycast

Press key 1 to attach
Press key 2 to detach
and use mouse

Code: Select all


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

Structure NodeAndObjects
  spid.i
  spms.i
  plid.i
  plms.i
  nod.i
  attached.i
EndStructure

Structure NodeAndObject
  id.i
  ms.i
  tx.i
  nod.i
EndStructure

Global nd.NodeAndObjects, n2.NodeAndObject
Global cam.i, lgt.i, sp.i


Declare.i MakeMaterial()
Declare.i MakeNodeGroup()
Declare.i MakeGroup()
Declare.i DetachGroup()
Declare.i MakeSecondNodeGroup()
Declare.i Makecursor()

Procedure.i MakeMaterial()
  Protected tx.i, mt.i
  tx = CreateTexture(#PB_Any,129,128)
  StartDrawing(TextureOutput(tx))
  DrawingMode(#PB_2DDrawing_Gradient)
  FrontColor(Random(16777215,3355443))
  BackColor(Random(16777215,3355443))
  BoxedGradient(0, 0, 128, 128)
  Box(0, 0, 128, 128)
  StopDrawing()
  mt = CreateMaterial(#PB_Any, TextureID(tx))
  ProcedureReturn mt
EndProcedure

Procedure.i MakeNodeGroup()
  If nd\attached > 0 ;Already active
    ProcedureReturn 0
  EndIf
  DisableEntityBody(nd\spid,#True)
  DisableEntityBody(nd\plid,#True)
  DisableEntityBody(n2\id, #True)
  AttachNodeObject(nd\nod, EntityID(nd\plid))
  AttachNodeObject(nd\nod, EntityID(nd\spid))
  nd\attached = 1
  MoveEntity(nd\spid, 0, 9, -15,#PB_Absolute)
 
  AttachNodeObject(n2\nod, EntityID(n2\id))
  MoveEntity(n2\id, 0, 19, -16,#PB_Absolute)
 
  DisableEntityBody(nd\spid,#False)
  DisableEntityBody(nd\plid,#False)
  DisableEntityBody(n2\id, #False)
EndProcedure

Procedure.i MakeGroup()
  Protected mat.i
  nd\plms = CreatePlane(#PB_Any, 100, 100, 2, 2, 1, 1)
  nd\plid = CreateEntity(#PB_Any, MeshID(nd\plms),MaterialID(MakeMaterial()), 0, 0, 0, 1)
  EntityPhysicBody(nd\plid , #PB_Entity_StaticBody, 1)
  nd\spms = CreateSphere(#PB_Any, 1, 20, 10)
  mat = MakeMaterial()
  ScaleMaterial(mat, 0.1, 0.1)
  nd\spid = CreateEntity(#PB_Any, MeshID(nd\spms),MaterialID(mat), 0, 9, -15, 1)
  EntityPhysicBody(nd\spid, #PB_Entity_SphereBody, 0.5, 2.8, 0.42)
EndProcedure 

Procedure.i DetachGroup()
  If nd\attached = 0
    ProcedureReturn 0
  EndIf
  DetachNodeObject(nd\nod, EntityID(nd\plid))
  DetachNodeObject(nd\nod, EntityID(nd\spid))
  DetachNodeObject(n2\nod, EntityID(n2\id))
  nd\attached = 0
  MoveEntity(nd\spid, 0, 9, -15, #PB_Absolute)
  MoveEntity(n2\id, 0, 19, -16, #PB_Absolute)
EndProcedure

Procedure.i MakeSecondNodeGroup()
  Protected mat.i
  n2\ms = CreateSphere(#PB_Any, 1, 20, 10)
  mat = MakeMaterial()
  ScaleMaterial(mat, 0.1, 0.1)
  n2\id = CreateEntity(#PB_Any, MeshID(n2\ms),MaterialID(mat), 0, 19, -16, 1)
  EntityPhysicBody(n2\id, #PB_Entity_SphereBody, 0.4, 0.8, 0.62)
EndProcedure

Procedure.i Makecursor()
  sp = CreateSprite(#PB_Any, 10, 10)
  StartDrawing(SpriteOutput(sp))
  DrawingMode(#PB_2DDrawing_Outlined)
  Box(0, 0, 10, 10, $FFFF6E)
  StopDrawing()
  TransparentSpriteColor(sp,0)
EndProcedure


OpenWindow(0, 0, 0, 1024, 768, "Node test", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768, 0, 0, 0, #PB_Screen_SmartSynchronization)
cam = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(cam, 0, 5, 0)
lgt = CreateLight(#PB_Any, $AAFFAA, 0, 100, -200, #PB_Light_Directional)
nd\nod = CreateNode(#PB_Any, 0, 0, 0)
n2\nod = CreateNode(#PB_Any, 0, 0, 0)

MakeGroup()
MakeSecondNodeGroup()
Makecursor()

Repeat
 
 
  Repeat:ev=WindowEvent():Until ev=0
 
  ExamineKeyboard()
 
  If KeyboardReleased(#PB_Key_1) ;Attach to node
    MakeNodeGroup()
  EndIf
 
  If KeyboardPushed(#PB_Key_2)  ;Detach From node
    DetachGroup()
  EndIf
  
  If EntityCollide(nd\spid, n2\id)
    Debug "Hit"
  EndIf
  
  ExamineMouse()

  RenderWorld()
  
  DisplayTransparentSprite(sp, MouseX(), MouseY())
  
  ret = MouseRayCast(cam, MouseX(), MouseY(),1)
  If ret <> -1
    Debug ret
  EndIf
  
  FlipBuffers()
   
Until KeyboardPushed(#PB_Key_Escape)

Re: EntityCollide problem

Posted: Wed Aug 06, 2014 9:36 am
by Bananenfreak
@Samuel:
Just try my second code. First code is without nodes, but second one is with a node.
In my second code, EntityCollide() detects a collision with the ground, but there is a gap with the length of 10!

Re: EntityCollide problem

Posted: Thu Aug 07, 2014 7:43 pm
by Samuel
@Bananenfreak
You're assigning a static body to a node in a different location. They are called static bodies for a reason.
They should not be moved or animated once given a static status.

Even if Purebasic allows the entities to move. According to Bullet it will not recognize their new location.

This is why your example is reporting collisions. Your static entities original location was within the plane.
http://www.bulletphysics.org/mediawiki-1.5.8/index.php/MotionStates wrote: Static objects don't move so there is no need to communicate movement. They don't need a motion state.
Here's some code, using a node with static bodies, that's based on your example.
In this case the entities are assigned to a node before they are set as a static body.

Code: Select all

Enumeration
  #Window
  #Node
  #Plane
  #Cube
  #Sphere
  #Camera
  #Light
  #Material
EndEnumeration

Define.f MouseX, MouseY
Define.f KeyX, KeyY

ExamineDesktops()
DeskW = DesktopWidth(0)
DeskH = DesktopHeight(0)

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
 
  OpenWindow(#Window, 0, 0, DeskW, DeskH, "Node physics", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(#Window), 0, 0, DeskW, DeskH, 0, 0, 0)
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  GetScriptMaterial(#Material, "Scene/GroundBlend")
  
  CreatePlane (#Plane , 100, 100, 1, 1, 10, 10)
  CreateSphere(#Sphere, 1, 16, 16)
  CreateCube  (#Cube  , 2)
  
  CreateEntity(#Plane , MeshID(#Plane) , MaterialID(#Material), 0, 0, 0) 
  CreateEntity(#Sphere, MeshID(#Sphere), 0, -10, 0, 0)
  CreateEntity(#Cube  , MeshID(#Cube)  , 0,  10, 0, 0)
  
  EntityRenderMode(#Plane, 0)
  
  CreateNode      (#Node, 0, 10, 0)
  AttachNodeObject(#Node, EntityID(#Sphere))
  AttachNodeObject(#Node, EntityID(#Cube))
  
  EntityPhysicBody(#Sphere, #PB_Entity_StaticBody)
  EntityPhysicBody(#Cube  , #PB_Entity_StaticBody)
  EntityPhysicBody(#Plane , #PB_Entity_StaticBody)
  
  CreateCamera   (#Camera, 0, 0, 100, 100)
  MoveCamera     (#Camera, 0, 20, 100, #PB_Absolute)
  CameraLookAt   (#Camera, 0, 0, 0)
  CameraBackColor(#Camera, RGB(0,0,0))
  
  CreateLight(#Light, RGB(255,255,255), 50, 100, 50)
  
  AmbientColor(RGB(120,120,120))
  
  WorldShadows(#PB_Shadow_TextureAdditive, 6000, RGB(120,120,120), 4096)
  
  WorldDebug(#PB_World_DebugBody)
  
  Repeat
    Event = WaitWindowEvent()
   
      If ExamineMouse()
        
        MouseX = -MouseDeltaX() / 10
        MouseY = -MouseDeltaY() / 10
        
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -0.4
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 0.4
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -0.4
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 0.4
        Else
          KeyY = 0
        EndIf
        
      EndIf      
   
    If EntityCollide(#Plane, #Sphere)
      Debug "The sphere has collided with the plane!"
    EndIf
    If EntityCollide(#Plane, #Cube)
      Debug "The cube has collided with the plane!"
    EndIf
         
    RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
    MoveCamera  (#Camera, KeyX, 0, KeyY)
      
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape)
 
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Re: EntityCollide problem

Posted: Thu Aug 07, 2014 8:35 pm
by Bananenfreak
@Thorium:
Assuming to Samuel, you can't do that. So you have to use other physicbodies for moveable parts.
You could design a part of your CNC-machine (Spindel, Futter und Antrieb) in a 3D-Designer (Blender, Wings3D,...) as one object and/or attach them to other entities with joints (conejoint and angles to 0, so it can't move). But there's a gap between those entities with length of 0.04WU (I think so).
I hope Comtois "fixes" this with new version :-)

Re: EntityCollide problem

Posted: Thu Aug 07, 2014 9:16 pm
by Thorium
Bananenfreak wrote:@Thorium:
Assuming to Samuel, you can't do that. So you have to use other physicbodies for moveable parts.
No, as i wrote that was one of the first things i tried. It makes no difference to use another body type.
I am going to write my own node system. I think the possibility to group entitys together for movement and rotation is a basic task and needs to be there.

Re: EntityCollide problem

Posted: Fri Aug 08, 2014 1:35 am
by Samuel
I found a bug with nodes and physics or it's at least a limitation which needs to be considered when using both things together.

This means portions of my previous statements are most likely incorrect. I guess I didn't know what I was talking about before.:oops:

I'll post an example with the problem in a little while.

Re: EntityCollide problem

Posted: Fri Aug 08, 2014 7:24 am
by Samuel
Here's the example. This bug may have been reported in the past, but it's the first I've seen of it.
There also may be more to it, but this is what I have so far.

In the example you'll see a sphere and a cube both fall towards a plane, but they will come in contact with it 10 units above the plane.

This is the exact height that the node, which had the sphere and cube attached to, was set at.

@Thorium
Your cnc example seems to work, but it is just having the same problem/bug as my example shows.
Your tool entity is set to (0, -0.3, 0), but the node is (0, 0, 0). So your tool does not come in contact with the workpiece until it sinks at least 0.3 units into it.

Code: Select all

Enumeration
  #Window
  #Node
  #Plane
  #Cube
  #Sphere
  #Camera
  #Light
  #Material
  #Line
EndEnumeration

Define.f MouseX, MouseY
Define.f KeyX, KeyY

ExamineDesktops()
DeskW = DesktopWidth(0)
DeskH = DesktopHeight(0)

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
 
  OpenWindow(#Window, 0, 0, DeskW, DeskH, "Node physics", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(#Window), 0, 0, DeskW, DeskH, 0, 0, 0)
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  GetScriptMaterial(#Material, "Scene/GroundBlend")
  
  CreatePlane (#Plane , 100, 100, 1, 1, 10, 10)
  CreateSphere(#Sphere, 1, 16, 16)
  CreateCube  (#Cube  , 2)
  
  CreateEntity(#Plane , MeshID(#Plane) , MaterialID(#Material), 0, 0, 0) 
  CreateEntity(#Sphere, MeshID(#Sphere), 0, -10, 0, 0)
  CreateEntity(#Cube  , MeshID(#Cube)  , 0,  10, 0, 0)
  
  CreateLine3D(#Line, 0, 0, 0, RGB(255,0,0), 0, 10, 0, RGB(255,0,0))
  
  EntityRenderMode(#Plane, 0)
  
  CreateNode      (#Node, 0, 10, 0)
  AttachNodeObject(#Node, EntityID(#Sphere))
  AttachNodeObject(#Node, EntityID(#Cube))
  
  EntityPhysicBody(#Sphere, #PB_Entity_BoxBody)
  EntityPhysicBody(#Cube  , #PB_Entity_BoxBody)
  EntityPhysicBody(#Plane , #PB_Entity_StaticBody)
  
  CreateCamera   (#Camera, 0, 0, 100, 100)
  MoveCamera     (#Camera, 0, 20, 100, #PB_Absolute)
  CameraLookAt   (#Camera, 0, 0, 0)
  CameraBackColor(#Camera, RGB(0,0,0))
  
  CreateLight(#Light, RGB(255,255,255), 50, 100, 50)
  
  AmbientColor(RGB(120,120,120))
  
  WorldShadows(#PB_Shadow_TextureAdditive, 6000, RGB(120,120,120), 4096)
  
  WorldDebug(#PB_World_DebugBody)
  
  Repeat
    Event = WaitWindowEvent()
   
      If ExamineMouse()
        
        MouseX = -MouseDeltaX() / 10
        MouseY = -MouseDeltaY() / 10
        
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -0.4
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 0.4
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -0.4
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 0.4
        Else
          KeyY = 0
        EndIf
        
        If KeyboardReleased(#PB_Key_W)
          
          MoveEntity(#Sphere, 0, -10, 0, #PB_Absolute)
          
        EndIf
          
      EndIf      
   
    If EntityCollide(#Plane, #Sphere)
      Debug "The sphere has collided with the plane!"
    EndIf
    If EntityCollide(#Plane, #Cube)
      Debug "The cube has collided with the plane!"
    EndIf
         
    RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
    MoveCamera  (#Camera, KeyX, 0, KeyY)
      
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape)
 
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Re: EntityCollide problem

Posted: Fri Aug 08, 2014 8:46 am
by Bananenfreak
So, to follow you, Samuel, my second code does show this bug, or not?