Conversion of Tutorial 04.Movement Code to VB .NET

Irrlicht.Net is no longer developed or supported, Irrlicht.Net Cross Platform is a much more complete wrapper. Please ask your C# related questions on their forums first.
Locked
jaf1234
Posts: 1
Joined: Fri Aug 26, 2005 10:26 am

Conversion of Tutorial 04.Movement Code to VB .NET

Post by jaf1234 »

Hi,

I saw the work that gagagu has started on converting the tutorial code and thought it was very helpful. So I thought I'd join in, with a code conversion of the 04 Movement Tutorial written in VB .NET.

Hope its of use to someone :)

Code: Select all


Option Explicit On 
Option Strict On

Imports Irrlicht
Imports Irrlicht.Video
Imports Irrlicht.Core
Imports Irrlicht.Scene


'   Visual Basic .NET Conversion of 04.Movement Tutorial Code

'   In this tutorial, one of our goals is to move a scene node using some
'   keys on the keyboard. 


'   To get events like mouse and keyboard input, or GUI events like 
'   "the OK button has been clicked", we need an object wich is derived from the 
'   IEventReceiver object. There is only one method to override: OnEvent. 
'   This method will be called by the engine when an event happened. 
'   We will use this input to move the scene node with the keys W and S.
Public Class MyEventReceiver
    Implements IEventReceiver

    Function OnEvent(ByVal e As [Event]) As Boolean Implements IEventReceiver.OnEvent
        '   If the key 'W' or 'S' was left up, we get the position of the scene node,
        '   and modify the Y coordinate a little bit. So if you press 'W', the node
        '   moves up, and if you press 'S' it moves down.
        If Not Movement.node Is Nothing And e.Type = EventType.KeyInput And Not e.KeyPressedDown Then
            Dim v As Vector3D = Movement.node.Position
            Select Case e.Key
                Case KeyCode.KEY_KEY_S
                    v.Y -= 2
                Case KeyCode.KEY_KEY_W
                    v.Y += 2
            End Select
            Movement.node.Position = v
            Return True
        End If
        Return False
    End Function

End Class


'   Main Class
Public Class Movement

    '   We store a pointer to the scene node we want to
    '   move with the keys here.
    '   The other pointer is a pointer to the Irrlicht Device, which we need
    '   int the EventReceiver to manipulate the scene node and to get the 
    '   active camera.
    Public Shared node As ISceneNode
    Public Shared device As IrrlichtDevice


    '   The main entry point for the application.
    Public Shared Sub Main()

        Dim receiver As New MyEventReceiver

        '   Create the device and exit if creation failed
        device = New IrrlichtDevice(DriverType.OPENGL, New Dimension2D(640, 480), 16, False, False, False)
        If device Is Nothing Then Return '   could not create selected driver

        '   Give the device our MyEventReceiver class
        device.EventReceiver = receiver

        '   Create video driver and the SceneManager objects so that
        '   we do not always have to write device.VideoDriver() and
        '   device.SceneManager()
        Dim driver As IVideoDriver = device.VideoDriver
        Dim smgr As ISceneManager = device.SceneManager

        '   Create the node for moving it with the 'W' and 'S' key. We create a
        '   test node', which is a cube built in into the engine for testing 
        '   purposes. We place the node a (0,0,30) and we	assign a texture to it
        '   to let it look a little bit more interesting.
        node = smgr.AddTestSceneNode(10, Nothing, -1, New Vector3D(0, 0, 30))
        node.SetMaterialTexture(0, driver.GetTexture("../../../media/wall.bmp"))

        '   Now we create another node, moving using a scene node animator. Scene node
        '   animators modify scene nodes and can be attached to any scene node like
        '   mesh scene nodes, billboards, lights and even camera scene nodes. Scene node
        '   animators are not only able to modify the position of a scene node, they can
        '   also animate the textures of an object for example.
        '   We create a test scene node again an attach a 'fly circle' scene node to it, letting
        '   this node fly around our first test scene node.
        Dim n As ISceneNode = smgr.AddTestSceneNode(10, Nothing, -1, New Vector3D(0, 0, 0))
        n.SetMaterialTexture(0, driver.GetTexture("../../../media/t351sml.jpg"))
        Dim anim As ISceneNodeAnimator = smgr.CreateFlyCircleAnimator(New Vector3D(0, 0, 30), 20, 0.001)
        n.AddAnimator(anim)

        '   The last scene node we add to show possibilities of scene node animators is 
        '   a md2 model, which uses a 'fly straight' animator to run between to points.
        Dim anms As IAnimatedMeshSceneNode = smgr.AddAnimatedMeshSceneNode(smgr.GetMesh("../../../media/sydney.md2"), Nothing, -1)


        If Not n Is Nothing Then
            anim = smgr.CreateFlyStraightAnimator(New Vector3D(100, 0, 60), New Vector3D(-100, 0, 60), 10000, True)
            anms.AddAnimator(anim)

            '   To make to model look better, we disable lighting (we have created no lights,
            '   and so the model would be black), set the frames between which the animation
            '   should loop, rotate the model around 180 degrees, and adjust the animation speed
            '   and the texture.
            '   To set the right animation (frames and speed), we would also be able to just
            '   call "anms->setMD2Animation(scene::EMAT_RUN)" for the 'run' animation 
            '   instead of "setFrameLoop" and "setAnimationSpeed",
            '   but this only works with MD2 animations, and so you know how to start other animations.

            anms.SetMaterialFlag(MaterialFlag.LIGHTING, False)
            anms.SetFrameLoop(320, 360)
            anms.AnimationSpeed = 30
            anms.Rotation = New Vector3D(0, 180.0, 0)
            anms.SetMaterialTexture(0, driver.GetTexture("../../../media/sydney.bmp"))
        End If

        '   To be able to look at and move around in this scene, 
        '   we create a first person shooter style camera and make the 
        '   mouse cursor invisible.
        smgr.AddCameraSceneNodeFPS(Nothing, 100.0, 100.0, -1)
        device.CursorControl.Visible = False

        '   We have done everything, so lets draw it. We also write the current
        '   frames per second and the drawn primitives to the caption of the
        '   window. The "if device.WindowActive() then" construct is optional, but 
        '   prevents the engine render to set the position of the mouse cursor 
        '   after task switching when other program are active.
        Dim lastFPS As Integer = -1
        Dim fps As Integer
        Dim caption As String = ""
        Do
            If device.WindowActive Then
                driver.BeginScene(True, True, New Color(255, 113, 113, 133))
                smgr.DrawAll()
                driver.EndScene()

                fps = driver.FPS

                If fps <> lastFPS Then
                    caption = "Movement Example - Irrlicht Engine ["
                    caption &= driver.Name
                    caption &= "] FPS: "
                    caption &= fps

                    device.WindowCaption = caption
                    lastFPS = fps
                End If
            End If
        Loop Until device.Run = False

    End Sub


End Class
Locked