
Our intention is to create 4 buttons to change the position of the camera to: South(default), East, West and North of the "vista".
Having one of these "cardinality", using the keyboard keys "w", "s", "a", "d" we will move the camera ahead, back, left, right.
To capture the keyboard key press we need to put inside the Window tags:
KeyDown="OnKeyDownHandler"
And to create the buttons we have:
...
<Button Canvas.Top="700" Canvas.Left="10"
Background="Red" Width="100" Height="35"
Click="South" >SOUTH</Button>
<Button Canvas.Top="700" Canvas.Left="110"
Background="Red" Width="100" Height="35"
Click="West" >WEST</Button>
<Button Canvas.Top="700" Canvas.Left="210"
Background="Red" Width="100" Height="35"
Click="North" >NORTH</Button>
<Button Canvas.Top="700" Canvas.Left="310"
Background="Red" Width="100" Height="35"
Click="East" >EAST</Button>
...
We need to write the buttons definition AFTER the Viewport definition or they will be hided. Be careful because at XAML we have layers...
At the code-behind we create a variable: int side=0, another of the type Point (pos) and the code to define new "cardinalities":
...
private void South(object sender, EventArgs e){
side=0;
pos =new Point3D(0, 5, 50);
myPCamera.Position = pos;
myPCamera.LookDirection = new Vector3D(0, 5, -30 );
}
private void West(object sender, EventArgs e){
side=1;
pos=new Point3D(-50, 5, 2);
myPCamera.Position = pos ;
myPCamera.LookDirection = new Vector3D( 30 , 5, -2);
}
private void East(object sender, EventArgs e){
side=3;
pos=new Point3D(50, 5, 2);
myPCamera.Position = pos ;
myPCamera.LookDirection = new Vector3D( -30 , 5, +2);
}
private void North(object sender, EventArgs e){
side=4;
pos =new Point3D(0, 5, -50);
myPCamera.Position = pos;
myPCamera.LookDirection = new Vector3D(0, 5, 30 );
}
...
And, to capture the keyboard key press and move the camera:
...
void OnKeyDownHandler(object sender, KeyEventArgs e){
if (e.Key == Key.W ){
if(side==0){
if(pos.Z>-16)pos.Z=pos.Z-1;
myPCamera.Position = pos;
}
if(side==4){
if(pos.Z<16)pos.Z=pos.Z+1;
myPCamera.Position = pos;
}
if(side==1){
if(pos.X<16)pos.X=pos.X+1;
myPCamera.Position = pos;
}
if(side==3){
if(pos.X>-16)pos.X=pos.X-1;
myPCamera.Position = pos;
}
}
if (e.Key == Key.S ){
if(side==0){
pos.Z=pos.Z+1;
myPCamera.Position = pos;
}
if(side==4){
pos.Z=pos.Z-1;
myPCamera.Position = pos;
}
if(side==1){
pos.X=pos.X-1;
myPCamera.Position = pos;
}
if(side==3){
pos.X=pos.X+1;
myPCamera.Position = pos;
}
}
if (e.Key == Key.D ){
if(side==0){
if(pos.X<= 25)pos.X=pos.X+1;
myPCamera.Position = pos;
}
if(side==4){
if(pos.X>= -25)pos.X=pos.X-1;
myPCamera.Position = pos;
}
if(side==1){
if(pos.Z<= 25)pos.Z=pos.Z+1;
myPCamera.Position = pos;
}
if(side==3){
if(pos.Z>= -25)pos.Z=pos.Z-1;
myPCamera.Position = pos;
}
}
if (e.Key == Key.A ){
if(side==0){
if(pos.X>= -25)pos.X=pos.X-1;
myPCamera.Position = pos;
}
if(side==4){
if(pos.X<= 25)pos.X=pos.X+1;
myPCamera.Position = pos;
}
if(side==1){
if(pos.Z>= -25)pos.Z=pos.Z-1;
myPCamera.Position = pos;
}
if(side==3){
if(pos.Z<= 25)pos.Z=pos.Z+1;
myPCamera.Position = pos;
}
}
}//OnKeyDownHandler
...
Important to note that the movement has limitations.
Look an image of the exercice runing:
To look the source codes and/or run the exercice, create a folder having the name of the .ZIP file and download it - right-clicking the link - and unzip inside the folder :
Try to recreate the exercice changing the code or some texture.
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
