IrrlichtWrapper RTS Camera example

Advanced game related topics
Philippe-felixer76
User
User
Posts: 57
Joined: Mon Dec 18, 2006 2:02 pm
Location: Holland

IrrlichtWrapper RTS Camera example

Post by Philippe-felixer76 »

Anyone?, i only get shockery movement..

As example i have a quake level and a md2 object and
move it with advanced collission detection with NO FPS
camera but just a camera the camera coordinates with
the object and targets at the object position.
Philippe-felixer76
User
User
Posts: 57
Joined: Mon Dec 18, 2006 2:02 pm
Location: Holland

Example code

Post by Philippe-felixer76 »

Hi i see 70 views and nobody helping me so i thought
ill make and post example code..

It's part code from a program i'm developing so there are some
useless things in here for this example..

Code: Select all


;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper For Imperative Languages - Purebasic Examples
;; IrrlichtWrapper and FreeBasic-Example by Frank Dodd (2006)
;; Improved IrrlichtWrapper and PureBasic-Example by Michael Taupitz (2006)
;; ----------------------------------------------------------------------------
;; RTS Style camera example for the PUREBASIC forms... by Felix
;; ---------------------------------------------------
;; ----------------------------------------------------------------------------

;; ////////////////////////////////////////////////////////////////////////////
;; Includes For extension libraries
XIncludeFile "IrrlichtWrapper.pbi"

;; ////////////////////////////////////////////////////////////////////////////
;; Init the DLL
InitIrrlichtWrapperDll()

;; ////////////////////////////////////////////////////////////////////////////
;; irrlicht structure
Structure world
   *level.irr_mesh              ; level mesh (QIII map)
   *node.irr_node               ; level node (pointer?)
   *cam.irr_camera              ; camera
   *camnode.irr_node            ; camera node
   *levelcollision.irr_selector ; level collision pointer
   *ret.irr_selector            ; return selector.. ??
   *ret1.irr_selector           ; second return selector ??!
   *KeyEvent.IRR_KEY_EVENT      ; keyboard event
   *MouseEvent.IRR_MOUSE_EVENT  ; mouse event
   *playermesh.irr_mesh            ; player mesh
   *playertext.irr_texture         ; player texture
   *playernode.irr_node            ; player node, ??!
   *bitmapfont.irr_font         ; bitmapfont pointer
   smooth.b                     ; global smoothness
   jump.b                       ; jump parameter
   mx.l                         ; mousex value
   omx.l                        ; old mousex value
   lopen.l                      ; loop value
   x_pos.f                  ; x pos +
   y_pos.f                  ; y pos +
   z_pos.f                  ; z pos +
   x_rot.f                  ; x rot +
   y_rot.f                  ; y rot +
   z_rot.f                  ; z rot +
EndStructure

;; ////////////////////////////////////////////////////////////////////////////
;; Global variables
Global game.world

;; ////////////////////////////////////////////////////////////////////////////
;; Procedures

Procedure movecam(*cam, speed.f)
   irrgetNodeposition(*cam, @myx.f,  @myy.f, @myz.f)
   irrgetNoderotation(*cam, @rotx.f, @rot.f, @rotz.f)
   toRadians.f = #PI /180 ;3.141592654/180;  
   myz - speed*Sin((rot+180)*toRadians); 
   myx + speed*Cos((rot+180)*toRadians);
   irrgetNodeposition(*cam, @x.f,  @y.f, @z.f)
   If *cam=game\playernode
      game\x_pos = myx-x
      game\z_pos = myz-z
   EndIf
   If *cam=game\cam
      irrgetnodeposition(game\playernode, @x1.f, @y1.f, @z1.f)
      irrsetNodeposition(game\cam, x1+(myx-x),  y1+190, z1+(myz-z))
      IrrSetCameraTarget(game\cam, x1,          y1+100, z1)
   EndIf
EndProcedure
Procedure setpositions()
    ; are we jumping ?
    If game\jump>0: game\jump-4: EndIf
    If game\jump<0: game\jump=0: EndIf
    ; get pos
    IrrgetNodePosition(game\playernode, @px.f, @py.f, @pz.f)
    px + game\x_pos: game\x_pos = 0
    py + game\y_pos: game\y_pos = 0
    pz + game\z_pos: game\z_pos = 0
    ; get rot
    IrrgetNodeRotation(game\playernode, @rx.f, @ry.f, @rz.f)
    rx + game\x_rot: game\x_rot = 0
    ry + game\y_rot: game\y_rot = 0
    rz + game\z_rot: game\z_rot = 0
    ; set pos player
    IrrSetNodePosition(game\playernode,  px,    py+game\jump, pz); kat gelijk zetten met camera
    IrrSetNodeRotation(game\playernode,  0,    ry, 0)
    ;; set cam rotation
    IrrSetNodeRotation(game\cam,  0,    ry, 0)
    ;; move cam  away from player
    movecam(game\cam, 90)  
 EndProcedure
Procedure inputevents()
    niks=1
    While irrmouseeventavailable()
       ; mouse events 
       game\MouseEvent  = IrrReadMouseEvent()  
       ; leftmousebutton up!  
       If game\MouseEvent\action = #IRR_EMIE_LMOUSE_LEFT_UP 
          game\jump = 50
          niks=0
       EndIf
       ; muis bewoog!   
       If game\MouseEvent\action = #IRR_EMIE_MOUSE_MOVED
           niks=0
           game\mx = game\MouseEvent\x
           If game\mx=game\omx  
           Else
              If game\mx>game\omx
                 speed = game\mx-game\omx
                 game\y_rot + speed 
              Else
                 speed = game\omx-game\mx
                 game\y_rot - speed
              EndIf
              game\omx = game\mx
              If game\lopen=0: IrrSetNodeAnimationRange(game\playernode, 0, 78): EndIf
              game\lopen=1
           EndIf
       EndIf       
    Wend
     ; keyevents 
    While IrrKeyEventAvailable()
        game\KeyEvent = IrrReadKeyEvent()
        ; key event select
        Select game\KeyEvent\key
           Case 27     ; esc
                IrrStop(): End
           Case 37     ; Left Arrow 
              If game\KeyEvent\direction = #IRR_KEY_DOWN  
                 game\y_rot - game\smooth ;speed
                 If game\lopen=0: IrrSetNodeAnimationRange(game\playernode, 0, 78): EndIf
                 game\lopen=1
              Else
                 game\lopen=0
                 IrrSetNodeAnimationRange(game\playernode, 114, 460)
              EndIf
              niks=0
           Case 39     ; Right Arrow            ; if the key is going down
               If game\KeyEvent\direction = #IRR_KEY_DOWN ; rechts draaien 
                  game\y_rot + game\smooth    
                  If game\lopen=0: IrrSetNodeAnimationRange(game\playernode, 0, 78): EndIf
                  game\lopen=1
               Else
                  game\lopen=0
                  IrrSetNodeAnimationRange(game\playernode, 114, 460)
               EndIf   
               niks=0
           Case 38     ; Up Arrow            ; if the key is going down
               If game\KeyEvent\direction = #IRR_KEY_DOWN ; vooruit
                  If game\lopen=0: IrrSetNodeAnimationRange(game\playernode, 0, 78): EndIf
                  game\lopen=2
                  movecam(game\playernode,  -game\smooth*2) ;-4)
               Else
                  IrrSetNodeAnimationRange(game\playernode, 114, 460): 
                  game\lopen=0
               EndIf
               niks=0
           Case 40     ; Down Arrow            ; if the key is going down
               If game\KeyEvent\direction = #IRR_KEY_DOWN ; vooruit
                  movecam(game\playernode,  game\smooth*2)
               EndIf
               niks=0
        EndSelect
    Wend
    If niks=1
       If game\lopen=1
          game\lopen=0
          IrrSetNodeAnimationRange(game\playernode, 114, 460)
       EndIf
    EndIf
EndProcedure 

; -----------------------------------------------------------------------------
; start the irrlicht interface
;IrrStart( #IRR_EDT_OPENGL, 800, 400, #IRR_WINDOWED, #IRR_NO_SHADOWS, #IRR_IGNORE_EVENTS )
IrrStart( #IRR_EDT_DIRECT3D9, 800, 600, #IRR_WINDOWED, #IRR_SHADOWS, #IRR_CAPTURE_EVENTS )

; send the window caption
IrrSetWindowCaption( "RTS Camera Example by Felix" )

; first we add the pk3 archive to our filing system. once we have done this
; we can open any of the files in the archive as if they were in the current
; working directory
IrrAddZipFile( "..\media\map-20kdm2.pk3", #IRR_IGNORE_CASE, #IRR_IGNORE_PATHS )

; load the BSP map from the archive as a mesh object. any polygons in the mesh
; that do not have textures will be removed from the scene!
game\level = IrrGetMesh( "20kdm2.bsp" )

; add the map to the scene as a node. when adding the mesh this call uses a 
; mechanism called an octtree that if very efficient at rendering large amounts
; of complext geometry most of which cant be seen, using this call for maps
; will greatly improve your framerates
game\node  = IrrAddMeshToSceneAsOcttree( game\level ) ;*BSPNode = IrrAddMeshToSceneAsOcttree( *BSPMesh )

; add a first person perspective camera into the scene that is controlled with
; the mouse and the cursor keys. if however you capture events when starting
; irrlicht this will become a normal camera that can only be moved by code
game\cam   = IrrAddCamera(0, 0, 0,   0, 0, 0)    ;*Camera = IrrAddFPSCamera()

; load a mesh object, a nice female this time :)
game\playermesh = IrrGetMesh( "..\media\sydney.md2" ) ;IrrGetMeshFromMemory(?object_cat, ?object_cat_end-?object_cat,"cat.md2",#True )
game\playertext = IrrGetTexture( "..\media\sydney.bmp" )
game\playernode = IrrAddMeshToScene( game\playermesh )  
IrrSetNodeMaterialTexture( game\playernode, game\playertext,    0)
IrrSetNodeMaterialFlag(    game\playernode, #IRR_EMF_LIGHTING, #IRR_oFF )
IrrSetNodePosition( game\playernode, 1000, 700, 600 )
IrrSetNoderotation( game\playernode, 0, 90, 0 )

; collision detection
game\levelcollision = IrrGetCollisionGroupFromComplexMesh( game\level, game\node )
game\ret            = IrrAddCollisionAnimator( game\levelcollision, game\playernode,  10.0,  10.0,  10.0, 0.0,-1.0,0.0, 10.0, 10.0, 20.0 )

; load bitmapfont
game\BitmapFont = IrrGetFont ( "..\media\bitmapfont.bmp" )

; set initial smoothness
game\smooth = 8
   
; hide the mouse pointer
IrrHideMouse()

; -----------------------------------------------------------------------------
; while the irrlicht environment is still running
While IrrRunning()
      ; begin the scene, erasing the canvas with sky-blue before rendering
      IrrBeginScene( 0,0,0)
      ; input events
      inputevents()
      ; set 3d positions
      setpositions()
      ; draw the scene
      IrrDrawScene()
      ; draw text
      Irr2DFontDraw ( game\BitmapFont, "STATUS: SHAKERY WHEN WALKING.", 10, 10, 240, 140)
      ; end scene
      IrrEndScene()
Wend

; -----------------------------------------------------------------------------
; Stop the irrlicht engine and release resources
IrrStop()

; End
End

;; ////////////////////////////////////////////////////////////////////////////
;; Free the DLL
FreeIrrlichtWrapperDll()
Should compile and execute with latest PB 4.02 and the
irrlightwrapper installed.

(Save it into the irrlightwrapper example source directory!)

Gr,
Felix.
Post Reply