TUTORIAL TEN
3D THAT WALKS

The purpose of this program is to demonstrate how you can load a fully textured, animating 3D character and walk it around the screen. This program shows you how to position the camera, append animation data, position, rotate and animate your 3D objects. The program also contains code that shows you how to use and calculate variables responsible for positioning and moving your objects in 3D space.

This tutorial program can be loaded directly into the editor by clicking SAMPLE10.DBA.

rem Position camera off center
POSITION CAMERA 0.0,0.0,-1000.0
rem Load 3D object and append walking data to it
LOAD OBJECT "idle.x",1 : APPEND OBJECT "walk.x",1,100
YROTATE OBJECT 1,180 : FIX OBJECT PIVOT 1

rem Loop 3D object animation from 0 to 20 (idle)
LOOP OBJECT 1,0,20 : SET OBJECT SPEED 1,10

rem Activate manual syncronization
SYNC ON

rem Begin loop
DO

rem Modify character angle based on left/right keys
stage=0
IF LEFTKEY()=1 THEN a#=a#-8.0
IF RIGHTKEY()=1 THEN a#=a#+8.0
a#=wrapvalue(a#)

rem Modify character position based on up/down keys
IF UPKEY()=1 THEN x#=NEWXVALUE(x#,a#,3) : z#=NEWZVALUE(z#,a#,3) : stage=1
IF DOWNKEY()=1 THEN x#=NEWXVALUE(x#,a#,-3) : z#=NEWZVALUE(z#,a#,-3) : stage=1

rem If character action changes
IF stage<>oldstage
IF stage=0
SET OBJECT FRAME 1,0.0
LOOP OBJECT 1,0,20
SET OBJECT SPEED 1,10
ENDIF
IF stage=1
SET OBJECT FRAME 1,105.0
LOOP OBJECT 1,105,125
SET OBJECT SPEED 1,40
ENDIF
oldstage=stage
ENDIF

rem Update character position and angle
POSITION OBJECT 1,x#,0.0,z#
YROTATE OBJECT 1,a#

rem Refresh screen
SYNC

rem End loop
LOOP

You will have noticed how useful the REM statements have become since tutorial six. Documenting a program not only explains what your code does, but allows you to break up your program into meaningful tasks. In addition to the information provided by the commentary, Dark Basic offers you the ability to request information on the command itself. Simply go to the editor that lists your program, click on a command you would like to know more about and then hit F1. This feature is called context sensitive help and you will be taken directly to a help page that fully describes the command and explains how to use it. Additionally, each help page has its own example program and references to the glossary or other related commands.

Final Step : Things for you to do

1. Change the program to make the character walk faster
2. Change the program to create a landscape for the character to walk on
3. Change the program to make the camera always point to the character

If you have completed all ten tutorials, by successfully making the changes listed in the Final Step segment at the end of each tutorial, you have done extremely well. Don't worry if you haven't understood all the tutorial samples. It takes time to completely grasp the fundamentals, especially in the early stages of learning. If this is the case, it is advised that you return to the last tutorial you were comfortable with and adapt it by making your own changes. When you are practiced at making changes to that tutorial, you should be able to proceed to the next one with some confidence.

You can return to the main tutorial menu by selecting MAIN TUTORIAL MENU.