
At this lesson we include one avatar more and create the synchronization for them. To have more than 2 avatares the logic is the same (only you will have more hard work to do).
First of all you need to create a new table at the database ServerTu.mdb. Use Access directly and reload the .MDB over the previous copy, at the webhoster. The name of the table is: avatar1 and the columns
code CHAR 3 m11 CHAR 50 m12 CHAR 50 m13 CHAR 50 m14 CHAR 50 m21 CHAR 50 m22 CHAR 50 m23 CHAR 50 m24 CHAR 50 m31 CHAR 50 m32 CHAR 50 m33 CHAR 50 m34 CHAR 50 m41 CHAR 50 m42 CHAR 50 m43 CHAR 50 m44 CHAR 50
The columns define, for the lines to have the values of the transform matrix of each avatar.
IMPORTANT: You need to put values for 2 lines. The first has "code" 00A and the second 00B. The lines m11, m22 and m33 will have value: 0.1 and the m44 will be: 1.The other needs to have: "0".
We need to modify our WebService. Look the new code for the ServerDB.asmx(available at the downloadable material):
REMEMBER: When you create a new version of a WebService it needs to be retro-compatible for old applications using it.
<%@ WebService Language="C#" class="ServerDB"%>
using System;
using System.Text;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;
public class ServerDB:WebService {
public ServerDB(){
}
[WebMethod]
public String Door001Sit() {
String select = "SELECT * FROM objstatus WHERE code='001'";
OleDbConnection oConn = new OleDbConnection();
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/ServerTu.mdb");
oConn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter datadapt = new OleDbDataAdapter(select, oConn);
datadapt.Fill(ds, "dor");
return ds.Tables["dor"].Rows[0]["status"].ToString();
oConn.Close();
}
[WebMethod]
public String Opened001() {
OleDbConnection oConn = new OleDbConnection();
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/ServerTu.mdb");
oConn.Open();
System.Data.OleDb.OleDbCommand oCmd= new OleDbCommand();
oCmd = oConn.CreateCommand();
oCmd.Connection = oConn;
oCmd.CommandText = "UPDATE objstatus SET status='T' WHERE code='001'";
oCmd.ExecuteNonQuery();
oConn.Close();
return "Door 001 open!!";
}
[WebMethod]
public String Closed001() {
OleDbConnection oConn = new OleDbConnection();
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/ServerTu.mdb");
oConn.Open();
System.Data.OleDb.OleDbCommand oCmd= new OleDbCommand();
oCmd = oConn.CreateCommand();
oCmd.Connection = oConn;
oCmd.CommandText = "UPDATE objstatus SET status='F' WHERE code='001'";
oCmd.ExecuteNonQuery();
oConn.Close();
return "Door 001 closed";
}
[WebMethod]
public DataSet SelAvatar1(){
String select = "SELECT * FROM avatar1";
OleDbConnection oConn = new OleDbConnection();
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/ServerTu.mdb");
oConn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter datadapt = new OleDbDataAdapter(select, oConn);
datadapt.Fill(ds, "gen1");
oConn.Close();
return ds;
}
[WebMethod]
public String UpdAvatar1A(String p11,String p12,String p13,String p14,String p21,String p22,String p23,String p24,String p31,String p32,String p33,String p34,String p41,String p42,String p43,String p44) {
OleDbConnection oConn = new OleDbConnection();
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/ServerTu.mdb");
oConn.Open();
System.Data.OleDb.OleDbCommand oCmd= new OleDbCommand();
oCmd = oConn.CreateCommand();
oCmd.Connection = oConn;
oCmd.CommandText = "UPDATE avatar1 SET m11='"+p11+"',m12='"+p12+"',m13='"+p13+"',m14='"+p14+"',m21='"+p21+"',m22='"+p22+"',m23='"+p23+ "',m24='"+p24+"',m31='"+p31+"',m32='"+p32+"',m33='"+p33+"',m34='"+p34+"',m41='"+p41+"',m42='"+p42+"',m43='"+p43+"',m44='"+p44+"' WHERE code='00A'";
oCmd.ExecuteNonQuery();
oConn.Close();
return "avatar1 updated for A";
}
[WebMethod]
public String UpdAvatar1B(String p11,String p12,String p13,String p14,String p21,String p22,String p23,String p24,String p31,String p32,String p33,String p34,String p41,String p42,String p43,String p44) {
OleDbConnection oConn = new OleDbConnection();
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/ServerTu.mdb");
oConn.Open();
System.Data.OleDb.OleDbCommand oCmd= new OleDbCommand();
oCmd = oConn.CreateCommand();
oCmd.Connection = oConn;
oCmd.CommandText = "UPDATE avatar1 SET m11='"+p11+"',m12='"+p12+"',m13='"+p13+"',m14='"+p14+"',m21='"+p21+"',m22='"+p22+"',m23='"+p23+ "',m24='"+p24+"',m31='"+p31+"',m32='"+p32+"',m33='"+p33+"',m34='"+p34+"',m41='"+p41+"',m42='"+p42+"',m43='"+p43+"',m44='"+p44+"' WHERE code='00B'";
oCmd.ExecuteNonQuery();
oConn.Close();
return "avatar1 updated for B";
}
}
Put this file at your webhoster and , using the instructions of the previous lesson you will create a new BServerDB.cs at your folder Tu14p. Using MSBUILD (and the same .CSPROJ you will have the new version of the Tu14p.dll that needs to be placed at a new folder Tu18v that will be the folder where you will have the application (all available at the downloadable material).
Now, step by step the changes in the other files.
1 - At the Window1.xaml you need to change the names for the first avatar to: avatarAM, avatarAT, avatarAG and change the "installation". You need to "define" and "install" a new avatar. Remember to change the name of the texture file, add another and make the includes at the .CSPROJ file.
2 - We will have 2 new Buttons and a TextBlock to, dinamically, choose an avatar for your copy of the application (if there are any avatar free available when you run the application)
3 - At the code-behind some news. We need to define new variables: m11, m12, m13,.........,m34,m44 and flags: avatar="NOBODY" (this variable will have the name of the "avatar active" for the user) and init=0 (another counter).
4 - At the moment of the synchronization we need to write in the database table the transform of the "avatar active" and to read the transform of the other avatar and actualize his transform. We have four new methods to do this: captureTA, captureTB, actualizeTA and actualizeTB.
5 - We will put all the avatares initially at a position having Y=100 (not viewed). Controled by the timer, when we have 15 seconds after the application is loaded, we verify the position of all the avatares. At the first syncronization (at the moment 10 seconds) if there are any avatar beeing used by another user it will be not at the Y=100. At the moment 15 seconds we will enable the buttons to the user to "acquire" his avatar if any are at the Y=100. Look the figure of 3 different situations:
5 - At the definitions of the movements of the avatar (keys:"i", "k", "j" and "l") we need to define what avatar will be moved. Depends of the "avatar active". Idem for the code of the collision detection.
OK. I don't know if I talk about everything. Better that you do the download, run the application, analise the code, try to change something etc.
Create a folder having the name of the .ZIP file and download it - right-clicking the link - and unzip inside the folder :
TIP:After the compilation using the Visual C# Express, run Tu18v.exe 3 times. But "acquire" an avatar before to run another copy. Move the avatares, open the door, wait the syncronization...
NOTES: 1 - You need to liberate the firewall for the access to our webhoster, for this application. 2 - Starting the application, wait some seconds for the comunication to the server. 3 - At the moment of the synchronisation the avatar doesn't move.At the next lesson we will teach how to resolve this problem using threads... 4 -REMEMBER THAT MANY READERS OF THIS TUTORIAL CAN BE TESTING THE APPLICATION AT HE SAME TIME AND THE DOOR AND THE AVATARES CAN HAVE SURPRISED STATUS!!!! IT'S A MULTIPLAYER REALTIME DEMO !!!!!
If you like only to see the application running you don't need to do the above download. Only liberate the firewall and play this web3d application (wait the contact with the server at the 999 in the counter (synchronization), liberate the firewall etc. Wait a little more to choose an avatar.). Click this button again to create another copy of the application.Close/open the door at a copy, move the avatar and at the new 999 they will change their status at the other copy.:
![]() |
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
