Page 1 of 1

Problem with MoveCamera()

Posted: Thu Apr 24, 2014 5:13 pm
by Bananenfreak
Heyho,
here´s a small snippet for testing. I want to move my camera near to (0, 0, 0) or away from it... But this doesn´t work.
What I´ve done wrong?

Code: Select all

EnableExplicit


Structure Vektor2
  x.d
  y.d
EndStructure


;Für jeden Punkt in der Welt ein Typ
Structure Vektor3 Extends Vektor2
  z.d
EndStructure


Define.i Quit, event
Define.Vektor3 cam
#kam_0 = 0
#window = 0
#plane = 0
#light = 0
#tex_0 = 0
#mat_0 = 0
#node_0 = 0


Enumeration Entities
  #planent
EndEnumeration


Procedure.w MouseWheelDelta()       ;By Danilo
  Protected x.w
  
  x.w = ((EventwParam()>>16)&$FFFF) 
  ProcedureReturn -(x / 120) 
EndProcedure


Procedure Debugger(title.s, x.d, y.d, z.d)
  Debug title
  Debug StrD(x)
  Debug StrD(y)
  Debug StrD(z)
  Debug "-----------------"
EndProcedure


If InitEngine3D()
  InitSprite()
  InitKeyboard()
  
  OpenWindow(#window, 0, 0, 1000, 500, "TOM-Calculator", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  OpenWindowedScreen(WindowID(#window), 10, 10, 980, 480, 0, 10, 10, #PB_Screen_SmartSynchronization)
  
  ;-Licht
  CreateLight(#light, RGB(255, 255, 255), 30, 20, 30)
  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)
  CreateTexture(#tex_0, 128, 128)
  StartDrawing(TextureOutput(#tex_0))
  Box(0, 0, 128, 128, RGB(255, 255, 255))
  Box(64, 0, 64, 64, RGB(0, 0, 255))
  Box(0, 64, 64, 64, RGB(0, 0, 255))
  StopDrawing()
  CreateMaterial(#mat_0, TextureID(#tex_0))
  CreateEntity(#planent, MeshID(#plane), MaterialID(#mat_0), 0, 0, 0)
  
  ;-Node
  CreateNode(#node_0, 0, 0, 0)
  
  ;-Camera
  CreateCamera(#kam_0, 0, 0, 100, 100)
  cam\x = 100
  cam\y = 100
  cam\z = 100
  MoveCamera(#kam_0, cam\x, cam\y, cam\z, #PB_Absolute)
  CameraLookAt(#kam_0, 0, 0, 0)
  CameraBackColor(#kam_0, RGB(50, 50, 250))
  AttachNodeObject(#node_0, CameraID(#kam_0))
  
  
  Repeat
    Repeat
      event = WindowEvent()
      
      Select event
        Case #WM_MOUSEWHEEL       ;Es liegt ein Mausradevent vor.
;           cam\x = CameraX(#kam_0)
;           cam\y = CameraY(#kam_0)
;           cam\z = CameraZ(#kam_0)
          Debugger("Old Position", CameraX(#kam_0), CameraY(#kam_0), CameraZ(#kam_0))
          ;Debugger("Old Position", cam\x, cam\y, cam\z)
          If MouseWheelDelta() > 0
            cam\x / 0.75
            cam\y / 0.75
            cam\z / 0.75
            ;MoveCamera(#kam_0, cam\x / 0.75, cam\y / 0.75, cam\z / 0.75, #PB_Absolute)
            Debugger("Position it should be (>0)", cam\x / 0.75, cam\y / 0.75, cam\z / 0.75)
            
          Else
            cam\x * 0.75
            cam\y * 0.75
            cam\z * 0.75
            ;MoveCamera(#kam_0, cam\x * 0.75, cam\y * 0.75, cam\z * 0.75, #PB_Absolute)
            Debugger("Position it should be (<0)", cam\x * 0.75, cam\y * 0.75, cam\z * 0.75)
          EndIf
          MoveCamera(#kam_0, cam\x, cam\y, cam\z, #PB_Absolute)
          Debugger("New Position", CameraX(#kam_0), CameraY(#kam_0), CameraZ(#kam_0))
          ;CameraLookAt(#kam_0, 0, 0, 0)
          
        Case #PB_Event_CloseWindow
          Quit = 1
          
      EndSelect
    Until event = 0
    
    If ExamineKeyboard()
      If KeyboardPushed(#PB_Key_Left)
        RotateNode(#node_0, 0, -5, 0, #PB_Relative)
      ElseIf KeyboardPushed(#PB_Key_Right)
        RotateNode(#node_0, 0, 5, 0, #PB_Relative)
      EndIf
    EndIf
    
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Re: Problem with MoveCamera()

Posted: Thu Apr 24, 2014 6:09 pm
by STARGÅTE
MouseWheelDelta() is for ExamineMouse()
So you have to use the ExamineMouse() and InitMouse()

#WM_MOUSEWHEEL is a Win-API event, so you have to use EventwParam() or EventlParam() for mor information about the wheel delta

Re: Problem with MoveCamera()

Posted: Fri Apr 25, 2014 6:47 am
by Bananenfreak
@Stargate:
Err, do you read my code? There´s a procedure called MouseWheelDelta() with EventwParam() from Danilo (Thanks again)...

Perhaps I didn´t Point the Problem... My Problem is, I want to move camera to Point Zero and in same way back with my mousewheel and rotate the camera round Point Zero with Cursor Left and Right. So, whenever I rotate my camera and then zooming in or out, CameraPosition is not the mentioned one.

Re: Problem with MoveCamera()

Posted: Mon Apr 28, 2014 4:49 pm
by PMV
MouseWheelDelta() can be bigger as 1

Code: Select all

If MouseWheelDelta() = 1
has to be

Code: Select all

If MouseWheelDelta() >= 1
:D


btw. you still have just one step even the user has turned the wheel very fast. :wink:

MFG PMV

Re: Problem with MoveCamera()

Posted: Tue Apr 29, 2014 4:57 pm
by Bananenfreak
Good, thank you, I didn´t noticed that.

But there is no interest in MouseWheelDelta() from my side. This Topic belongs to MoveCamera() and there is still a Problem with Camera is not located correct after MoveCamera(). (I edited first post with new code.)

Re: Problem with MoveCamera()

Posted: Tue Apr 29, 2014 5:05 pm
by PMV
When i move the wheel, it seems to work right for me. Don't know what you see, that i don't see. Sorry.

Re: Problem with MoveCamera()

Posted: Tue Apr 29, 2014 6:36 pm
by Samuel
I think I see the problem, and the strange thing is it only happens after you rotate the camera.
I wonder if the camera attached node is causing the problem?

Re: Problem with MoveCamera()

Posted: Tue Apr 29, 2014 7:20 pm
by STARGÅTE
problem is:
CameraXYZ() give the coordinates in world system but you move the camera in the node system!

Re: Problem with MoveCamera()

Posted: Wed Apr 30, 2014 12:44 pm
by PMV
Oh .. i have completely overseen the part with the rotate-stuff and the node. :oops:

All move-commands are working relativ to the node they are attached to.
Thats what a node is for. But as Stargate already mentioned, CameraX/Y/Z will return
world coordinates. Here a small changed example that moves the node and rotates
the camera correct. :)

Code: Select all

EnableExplicit


Structure Vektor2
  x.d
  y.d
EndStructure


;Für jeden Punkt in der Welt ein Typ
Structure Vektor3 Extends Vektor2
  z.d
EndStructure


Define.i Quit, event
Define.Vektor3 cam
#kam_0 = 0
#window = 0
#plane = 0
#plane_1 = 1
#light = 0
#tex_0 = 0
#tex_1 = 1
#mat_0 = 0
#mat_1 = 1
#node_0 = 0
#display = 1


Enumeration Entities
  #planent
EndEnumeration


Procedure.w MouseWheelDelta()       ;By Danilo
  Protected x.w
  
  x.w = ((EventwParam()>>16)&$FFFF) 
  ProcedureReturn -(x / 120) 
EndProcedure


If InitEngine3D()
  InitSprite()
  InitKeyboard()
  
  OpenWindow(#window, 0, 0, 1000, 500, "Test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  OpenWindowedScreen(WindowID(#window), 10, 10, 980, 480, 0, 10, 10, #PB_Screen_SmartSynchronization)
  
  ;-Licht
  CreateLight(#light, RGB(255, 255, 255), 30, 20, 30)
  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)
  CreateTexture(#tex_0, 128, 128)
  StartDrawing(TextureOutput(#tex_0))
  Box(0, 0, 128, 128, RGB(255, 255, 255))
  Box(64, 0, 64, 64, RGB(0, 0, 255))
  Box(0, 64, 64, 64, RGB(0, 0, 255))
  StopDrawing()
  CreateMaterial(#mat_0, TextureID(#tex_0))
  CreateEntity(#planent, MeshID(#plane), MaterialID(#mat_0), 0, 0, 0)
  
  ;-Node
  CreateNode(#node_0, 0, 100, 100)
  NodeLookAt(#node_0, 0, 0, 0)
  
  ;-Camera
  CreateCamera(#kam_0, 0, 0, 100, 100)
  CameraBackColor(#kam_0, RGB(50, 50, 250))
  AttachNodeObject(#node_0, CameraID(#kam_0))
  
  CreatePlane(#plane_1, 20, 20, 2, 2, 1, 1)
  CreateTexture(#tex_1, 128, 128)
  CreateMaterial(#mat_1, TextureID(#tex_1))
  MaterialBlendingMode(#mat_1, #PB_Material_AlphaBlend)
  SetMaterialColor(#mat_1, #PB_Material_AmbientColor, $FFFFFF)
  SetMaterialColor(#mat_1, #PB_Material_SelfIlluminationColor, $FFFFFF)
  CreateEntity(#display, MeshID(#plane_1), MaterialID(#mat_1), 0, 11, -50)
  RotateEntity(#display, 45, 0, 180)
  AttachNodeObject(#node_0, EntityID(#display))
  
  StartDrawing(TextureOutput(#tex_1))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0, 50, 128, 78, 0)
  DrawText(10, 10, Str(CameraPitch(#kam_0)) + " | " + Str(CameraRoll(#kam_0)) + " | " + Str(CameraYaw(#kam_0)), $ffffffff, $000000)
  StopDrawing()
  
  Repeat
    Repeat
      event = WindowEvent()
      
      Select event
        Case #WM_MOUSEWHEEL       ;Es liegt ein Mausradevent vor.
          cam\x = NodeX(#node_0)
          cam\y = NodeY(#node_0)
          cam\z = NodeZ(#node_0)
          Debug "Old Position"
          Debug cam\x
          Debug cam\y
          Debug cam\z
          Debug "-----------------"
          If MouseWheelDelta() > 0
            MoveNode(#node_0, cam\x / 0.75, cam\y / 0.75, cam\z / 0.75, #PB_Absolute)
            Debug "Position it should be"
            Debug cam\x / 0.75
            Debug cam\y / 0.75
            Debug cam\z / 0.75
            Debug "-----------------"
          Else
            MoveNode(#node_0, cam\x * 0.75, cam\y * 0.75, cam\z * 0.75, #PB_Absolute)
            Debug "Position it should be"
            Debug cam\x * 0.75
            Debug cam\y * 0.75
            Debug cam\z * 0.75
            Debug "-----------------"
          EndIf
          Debug "New Position"
          Debug NodeX(#node_0)
          Debug NodeY(#node_0)
          Debug NodeZ(#node_0)
          Debug "-----------------"
          ;CameraLookAt(#kam_0, 0, 0, 0)
          
        Case #PB_Event_CloseWindow
          Quit = 1
          
      EndSelect
    Until event = 0
    
    If ExamineKeyboard()
      If KeyboardPushed(#PB_Key_Left)
        RotateCamera(#kam_0, 0, -5, 0, #PB_Relative)
        StartDrawing(TextureOutput(#tex_1))
          DrawingMode(#PB_2DDrawing_AllChannels)
          Box(0, 0, 128, 50, 0)
          DrawText(10, 10, Str(CameraPitch(#kam_0)) + " | " + Str(CameraRoll(#kam_0)) + " | " + Str(CameraYaw(#kam_0)), $ffffffff, $000000)
        StopDrawing() 
      ElseIf KeyboardPushed(#PB_Key_Right)
        RotateCamera(#kam_0, 0, 5, 0, #PB_Relative)
        StartDrawing(TextureOutput(#tex_1))
          DrawingMode(#PB_2DDrawing_AllChannels)
          Box(0, 0, 128, 50, 0)
          DrawText(10, 10, Str(CameraPitch(#kam_0)) + " | " + Str(CameraRoll(#kam_0)) + " | " + Str(CameraYaw(#kam_0)), $ffffffff, $000000)
        StopDrawing() 
      EndIf
    EndIf
    
    
    
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
MFG PMV

Re: Problem with MoveCamera()

Posted: Wed Apr 30, 2014 2:38 pm
by Bananenfreak
Thank you PMV, but this isn´t what I want.

With Left and Right, Camera should be rotated round (0, 0, 0).
With Mousewheel you can zoom in and out (To (0, 0, 0)).

But this:

Code: Select all

MoveCamera(#kam_0, cam\x / 0.75, cam\y / 0.75, cam\z / 0.75, #PB_Absolute | #PB_World)
Should work... or not? But it doesn´t work.

(I edited first post with new code)

Re: Problem with MoveCamera()

Posted: Wed Apr 30, 2014 3:02 pm
by PMV
You have no possibility to get the coordinates relative to the node.
So you have to store the relative values yourself:

Code: Select all

EnableExplicit


Structure Vektor2
  x.d
  y.d
EndStructure


;Für jeden Punkt in der Welt ein Typ
Structure Vektor3 Extends Vektor2
  z.d
EndStructure


Define.i Quit, event
Define.Vektor3 cam
#kam_0 = 0
#window = 0
#plane = 0
#light = 0
#tex_0 = 0
#mat_0 = 0
#node_0 = 0


Enumeration Entities
  #planent
EndEnumeration


Procedure.w MouseWheelDelta()       ;By Danilo
  Protected x.w
  
  x.w = ((EventwParam()>>16)&$FFFF) 
  ProcedureReturn -(x / 120) 
EndProcedure


If InitEngine3D()
  InitSprite()
  InitKeyboard()
  
  OpenWindow(#window, 0, 0, 1000, 500, "Test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  OpenWindowedScreen(WindowID(#window), 10, 10, 980, 480, 0, 10, 10, #PB_Screen_SmartSynchronization)
  
  ;-Licht
  CreateLight(#light, RGB(255, 255, 255), 30, 20, 30)
  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)
  CreateTexture(#tex_0, 128, 128)
  StartDrawing(TextureOutput(#tex_0))
  Box(0, 0, 128, 128, RGB(255, 255, 255))
  Box(64, 0, 64, 64, RGB(0, 0, 255))
  Box(0, 64, 64, 64, RGB(0, 0, 255))
  StopDrawing()
  CreateMaterial(#mat_0, TextureID(#tex_0))
  CreateEntity(#planent, MeshID(#plane), MaterialID(#mat_0), 0, 0, 0)
  
  ;-Node
  CreateNode(#node_0, 0, 0, 0)
  
  ;-Camera
  CreateCamera(#kam_0, 0, 0, 100, 100)
  CameraBackColor(#kam_0, RGB(50, 50, 250))
  AttachNodeObject(#node_0, CameraID(#kam_0))
  cam\x = 0
  cam\y = 100
  cam\z = 100
  MoveCamera(#kam_0, cam\x, cam\y, cam\z, #PB_Absolute)
  CameraLookAt(#kam_0, 0, 0, 0)
  
  
  Repeat
    Repeat
      event = WindowEvent()
      
      Select event
        Case #WM_MOUSEWHEEL       ;Es liegt ein Mausradevent vor.
          If MouseWheelDelta() > 0
            cam\x / 0.75
            cam\y / 0.75
            cam\z / 0.75
          Else
            cam\x * 0.75
            cam\y * 0.75
            cam\z * 0.75
          EndIf
          MoveCamera(#kam_0, cam\x, cam\y, cam\z, #PB_Absolute)
          ;CameraLookAt(#kam_0, 0, 0, 0)
          
        Case #PB_Event_CloseWindow
          Quit = 1
          
      EndSelect
    Until event = 0
    
    If ExamineKeyboard()
      If KeyboardPushed(#PB_Key_Left)
        RotateNode(#kam_0, 0, -5, 0, #PB_Relative)
      ElseIf KeyboardPushed(#PB_Key_Right)
        RotateNode(#kam_0, 0, 5, 0, #PB_Relative)
      EndIf
    EndIf
    
    
    
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
MFG PMV

Re: Problem with MoveCamera()

Posted: Wed Apr 30, 2014 4:39 pm
by Bananenfreak
@ PMV:
Thank you very much. I understand... New Point zero for my Camera is this node and if I rotate my node, I rotate also my System of coordinates (So I can calculate much easier).

But again, with additional PB-Flag

Code: Select all

#PB_World
camera has to Position itself with Worldcoordinates relative to the world and not to my node, or not?

Re: Problem with MoveCamera()

Posted: Fri May 02, 2014 12:42 pm
by PMV
Bananenfreak wrote:But again, with additional PB-Flag

Code: Select all

#PB_World
camera has to Position itself with Worldcoordinates relative to the world and not to my node, or not?
I have look again and you are right. Of course #PB_World should to the trick.
But it seems, that #PB_Absolute is ignored and the values are then relative.

A Bug.

If i use your example from the startpost and turn the wheel back and for i get this:

Code: Select all

Old Position
100
100
100
-----------------
Position it should be (<0)
75
75
75
-----------------
New Position
175
175
175
-----------------
Old Position
175
175
175
-----------------
Position it should be (>0)
233
233
233
-----------------
New Position
408
408
408
-----------------
The new values are added. :?
Do you post this in bug section? :)

MFG PMV

Re: Problem with MoveCamera()

Posted: Fri May 02, 2014 4:44 pm
by Bananenfreak
No, I post it as soon as possible (Could take a day... i'm online with my Tablet). Or a admin moves this topic...

Re: Problem with MoveCamera()

Posted: Tue May 06, 2014 12:40 pm
by PMV
just for completion the answer from Comtois in bug-forum:
http://www.purebasic.fr/english/viewtop ... =4&t=59157
Comtois wrote:Doc is wrong, for camera you can use one of these constantes :
#PB_Absolute --> Sets the camera's position.
#PB_local --> Moves the camera's position by the vector offset provided along it's own axes (relative to orientation).
#PB_relative --> Moves the camera's position by the vector offset provided along world axes.
That means, there is no #PB_World. So it is not possible without
saving the local coordinates in variables to use absolute position
relative to the node.

MFG PMV