It is currently Fri Dec 06, 2019 1:59 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: rudder simulation
PostPosted: Thu Oct 10, 2019 5:07 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 24, 2012 10:19 am
Posts: 116
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:
;
; ------------------------------------------------------------
;
;   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.


Top
 Profile  
Reply with quote  
 Post subject: Re: rudder simulation
PostPosted: Fri Oct 11, 2019 2:53 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Feb 19, 2011 10:06 am
Posts: 799
Location: Denmark
You could..or you could take a look at pf shadokos' nice airplane.
viewtopic.php?f=36&t=72732

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 7/10, Intel 6800K, Gtx 970, 32 gb ram.


Top
 Profile  
Reply with quote  
 Post subject: Re: rudder simulation
PostPosted: Sat Oct 12, 2019 6:38 am 
Offline
Addict
Addict

Joined: Sun Jun 25, 2006 7:28 pm
Posts: 1382
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:
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye