|
----TUTORIAL 9---
Loading 3D
studio Models You
will Need The NemoX engine Get it Here
download the Visual Basic Source code here
Summary
1.
InitEngine modification
2.
Game Loop Modification
3.
Collision Detection
Code
1. InitEngine modification
In this tutorial we will use
New Classes cNemo_3DSmesh for managing 3DS files, cNemo_Camera
our new camera Class ,cNemo_Input for handle input devices (Mouse,keyBoard,joypad....not
forceFeedBack for now)
For objects declaration we use
vb classic class declaration for 3 Objects:
'here is the class
for our 3d studio objects
Dim Mesh_3DS1 As
cNemo_3DSmesh
Dim Mesh_3DS2 As
cNemo_3DSmesh
Dim Mesh_3DS3 As
cNemo_3DSmesh
InitEngine()
'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======
'
'.......MEMORY ALLOCATION....
Set KAMERA = New cNemo_Camera
Set KEY = New cNemo_Input
Set Mesh_3DS1 = New cNemo_3DSmesh
Set Mesh_3DS2 = New cNemo_3DSmesh
Set Mesh_3DS3 = New cNemo_3DSmesh
'we use this method
'now we allow the user to choose options
'32/16 bit backbuffer
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(125, 125, 125) 'Gray
'set some parameters
Nemo.Set_ViewFrustum 10, 16500, 3.14 / 4, 1.01
Nemo.Set_light True
'set our camera
KAMERA.Set_YRotation 45
KAMERA.Set_EYE Tool.Vector(480, 50, -450)
End Sub
|
2.
Game Loop Modification
'this sub is the main loop for a game or 3d apllication
SuSub gameLoop()
'loop untill player press 'ESCAPE'
'Nemo.Set_CullMode D3DCULL_NONE
Nemo.Set_EngineRenderState D3DRS_ZENABLE, 1
Do
Call GetKey '=====Keyboard handler sub
DoEvents
CheckCollision 'Call Our Collision detection sub
'start the 3d renderer
Nemo.Begin3D
'===============ADD game rendering mrthod here
'draw our ground here
Mesh.Render
'This part of code will render the 3DS meshes
'Note that NemoX engine use built-in View Frustum Test
'To render object only if it's visible
'render 3ds mesh
Mesh_3DS1.Render
Mesh_3DS2.Render
Mesh_3DS3.Render
'show the FPS at pixel(5,10) color White
Nemo.Draw_Text "FPS:" + Str(Nemo.Framesperseconde), 5, 10, &HFFFF0000
Nemo.End3D
'end the 3d renderer
'check the player keyPressed
Loop Until KEY.Get_KeyBoardKeyPressed(NEMO_KEY_ESCAPE)
Call EndGame
En End Sub
So so start
the game you add this in the form_Load code as below:
Private
Sub Form_Load()
Me.Show
Me.Refresh
'call the initializer sub
Call InitEngine
'make geometry
Call BuilGeometry
'call the main game Loop
Call gameLoop
End Sub
To Terminate the
Game we Use this:
Sub EndGame()
'end of the demo
Set KEY = Nothing
Set KAMERA = Nothing
Set Mesh_3DS = Nothing
Set Mesh = Nothing
Nemo.Free 'free resources used by the engine
Set Nemo = Nothing
End
End Sub
|
3. Collision Detection Code
In this code we're checking if
a collision occurs between the player camera postion and Our world and 3DS
meshes
Sub CheckCollision()
Dim
Dest As D3DVECTOR
If
Mesh.CheckCollisionSliding(Nemo.Camera_GetPosition, Dest, 20)
Then
Nemo.Camera_set_EYE Dest
End
If
End
Sub
'New Code for handle
collision with 3DS
'if any collision with our 3d studio mesh we slide along
If Mesh_3DS1.Get_ColisionSliding(KAMERA.Get_Position, Dest, 20) Then
KAMERA.Set_EYE Dest
End If
'if any collision with our 3d studio mesh we slide along
If Mesh_3DS2.Get_ColisionSliding(KAMERA.Get_Position, Dest, 20) Then
KAMERA.Set_EYE Dest
End If
'if any collision with our 3d studio mesh we slide along
If Mesh_3DS3.Get_ColisionSliding(KAMERA.Get_Position, Dest, 20) Then
KAMERA.Set_EYE Dest
End If
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 10 :Loading
MilkShape3D models The Previous:
Tutorial 8
:Polygons Collision Detection with Response
Tutorial Written on April, 11 th 2003 |
|