
It's normal to a game to have many "levels". The gamer tries to go allways a level ahead after to win some challenge.
For us, each level will be at a new "floor".
The avatar will have a translation in the Y axis when it goes to other level. We will change also the Y of the camera.
We will use a flag to define if the transition is permited.
The code of the transition will be placed in the code defining the movement of the avatar. By example, at the demo of this lesson, the avatar goes to the level 2 walking to East. The code for the keyboard key "L" will have new lines:
...
if (e.Key == Key.L ){
if(TheY==1.5 && TheX==16 && GoTo2!=0){
camY=102;
TheY=101.5;
TheX=-16;
}
if(side==0){
pos =new Point3D(0, camY+3, 50);
myPCamera.LookDirection = new Vector3D(0, camY, -300000);
}
if(side==1){
pos=new Point3D(-50, camY+3, 2);
myPCamera.LookDirection = new Vector3D(300000, camY, -2);
}
if(side==3){
pos=new Point3D(50, camY+3, 2);
myPCamera.LookDirection = new Vector3D(-300000, camY, +2);
}
if(side==4){
pos =new Point3D(0, camY+3, -50);
myPCamera.LookDirection = new Vector3D(0, camY, 300000);
}
myPCamera.Position = pos;
transold=trans;
...
When the avatar is at X=16, at the level 1, he goes to X=-16, at level 2, in the next press of "L" (if liberated by the flag).
IMPORTANT: When we have more than 1 floor, the "in front" of the LookDirection of the camera needs to be a point far (at 300000 units of distance ?) or when we change the Y of the the camera it will lost the general plane vision. Or we need to change the "in front" point of the LookDirection.
Look to the code that you can download, to understand all this tricks better.
The other important concept of this lesson is: how we capture the collision using vectors, if we have pieces defined using the "basecentral point". These pieces have a format that can be represented by a cylynder (a tree, a character etc.). A wall, by example, is inadequate to be used with this technique.
Look the figure. We will find the distance between the avatar and another character using the vectors of their positions:
How we capture that there are collision between the two pieces? Look:
We need to administrade this collision inside the "main loop" (the timer). At our demo, when the avatar "is near"/collides against a character he goes back one movement and, at the "ChatReceived" TextBox we write what the character is "saying":
...
void OnTimerEvent(object sender, System.EventArgs args){
//avatar x charac1
Vector3D distavch1 = Point3D.Subtract(new Point3D(TheX,TheY,TheZ),new Point3D(-15, 1.5, -5));
if(distavch1.Length < 4){
mtrans= new MatrixTransform3D(transold);
ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D;
Model3DGroup m3Dg = mv3D.Content as Model3DGroup;
m3Dg.Children[4].Transform= mtrans ;
TheX=TheXold;
TheZ=TheZold;
trans = transold;
doorbell.Play();
ChatReceived.Text="The King's crown was stolen! Get it back is your mission!";
}
...
Another new important trick we have at this lesson is the use of the "inventory". Now, when the user clicks in the "money-bag" the ComboBox will have a new line.
When the user clicks an item of the ComboBox, a flag is changed.
Using this trick, when the user "pays" 100$ to the soldier, he goes out and the passage to the level 2 is liberated.
...
private void UseItem(object Sender, EventArgs e){
ComboBoxItem cboxitem = new ComboBoxItem();
cboxitem= Invent.SelectedItem as ComboBoxItem;
doorbell.Play();
ChatReceived.Text="Used the item: " + cboxitem.Content.ToString();
if(cboxitem.Content.ToString()=="MONEY BAG: 100$"){
GoTo2=1;
cboxitem.Content="MONEY BAG:100$[Used]";
}
}
...
The coding of the movement of the soldier is easy and inside the "main loop":
...
//Charac1 going out
if(GoTo2!=0){
GoTo2=GoTo2-0.02;
Matrix3D matMove = new Matrix3D(0.1,0,0,0,0,0.1,0,0,0,0,0.1,0,15,1.5,GoTo2,1);
Quaternion qt = new Quaternion(new Vector3D(1, 0, 0),90);
Point3D pt = new Point3D(15 , 1.5,GoTo2);
matMove.RotateAt(qt,pt);
qt = new Quaternion(new Vector3D(0, 1, 0),0);
pt = new Point3D(15 ,1.5,GoTo2);
matMove.RotateAt(qt,pt);
MatrixTransform3D transMove= new MatrixTransform3D(matMove);
ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D;
Model3DGroup m3Dg = mv3D.Content as Model3DGroup;
m3Dg.Children[9].Transform= transMove ;
}
...
OK. I don't know if I talk about everything. Better that you do the download, run the application, find and pick the money, use the inventory, go to another level, analise the code, try to change something etc.
TIP: Sometimes the use of .JPG files for textures is a better option. The files are small.
PAY ATTENTION: that the soldier, going to North to spend his money, will disappear at the "myPCamera.FarPlaneDistance = 200". Changing to the North-vision you can find it going...
For the demo, create a folder having the name of the .ZIP file and download it - right-clicking the link - and unzip inside the folder :
Liberate the firewall and play this web3d application:
![]() |
We are doing our tests at a: Notebook DELL Latitude D600 - Intel Pentium M755 2.0 GHZ - Memory: 512 MB - VideoCard ATI Radeon 9000 32MB DDR 4xAGP - Resolution 1400x1050 - Communication: ADSL 256 kbps (Download: 45 kBps, Upload: 14 kBps)
PREVIOUS LESSON NEXT LESSON T.CONTENTS HOMEPAGE
