GenericJoint and Y axis

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

GenericJoint and Y axis

Post by applePi »

look generic joint example, for pinpointing the problem here it is below with only the turnstile , shoot the turnstile with a ball , it will rotate correctly around X, now change
SetJointAttribute(4, #PB_Joint_NoLimit, 0, 3)
to this
SetJointAttribute(4, #PB_Joint_NoLimit, 0, 5)
in line 74
shoot again and it will rotate correctly around Z.
now change it to
SetJointAttribute(4, #PB_Joint_NoLimit, 0, 4)
shoot it and it will rotate erratically around Y
so it works okay around X,Z but not around Y
tested with several objects, and different bodies

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - GenericJoint
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

#Speed = 50
    
Define.f KeyX, KeyY, ex,ey

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    CreateSprite(1, 200, 120, #PB_Sprite_AlphaBlending)
    LoadFont(1, "Arial", 16, #PB_Font_Bold)
    
    ;====================== Materials/textures ========================
    
    CreateMaterial(0, LoadTexture(0,"wood.jpg"))   
    GetScriptMaterial(1, "Examples/SphereMappedRustySteel")
    GetScriptMaterial(2, "Color/Red")
    GetScriptMaterial(3, "Color/Green")
    CreateMaterial(4, LoadTexture(4, "Dirt.jpg"))
    CreateMaterial(10, LoadTexture(10, "ground_diffuse.png")) : ScaleMaterial(10,1/8,1/8)
    GetScriptMaterial(11, "Color/Yellow")
    
    ;- Aim
    CreateSprite(0,256,256,#PB_Sprite_AlphaBlending )
    StartDrawing(SpriteOutput(0))
    DrawingMode(#PB_2DDrawing_AllChannels)
    Box(0, 0, 256, 256, $0)
    Circle(128, 128, 102, $ffffffff)
    Circle(128, 128, 100, $77aaaaaa)
    LineXY(128, 0, 128, 255, $ffffffff)
    LineXY(0, 128, 255, 128, $ffffffff)
    StopDrawing()
    
        ;====================== Meshes/Entities/bodies/joints ========================
    
    ; Ground
    ;
    CreateCube(10, 2)
    CreateEntity(10, MeshID(10), MaterialID(10),  0, -4.0, 0) : ScaleEntity(10, 20, 0.2, 20)
    CreateEntityBody(10, #PB_Entity_StaticBody , 0,0.5,0.5)
    
    ; Shoot ball
    ;
    CreateSphere(11, 1)
        
    
    
    ; turnstile - free x rotation
    ;
    CreateCube(4, 4)
    CreateEntity(4, MeshID(4), MaterialID(4),  -12,  0.0, 0) : ScaleEntity(4, 1, 1, 0.2)
    CreateEntityBody(4, #PB_Entity_BoxBody, 1, 0.5, 0.5)
    
    GenericJoint(4,EntityID(10), 0, 10, 0,EntityID(4), 0, 0, 0)
    SetJointAttribute(4, #PB_Joint_NoLimit, 0, 3)
    
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, -1, 8, 25, #PB_Absolute)
    CameraLookAt(0, -3, 0, 0)
    
    ; Light
    ;
    CreateLight(0, RGB(255, 255, 255), 1560, 900, 500)
    AmbientColor($777777)
    
    WorldShadows(#PB_Shadow_Additive) 
    
    MouseLocate(ex/2, ey/2)
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        If MouseButton(#PB_MouseButton_Left)
          If Clic = 0
            If PointPick(0, MouseX(), MouseY())
              Clic = 1
              Shoot = CreateEntity(#PB_Any, MeshID(11), MaterialID(11), CameraX(0), CameraY(0), CameraZ(0))
              CreateEntityBody(Shoot, #PB_Entity_SphereBody, 1,0.5,0.5)
              ApplyEntityImpulse(Shoot, PickX() * #Speed, PickY() * #Speed, PickZ() * #Speed)
            EndIf
          EndIf
        Else
          Clic = 0
        EndIf
      EndIf
      
      If ExamineKeyboard() 
        keyy = -(Bool(KeyboardPushed(#PB_Key_Up)<>0)-Bool(KeyboardPushed(#PB_Key_Down)<>0))-MouseWheel()*5
        keyx = -(Bool(KeyboardPushed(#PB_Key_Left)<>0)-Bool(KeyboardPushed(#PB_Key_Right)<>0))
      EndIf
      
      CameraLookAt(0, 0, 0, 0)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      RenderWorld()
      DisplayTransparentSprite(0,  MouseX()-SpriteWidth(0)/2,  MouseY()-SpriteHeight(0)/2)
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
EndIf
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: GenericJoint and Y axis

Post by Comtois »

I dont know what's happen, it's probably not a PB bug.

Try with this, it work fine

Code: Select all

    ;SetJointAttribute(4, #PB_Joint_NoLimit, 0, 4)
    
    SetJointAttribute(4, #PB_Joint_LowerLimit, -70, 4)
    SetJointAttribute(4, #PB_Joint_UpperLimit, 70, 4)
Then try this, it dont work :

Code: Select all

    SetJointAttribute(4, #PB_Joint_LowerLimit, -89, 4)
    SetJointAttribute(4, #PB_Joint_UpperLimit, 89, 4)
Note : For each axis, if :

lower limit = upper limit --> The axis is locked

lower limit < upper limit --> The axis is limited between the specified values

lower limit > upper limit --> The axis is free and has no limits
This is what does #PB_Joint_NoLimit --> lower limit > upper limit.

And to finish a workaround (use EntityAngularFactor()) :

Code: Select all

    SetJointAttribute(4, #PB_Joint_NoLimit, 0, 4)
    EntityAngularFactor(4, 0, 1, 0)
Please correct my english
http://purebasic.developpez.com/
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: GenericJoint and Y axis

Post by applePi »

thats it EntityAngularFactor(4, 0, 1, 0) which stabilize it on Y axis, thanks Comtois for this suggestion and the other infos.
the cylinder below was not rotating around Y correctly, but now with EntityAngularFactor(.., 0, 1, 0) it works
Z/X to apply angular torque to the Wheel around Y axis, run it in windowed mode

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

#CameraSpeed = 0.4

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/"              , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures"  , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models"    , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"    , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI"        , #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ;WorldDebug(#PB_World_DebugBody)
        
    ;-------------------------------
    ; create  material
    CreateMaterial(1, LoadTexture(1, "Tire_tex.png"))
    ;RotateMaterial(1, -70, #PB_Material_Fixed)
    SetMaterialColor(1, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    ; 
    CreateMaterial(2, LoadTexture(2, "snow_1024.jpg"))
    SetMaterialColor(2, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(3, LoadTexture(3, "Geebee2.bmp"))
    SetMaterialColor(3, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(4, LoadTexture(4, "Wood.jpg"))
    
    ;-------------------------------

CreateCylinder(50, 1, 0.5)
CreateEntity(50, MeshID(50),MaterialID(1),  0, 0, 0)
axis = CreateEntity(#PB_Any, MeshID(50),MaterialID(4),  0, 0, 0)
ScaleEntity(axis, 0.15, 3, 0.15)
;RotateEntity(50, 90,0, 0, #PB_Absolute)
CreateCube(1, 1)

Global Compound = CreateEntity(#PB_Any,0,0)

AddSubEntity(Compound, 50, #PB_Entity_CylinderBody) 
AddSubEntity(Compound, axis, #PB_Entity_CylinderBody) 
CreateEntityBody(Compound, #PB_Entity_CompoundBody, 1, 0.4, 0.5)

;-----------------------------------------
    ;Ground
    ;
    Ground = CreateEntity(#PB_Any, MeshID(1), MaterialID(2), 0, -7, 0)
    ScaleEntity(Ground, 40, 0.4, 40)
    CreateEntityBody(Ground, #PB_Entity_StaticBody)
    
    GenericJoint(4,EntityID(Ground), 0, 5, 0,EntityID(Compound), 0, 0, 0)
    ;the following will make the wheel stop if we stop the angular force
    SetJointAttribute(4, #PB_Joint_EnableSpring, #True, 4)
    SetJointAttribute(4, #PB_Joint_Stiffness, 5, 4)
    SetJointAttribute(4, #PB_Joint_Damping, 0.0, 4)
    ;SetJointAttribute(4, #PB_Joint_NoLimit, 0, 4)
    EntityAngularFactor(Compound, 0, 1, 0)
    ;SetJointAttribute(4, #PB_Joint_LowerLimit, -70, 4)
    ;SetJointAttribute(4, #PB_Joint_UpperLimit, 70, 4)
    
     ; camera
    CreateCamera(0, 0, 0, 100, 100, #True)
    MoveCamera(0,0,0,2.5, #PB_Absolute)
    CameraLookAt(0,0,-2,0)
    
    ; GUI
    OpenWindow3D(0, 0, 0, 50 , 10 , "")
    HideWindow3D(0,1)
    ShowGUI(128, 1) ; Display the GUI, semi-transparent and display the mouse cursor
    SetWindowTitle(0, "PureBasic-3D Demos ...Z/X apply torque to the Wheel,..'space':to drop Cubes..Mouse to Pick objects")
    
  #stone = 30
  Global Dim stone(1000)
  CreateCube(#stone, 0.2)
  For i = 1 To 1000
    stone(i) = CreateEntity(#PB_Any, MeshID(#stone),MaterialID(3))
    HideEntity(stone(i), #True)
  Next

    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.1
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.1
        InputEvent3D(MouseX(), MouseY(),0)
        BodyPick(CameraID(0), MouseButton(#PB_MouseButton_Left), MouseX(), MouseY(), 1)
      EndIf
    
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Z)
          ApplyEntityTorque(Compound, 0, 10, 0)
          ;ApplyEntityTorqueImpulse(Compound, 0, 0, 1)

          ElseIf KeyboardPushed(#PB_Key_X)
            ApplyEntityTorque(Compound, 0, -10, 0)
            
          EndIf
          
    If KeyboardReleased(#PB_Key_Space); throw cubes over the gear
      stn+1
      If stn > 1000: stn=1: EndIf
      MoveEntity(stone(stn), 0.3,-1,0)
      HideEntity(stone(stn), #False)
      CreateEntityBody(stone(stn),#PB_Entity_BoxBody,2,0.1,4)
    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

End
Fred
Administrator
Administrator
Posts: 18384
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: GenericJoint and Y axis

Post by Fred »

So is it something to fix on our side ?
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: GenericJoint and Y axis

Post by Comtois »

No. You can move it in '3D programming'
Please correct my english
http://purebasic.developpez.com/
Post Reply