Page 1 of 1

3D Attach function change position of the attached

Posted: Sat Mar 16, 2019 9:26 pm
by Psychophanta

Code: Select all

InitEngine3D()
InitSprite():InitKeyboard():InitMouse()
OpenWindow(0,0,0,800,600,"tip",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,800,600,0,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
luz.i=CreateLight(#PB_Any,$EEEEEE,4,4,2,#PB_Light_Point)
cuerpotextura.i=LoadTexture(#PB_Any,"soil_wall.jpg")
cuerpomaterial.i=CreateMaterial(#PB_Any,TextureID(cuerpotextura.i))
cuerpomalla.i=Createcube(#PB_Any,0.5)
cilinmalla.i=CreateCylinder(#PB_Any,0.1,0.4)
cilin.i=CreateEntity(#PB_Any,MeshID(cilinmalla.i),MaterialID(cuerpomaterial.i),-1,0,0)
cuerpo.i=CreateEntity(#PB_Any,MeshID(cuerpomalla.i),MaterialID(cuerpomaterial.i),1,0,0)

CreateNode(0,0,0,0)
RotateNode(0,84,123,-239,#PB_Absolute)

camara.i=CreateCamera(#PB_Any,0,0,100,100)
MoveCamera(camara.i,1,1.4,6.5,#PB_Absolute)

EntityLookAt(cilin,NodeX(0,#PB_Absolute),NodeY(0,#PB_Absolute),NodeZ(0,#PB_Absolute))
Repeat
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_z)
    AttachNodeObject(0,EntityID(cilin)); <- Attach function should not change position of the attached element !
  ElseIf KeyboardReleased(#PB_Key_x)
    DetachNodeObject(0,EntityID(cilin))
  EndIf
  RenderWorld()
  FlipBuffers():delay(9)
Until KeyboardPushed(#PB_Key_Escape)

Re: 3D Attach function shouldn't change position of the atta

Posted: Fri Feb 07, 2020 11:53 am
by pf shadoko
with
While WindowEvent (): wend
in the rendering loop, I don't see a problem

Re: 3D Attach function change position of the attached

Posted: Fri Feb 07, 2020 2:34 pm
by Fred
I changed the key to F1/F2 as Z isn't at the same place on french keyboard:

Code: Select all

InitEngine3D()
InitSprite():InitKeyboard():InitMouse()
OpenWindow(0,0,0,800,600,"tip",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,800,600,0,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
luz.i=CreateLight(#PB_Any,$EEEEEE,4,4,2,#PB_Light_Point)
cuerpotextura.i=LoadTexture(#PB_Any,"soil_wall.jpg")
cuerpomaterial.i=CreateMaterial(#PB_Any,TextureID(cuerpotextura.i))
cuerpomalla.i=CreateCube(#PB_Any,0.5)
cilinmalla.i=CreateCylinder(#PB_Any,0.1,0.4)
cilin.i=CreateEntity(#PB_Any,MeshID(cilinmalla.i),MaterialID(cuerpomaterial.i),-1,0,0)
Debug cilin
cuerpo.i=CreateEntity(#PB_Any,MeshID(cuerpomalla.i),MaterialID(cuerpomaterial.i),1,0,0)

CreateNode(0,0,0,0)
RotateNode(0,84,123,-239,#PB_Absolute)

camara.i=CreateCamera(#PB_Any,0,0,100,100)
MoveCamera(camara.i,1,1.4,6.5,#PB_Absolute)

EntityLookAt(cilin,NodeX(0,#PB_Absolute),NodeY(0,#PB_Absolute),NodeZ(0,#PB_Absolute))
Repeat
  While WindowEvent() : Wend
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_F1)
    Debug "F1 pressed"
    AttachNodeObject(0,EntityID(cilin)); <- Attach function should not change position of the attached element !
  ElseIf KeyboardReleased(#PB_Key_F2)
    Debug "F2 pressed"
    DetachNodeObject(0,EntityID(cilin))
  EndIf
  RenderWorld()
  FlipBuffers():Delay(9)
Until KeyboardPushed(#PB_Key_Escape)
I can see the position change, but I think it's OK as the node isn't at the same place than the entity, and is rotated.

Re: 3D Attach function change position of the attached

Posted: Fri Feb 07, 2020 7:02 pm
by DK_PETER
I think I know what Psychophanta is talking about.
The object should attach itself at the current location - no matter the rotation performed of the node.
As it is you must recalculate the position to attach an object at a specific location - regardless of the node rotation or reset the node before attaching it.
Wrong behaviour:

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()


Structure Object
  id.i
  ma.i
  ms.i
  tx.i
EndStructure
Global o.Object, node.i, no.Object, ret.i, ev.i, Attached.i = #False, Quit.i = #False


Declare.i CreateObjects()

OpenWindow(0, 0, 0, 1024, 768, "Node detach/attach", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768)
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 0, 5)

ret = CreateObjects()

Repeat
  Repeat : ev = WindowEvent() : If ev = #PB_Event3D_CloseWindow : Quit = #True : EndIf : Until ev = 0
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Left)
    RotateNode(node, 0, 0, 0.2, #PB_Relative)
  ElseIf KeyboardPushed(#PB_Key_Right)
    RotateNode(node, 0, 0, -0.2, #PB_Relative)
  EndIf
  If KeyboardReleased(#PB_Key_A)
    If Attached = #False
      AttachNodeObject(node, EntityID(o\id))
      Attached = #True
    EndIf
  ElseIf KeyboardReleased(#PB_Key_D)
    If Attached = #True
      DetachNodeObject(node, EntityID(o\id))
      Attached = #False
    EndIf
  EndIf 
      
  RenderWorld()
  
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape) Or Quit = #True

Procedure.i CreateObjects()
  node = CreateNode(#PB_Any, 0, 0, 0)
  no\ms = CreateCube(#PB_Any, 0.3)
  no\tx = CreateTexture(#PB_Any, 100, 100, "GradBox")
  StartDrawing(TextureOutput(no\tx))
  DrawingMode(#PB_2DDrawing_Gradient)
  FrontColor($00FF31) : BackColor($00B431)
  LinearGradient(50, 0, 50, 50)
  Box(0, 0, 100, 50)
  LinearGradient(50, 100, 50, 50)
  Box(0, 50, 100, 50)
  StopDrawing()
  no\ma = CreateMaterial(#PB_Any, TextureID(no\tx))
  MaterialBlendingMode(no\ma, #PB_Material_Color)
  no\id = CreateEntity(#PB_Any, MeshID(no\ms), MaterialID(no\ma))
  AttachNodeObject(node, EntityID(no\id))
  
  o\ms = CreateCone(#PB_Any, 0.3, 0.2, 16, 16)
  o\tx = CreateTexture(#PB_Any, 10, 10)
  StartDrawing(TextureOutput(o\tx))
  Box(0, 0, 10, 10, $00FFFF)
  StopDrawing()
  o\ma = CreateMaterial(#PB_Any, TextureID(o\tx))
  MaterialBlendingMode(o\ma, #PB_Material_Add)
  o\id = CreateEntity(#PB_Any, MeshID(o\ms), MaterialID(o\ma), -1, 0, 0)
EndProcedure
EDIT: Same wrong behaviour goes for AttachEntityObject()/DetachEntityObject()
The object should simply attach itself at current angle and position - regardless of parent rotation/position.

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()


Structure Object
  id.i
  ma.i
  ms.i
  tx.i
EndStructure
Global o.Object, node.i, no.Object, ret.i, ev.i, Attached.i = #False, Quit.i = #False


Declare.i CreateObjects()

OpenWindow(0, 0, 0, 1024, 768, "Node detach/attach", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768)
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 0, 5)

ret = CreateObjects()

Repeat
  Repeat : ev = WindowEvent() : If ev = #PB_Event3D_CloseWindow : Quit = #True : EndIf : Until ev = 0
 
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Left)
    RotateNode(node, 0, 0, 0.2, #PB_Relative)
  ElseIf KeyboardPushed(#PB_Key_Right)
    RotateNode(node, 0, 0, -0.2, #PB_Relative)
  EndIf
  If KeyboardReleased(#PB_Key_A)
    If Attached = #False
      ;AttachNodeObject(node, EntityID(o\id))
      AttachEntityObject(no\id, "", EntityID(o\id))
      Attached = #True
    EndIf
  ElseIf KeyboardReleased(#PB_Key_D)
    If Attached = #True
      ;DetachNodeObject(node, EntityID(o\id))
      DetachEntityObject(no\id, EntityID(o\id))
      Attached = #False
    EndIf
  EndIf
     
  RenderWorld()
 
  FlipBuffers()
 
Until KeyboardPushed(#PB_Key_Escape) Or Quit = #True

Procedure.i CreateObjects()
  node = CreateNode(#PB_Any, 0, 0, 0)
  no\ms = CreateCube(#PB_Any, 0.3)
  no\tx = CreateTexture(#PB_Any, 100, 100, "GradBox")
  StartDrawing(TextureOutput(no\tx))
  DrawingMode(#PB_2DDrawing_Gradient)
  FrontColor($00FF31) : BackColor($00B431)
  LinearGradient(50, 0, 50, 50)
  Box(0, 0, 100, 50)
  LinearGradient(50, 100, 50, 50)
  Box(0, 50, 100, 50)
  StopDrawing()
  no\ma = CreateMaterial(#PB_Any, TextureID(no\tx))
  MaterialBlendingMode(no\ma, #PB_Material_Color)
  no\id = CreateEntity(#PB_Any, MeshID(no\ms), MaterialID(no\ma))
  AttachNodeObject(node, EntityID(no\id))
 
  o\ms = CreateCone(#PB_Any, 0.3, 0.2, 16, 16)
  o\tx = CreateTexture(#PB_Any, 10, 10)
  StartDrawing(TextureOutput(o\tx))
  Box(0, 0, 10, 10, $00FFFF)
  StopDrawing()
  o\ma = CreateMaterial(#PB_Any, TextureID(o\tx))
  MaterialBlendingMode(o\ma, #PB_Material_Add)
  o\id = CreateEntity(#PB_Any, MeshID(o\ms), MaterialID(o\ma), -1, 0, 0)
EndProcedure

Re: 3D Attach function change position of the attached

Posted: Sun Feb 09, 2020 7:08 pm
by pf shadoko
Yes, I see.
(however, this behavior is correct, when we attach the entity, its coordinates remain the same, but applying now to the node, the position of the entity changes)
it would indeed be useful to have an option to keep the same position (world) once the entity is attached

is that how you'd like it to work?
(use [Space] to attach/detach the node)

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()


Structure Object
    id.i
    ma.i
    ms.i
    tx.i
EndStructure
Global o.Object, node.i, no.Object, ret.i, ev.i, Attached.i = #False, Quit.i = #False


Declare.i CreateObjects()

OpenWindow(0, 0, 0, 1024, 768, "Node detach/attach", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768)
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 0, 5)

ret = CreateObjects()

Repeat
    Repeat : ev = WindowEvent() : If ev = #PB_Event3D_CloseWindow : Quit = #True : EndIf : Until ev = 0
    
    ExamineKeyboard()
    If KeyboardPushed(#PB_Key_Left)
        RotateNode(node, 0, 0, 1, #PB_Relative)
    ElseIf KeyboardPushed(#PB_Key_Right)
        RotateNode(node, 0, 0, -1, #PB_Relative)
    EndIf
    
    If KeyboardReleased(#PB_Key_Space)
        If Attached = #False
            noderoll.f=NodeRoll(node) 
            AttachNodeObject(node, EntityID(o\id))
            RotateNode(node,0,0,-noderoll,PB_Absolute)
            DetachNodeObject(node, EntityID(o\id))
            
            AttachNodeObject(node, EntityID(o\id))
            RotateNode(node,0,0,noderoll,PB_Absolute)
            Attached = #True
        Else
            DetachNodeObject(node, EntityID(o\id))
            Attached = #False
        EndIf
    EndIf
    RenderWorld()
    FlipBuffers()
    
Until KeyboardPushed(#PB_Key_Escape) Or Quit = #True

Procedure.i CreateObjects()
    node = CreateNode(#PB_Any, 0, 0, 0)
    no\ms = CreateCube(#PB_Any, 0.3)
    no\tx = CreateTexture(#PB_Any, 100, 100, "GradBox")
    StartDrawing(TextureOutput(no\tx))
    DrawingMode(#PB_2DDrawing_Gradient)
    FrontColor($00FF31) : BackColor($00B431)
    LinearGradient(50, 0, 50, 50)
    Box(0, 0, 100, 50)
    LinearGradient(50, 100, 50, 50)
    Box(0, 50, 100, 50)
    StopDrawing()
    no\ma = CreateMaterial(#PB_Any, TextureID(no\tx))
    MaterialBlendingMode(no\ma, #PB_Material_Color)
    no\id = CreateEntity(#PB_Any, MeshID(no\ms), MaterialID(no\ma))
    AttachNodeObject(node, EntityID(no\id))
    
    o\ms = CreateCone(#PB_Any, 0.3, 0.2, 16, 16)
    o\tx = CreateTexture(#PB_Any, 10, 10)
    StartDrawing(TextureOutput(o\tx))
    Box(0, 0, 10, 10, $00FFFF)
    StopDrawing()
    o\ma = CreateMaterial(#PB_Any, TextureID(o\tx))
    MaterialBlendingMode(o\ma, #PB_Material_Add)
    o\id = CreateEntity(#PB_Any, MeshID(o\ms), MaterialID(o\ma), -1, 0, 0)
EndProcedure

Re: 3D Attach function change position of the attached

Posted: Sun Feb 09, 2020 7:14 pm
by DK_PETER
That would be an extremely nice option to have and it lightens the calculation needed to
(re)attach again at the new position after a rotation. So, yes..please. :-)

Re: 3D Attach function change position of the attached

Posted: Tue Feb 11, 2020 10:47 pm
by Psychophanta
Sorry, but i don't receive email from topics since years ago, don't know why.

Yes, looks like the behaviour is ok.
However I would add a warning or so, in the manual at AttachEntityObject() and AttachNodeObject().
Or might be to add a flag to #PreserveGlobalPosition
:)