TUTORIAL 12:   This tutorial explains how To load Quake 2 md2 characters. In this tutorial quake 2model animation we be covered, also Model Oriented Bounding Box collision detection.


----TUTORIAL 12---

Loading and Rendering Quake 2(.md2) Models


You will Need The NemoX engine Get it Here

download the Visual Basic Source code here




1. InitEngine modification

2. How to Load Quake 2(.md2) Models

3. Game Loop Modification

4. Collision Detection Code



1. InitEngine modification

In this tutorial we will use New Classes cNemo_MD2  for managing Quake 2Model..

For objects declaration we use vb classic class declaration  for 3 Objects:

'here is the class for our Quake 2Model
Dim Q2model As cNemo_MD2




'we will used that sub for the engine initialization

Sub InitEngine()

 'first thing allocate memory for the main Object
  Set Nemo = New NemoX
  Set Tool = New cNemo_Tools
   'allocate memory for our meshbuilder
    Set Mesh = New cNemo_Mesh
    '====New code======
    Set Q2model = New cNemo_MD2
    '.......MEMORY ALLOCATION....
    Set KAMERA = New cNemo_Camera
    Set KEY = New cNemo_Input

'we use this method
'now we allow the user to choose options
'32/16 bit backbuffer
  'for this demo Windowed mode is recommanded
  If Not (Nemo.INIT_ShowDeviceDLG(Form1.hWnd)) Then
   End 'terminate here if error
  End If
  'Nemo.Initialize Me.hWnd
  'set the back clearcolor
  Nemo.BackBuffer_ClearCOLOR = RGB(80, 80, 80) 'Gray
  'set some parameters 'near far  FOVangle,Aspect
  Nemo.Set_ViewFrustum 10, 5000, 3.14 / 4, 1.01
  Nemo.Set_light True
  'set our camera
  KAMERA.Set_YRotation 45
  KAMERA.Set_EYE Tool.Vector(200, 50, -250) 'Starting Position

End Sub


2. Loading Quake 2(.md2) Models

'  IN THIS sub we will Load ,rescale,objects
Sub Load_Meshes()
  Dim I As Long
  'load model file first
   Q2model.LoadMD2 App.Path + _
   'load model Texture
              Q2model.load_Texture App.Path + _
   'load Weapon
   Q2model.LoadWeapon App.Path + _
    'load Weapon Texture
              Q2model.LoadWeapon App.Path + _
  'rescale and translate model
  Q2model.Set_Position -100, 100, 0
  Q2model.Set_Scale 4, 4, 4

  'fill listbox with animations
'because no gun animation for the 4 last model animation
  For I = 0 To Q2model.GetNumAnimations - 5 
    List1.AddItem Q2model.GetAnimationName(I)
  Next I
End Sub
In this sub we use LoadMD2 function to load our Quake 2Model.
We just pass the Model long pathName to the function.


3. Game Loop Modification


'this sub is the main loop for a game or 3d apllication
Sub gameLoop()

            'loop untill player press 'ESCAPE'
      'Nemo.Set_CullMode D3DCULL_NONE
      Nemo.Set_EngineRenderState D3DRS_ZENABLE, 1
      Nemo.Set_light 1
               '=====Keyboard handler can be added here
               Call GetKey
               'check collision here
               'start the 3d renderer
                     '===============ADD game rendering mrthod here
               'draw our ground here
               'render Quake 2Model
               'show the FPS at pixel(5,10) color White
               Nemo.Draw_Text "FPS:" + Str(Nemo.Framesperseconde), 5, 10, &HFFFFFFFF
               'end the 3d renderer
            'check the player keyPressed
    Loop Until KEY.Get_KeyBoardKeyPressed(NEMO_KEY_ESCAPE)
    Call EndGame

End Sub




4. Collision Detection Code

In this code we're checking if a collision occurs between the player camera postion and Our world and 3DS meshes


'  IN THIS sub we will handle with collision
'  detection
Sub CheckCollision()
    Dim Dest As D3DVECTOR
    'if any collision with our world we slide along
    If Mesh.CheckCollisionSliding(KAMERA.Get_position, Dest, 40) Then
       KAMERA.Set_EYE Dest
    End If

 'check collision detection
 If Q2model.Get_Colision(KAMERA.Get_position) Then
       KAMERA.Set_EYE KAMERA.Get_LastPosition
    End If

End Sub

We use  this function:CheckCollisionSliding

PointVec : the point coordonates to test

RetPos:  A Point (D3DVECTOR) to receive the collision response

Radius: The radius of the Sphere to be collided with Mesh Polygons




Here is a screenshot for our tutorial project

download the Visual Basic Source code here

See you in the next Tutorial.....sincerly Polaris..Don't forget Any Bugs detected mailMe


                      The Next: Tutorial 13 :Loading and Rendering Quake 3 (.md3) Models

The Previous: Tutorial 11 :Loading and Rendering DirectX .x models


Tutorial Written on April, 28 th 2003 by Polaris: johna_pop@yahoo.fr