rudder simulation

Everything related to 3D programming
Mesa
Enthusiast
Enthusiast
Posts: 345
Joined: Fri Feb 24, 2012 10:19 am

rudder simulation

Post by Mesa »

I need to simulate a rudder (airplane or boat).
I want to rotate an entity with arrow keys, but the rotation needs to be on a face of the entity, like a door into its door-frame.

Do i need a hingejoint ?

Any help, thanx.

Code: Select all

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


#CameraSpeed = 1

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  
  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/Packs/skybox.zip", #PB_3DArchive_Zip)
  
  InitSprite()
  InitKeyboard()
  ;   InitMouse()
  
  If OpenWindow(0, 0, 0, 800,600, "rudder simulation: Left and right Arrows", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    Result = OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
    
    
    ; First create materials
    ;
    CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
    CreateMaterial(1, LoadTexture(1, "Wood.jpg"))
    
    ; Meshes
    ;
    CreateCube(0, 1.0)
    CreateSphere(1, 0.66)
    
    ; Entities
    ;
    CreateEntity(0, MeshID(0), MaterialID(0), 0, 0, 0)
    ScaleEntity(0, 2, 4, 0.5)
    CreateEntity(1, MeshID(0), MaterialID(1), 3, 0, 0)
    ScaleEntity(1, 2, 4, 0.5)
    
    ; Bodies
    ;
    
    CreateEntityBody(0, #PB_Entity_StaticBody)
    CreateEntityBody(1, #PB_Entity_BoxBody, 1.0,0,1000)
    
    ; HingeJoint
    ;
    HingeJoint(0, EntityID(0), 1.0, 0.5, 0.25, 0, 1, 0, EntityID(1), -1.0, 0.5, 0.25, 0, 1, 0)
    SetJointAttribute(0, #PB_HingeJoint_LowerLimit, Degree(-#PI/3)); doesn't work
    SetJointAttribute(0, #PB_HingeJoint_UpperLimit, Degree(#PI/3))
    
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 12, 15, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    CameraBackColor(0, RGB(228, 228, 230))
    
    ;Light
     CreateLight(0, #White, 0, 5, 5)
     
     
    EnableHingeJointAngularMotor(0, #True , 0, 1)
    
    
    Repeat
      Repeat
        Event = WindowEvent()
        
        Select Event    
          Case #PB_Event_CloseWindow
            Quit = 1  
        EndSelect
        
        If Quit = 1 
          End 
        EndIf  ; Quit the app immediately
      Until Event = 0
      

      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Space)
          Debug "reset rudder"  
        EndIf
        
        
        If KeyboardPushed(#PB_Key_Left)
          EnableHingeJointAngularMotor(0, #True , 1, 1)
          HingeJointMotorTarget(0, 5, 1)
          Debug "Rudder: Turn left"
        ElseIf KeyboardPushed(#PB_Key_Right)
          EnableHingeJointAngularMotor(0, #True , 1, 1)
          HingeJointMotorTarget(0, -5, 1)
          Debug "Rudder: Turn right"
        EndIf
        
        If KeyboardReleased(#PB_Key_Left)
          EnableHingeJointAngularMotor(0, #False , 0, 0)
        ElseIf KeyboardPushed(#PB_Key_Down)
          EnableHingeJointAngularMotor(0, #False , 0, 0) 
        EndIf
        
      EndIf
      
     
      RenderWorld()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

M.
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: rudder simulation

Post by DK_PETER »

You could..or you could take a look at pf shadokos' nice airplane.
viewtopic.php?f=36&t=72732
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: rudder simulation

Post by applePi »

or if you want just a graphical demo to show what is the rudder is (without physics) then i suggest this:
1- make a rectangle as a rudder (thinned cube)
2- make another cube call it motherCube.
3- position one edges of the rudder to the center of the motherCube
4- AttachEntityObject(motherCube, "", EntityID(Cube))
5- hide the motherCube: HideEntity(motherCube, 1)
now when we rotate the motherCube the rudder will follow it and will appear as rotating around its edge like the real rudder

rotate the rudder to left/right with arrow keys, limited to 45 degrees

Code: Select all

Enumeration
  #Mainform
EndEnumeration

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

OpenWindow(#Mainform,0,0, 1024, 768, "Rudder, ... use Left/Right to rotate the rudder", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(#Mainform),0,0,1024,768,0, 0, 0)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)

;Light & Shadow
CreateLight(#PB_Any,RGB(255, 255, 255), -200, 400, 1500)
;WorldShadows(#PB_Shadow_Modulative) ;i get error here in windows7

;Camera
CreateCamera(0, 0, 0, 100,100)
CameraBackColor(0, RGB(145, 190, 20))
MoveCamera(0, 3, 7, 15, #PB_Absolute)  
CameraLookAt(0, 0,0,0)   

;Ground
Mesh = CreatePlane(#PB_Any, 20, 20, 10, 10, 2, 2)
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "MRAMOR6X6.jpg")))
Ground = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(material))

;Cube
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "ground_diffuse.png")))
Mesh = CreateCube(#PB_Any, 2)

Cube = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 0, 1, -2)
ScaleEntity(Cube, 0.05,2,2)
motherCube = CreateEntity(#PB_Any, MeshID(Mesh), #PB_Material_None , 0, 1, 2)

AttachEntityObject(motherCube, "", EntityID(Cube))
HideEntity(motherCube, 1)
rot.f = 0
Repeat
    Repeat
    event = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        quit = 1
    EndSelect
    Until event = 0
         
  If ExamineKeyboard()
    If KeyboardPushed (#PB_Key_Escape)
      Break
    EndIf
    
    If KeyboardPushed (#PB_Key_Left)
      rot+1
      If rot > 45: rot = 45: EndIf
      RotateEntity(motherCube, 0,rot,0) 
   EndIf
   If KeyboardPushed (#PB_Key_Right)
     rot-1
      If rot < -45: rot = -45: EndIf
     RotateEntity(motherCube, 0,rot,0) 
   EndIf
   
 EndIf
 
     
  RenderWorld()
  FlipBuffers()  
  
  Until KeyboardPushed(#PB_Key_Escape) Or quit = 1
Post Reply