It is currently Sat May 25, 2013 12:22 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: SliderJoint & HingeJoint example
PostPosted: Sun Oct 23, 2011 8:06 am 
Offline
Addict
Addict
User avatar

Joined: Tue Aug 19, 2003 11:36 am
Posts: 1113
Location: Doubs - France
Here is a mix from SliderJoint.pb & HingeJoint.pb examples
Save this code in your PureBasic\Examples\3D folder
Code:
;
; ------------------------------------------------------------
;
;   PureBasic - sliderJoint & HingeJoint
;
;    (c) 2011 - Fantaisie Software
;
; ------------------------------------------------------------
;

#CameraSpeed = 1
#PB_SliderJoint_LowerLimit = 3
#PB_SliderJoint_UpperLimit = 4

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY
Define Direction = 0, Time = ElapsedMilliseconds()

Macro DoorClosed()
  (EntityX(1) > -1.07) 
EndMacro

If InitEngine3D()
 
  Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Models", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
 
  InitSprite()
  InitKeyboard()
  InitMouse()
 
  If Screen3DRequester()
   
    ; First create materials
    ;
    CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
    CreateMaterial(1, LoadTexture(1, "Wood.jpg"))
    GetScriptMaterial(2, "Color/Green")
    GetScriptMaterial(3, "Color/Red") 
   
    ; Meshes
    ;
    CreateCube(0, 1.0)
    CreateSphere(1, 0.5)
   
    ; Entities
    ;
    CreateEntity(0, MeshID(0), MaterialID(0),  1, 0, 0)
    ScaleEntity(0, 2, 4, 0.5)
    CreateEntity(1, MeshID(0), MaterialID(1), -3, 0, 0)
    ScaleEntity(1, 2, 4, 0.5)
   
    CreateEntity(3, MeshID(1), MaterialID(3), -1, -1.25, 0.22)
   
    CreateEntity(4, MeshID(0), MaterialID(0),  -2, -2.3, -1.0)
    ScaleEntity(4, 8, 0.5, 4)
    CreateEntity(5, MeshID(0), MaterialID(1), -2, -2.3, 3)
    ScaleEntity(5, 8, 0.5, 4)
   
    ; Bodies
    ;
    EntityPhysicBody(0, #PB_Entity_StaticBody)
    EntityPhysicBody(1, #PB_Entity_BoxBody, 1.0)
   
    EntityPhysicBody(3, #PB_Entity_SphereBody, 0.5)
   
    EntityPhysicBody(4, #PB_Entity_StaticBody)
    EntityPhysicBody(5, #PB_Entity_BoxBody, 0.01)
   
    ; SliderJoint
    ;
    SliderJoint(0, -1, 0, 0, 1, 1, 0, 0)
    SetJointAttribute(0, #PB_SliderJoint_LowerLimit, -3) 
    SetJointAttribute(0, #PB_SliderJoint_UpperLimit,  0)   
   
    ; HingeJoint
    ;
    HingeJoint(4, 4, 0.1, 2, 1, 0, 0, 5, 4, 0.1, -2, 1, 0, 0)
   
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    CameraLocate(0, 10, 6, 19)
    CameraLookAt(0, 0, 0, 0)
   
    ; Light
    CreateLight(0, $FFFFFF, 1560, 900, 500)
    AmbientColor($330000)
   
    Repeat
      Screen3DEvents()
     
      If ElapsedMilliseconds() - Time > 1500 And Not DoorClosed()
        Direction = 1 - Direction
        Time = ElapsedMilliseconds()
      EndIf
     
      If Direction = 1
        ApplyEntityImpulse(1, 0.1, 0, 0)
      Else
        ApplyEntityImpulse(1, -0.1, 0, 0)
      EndIf
     
      If ExamineKeyboard()
       
        If KeyboardReleased(#PB_Key_Space)
          direction = 0
          ApplyEntityImpulse(1, -1, 0, 0)
          Time = ElapsedMilliseconds()
          FreeEntity(3)
          CreateEntity(3, MeshID(1), MaterialID(3), -1, 15, 0.22)
          EntityPhysicBody(3, #PB_Entity_SphereBody, 0.5)
        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
     
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)*#CameraSpeed/2
        MouseY = -(MouseDeltaY()/10)*#CameraSpeed/2
      EndIf
     
      RotateCamera(0, MouseY, MouseX, RollZ, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
     
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
 
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

_________________
Please correct my english
http://purebasic.developpez.com/


Top
 Profile  
 
 Post subject: Re: SliderJoint & HingeJoint example
PostPosted: Sun Oct 23, 2011 11:50 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Jun 26, 2003 2:09 am
Posts: 731
Location: Spain (Galicia)
Thanks Comtois. Great example as usual! :D


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 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